public DoExpressCheckoutPaymentReq GetDoExpressCheckoutPaymentRequest(ProcessPaymentRequest processPaymentRequest)
        {
            // populate payment details
            var currencyCodeType = _payPalCurrencyCodeParser.GetCurrencyCodeType(_workContext.WorkingCurrency);

            var paymentDetails = new PaymentDetailsType
            {
                OrderTotal   = processPaymentRequest.OrderTotal.GetBasicAmountType(currencyCodeType),
                Custom       = processPaymentRequest.OrderGuid.ToString(),
                ButtonSource = PayPalHelper.BnCode,
                InvoiceID    = processPaymentRequest.OrderGuid.ToString()
            };

            // build the request
            return(new DoExpressCheckoutPaymentReq
            {
                DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType
                {
                    Version = GetVersion(),
                    DoExpressCheckoutPaymentRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType
                    {
                        Token = processPaymentRequest.CustomValues["PaypalToken"].ToString(),
                        PayerID = processPaymentRequest.CustomValues["PaypalPayerId"].ToString(),
                        PaymentAction = _payPalExpressCheckoutPaymentSettings.PaymentAction,
                        PaymentActionSpecified = true,
                        ButtonSource = PayPalHelper.BnCode,
                        PaymentDetails = new[] { paymentDetails }
                    }
                }
            });
        }
        public PaymentDetailsItemType CreatePaymentItem(ShoppingCartItem item)
        {
            var productPrice = _taxService.GetProductPrice(item.Product,
                                                           _priceCalculationService.GetUnitPrice(item), false,
                                                           _workContext.CurrentCustomer, out _);

            var currencyCodeType       = _payPalCurrencyCodeParser.GetCurrencyCodeType(_workContext.WorkingCurrency);
            var paymentDetailsItemType = new PaymentDetailsItemType
            {
                Name = item.Product.Name,
                //Description = _productAttributeFormatter.FormatAttributes(item.ProductVariant, item.AttributesXml),
                Amount       = productPrice.GetBasicAmountType(currencyCodeType),
                ItemCategory =
                    item.Product.IsDownload
                        ? ItemCategoryType.Digital
                        : ItemCategoryType.Physical,
                Quantity = item.Quantity.ToString()
            };

            return(paymentDetailsItemType);
        }
        public CreateRecurringPaymentsProfileRequestDetailsType GetCreateRecurringPaymentProfileRequestDetails(
            ProcessPaymentRequest processPaymentRequest)
        {
            var details = new CreateRecurringPaymentsProfileRequestDetailsType();

            details.Token = processPaymentRequest.CustomValues["PaypalToken"].ToString();

            var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);

            details.CreditCard = new CreditCardDetailsType
            {
                CreditCardNumber  = processPaymentRequest.CreditCardNumber,
                CreditCardType    = GetPaypalCreditCardType(processPaymentRequest.CreditCardType),
                ExpMonthSpecified = true,
                ExpMonth          = processPaymentRequest.CreditCardExpireMonth,
                ExpYearSpecified  = true,
                ExpYear           = processPaymentRequest.CreditCardExpireYear,
                CVV2      = processPaymentRequest.CreditCardCvv2,
                CardOwner = new PayerInfoType
                {
                    PayerCountry = GetPaypalCountryCodeType(customer.BillingAddress.Country)
                },
                CreditCardTypeSpecified = true
            };

            details.CreditCard.CardOwner.Address = new AddressType
            {
                CountrySpecified = true,
                Street1          = customer.BillingAddress.Address1,
                Street2          = customer.BillingAddress.Address2,
                CityName         = customer.BillingAddress.City,
                StateOrProvince  = customer.BillingAddress.StateProvince != null ? customer.BillingAddress.StateProvince.Abbreviation : "CA",
                Country          = GetPaypalCountryCodeType(customer.BillingAddress.Country),
                PostalCode       = customer.BillingAddress.ZipPostalCode
            };
            details.CreditCard.CardOwner.Payer     = customer.BillingAddress.Email;
            details.CreditCard.CardOwner.PayerName = new PersonNameType
            {
                FirstName = customer.BillingAddress.FirstName,
                LastName  = customer.BillingAddress.LastName
            };

            //start date
            details.RecurringPaymentsProfileDetails = new RecurringPaymentsProfileDetailsType
            {
                BillingStartDate = DateTime.UtcNow,
                ProfileReference = processPaymentRequest.OrderGuid.ToString()
            };

            //schedule
            details.ScheduleDetails = new ScheduleDetailsType();
            var store     = _storeService.GetStoreById(processPaymentRequest.StoreId);
            var storeName = store == null ? string.Empty : store.Name;

            details.ScheduleDetails.Description = string.Format("{0} - {1}", storeName, "recurring payment");
            var currencyCodeType = _payPalCurrencyCodeParser.GetCurrencyCodeType(_workContext.WorkingCurrency);

            details.ScheduleDetails.PaymentPeriod = new BillingPeriodDetailsType
            {
                Amount           = processPaymentRequest.OrderTotal.GetBasicAmountType(currencyCodeType),
                BillingFrequency = processPaymentRequest.RecurringCycleLength
            };
            switch (processPaymentRequest.RecurringCyclePeriod)
            {
            case RecurringProductCyclePeriod.Days:
                details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Day;
                break;

            case RecurringProductCyclePeriod.Weeks:
                details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Week;
                break;

            case RecurringProductCyclePeriod.Months:
                details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Month;
                break;

            case RecurringProductCyclePeriod.Years:
                details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Year;
                break;

            default:
                throw new NopException("Not supported cycle period");
            }
            details.ScheduleDetails.PaymentPeriod.TotalBillingCycles          = processPaymentRequest.RecurringTotalCycles;
            details.ScheduleDetails.PaymentPeriod.TotalBillingCyclesSpecified = true;

            return(details);
        }
Пример #4
0
        public PaymentDetailsType[] GetPaymentDetails(IList <ShoppingCartItem> cart)
        {
            var currencyCode = _payPalCurrencyCodeParser.GetCurrencyCodeType(_workContext.WorkingCurrency);

            decimal orderTotalDiscountAmount;
            List <DiscountForCaching> appliedDiscounts;
            int     redeemedRewardPoints;
            decimal redeemedRewardPointsAmount;
            List <AppliedGiftCard> appliedGiftCards;
            var orderTotalWithDiscount = _payPalCartItemService.GetCartTotal(cart, out orderTotalDiscountAmount,
                                                                             out appliedDiscounts,
                                                                             out redeemedRewardPoints,
                                                                             out redeemedRewardPointsAmount,
                                                                             out appliedGiftCards);

            decimal subTotalWithDiscount;
            decimal subTotalWithoutDiscount;
            List <DiscountForCaching> subTotalAppliedDiscounts;
            decimal subTotalDiscountAmount;
            var     itemTotalWithDiscount = _payPalCartItemService.GetCartItemTotal(cart,
                                                                                    out subTotalDiscountAmount,
                                                                                    out subTotalAppliedDiscounts,
                                                                                    out subTotalWithoutDiscount,
                                                                                    out subTotalWithDiscount);

            var giftCardsAmount = appliedGiftCards.Sum(x => x.AmountCanBeUsed);

            itemTotalWithDiscount = itemTotalWithDiscount - orderTotalDiscountAmount - giftCardsAmount;

            var taxTotal      = _payPalCartItemService.GetTax(cart);
            var shippingTotal = _payPalCartItemService.GetShippingTotal(cart);
            var items         = GetPaymentDetailsItems(cart);

            // checkout attributes
            var customer = cart.GetCustomer();

            if (customer != null)
            {
                var checkoutAttributesXml = customer.GetAttribute <string>(SystemCustomerAttributeNames.CheckoutAttributes, _genericAttributeService, _storeContext.CurrentStore.Id);
                var caValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(checkoutAttributesXml);
                if (caValues != null)
                {
                    foreach (var caValue in caValues)
                    {
                        if (caValue.PriceAdjustment > 0)
                        {
                            var checkoutAttrItem = new PaymentDetailsItemType
                            {
                                Name     = caValue.Name,
                                Amount   = caValue.PriceAdjustment.GetBasicAmountType(currencyCode),
                                Quantity = "1"
                            };
                            items.Add(checkoutAttrItem);
                        }
                    }
                }
            }
            if (orderTotalDiscountAmount > 0 || subTotalDiscountAmount > 0)
            {
                var discountItem = new PaymentDetailsItemType
                {
                    Name   = "Discount",
                    Amount =
                        (-orderTotalDiscountAmount + -subTotalDiscountAmount).GetBasicAmountType(
                            currencyCode),
                    Quantity = "1"
                };

                items.Add(discountItem);
            }

            foreach (var appliedGiftCard in appliedGiftCards)
            {
                var giftCardItem = new PaymentDetailsItemType
                {
                    Name     = string.Format("Gift Card ({0})", appliedGiftCard.GiftCard.GiftCardCouponCode),
                    Amount   = (-appliedGiftCard.AmountCanBeUsed).GetBasicAmountType(currencyCode),
                    Quantity = "1"
                };

                items.Add(giftCardItem);
            }

            return(new[]
            {
                new PaymentDetailsType
                {
                    OrderTotal = orderTotalWithDiscount.GetBasicAmountType(currencyCode),
                    ItemTotal = itemTotalWithDiscount.GetBasicAmountType(currencyCode),
                    TaxTotal = taxTotal.GetBasicAmountType(currencyCode),
                    ShippingTotal = shippingTotal.GetBasicAmountType(currencyCode),
                    PaymentDetailsItem = items.ToArray(),
                    PaymentAction = _payPalExpressCheckoutPaymentSettings.PaymentAction,
                    PaymentActionSpecified = true,
                    ButtonSource = PayPalHelper.BnCode
                }
            });
        }
        public PaymentDetailsType[] GetPaymentDetails(IList <ShoppingCartItem> cart)
        {
            var currencyCode = _payPalCurrencyCodeParser.GetCurrencyCodeType(_workContext.WorkingCurrency);

            var orderTotalWithDiscount = _payPalCartItemService.GetCartTotal(cart, out var orderTotalDiscountAmount,
                                                                             out _,
                                                                             out _,
                                                                             out _,
                                                                             out var appliedGiftCards);

            var itemTotalWithDiscount = _payPalCartItemService.GetCartItemTotal(cart,
                                                                                out var subTotalDiscountAmount,
                                                                                out _,
                                                                                out _,
                                                                                out _);

            var giftCardsAmount = appliedGiftCards.Sum(x => x.AmountCanBeUsed);

            itemTotalWithDiscount = itemTotalWithDiscount - orderTotalDiscountAmount - giftCardsAmount;

            var taxTotal      = _payPalCartItemService.GetTax(cart);
            var shippingTotal = _payPalCartItemService.GetShippingTotal(cart);
            var items         = GetPaymentDetailsItems(cart);

            // checkout attributes
            var customer = _workContext.CurrentCustomer;

            if (customer != null)
            {
                var checkoutAttributesXml = _genericAttributeService.GetAttribute <string>(customer, NopCustomerDefaults.CheckoutAttributes, _storeContext.CurrentStore.Id);
                var caValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(checkoutAttributesXml);
                if (caValues != null)
                {
                    foreach (var caValue in caValues)
                    {
                        if (caValue.PriceAdjustment <= 0)
                        {
                            continue;
                        }

                        var checkoutAttrItem = new PaymentDetailsItemType
                        {
                            Name     = caValue.Name,
                            Amount   = caValue.PriceAdjustment.GetBasicAmountType(currencyCode),
                            Quantity = "1"
                        };

                        items.Add(checkoutAttrItem);
                    }
                }
            }

            if (orderTotalDiscountAmount > 0 || subTotalDiscountAmount > 0)
            {
                var discountItem = new PaymentDetailsItemType
                {
                    Name     = "Discount",
                    Amount   = (-orderTotalDiscountAmount + -subTotalDiscountAmount).GetBasicAmountType(currencyCode),
                    Quantity = "1"
                };

                items.Add(discountItem);
            }

            foreach (var appliedGiftCard in appliedGiftCards)
            {
                var giftCardItem = new PaymentDetailsItemType
                {
                    Name     = $"Gift Card ({appliedGiftCard.GiftCard.GiftCardCouponCode})",
                    Amount   = (-appliedGiftCard.AmountCanBeUsed).GetBasicAmountType(currencyCode),
                    Quantity = "1"
                };

                items.Add(giftCardItem);
            }

            return(new[]
            {
                new PaymentDetailsType
                {
                    OrderTotal = orderTotalWithDiscount.GetBasicAmountType(currencyCode),
                    ItemTotal = itemTotalWithDiscount.GetBasicAmountType(currencyCode),
                    TaxTotal = taxTotal.GetBasicAmountType(currencyCode),
                    ShippingTotal = shippingTotal.GetBasicAmountType(currencyCode),
                    PaymentDetailsItem = items.ToArray(),
                    PaymentAction = _payPalExpressCheckoutPaymentSettings.PaymentAction,
                    PaymentActionSpecified = true,
                    ButtonSource = PayPalHelper.BnCode
                }
            });
        }