Beispiel #1
0
        public SetExpressCheckoutResponseType SetExpressCheckout(PaymentDetailsItemType[] itemsDetails,
                                                                 string itemsTotal, string taxTotal, string orderTotal,
                                                                 string returnURL, string cancelURL, PaymentActionCodeType paymentAction, CurrencyCodeType currencyCodeType,
                                                                 SolutionTypeType solutionType, string invoiceId, bool isNonShipping)
        {
            // Create the request object
            var pp_request = new SetExpressCheckoutRequestType
            {
                // Create the request details object
                SetExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType
                {
                    CancelURL             = cancelURL,
                    ReturnURL             = returnURL,
                    NoShipping            = isNonShipping ? "1" : "0",
                    SolutionType          = solutionType,
                    SolutionTypeSpecified = true
                }
            };

            //pp_request.SetExpressCheckoutRequestDetails.ReqBillingAddress = (isNonShipping ? "1" : "0");

            var paymentDetails = new PaymentDetailsType
            {
                InvoiceID              = invoiceId,
                PaymentAction          = paymentAction,
                PaymentActionSpecified = true,
                PaymentDetailsItem     = itemsDetails,
                ItemTotal              = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = itemsTotal
                },
                TaxTotal = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = taxTotal
                },
                OrderTotal = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = orderTotal
                }
            };

            pp_request.SetExpressCheckoutRequestDetails.PaymentDetails = new[] { paymentDetails };

            return((SetExpressCheckoutResponseType)caller.Call("SetExpressCheckout", pp_request));
        }
        public ActionResult PaypalExpress(CheckoutModel model, SolutionTypeType solutionType = SolutionTypeType.SOLE)
        {
            // Create request object
            var request = new SetExpressCheckoutRequestType();
            var ecDetails = new SetExpressCheckoutRequestDetailsType
            {
                CallbackTimeout = "3",
                ReturnURL = Url.Action("PaypalExpressSuccess", "Checkout", null, "http"),
                CancelURL = Url.Action("Index", "Checkout", null, "http"),
                SolutionType = solutionType
            };

            var currency = (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), Ch.CustomerSession.Currency.ToUpper());
            model = PrepareCheckoutModel(model);
            model.Payments = model.Payments ?? GetPayments().ToArray();
            var payment = _paymentClient.GetPaymentMethod(model.PaymentMethod ?? "Paypal");
            var configMap = payment.CreateSettings();

            //Create Shipping methods
            var shippingMethods = GetShipinngMethodModels();
            var noShippingMethod = !shippingMethods.Any(x => x.IsCurrent);
            string currentShippingOption = null;

            for (var i = 0; i < shippingMethods.Length; i++)
            {
                var shipping = shippingMethods[i];
                var shippingOptionIsDefault = "0";
                if (shipping.IsCurrent || noShippingMethod && i == 0)
                {
                    shippingOptionIsDefault = "1";
                    currentShippingOption = shipping.Method.Name;
                }

                ecDetails.FlatRateShippingOptions.Add(new ShippingOptionType
                {
                    ShippingOptionAmount = new BasicAmountType(currency, FormatMoney(shipping.Price)),
                    ShippingOptionIsDefault = shippingOptionIsDefault,
                    ShippingOptionName = shipping.Method.Name,
                });
            }

            var recalcualteCart = false;

            if (!string.Equals(model.ShippingMethod,currentShippingOption))
            {
                model.ShippingMethod = currentShippingOption;
                recalcualteCart = true;
            }

            if (!string.Equals(model.PaymentMethod, payment.Name))
            {
                model.PaymentMethod = payment.Name;
                recalcualteCart = true;
            }

            if (recalcualteCart)
            {
                //Must recalculate cart as prices could have changed
                RecalculateCart(model);
            }

            // (Optional) Email address of the buyer as entered during checkout. PayPal uses this value to pre-fill the PayPal membership sign-up portion on the PayPal pages.
            if (model.BillingAddress != null && !string.IsNullOrEmpty(model.BillingAddress.Address.Email))
            {
                ecDetails.BuyerEmail = model.BillingAddress.Address.Email;
            }

            ecDetails.NoShipping = "2";
            ecDetails.PaymentDetails.Add(GetPaypalPaymentDetail(currency, PaymentActionCodeType.SALE));
            ecDetails.MaxAmount = new BasicAmountType(currency, FormatMoney(Math.Max(Ch.Cart.Total, Ch.Cart.Subtotal)));
            ecDetails.LocaleCode = new RegionInfo(Thread.CurrentThread.CurrentUICulture.LCID).TwoLetterISORegionName;
            //paymentDetails.OrderDescription = Ch.Cart.Name;

            AddressModel modelAddress = null;

            if (!model.UseForShipping && model.ShippingAddress != null && DoValidateModel(model.ShippingAddress.Address))
            {
                modelAddress = model.ShippingAddress.Address;
            }
            else if (model.BillingAddress != null && DoValidateModel(model.BillingAddress.Address))
            {
                modelAddress = model.BillingAddress.Address;
            }

            if (modelAddress != null)
            {
                ecDetails.AddressOverride = "1";

                var shipAddress = new AddressType
                {
                    Name = string.Format("{0} {1}", modelAddress.FirstName, modelAddress.LastName),
                    Street1 = modelAddress.Line1,
                    Street2 = modelAddress.Line2,
                    CityName = modelAddress.City,
                    StateOrProvince = modelAddress.StateProvince,
                    Country = (CountryCodeType)Enum.Parse(typeof(CountryCodeType), modelAddress.CountryCode.Substring(0, 2)),
                    PostalCode = modelAddress.PostalCode,
                    Phone = modelAddress.DaytimePhoneNumber
                };
                ecDetails.PaymentDetails[0].ShipToAddress = shipAddress;
            }

            request.SetExpressCheckoutRequestDetails = ecDetails;


            // Invoke the API
            var wrapper = new SetExpressCheckoutReq { SetExpressCheckoutRequest = request };

            // Create the PayPalAPIInterfaceServiceService service object to make the API call
            var service = new PayPalAPIInterfaceServiceService(configMap);

            SetExpressCheckoutResponseType setEcResponse = null;

            try
            {
                setEcResponse = service.SetExpressCheckout(wrapper);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", @"Paypal failure".Localize());
                ModelState.AddModelError("", ex.Message);
            }

            if (setEcResponse != null)
            {
                // Check for API return status
                if (setEcResponse.Ack.Equals(AckCodeType.FAILURE) ||
                    (setEcResponse.Errors != null && setEcResponse.Errors.Count > 0))
                {
                    ModelState.AddModelError("", @"Paypal failure".Localize());

                    foreach (var error in setEcResponse.Errors)
                    {
                        ModelState.AddModelError("", error.LongMessage);
                    }
                }
                else
                {
                    var redirectUrl =
                        string.Format(
                            configMap.ContainsKey("URL")
                                ? configMap["URL"]
                                : "https://www.sandbox.paypal.com/webscr&amp;cmd={0}",
                            "_express-checkout&token=" + setEcResponse.Token);
                    TempData.Add("checkout_" + setEcResponse.Token,model);
                    return Redirect(redirectUrl);
                }
            }

            return View("Index", model);
        }
Beispiel #3
0
        public SetExpressCheckoutResponseType SetExpressCheckout(PaymentDetailsItemType[] itemsDetails,
                                                                 string itemsTotal, string taxTotal, string shippingTotal,
                                                                 string orderTotal, string returnURL, string cancelURL, PaymentActionCodeType paymentAction,
                                                                 CurrencyCodeType currencyCodeType, SolutionTypeType solutionType, string name,
                                                                 string countryISOCode, string street1, string street2, string city, string region, string postalCode,
                                                                 string phone, string invoiceId, bool isNonShipping)
        {
            // Create the request object
            var pp_request = new SetExpressCheckoutRequestType
            {
                SetExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType
                {
                    CancelURL             = cancelURL,
                    ReturnURL             = returnURL,
                    AddressOverride       = "1",
                    NoShipping            = isNonShipping ? "1" : "0",
                    SolutionType          = solutionType,
                    SolutionTypeSpecified = true
                }
            };

            // Create the request details object

            //pp_request.SetExpressCheckoutRequestDetails.ReqBillingAddress = (isNonShipping ? "1" : "0");

            var paymentDetails = new PaymentDetailsType
            {
                InvoiceID              = invoiceId,
                PaymentAction          = paymentAction,
                PaymentActionSpecified = true,
                PaymentDetailsItem     = itemsDetails,
                ItemTotal              = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = itemsTotal
                },
                TaxTotal = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = taxTotal
                },
                ShippingTotal = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = shippingTotal
                },
                OrderTotal = new BasicAmountType
                {
                    currencyID = currencyCodeType,
                    Value      = orderTotal
                },
                ShipToAddress = new AddressType
                {
                    AddressStatusSpecified = false,
                    AddressOwnerSpecified  = false,
                    Street1          = street1,
                    Street2          = street2,
                    CityName         = city,
                    StateOrProvince  = region,
                    PostalCode       = postalCode,
                    CountrySpecified = true,
                    Country          = GetCountryCode(countryISOCode),
                    Phone            = phone,
                    Name             = name
                }
            };

            pp_request.SetExpressCheckoutRequestDetails.PaymentDetails = new[] { paymentDetails };

            return((SetExpressCheckoutResponseType)caller.Call("SetExpressCheckout", pp_request));
        }