コード例 #1
0
        /// <summary>
        /// Builds the <see cref="PaymentDetailsItemType"/>.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="actionCode">
        /// The <see cref="PaymentActionCodeType"/>.
        /// </param>
        /// <returns>
        /// The <see cref="PaymentDetailsType"/>.
        /// </returns>
        public PaymentDetailsType Build(IInvoice invoice, PaymentActionCodeType actionCode)
        {
            // Get the decimal configuration for the current currency
            var currencyCodeType = PayPalApiHelper.GetPayPalCurrencyCode(invoice.CurrencyCode);
            var basicAmountFactory = new PayPalBasicAmountTypeFactory(currencyCodeType);

            // Get the tax total
            var itemTotal = basicAmountFactory.Build(invoice.TotalItemPrice());
            var shippingTotal = basicAmountFactory.Build(invoice.TotalShipping());
            var taxTotal = basicAmountFactory.Build(invoice.TotalTax());
            var invoiceTotal = basicAmountFactory.Build(invoice.Total);

            var items = BuildPaymentDetailsItemTypes(invoice.ProductLineItems(), basicAmountFactory);

            var paymentDetails = new PaymentDetailsType
            {
                PaymentDetailsItem = items.ToList(),
                ItemTotal = itemTotal,
                TaxTotal = taxTotal,
                ShippingTotal = shippingTotal,
                OrderTotal = invoiceTotal,
                PaymentAction = actionCode,
                InvoiceID = invoice.PrefixedInvoiceNumber()
            };

            // ShipToAddress
            if (invoice.ShippingLineItems().Any())
            {
                var addressTypeFactory = new PayPalAddressTypeFactory();
                paymentDetails.ShipToAddress = addressTypeFactory.Build(invoice.GetShippingAddresses().FirstOrDefault());
            }

            return paymentDetails;
        }
コード例 #2
0
        /// <summary>
        /// Builds the <see cref="PaymentDetailsItemType"/>.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="actionCode">
        /// The <see cref="PaymentActionCodeType"/>.
        /// </param>
        /// <returns>
        /// The <see cref="PaymentDetailsType"/>.
        /// </returns>
        public PaymentDetailsType Build(IInvoice invoice, PaymentActionCodeType actionCode)
        {
            // Get the decimal configuration for the current currency
            var currencyCodeType   = PayPalApiHelper.GetPayPalCurrencyCode(invoice.CurrencyCode);
            var basicAmountFactory = new PayPalBasicAmountTypeFactory(currencyCodeType);

            // Get the tax total
            var itemTotal     = basicAmountFactory.Build(invoice.TotalItemPrice());
            var shippingTotal = basicAmountFactory.Build(invoice.TotalShipping());
            var taxTotal      = basicAmountFactory.Build(invoice.TotalTax());
            var invoiceTotal  = basicAmountFactory.Build(invoice.Total);

            var items = BuildPaymentDetailsItemTypes(invoice.ProductLineItems(), basicAmountFactory);

            var paymentDetails = new PaymentDetailsType
            {
                PaymentDetailsItem = items.ToList(),
                ItemTotal          = itemTotal,
                TaxTotal           = taxTotal,
                ShippingTotal      = shippingTotal,
                OrderTotal         = invoiceTotal,
                PaymentAction      = actionCode,
                InvoiceID          = invoice.PrefixedInvoiceNumber()
            };

            // ShipToAddress
            if (invoice.ShippingLineItems().Any())
            {
                var addressTypeFactory = new PayPalAddressTypeFactory();
                paymentDetails.ShipToAddress = addressTypeFactory.Build(invoice.GetShippingAddresses().FirstOrDefault());
            }

            return(paymentDetails);
        }
コード例 #3
0
        /// <summary>
        /// The build product payment details item type.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>        
        /// <param name="factory">
        /// The <see cref="PayPalBasicAmountTypeFactory"/>.
        /// </param>
        /// <returns>
        /// The <see cref="PaymentDetailsItemType"/>.
        /// </returns>
        protected virtual PaymentDetailsItemType BuildProductPaymentDetailsItemType(ILineItem item, PayPalBasicAmountTypeFactory factory)
        {
            IProductContent product = null;
            if (_settings.UsesProductContent)
            {
                var productKey = item.ExtendedData.GetProductKey();
                product = _merchello.TypedProductContent(productKey);
            }

            var detailsItemType = new PaymentDetailsItemType
            {
                Name = item.Name,
                ItemURL = product != null ?
                    string.Format("{0}{1}", _settings.WebsiteUrl, product.Url) :
                    null,
                Amount = factory.Build(item.Price),
                Quantity = item.Quantity
            };

            return detailsItemType;
        }
コード例 #4
0
        /// <summary>
        /// Builds a <see cref="PaymentDetailsItemType"/>.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <param name="factory">
        /// The <see cref="PayPalBasicAmountTypeFactory"/>.
        /// </param>
        /// <param name="isDiscount">
        /// The is discount.
        /// </param>
        /// <returns>
        /// The <see cref="PaymentDetailsItemType"/>.
        /// </returns>
        protected virtual PaymentDetailsItemType BuildGenericPaymentDetailsItemType(ILineItem item, PayPalBasicAmountTypeFactory factory, bool isDiscount)
        {
            var detailsItemType = new PaymentDetailsItemType
            {
                Name = item.Name,
                ItemURL = null,
                Amount = factory.Build(item.Price),
                Quantity = item.Quantity,
            };

            return detailsItemType;
        }
コード例 #5
0
 /// <summary>
 /// Builds a list of <see cref="PaymentDetailsItemType"/>.
 /// </summary>
 /// <param name="items">
 /// The items.
 /// </param>        
 /// <param name="factory">
 /// The <see cref="PayPalBasicAmountTypeFactory"/>.
 /// </param>
 /// <param name="areDiscounts">
 /// The are discounts.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable{PaymentDetailItemType}"/>.
 /// </returns>
 public virtual IEnumerable<PaymentDetailsItemType> BuildPaymentDetailsItemTypes(IEnumerable<ILineItem> items, PayPalBasicAmountTypeFactory factory, bool areDiscounts = false)
 {
     return items.ToArray().Select(
         item => item.ExtendedData.ContainsProductKey() ?
             this.BuildProductPaymentDetailsItemType(item, factory) :
             this.BuildGenericPaymentDetailsItemType(item, factory, areDiscounts)).ToList();
 }
コード例 #6
0
        /// <summary>
        /// Builds a list of <see cref="PaymentDetailsItemType"/>.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="factory">
        /// The <see cref="PayPalBasicAmountTypeFactory"/>.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable{PaymentDetailsItemType}"/>.
        /// </returns>
        public virtual IEnumerable<PaymentDetailsItemType> BuildPaymentDetailsItemTypes(IInvoice invoice, PayPalBasicAmountTypeFactory factory)
        {
            var paymentDetailItems = new List<PaymentDetailsItemType>();
            paymentDetailItems.AddRange(BuildPaymentDetailsItemTypes(invoice.ProductLineItems(), factory));
            paymentDetailItems.AddRange(BuildPaymentDetailsItemTypes(invoice.CustomLineItems(), factory));
            paymentDetailItems.AddRange(BuildPaymentDetailsItemTypes(invoice.DiscountLineItems(), factory, true));

            return paymentDetailItems;
        }
コード例 #7
0
        /// <summary>
        /// The build product payment details item type.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <param name="factory">
        /// The <see cref="PayPalBasicAmountTypeFactory"/>.
        /// </param>
        /// <returns>
        /// The <see cref="PaymentDetailsItemType"/>.
        /// </returns>
        protected virtual PaymentDetailsItemType BuildProductPaymentDetailsItemType(ILineItem item, PayPalBasicAmountTypeFactory factory)
        {
            IProductContent product = null;

            if (_settings.UsesProductContent)
            {
                var productKey = item.ExtendedData.GetProductKey();
                product = _merchello.TypedProductContent(productKey);
            }

            var detailsItemType = new PaymentDetailsItemType
            {
                Name    = item.Name,
                ItemURL = product != null?
                          string.Format("{0}{1}", _settings.WebsiteUrl, product.Url) :
                              null,
                              Amount   = factory.Build(item.Price),
                              Quantity = item.Quantity
            };

            return(detailsItemType);
        }
コード例 #8
0
        /// <summary>
        /// Builds a <see cref="PaymentDetailsItemType"/>.
        /// </summary>
        /// <param name="item">
        /// The item.
        /// </param>
        /// <param name="factory">
        /// The <see cref="PayPalBasicAmountTypeFactory"/>.
        /// </param>
        /// <param name="isDiscount">
        /// The is discount.
        /// </param>
        /// <returns>
        /// The <see cref="PaymentDetailsItemType"/>.
        /// </returns>
        protected virtual PaymentDetailsItemType BuildGenericPaymentDetailsItemType(ILineItem item, PayPalBasicAmountTypeFactory factory, bool isDiscount)
        {
            var detailsItemType = new PaymentDetailsItemType
            {
                Name     = item.Name,
                ItemURL  = null,
                Amount   = factory.Build(item.Price),
                Quantity = item.Quantity,
            };

            return(detailsItemType);
        }
コード例 #9
0
 /// <summary>
 /// Builds a list of <see cref="PaymentDetailsItemType"/>.
 /// </summary>
 /// <param name="items">
 /// The items.
 /// </param>
 /// <param name="factory">
 /// The <see cref="PayPalBasicAmountTypeFactory"/>.
 /// </param>
 /// <param name="areDiscounts">
 /// The are discounts.
 /// </param>
 /// <returns>
 /// The <see cref="IEnumerable{PaymentDetailItemType}"/>.
 /// </returns>
 public virtual IEnumerable <PaymentDetailsItemType> BuildPaymentDetailsItemTypes(IEnumerable <ILineItem> items, PayPalBasicAmountTypeFactory factory, bool areDiscounts = false)
 {
     return(items.ToArray().Select(
                item => item.ExtendedData.ContainsProductKey() ?
                this.BuildProductPaymentDetailsItemType(item, factory) :
                this.BuildGenericPaymentDetailsItemType(item, factory, areDiscounts)).ToList());
 }
コード例 #10
0
        /// <summary>
        /// Builds a list of <see cref="PaymentDetailsItemType"/>.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="factory">
        /// The <see cref="PayPalBasicAmountTypeFactory"/>.
        /// </param>
        /// <returns>
        /// The <see cref="IEnumerable{PaymentDetailsItemType}"/>.
        /// </returns>
        public virtual IEnumerable <PaymentDetailsItemType> BuildPaymentDetailsItemTypes(IInvoice invoice, PayPalBasicAmountTypeFactory factory)
        {
            var paymentDetailItems = new List <PaymentDetailsItemType>();

            paymentDetailItems.AddRange(BuildPaymentDetailsItemTypes(invoice.ProductLineItems(), factory));
            paymentDetailItems.AddRange(BuildPaymentDetailsItemTypes(invoice.CustomLineItems(), factory));
            paymentDetailItems.AddRange(BuildPaymentDetailsItemTypes(invoice.DiscountLineItems(), factory, true));

            return(paymentDetailItems);
        }
コード例 #11
0
        /// <summary>
        /// Refunds or partially refunds a payment.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="payment">
        /// The payment.
        /// </param>
        /// <param name="amount">
        /// The amount of the refund.
        /// </param>
        /// <returns>
        /// The <see cref="PayPalExpressTransactionRecord"/>.
        /// </returns>
        public ExpressCheckoutResponse Refund(IInvoice invoice, IPayment payment, decimal amount)
        {
            var record = payment.GetPayPalTransactionRecord();

            // Ensure the currency code
            if (record.Data.CurrencyCode.IsNullOrWhiteSpace())
            {
                var ex = new PayPalApiException("CurrencyCode was not found in payment extended data PayPal transaction data record.  Cannot perform refund.");
                return _responseFactory.Build(ex);
            }

            // Ensure the transaction id
            if (record.Data.CaptureTransactionId.IsNullOrWhiteSpace())
            {
                var ex = new PayPalApiException("CaptureTransactionId was not found in payment extended data PayPal transaction data record.  Cannot perform refund.");
                return _responseFactory.Build(ex);
            }

            // Get the decimal configuration for the current currency
            var currencyCodeType = PayPalApiHelper.GetPayPalCurrencyCode(record.Data.CurrencyCode);
            var basicAmountFactory = new PayPalBasicAmountTypeFactory(currencyCodeType);

            ExpressCheckoutResponse result = null;

            if (amount > payment.Amount) amount = payment.Amount;

            try
            {
                var request = new RefundTransactionRequestType
                    {
                        InvoiceID = invoice.PrefixedInvoiceNumber(),
                        PayerID = record.Data.PayerId,
                        RefundSource = RefundSourceCodeType.DEFAULT,
                        Version = record.DoCapture.Version,
                        TransactionID = record.Data.CaptureTransactionId,
                        Amount = basicAmountFactory.Build(amount)
                    };

                var wrapper = new RefundTransactionReq { RefundTransactionRequest = request };

                var refundTransactionResponse = GetPayPalService().RefundTransaction(wrapper);

                result = _responseFactory.Build(refundTransactionResponse, record.Data.Token);
            }
            catch (Exception ex)
            {
                result = _responseFactory.Build(ex);
            }

            return result;
        }
コード例 #12
0
        /// <summary>
        /// The capture success.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="payment">
        /// The payment.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="isPartialPayment">
        /// The is partial payment.
        /// </param>
        /// <returns>
        /// The <see cref="ExpressCheckoutResponse"/>.
        /// </returns>
        public PayPalExpressTransactionRecord Capture(IInvoice invoice, IPayment payment, decimal amount, bool isPartialPayment)
        {
            // Get the transaction record
            var record = payment.GetPayPalTransactionRecord();

            ExpressCheckoutResponse result = null;

            try
            {
                var amountFactory = new PayPalBasicAmountTypeFactory(PayPalApiHelper.GetPayPalCurrencyCode(invoice.CurrencyCode));

                var authorizationId = record.Data.AuthorizationTransactionId;

                // do express checkout
                var request = new DoCaptureRequestType()
                {
                    AuthorizationID = authorizationId,
                    Amount = amountFactory.Build(amount),
                    CompleteType = isPartialPayment ? CompleteCodeType.NOTCOMPLETE : CompleteCodeType.COMPLETE
                };

                var doCaptureReq = new DoCaptureReq() { DoCaptureRequest = request };

                var service = GetPayPalService();
                var doCaptureResponse = service.DoCapture(doCaptureReq);
                result = _responseFactory.Build(doCaptureResponse, record.Data.Token);

                if (result.Success())
                {
                    var transactionId = doCaptureResponse.DoCaptureResponseDetails.PaymentInfo.TransactionID;
                    record.Data.CaptureTransactionId = transactionId;
                }
            }
            catch (Exception ex)
            {
                result = _responseFactory.Build(ex);
            }

            record.DoCapture = result;
            record.Success = result.Success();
            return record;
        }