Esempio n. 1
0
        public static void GetPaypalRequirements(out PayPalAPISoapBinding IPayPalRefund, out PayPalAPIAASoapBinding IPayPal)
        {
            IPayPal       = new PayPalAPIAASoapBinding();
            IPayPalRefund = new PayPalAPISoapBinding();

            if (AppLogic.AppConfigBool("UseLiveTransactions"))
            {
                IPayPal.Url = AppLogic.AppConfig("PayPal.API.LiveURL");
            }
            else
            {
                IPayPal.Url = AppLogic.AppConfig("PayPal.API.TestURL");
            }
            IPayPalRefund.Url = IPayPal.Url;

            IPayPal.UserAgent       = HttpContext.Current.Request.UserAgent;
            IPayPalRefund.UserAgent = IPayPal.UserAgent;

            UserIdPasswordType PayPalUser = new UserIdPasswordType();

            if (PayPalController.GetAppropriateExpressType() == ExpressAPIType.PayPalAcceleratedBording)
            {
                PayPalUser.Subject = AppLogic.AppConfig("PayPal.API.AcceleratedBoardingEmailAddress");
            }
            else
            {
                PayPalUser.Username  = AppLogic.AppConfig("PayPal.API.Username");
                PayPalUser.Password  = AppLogic.AppConfig("PayPal.API.Password");
                PayPalUser.Signature = AppLogic.AppConfig("PayPal.API.Signature");

                //Subject should be the Sellers e-mail address (if you are using 3-part API calls) with the correct account permissions. You also have
                //set up permissions for this e-mail address for the "type" of transaction you want to allow.
                //This access changes are made in the Sandbox.
                //The name of the entity on behalf of which this profile is issuing calls
                //This is for Third-Party access
                // You have to set up Virtual Terminals and complete the Billing Agreement in the Sandbox before you can make Direct Payments
                PayPalUser.Subject = AppLogic.AppConfig("PayPal.API.MerchantEMailAddress");
            }



            CustomSecurityHeaderType CSecHeaderType = new CustomSecurityHeaderType();

            CSecHeaderType.Credentials    = PayPalUser;
            CSecHeaderType.MustUnderstand = true;

            IPayPal.RequesterCredentials       = CSecHeaderType;
            IPayPalRefund.RequesterCredentials = CSecHeaderType;
        }
Esempio n. 2
0
        public static String ProcessEC(ShoppingCart cart, decimal OrderTotal, int OrderNumber, String PayPalToken, String PayerID, String TransactionMode, out String AuthorizationResult, out String AuthorizationTransID)
        {
            PayPalAPISoapBinding   IPayPalRefund;
            PayPalAPIAASoapBinding IPayPal;

            PayPalController.GetPaypalRequirements(out IPayPalRefund, out IPayPal);
            String result = String.Empty;

            AuthorizationResult  = String.Empty;
            AuthorizationTransID = String.Empty;

            DoExpressCheckoutPaymentReq                 ECRequest           = new DoExpressCheckoutPaymentReq();
            DoExpressCheckoutPaymentRequestType         varECRequest        = new DoExpressCheckoutPaymentRequestType();
            DoExpressCheckoutPaymentRequestDetailsType  varECRequestDetails = new DoExpressCheckoutPaymentRequestDetailsType();
            DoExpressCheckoutPaymentResponseType        ECResponse          = new DoExpressCheckoutPaymentResponseType();
            DoExpressCheckoutPaymentResponseDetailsType varECResponse       = new DoExpressCheckoutPaymentResponseDetailsType();

            ECRequest.DoExpressCheckoutPaymentRequest           = varECRequest;
            varECRequest.DoExpressCheckoutPaymentRequestDetails = varECRequestDetails;
            ECResponse.DoExpressCheckoutPaymentResponseDetails  = varECResponse;

            varECRequestDetails.Token   = PayPalToken;
            varECRequestDetails.PayerID = PayerID;

            varECRequestDetails.PaymentAction = PaymentActionCodeType.Authorization;
            if (TransactionMode == AppLogic.ro_TXModeAuthCapture || AppLogic.AppConfigBool("PayPal.ForceCapture") || PayPalController.GetAppropriateExpressType() == ExpressAPIType.PayPalAcceleratedBording)
            {
                varECRequestDetails.PaymentAction = PaymentActionCodeType.Sale;
            }
            varECRequestDetails.PaymentActionSpecified = true;

            PaymentDetailsType ECPaymentDetails    = new PaymentDetailsType();
            BasicAmountType    ECPaymentOrderTotal = new BasicAmountType();

            ECPaymentOrderTotal.Value      = Localization.CurrencyStringForGatewayWithoutExchangeRate(OrderTotal);
            ECPaymentOrderTotal.currencyID =
                (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), AppLogic.AppConfig("Localization.StoreCurrency"), true);
            ECPaymentDetails.InvoiceID    = OrderNumber.ToString();
            ECPaymentDetails.Custom       = cart.ThisCustomer.CustomerID.ToString();
            ECPaymentDetails.ButtonSource = PayPalController.BN + "_EC_US";
            ECPaymentDetails.NotifyURL    = AppLogic.GetStoreHTTPLocation(true) + AppLogic.AppConfig("PayPal.NotificationURL");

            varECRequest.Version = API_VER;

            ECPaymentDetails.OrderTotal = ECPaymentOrderTotal;

            List <PaymentDetailsType> ECPaymentDetailsList = new List <PaymentDetailsType>();

            ECPaymentDetailsList.Add(ECPaymentDetails);

            varECRequestDetails.PaymentDetails = ECPaymentDetailsList.ToArray();

            ECResponse = IPayPal.DoExpressCheckoutPayment(ECRequest);

            if (ECResponse.Ack.ToString().StartsWith("success", StringComparison.InvariantCultureIgnoreCase))
            {
                AuthorizationTransID = CommonLogic.IIF(varECRequestDetails.PaymentAction == PaymentActionCodeType.Sale, "CAPTURE=", "AUTH=") + ECResponse.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID;
                result = AppLogic.ro_OK;
                AuthorizationResult = ECResponse.Ack.ToString();
                SetECFaultRedirect(cart.ThisCustomer, string.Empty);
            }
            else
            {
                if (ECResponse.Errors != null)
                {
                    if (ECResponse.Errors.Any(e => e.ErrorCode == ECFaultRedirectErrorCode.ToString()))
                    {
                        // Express Checkout failed funding recovery feature returns a 10486 which signifies that
                        //  the user should be redirected back to paypal.
                        result =
                            AuthorizationResult = AppLogic.ro_PMPayPalExpressRedirect;
                    }
                    else
                    {
                        result =
                            AuthorizationResult = string.Join(",", ECResponse.Errors.Select(e => e.LongMessage).ToArray());
                    }
                }
            }

            return(result);
        }
Esempio n. 3
0
        public static string StartEC(ShoppingCart cart, bool boolBypassOrderReview, IDictionary <string, string> checkoutOptions)
        {
            var payPalRefund   = new PayPalAPISoapBinding();
            var payPalBinding  = new PayPalAPIAASoapBinding();
            var redirectUrl    = new StringBuilder();
            var ecOrderTotal   = new BasicAmountType();
            var request        = new SetExpressCheckoutReq();
            var requestType    = new SetExpressCheckoutRequestType();
            var requestDetails = new SetExpressCheckoutRequestDetailsType();
            var response       = new SetExpressCheckoutResponseType();
            var result         = string.Empty;
            var urlHelper      = DependencyResolver.Current.GetService <UrlHelper>();

            //Express checkout
            GetPaypalRequirements(out payPalRefund, out payPalBinding);

            ecOrderTotal.Value = Localization.CurrencyStringForGatewayWithoutExchangeRate(cart.Total(true));

            if (cart.HasRecurringComponents() && AppLogic.AppConfigBool("Recurring.UseGatewayInternalBilling"))
            {
                //Have to send extra details on the SetExpressCheckoutReq or the token will be invalid for creating a recurring profile later
                var recurringAgreement     = new BillingAgreementDetailsType();
                var recurringAgreementList = new List <BillingAgreementDetailsType>();

                recurringAgreement.BillingType = BillingCodeType.RecurringPayments;
                recurringAgreement.BillingAgreementDescription = "Recurring order created on " + System.DateTime.Now.ToShortDateString() + " from " + AppLogic.AppConfig("StoreName");
                recurringAgreementList.Add(recurringAgreement);
                requestDetails.BillingAgreementDetails = recurringAgreementList.ToArray();
            }

            request.SetExpressCheckoutRequest            = requestType;
            requestType.SetExpressCheckoutRequestDetails = requestDetails;

            ecOrderTotal.currencyID   = (CurrencyCodeType)Enum.Parse(typeof(CurrencyCodeType), AppLogic.AppConfig("Localization.StoreCurrency"), true);
            requestDetails.OrderTotal = ecOrderTotal;

            if (AppLogic.AppConfigBool("PayPal.RequireConfirmedAddress"))
            {
                requestDetails.ReqConfirmShipping = "1";
            }
            else
            {
                requestDetails.ReqConfirmShipping = "0";
            }

            requestDetails.ReturnURL = string.Format("{0}{1}",
                                                     AppLogic.GetStoreHTTPLocation(
                                                         useSsl: true,
                                                         includeScriptLocation: true,
                                                         noVirtualNoSlash: true),
                                                     urlHelper.Action(
                                                         actionName: ActionNames.PayPalExpressReturn,
                                                         controllerName: ControllerNames.PayPalExpress));
            if (boolBypassOrderReview)
            {
                requestDetails.ReturnURL = string.Format("{0}?BypassOrderReview=true", requestDetails.ReturnURL);
            }

            requestDetails.CancelURL = string.Format("{0}{1}",
                                                     AppLogic.GetStoreHTTPLocation(
                                                         useSsl: true,
                                                         includeScriptLocation: true,
                                                         noVirtualNoSlash: true),
                                                     urlHelper.Action(
                                                         actionName: ActionNames.Index,
                                                         controllerName: ControllerNames.Checkout));
            requestDetails.LocaleCode    = AppLogic.AppConfig("PayPal.DefaultLocaleCode");
            requestDetails.PaymentAction = PaymentActionCodeType.Authorization;

            if (AppLogic.TransactionModeIsAuthCapture() || AppLogic.AppConfigBool("PayPal.ForceCapture") || PayPalController.GetAppropriateExpressType() == ExpressAPIType.PayPalAcceleratedBording)
            {
                requestDetails.PaymentAction = PaymentActionCodeType.Sale;
            }

            requestDetails.SolutionType           = SolutionTypeType.Sole;
            requestDetails.PaymentActionSpecified = true;
            requestType.Version = API_VER;

            if (!string.IsNullOrWhiteSpace(AppLogic.AppConfig("PayPal.Express.PageStyle")))
            {
                requestDetails.PageStyle = AppLogic.AppConfig("PayPal.Express.PageStyle").Trim();
            }

            if (!string.IsNullOrWhiteSpace(AppLogic.AppConfig("PayPal.Express.HeaderImage")))
            {
                requestDetails.cppheaderimage = AppLogic.AppConfig("PayPal.Express.HeaderImage").Trim();
            }

            if (!string.IsNullOrWhiteSpace(AppLogic.AppConfig("PayPal.Express.HeaderBackColor")))
            {
                requestDetails.cppheaderbackcolor = AppLogic.AppConfig("PayPal.Express.HeaderBackColor").Trim();
            }

            if (!string.IsNullOrWhiteSpace(AppLogic.AppConfig("PayPal.Express.HeaderBorderColor")))
            {
                requestDetails.cppheaderbordercolor = AppLogic.AppConfig("PayPal.Express.HeaderBorderColor").Trim();
            }

            if (!string.IsNullOrWhiteSpace(AppLogic.AppConfig("PayPal.Express.PayFlowColor")))
            {
                requestDetails.cpppayflowcolor = AppLogic.AppConfig("PayPal.Express.PayFlowColor").Trim();
            }

            if (checkoutOptions != null && checkoutOptions.ContainsKey("UserSelectedFundingSource") && checkoutOptions["UserSelectedFundingSource"] == "BML")
            {
                var fundingSourceDetails = new FundingSourceDetailsType();

                fundingSourceDetails.AllowPushFunding                   = "0";
                fundingSourceDetails.UserSelectedFundingSource          = UserSelectedFundingSourceType.BML;
                fundingSourceDetails.UserSelectedFundingSourceSpecified = true;
                requestDetails.FundingSourceDetails = fundingSourceDetails;
            }

            try
            {
                response = payPalBinding.SetExpressCheckout(request);

                if (response.Ack.ToString().StartsWith("success", StringComparison.InvariantCultureIgnoreCase))
                {
                    result = AppLogic.ro_OK;
                }
                else
                {
                    if (response.Errors != null)
                    {
                        bool first = true;
                        for (int ix = 0; ix < response.Errors.Length; ix++)
                        {
                            if (!first)
                            {
                                result += ", ";
                            }
                            result += "Error: [" + response.Errors[ix].ErrorCode + "] " + response.Errors[ix].LongMessage;
                            first   = false;
                        }
                    }
                }
            }
            catch (Exception)
            {
                result = "Failed to start PayPal Express Checkout! Please try another payment method.";
            }

            if (result == AppLogic.ro_OK)
            {
                var useIntegratedCheckout = AppLogic.AppConfigBool("PayPal.Express.UseIntegratedCheckout");

                if (AppLogic.AppConfigBool("UseLiveTransactions") == true)
                {
                    redirectUrl.Append(useIntegratedCheckout
                                                ? AppLogic.AppConfig("PayPal.Express.IntegratedCheckout.LiveURL")
                                                : AppLogic.AppConfig("PayPal.Express.LiveURL"));
                }
                else
                {
                    redirectUrl.Append(useIntegratedCheckout
                                                ? AppLogic.AppConfig("PayPal.Express.IntegratedCheckout.SandboxURL")
                                                : AppLogic.AppConfig("PayPal.Express.SandboxURL"));
                }

                redirectUrl.Append(useIntegratedCheckout
                                        ? "?token="
                                        : "?cmd=_express-checkout&token=");

                redirectUrl.Append(response.Token);

                if (boolBypassOrderReview)
                {
                    redirectUrl.Append("&useraction=commit");
                }

                // Set active payment method to PayPalExpress
                DB.ExecuteSQL(string.Format("UPDATE Address SET PaymentMethodLastUsed={0} WHERE AddressID={1}",
                                            DB.SQuote(AppLogic.ro_PMPayPalExpress),
                                            cart.ThisCustomer.PrimaryBillingAddressID));

                SetECFaultRedirect(cart.ThisCustomer, redirectUrl.ToString());
            }
            else
            {
                var error = new ErrorMessage(HttpUtility.HtmlEncode(result));
                redirectUrl.Append(urlHelper.BuildCheckoutLink(error.MessageId));
            }

            return(redirectUrl.ToString());
        }