Ejemplo n.º 1
0
 public PayPalIPNService(IOrderService orderService, IOrderProcessingService orderProcessingService, PayPalPlusPaymentSettings payPalPlusPaymentSettings, ILogger logger)
 {
     _orderService              = orderService;
     _orderProcessingService    = orderProcessingService;
     _payPalPlusPaymentSettings = payPalPlusPaymentSettings;
     _logger = logger;
 }
Ejemplo n.º 2
0
        public void Copy(PayPalPlusPaymentSettings settings, bool fromSettings)
        {
            if (fromSettings)
            {
                SecurityProtocol        = settings.SecurityProtocol;
                UseSandbox              = settings.UseSandbox;
                AdditionalFee           = settings.AdditionalFee;
                AdditionalFeePercentage = settings.AdditionalFeePercentage;

                ClientId                        = settings.ClientId;
                Secret                          = settings.Secret;
                ExperienceProfileId             = settings.ExperienceProfileId;
                WebhookId                       = settings.WebhookId;
                ThirdPartyPaymentMethods        = settings.ThirdPartyPaymentMethods;
                DisplayPaymentMethodLogo        = settings.DisplayPaymentMethodLogo;
                DisplayPaymentMethodDescription = settings.DisplayPaymentMethodDescription;
            }
            else
            {
                settings.SecurityProtocol        = SecurityProtocol;
                settings.UseSandbox              = UseSandbox;
                settings.AdditionalFee           = AdditionalFee;
                settings.AdditionalFeePercentage = AdditionalFeePercentage;

                settings.ClientId                        = ClientId;
                settings.Secret                          = Secret;
                settings.ExperienceProfileId             = ExperienceProfileId;
                settings.WebhookId                       = WebhookId;
                settings.ThirdPartyPaymentMethods        = ThirdPartyPaymentMethods;
                settings.DisplayPaymentMethodLogo        = DisplayPaymentMethodLogo;
                settings.DisplayPaymentMethodDescription = DisplayPaymentMethodDescription;
            }
        }
 public void Copy(PayPalPlusPaymentSettings settings, bool fromSettings)
 {
     if (fromSettings)
     {
         MiniMapper.Map(settings, this);
     }
     else
     {
         MiniMapper.Map(this, settings);
         settings.ApiAccountName      = ApiAccountName.TrimSafe();
         settings.ApiAccountPassword  = ApiAccountPassword.TrimSafe();
         settings.ClientId            = ClientId.TrimSafe();
         settings.ExperienceProfileId = ExperienceProfileId.TrimSafe();
         settings.Secret    = Secret.TrimSafe();
         settings.Signature = Signature.TrimSafe();
         settings.WebhookId = WebhookId.TrimSafe();
     }
 }
 public PaymentPayPalPlusController(IWorkContext workContext,
                                    IStoreService storeService,
                                    ISettingService settingService,
                                    IPaymentService paymentService,
                                    IOrderService orderService,
                                    IOrderProcessingService orderProcessingService,
                                    IPermissionService permissionService,
                                    IGenericAttributeService genericAttributeService,
                                    ILocalizationService localizationService,
                                    IStoreContext storeContext,
                                    ILogger logger,
                                    IWebHelper webHelper,
                                    PaymentSettings paymentSettings,
                                    PayPalPlusPaymentSettings payPalPlusPaymentSettings,
                                    ShoppingCartSettings shoppingCartSettings,
                                    IOrderTotalCalculationService orderTotalCalculationService,
                                    IPayPalIPNService payPalIPNService,
                                    INotificationService notificationService)
 {
     this._workContext             = workContext;
     this._storeService            = storeService;
     this._settingService          = settingService;
     this._paymentService          = paymentService;
     this._orderService            = orderService;
     this._orderProcessingService  = orderProcessingService;
     this._permissionService       = permissionService;
     this._genericAttributeService = genericAttributeService;
     this._localizationService     = localizationService;
     this._storeContext            = storeContext;
     this._logger                       = logger;
     this._webHelper                    = webHelper;
     this._paymentSettings              = paymentSettings;
     this._payPalPlusPaymentSettings    = payPalPlusPaymentSettings;
     this._shoppingCartSettings         = shoppingCartSettings;
     this._orderTotalCalculationService = orderTotalCalculationService;
     this._payPalIPNService             = payPalIPNService;
     this._notificationService          = notificationService;
 }
        private async Task <PayPalPaymentCreatedResponse> CreatePaypalPaymentAsync(string host, AuthToken accessToken, Customer customer, PayPalPlusPaymentSettings paypalPlusPaymentSettings, DetailsAmountInfo amountInfo, List <Item> items, decimal totalAdjust)
        {
            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            HttpClient http = new HttpClient
            {
                BaseAddress = new Uri(host),
                Timeout     = TimeSpan.FromSeconds(30),
            };

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "v1/payments/payment");

            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Access_Token);
            request.Headers.Add("PayPal-Partner-Attribution-Id", "TecnofinPPP_Cart");
            // var model = new CreatePaymentModel();
            var    shippingOption     = _genericAttributeService.GetAttribute <ShippingOption>(customer, NopCustomerDefaults.SelectedShippingOptionAttribute, _storeContext.CurrentStore.Id);
            string shippingpreference = "SET_PROVIDED_ADDRESS";

            if (shippingOption == null || shippingOption.ShippingRateComputationMethodSystemName == "Pickup.PickupInStore")
            {
                shippingpreference = "NO_SHIPPING";
            }

            //get total discount amount
            var orderTotal = _orderTotalCalculationService.GetShoppingCartTotal(customer.ShoppingCartItems.ToList(),
                                                                                out decimal discountAmount,
                                                                                out List <DiscountForCaching> _, out List <AppliedGiftCard> _, out int _, out decimal _);

            if (discountAmount <= decimal.Zero)
            {
                discountAmount = 0;
            }
            var shippingRateComputationMethods = _shippingPluginManager
                                                 .LoadActivePlugins(customer, _storeContext.CurrentStore.Id);

            _orderTotalCalculationService.GetShoppingCartSubTotal(customer.ShoppingCartItems.ToList(), false,
                                                                  out decimal subdiscountAmount,
                                                                  out List <DiscountForCaching> subdiscountforCath, out decimal subTotalSinDescuento, out decimal subtotalConDescuento, out SortedDictionary <decimal, decimal> taxRates);
            var taxTotal      = _orderTotalCalculationService.GetTaxTotal(customer.ShoppingCartItems.ToList(), shippingRateComputationMethods, true);
            var shipping      = _orderTotalCalculationService.GetShoppingCartShippingTotal(customer.ShoppingCartItems.ToList(), shippingRateComputationMethods);
            var shippingTotal = shipping ?? 0;

            //List<RestObjects.Item> items = new List<RestObjects.Item>();
            //foreach (var ic in customer.ShoppingCartItems)
            //{
            //    var item = new RestObjects.Item
            //    {
            //        currency = paypalPlusPaymentSettings.Currency,
            //        name = ic.Product.Name,
            //        description = ic.Product.Name,
            //        quantity = ic.Quantity.ToString(),
            //        price = Math.Round(ic.Product.Price, 2).ToString(),
            //        sku = ic.Product.Sku,

            //    };
            //    items.Add(item);
            //}
            var itemList = new RestObjects.Items();

            itemList.items = items;
            if (shippingpreference == "SET_PROVIDED_ADDRESS")
            {
                var address = customer.ShippingAddress;
                itemList.shipping_address = new RestObjects.ShippingAddressInfo()
                {
                    city           = address.City,
                    country_code   = address.Country.TwoLetterIsoCode,
                    postal_code    = address.ZipPostalCode,
                    phone          = address.PhoneNumber,
                    state          = address.StateProvince.Name,
                    recipient_name = "direccion",
                    line1          = address.Address1,
                    line2          = address.Address2,
                };
            }

            List <RestObjects.Transactions> transactions = new List <RestObjects.Transactions>();

            RestObjects.Transactions transaction = new RestObjects.Transactions()
            {
                amount = new RestObjects.AmountInfo
                {
                    currency = paypalPlusPaymentSettings.Currency,
                    details  = amountInfo,
                    total    = Math.Round(totalAdjust, 2).ToString(),
                    //details = new DetailsAmountInfo
                    //{
                    //    subtotal = Math.Round(subtotalConDescuento, 2).ToString(),
                    //    shipping = Math.Round(shippingTotal, 2).ToString(),
                    //    shipping_discount = Math.Round(discountAmount, 2).ToString(), // ver descuentos
                    //},
                    //total = Math.Round(orderTotal.Value, 2).ToString(),
                },

                description     = "Compra en:" + _storeContext.CurrentStore.Name,
                custom          = "Compra en:" + _storeContext.CurrentStore.Name,
                notify_url      = new Uri(new Uri(_storeContext.CurrentStore.Url), "Plugins/PaymentPayPalPlus/IPNHandler").ToString(),
                payment_options = new RestObjects.PaymentOptions(),
                item_list       = itemList,
            };
            transactions.Add(transaction);

            RestObjects.Payment paymentRest = new RestObjects.Payment
            {
                intent = "sale",
                application_context = new RestObjects.AplicationContext()
                {
                    shipping_preference = shippingpreference
                },
                payer         = new RestObjects.Payer(),
                transactions  = transactions,
                redirect_urls = new RestObjects.RedirectUrlsInfo
                {
                    return_url = $"{_webHelper.GetStoreLocation()}Plugins/PaymentPayPalPlus/AprovalOrder",
                    cancel_url = $"{_webHelper.GetStoreLocation()}Plugins/PaymentPayPalPlus/CancelOrder",
                },
            };
            var data = Newtonsoft.Json.JsonConvert.SerializeObject(paymentRest);
            PayPalPaymentCreatedResponse paypalPaymentCreated = new PayPalPaymentCreatedResponse();

            try
            {
                request.Content = new StringContent(data, Encoding.UTF8, "application/json");
                HttpResponseMessage response = await http.SendAsync(request);

                string content = await response.Content.ReadAsStringAsync();

                paypalPaymentCreated = JsonConvert.DeserializeObject <PayPalPaymentCreatedResponse>(content);
            }
            catch (TaskCanceledException etce)
            {
                _logger.Error("PayPalPlus IPN. error_ ", new NopException(etce.Message));
            }

            return(paypalPaymentCreated);
        }