public IOrderAddress ConvertToAddress(AddressModel addressModel, IOrderGroup orderGroup)
        {
            var address = orderGroup.CreateOrderAddress(_orderGroupFactory, addressModel.Name);

            MapToAddress(addressModel, address);
            return(address);
        }
Example #2
0
        /// <summary>
        /// Creates a new <see cref="IOrderAddress"/> based on <paramref name="contactAddress"/>.
        /// </summary>
        /// <param name="orderGroup">The order group used for creating the new address.</param>
        /// <param name="contactAddress">The address from which all information is gathered.</param>
        /// <returns>An instance of a new <see cref="IOrderAddress"/>.</returns>
        private IOrderAddress CreateOrderAddress(IOrderGroup orderGroup, CustomerAddress contactAddress)
        {
            var orderAddress = orderGroup.CreateOrderAddress(_orderGroupFactory.Service, contactAddress.Name);

            orderAddress.Id                 = contactAddress.Name;
            orderAddress.City               = contactAddress.City;
            orderAddress.CountryCode        = contactAddress.CountryCode;
            orderAddress.CountryName        = contactAddress.CountryName;
            orderAddress.DaytimePhoneNumber = contactAddress.DaytimePhoneNumber;
            orderAddress.Email              = contactAddress.Email;
            orderAddress.EveningPhoneNumber = contactAddress.EveningPhoneNumber;
            orderAddress.FirstName          = contactAddress.FirstName;
            orderAddress.LastName           = contactAddress.LastName;
            orderAddress.Line1              = contactAddress.Line1;
            orderAddress.Line2              = contactAddress.Line2;
            orderAddress.Organization       = contactAddress.Organization;
            orderAddress.PostalCode         = contactAddress.PostalCode;
            orderAddress.RegionCode         = contactAddress.RegionCode;
            orderAddress.RegionName         = contactAddress.RegionName;

            return(orderAddress);
        }
Example #3
0
        /// <summary>
        /// Processes the successful transaction, was called when PayPal.com redirect back.
        /// </summary>
        /// <param name="orderGroup">The order group that was processed.</param>
        /// <param name="payment">The order payment.</param>
        /// <param name="acceptUrl">The redirect url when finished.</param>
        /// <param name="cancelUrl">The redirect url when error happens.</param>
        /// <returns>The url redirection after process.</returns>
        public string ProcessSuccessfulTransaction(IOrderGroup orderGroup, IPayment payment, string acceptUrl, string cancelUrl)
        {
            if (HttpContext.Current == null)
            {
                return(cancelUrl);
            }

            if (HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session.Remove("LastCouponCode");
            }

            if (!(orderGroup is ICart cart))
            {
                // return to the shopping cart page immediately and show error messages
                return(ProcessUnsuccessfulTransaction(cancelUrl, Utilities.Translate("CommitTranErrorCartNull")));
            }

            string redirectionUrl;

            using (var scope = new TransactionScope())
            {
                SetSecurityProtocolToTls12();

                var getDetailRequest = new GetExpressCheckoutDetailsRequestType
                {
                    Token = payment.Properties[PayPalExpTokenPropertyName] as string // Add request-specific fields to the request.
                };

                // Execute the API operation and obtain the response.
                var caller             = PayPalApiHelper.GetPayPalApiCallerServices(_paymentMethodConfiguration);
                var getDetailsResponse = caller.GetExpressCheckoutDetails(new GetExpressCheckoutDetailsReq
                {
                    GetExpressCheckoutDetailsRequest = getDetailRequest
                });

                var errorCheck = _payPalApiHelper.CheckErrors(getDetailsResponse);
                if (!string.IsNullOrEmpty(errorCheck))
                {
                    RestoreSecurityProtocol();

                    // unsuccessful get detail call
                    return(ProcessUnsuccessfulTransaction(cancelUrl, errorCheck));
                }

                var expressCheckoutDetailsResponse = getDetailsResponse.GetExpressCheckoutDetailsResponseDetails;
                // get commerceOrderId from what we put to PayPal instead of getting from cookie
                payment.Properties[PayPalOrderNumberPropertyName] = expressCheckoutDetailsResponse.InvoiceID;

                //process details sent from paypal, changing addresses if required
                string emptyAddressMsg;

                //process billing address
                var payPalBillingAddress = expressCheckoutDetailsResponse.BillingAddress;
                if (payPalBillingAddress != null && AddressHandling.IsAddressChanged(payment.BillingAddress, payPalBillingAddress))
                {
                    emptyAddressMsg = _payPalApiHelper.ProcessOrderAddress(expressCheckoutDetailsResponse.PayerInfo, payPalBillingAddress, payment.BillingAddress, CustomerAddressTypeEnum.Billing, "CommitTranErrorPayPalBillingAddressEmpty");
                    if (!string.IsNullOrEmpty(emptyAddressMsg))
                    {
                        RestoreSecurityProtocol();

                        return(ProcessUnsuccessfulTransaction(cancelUrl, emptyAddressMsg));
                    }
                }

                //process shipping address
                var payPalShippingAddress = expressCheckoutDetailsResponse.PaymentDetails[0].ShipToAddress;
                if (payPalShippingAddress != null && AddressHandling.IsAddressChanged(cart.GetFirstShipment().ShippingAddress, payPalShippingAddress))
                {
                    //when address was changed on PayPal site, it might cause changing tax value changed and changing order value also.
                    var taxValueBefore = _taxCalculator.GetTaxTotal(cart, cart.Market, cart.Currency);

                    var shippingAddress = orderGroup.CreateOrderAddress("address");

                    emptyAddressMsg = _payPalApiHelper.ProcessOrderAddress(expressCheckoutDetailsResponse.PayerInfo, payPalShippingAddress, shippingAddress, CustomerAddressTypeEnum.Shipping, "CommitTranErrorPayPalShippingAddressEmpty");
                    if (!string.IsNullOrEmpty(emptyAddressMsg))
                    {
                        RestoreSecurityProtocol();

                        return(ProcessUnsuccessfulTransaction(cancelUrl, emptyAddressMsg));
                    }

                    cart.GetFirstShipment().ShippingAddress = shippingAddress;

                    var taxValueAfter = _taxCalculator.GetTaxTotal(cart, cart.Market, cart.Currency);
                    if (taxValueBefore != taxValueAfter)
                    {
                        RestoreSecurityProtocol();

                        _orderRepository.Save(cart); // Saving cart to submit order address changed.
                        scope.Complete();
                        return(ProcessUnsuccessfulTransaction(cancelUrl, Utilities.Translate("ProcessPaymentTaxValueChangedWarning")));
                    }
                }

                // Add request-specific fields to the request.
                // Create the request details object.
                var doExpressChkOutPaymentReqDetails = CreateExpressCheckoutPaymentRequest(getDetailsResponse, orderGroup, payment);

                // Execute the API operation and obtain the response.
                var doCheckOutResponse = caller.DoExpressCheckoutPayment(new DoExpressCheckoutPaymentReq
                {
                    DoExpressCheckoutPaymentRequest = new DoExpressCheckoutPaymentRequestType(doExpressChkOutPaymentReqDetails)
                });

                errorCheck = _payPalApiHelper.CheckErrors(doCheckOutResponse);
                if (!string.IsNullOrEmpty(errorCheck))
                {
                    RestoreSecurityProtocol();

                    // unsuccessful doCheckout response
                    return(ProcessUnsuccessfulTransaction(cancelUrl, errorCheck));
                }

                // everything is fine, this is a flag to tell ProcessPayment know about this case: redirect back from PayPal with accepted payment
                var errorMessages = new List <string>();
                var cartCompleted = DoCompletingCart(cart, errorMessages);

                if (!cartCompleted)
                {
                    RestoreSecurityProtocol();

                    return(UriSupport.AddQueryString(cancelUrl, "message", string.Join(";", errorMessages.Distinct().ToArray())));
                }

                // Place order
                var purchaseOrder = MakePurchaseOrder(doCheckOutResponse, cart, payment);

                // Commit changes
                scope.Complete();

                redirectionUrl = CreateRedirectionUrl(purchaseOrder, acceptUrl, payment.BillingAddress.Email);

                RestoreSecurityProtocol();
            }

            _logger.Information($"PayPal transaction succeeds, redirect end user to {redirectionUrl}");
            return(redirectionUrl);
        }