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[Defaults.PaypalTokenKey].ToString(),
                        PayerID = processPaymentRequest.CustomValues[Defaults.PaypalPayerIdKey].ToString(),
                        PaymentAction = _payPalExpressCheckoutPaymentSettings.PaymentAction,
                        PaymentActionSpecified = true,
                        ButtonSource = PayPalHelper.BnCode,
                        PaymentDetails = new[] { paymentDetails }
                    }
                }
            });
        }
示例#2
0
        public PaymentDetailsItemType CreatePaymentItem(ShoppingCartItem item)
        {
            var product = _productService.GetProductById(item.ProductId);

            if (product is null)
            {
                throw new NopException("Product is not found");
            }

            var productPrice = _taxService.GetProductPrice(product,
                                                           _shoppingCartService.GetUnitPrice(item), false,
                                                           _workContext.CurrentCustomer, out _);

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

            return(paymentDetailsItemType);
        }
示例#3
0
        public CreateRecurringPaymentsProfileRequestDetailsType GetCreateRecurringPaymentProfileRequestDetails(
            ProcessPaymentRequest processPaymentRequest)
        {
            var details = new CreateRecurringPaymentsProfileRequestDetailsType
            {
                Token = processPaymentRequest.CustomValues[Defaults.PaypalTokenKey].ToString()
            };

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

            if (customer is null)
            {
                throw new NopException("Customer is not found");
            }

            if ((customer.BillingAddressId ?? 0) == 0)
            {
                throw new NopException("Customer doesn't have a billing address");
            }

            var billingAddress = _addressService.GetAddressById(customer.BillingAddressId.Value);

            var country = _countryService.GetCountryByAddress(billingAddress);

            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(country)
                },
                CreditCardTypeSpecified = true
            };



            details.CreditCard.CardOwner.Address = new AddressType
            {
                CountrySpecified = true,
                Street1          = billingAddress.Address1,
                Street2          = billingAddress.Address2,
                CityName         = billingAddress.City,
                StateOrProvince  = _stateProvinceService.GetStateProvinceByAddress(billingAddress)?.Abbreviation ?? "CA",
                Country          = GetPaypalCountryCodeType(country),
                PostalCode       = billingAddress.ZipPostalCode
            };
            details.CreditCard.CardOwner.Payer     = billingAddress.Email;
            details.CreditCard.CardOwner.PayerName = new PersonNameType
            {
                FirstName = billingAddress.FirstName,
                LastName  = 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 = $"{storeName} - recurring payment";
            var currencyCodeType = _payPalCurrencyCodeParser.GetCurrencyCodeType(_workContext.WorkingCurrency);

            details.ScheduleDetails.PaymentPeriod = new BillingPeriodDetailsType
            {
                Amount           = processPaymentRequest.OrderTotal.GetBasicAmountType(currencyCodeType),
                BillingFrequency = processPaymentRequest.RecurringCycleLength
            };

            details.ScheduleDetails.PaymentPeriod.BillingPeriod = processPaymentRequest.RecurringCyclePeriod switch
            {
                RecurringProductCyclePeriod.Days => BillingPeriodType.Day,
                RecurringProductCyclePeriod.Weeks => BillingPeriodType.Week,
                RecurringProductCyclePeriod.Months => BillingPeriodType.Month,
                RecurringProductCyclePeriod.Years => BillingPeriodType.Year,
                _ => 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);

            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
            if (_workContext.CurrentCustomer is Customer customer)
            {
                var checkoutAttributesXml = _genericAttributeService.GetAttribute <string>(customer, NopCustomerDefaults.CheckoutAttributes, _storeContext.CurrentStore.Id);
                var caValues = _checkoutAttributeParser.ParseCheckoutAttributeValues(checkoutAttributesXml);

                foreach (var(attribute, values) in caValues)
                {
                    foreach (var attributeValue in values)
                    {
                        if (attributeValue.PriceAdjustment <= 0)
                        {
                            continue;
                        }

                        var checkoutAttrItem = new PaymentDetailsItemType
                        {
                            Name     = attributeValue.Name,
                            Amount   = attributeValue.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
                }
            });
        }