Esempio n. 1
0
        public string MerchantSessionKey()
        {
            var processor = new SagePayPi();
            var key       = processor.ObtainSagePayPiMerchantSessionKey();

            return(key);
        }
Esempio n. 2
0
        public ActionResult SagePayPiCreditCard(FormCollection collection)
        {
            var cardErrorSegments = collection["sagePayPiCardError"]
                                    .ParseAsDelimitedList('|');

            if (cardErrorSegments.FirstOrDefault() == "ERROR")
            {
                var error = cardErrorSegments
                            .Skip(1)
                            .FirstOrDefault();

                if (string.IsNullOrEmpty(error) || error.Contains("\"httpErrorCode\":401"))
                {
                    NoticeProvider.PushNotice(StringResourceProvider.GetString("sagepaypi.payment.addingdetailserror"), NoticeType.Failure);
                    return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
                }

                var sagePayPi    = new SagePayPi();
                var errorObject  = Newtonsoft.Json.Linq.JObject.Parse(error);
                var errorDetails = sagePayPi.GetResponseError(errorObject, "errors");
                var errorMessage = string.Format("{0} {1}", StringResourceProvider.GetString("sagepaypi.payment.carderrorprompt"), errorDetails);

                NoticeProvider.PushNotice(errorMessage, NoticeType.Failure);
                return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
            }

            var customer = HttpContext.GetCustomer();
            var session  = new CustomerSession(customer.CustomerID);

            session[AppLogic.SagePayPiMerchantSessionKey] = collection["sagePayPiMerchantSessionKey"];

            var updatedPersistedCheckoutContext = new PersistedCheckoutContextBuilder()
                                                  .From(PersistedCheckoutContextProvider.LoadCheckoutContext(customer))
                                                  .WithCreditCard(new CreditCardDetails(
                                                                      name: null,
                                                                      number: null,
                                                                      issueNumber: null,
                                                                      cardType: collection["sagePayPiCardType"],
                                                                      expirationDate: null,
                                                                      startDate: null,
                                                                      cvv: null))
                                                  .WithSagePayPi(new SagePayPiDetails(
                                                                     cardIdentifier: collection["sagePayPiCardIdentifier"],
                                                                     merchantSessionId: collection["sagePayPiMerchantSessionKey"],
                                                                     paymentMethod: Gateway.SagePayPiCreditCardKey, //This is the Sage Pay PI payment method, not ours
                                                                     threeDSecureApproved: false))
                                                  .WithoutOffsiteRequiredBillingAddressId()
                                                  .WithoutOffsiteRequiredShippingAddressId()
                                                  .Build();

            PersistedCheckoutContextProvider.SaveCheckoutContext(customer, updatedPersistedCheckoutContext);

            customer.UpdateCustomer(requestedPaymentMethod: AppLogic.ro_PMCreditCard);

            return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
        }
        public ActionResult SagePayPiPaRes(string paRes, string mD)
        {
            var orderStatus         = StringResourceProvider.GetString("sagepaypi.error.unknownerror");
            var sagePayPi           = new SagePayPi();
            var customer            = HttpContext.GetCustomer();
            var session             = new CustomerSession(customer.CustomerID);
            var orderNumber         = customer.ThisCustomerSession.SessionUSInt("3Dsecure.OrderNumber");
            var useLiveTransactions = AppConfigProvider.GetAppConfigValue <bool>("UseLiveTransactions");

            var transactionUrl = string.Format(
                "{0}transactions/{1}",
                useLiveTransactions
                                        ? AppConfigProvider.GetAppConfigValue("SagePayPi.LiveUrl")
                                        : AppConfigProvider.GetAppConfigValue("SagePayPi.TestUrl"),
                session[AppLogic.SagePayPiMd]);

            var threeDSecureTransactionUrl = $"{transactionUrl}/3d-secure";

            var jsonObject = new JObject(
                new JProperty("paRes", paRes)
                );

            var formattedResponse           = JObject.Parse(sagePayPi.SagePayPiApiCall(jsonObject.ToString(), threeDSecureTransactionUrl, "POST"));
            var transactionResponseHasError = sagePayPi.ResponseHasError(formattedResponse, "status", "authenticated") &&
                                              sagePayPi.ResponseHasError(formattedResponse, "status", "attemptonly");

            if (transactionResponseHasError)
            {
                if (AppConfigProvider.GetAppConfigValue <bool>("sagepaypi.customerfriendlyerrors"))
                {
                    NoticeProvider.PushNotice(string.Format(
                                                  "{0} {1}",
                                                  StringResourceProvider.GetString("sagepaypi.threedsecure.didnotauthenticate"),
                                                  StringResourceProvider.GetString("sagepaypi.error.reentercarddetails")), NoticeType.Failure);
                }
                else
                {
                    orderStatus = sagePayPi.GetResponseError(formattedResponse, "status");

                    if (orderStatus.EqualsIgnoreCase(StringResourceProvider.GetString("sagepaypi.error.unknownresponseerror")))
                    {
                        orderStatus = sagePayPi.GetResponseError(formattedResponse, "statusDetail");
                    }
                    else
                    {
                        orderStatus = sagePayPi.GetThreeDSecureStatus(sagePayPi.GetResponseError(formattedResponse, "status"));
                    }

                    if (orderStatus.EqualsIgnoreCase(StringResourceProvider.GetString("sagepaypi.error.unknownresponseerror")))
                    {
                        orderStatus = sagePayPi.GetResponseError(formattedResponse, "description");
                    }

                    //display error when 3-D secure does not authenticate
                    NoticeProvider.PushNotice(string.Format(
                                                  "{0} {1} {2} {3}.",
                                                  StringResourceProvider.GetString("sagepaypi.threedsecure.didnotauthenticate"),
                                                  StringResourceProvider.GetString("sagepaypi.error.reentercarddetails"),
                                                  StringResourceProvider.GetString("sagepaypi.status.reason"),
                                                  orderStatus.TrimEnd('.')), NoticeType.Failure);
                }

                if (orderNumber > 0)
                {
                    sagePayPi.LogFailedTransaction($"URL: {threeDSecureTransactionUrl}, Request: {jsonObject}", formattedResponse.ToString(), orderNumber);
                }

                sagePayPi.ClearPaymentMethod(customer.CustomerID);
                return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
            }

            //retrieve transaction
            var emptyObject = "{}";
            var formattedTransactionResponse         = JObject.Parse(sagePayPi.SagePayPiApiCall(emptyObject.ToString(), transactionUrl, "GET"));
            var formattedTransactionResponseHasError = sagePayPi.ResponseHasError(formattedTransactionResponse, "status", "ok") &&
                                                       sagePayPi.ResponseHasError(formattedTransactionResponse, "status", "attemptonly");

            //if the transaction did not submit properly, return early, display an error from sage pay and do not make the order
            if (formattedTransactionResponseHasError)
            {
                var threeDSecureStatus = sagePayPi.GetThreeDSecureStatus(string.Empty);

                if (formattedTransactionResponse != null && formattedTransactionResponse["3DSecure"] != null && formattedTransactionResponse["3DSecure"]["status"] != null)
                {
                    threeDSecureStatus = sagePayPi.GetThreeDSecureStatus(formattedTransactionResponse["3DSecure"]["status"].ToString());
                }

                orderStatus = sagePayPi.GetResponseError(formattedTransactionResponse, "statusDetail");

                if (orderStatus.EqualsIgnoreCase(StringResourceProvider.GetString("sagepaypi.error.unknownresponseerror")))
                {
                    orderStatus = sagePayPi.GetResponseError(formattedTransactionResponse, "description");
                }

                if (AppConfigProvider.GetAppConfigValue <bool>("sagepaypi.customerfriendlyerrors"))
                {
                    NoticeProvider.PushNotice(string.Format(
                                                  "{0} {1}",
                                                  StringResourceProvider.GetString("sagepaypi.threedsecure.didnotauthenticate"),
                                                  StringResourceProvider.GetString("sagepaypi.error.reentercarddetails")), NoticeType.Failure);
                }
                else
                {
                    NoticeProvider.PushNotice(string.Format(
                                                  "{0}. {1} {2}. {3}",
                                                  orderStatus.TrimEnd('.'),
                                                  StringResourceProvider.GetString("sagepaypi.status.threedsecure"),
                                                  threeDSecureStatus.TrimEnd('.'),
                                                  StringResourceProvider.GetString("sagepaypi.error.reentercarddetails")), NoticeType.Failure);
                }

                if (orderNumber > 0)
                {
                    sagePayPi.LogFailedTransaction($"GET Method - URL: {transactionUrl}", formattedTransactionResponse.ToString(), customer.ThisCustomerSession.SessionUSInt("3Dsecure.OrderNumber"));
                }

                sagePayPi.ClearPaymentMethod(customer.CustomerID);
                return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
            }

            var persistedCheckoutContext = PersistedCheckoutContextProvider.LoadCheckoutContext(customer);
            var cart = CachedShoppingCartProvider.Get(customer, CartTypeEnum.ShoppingCart, AppLogic.StoreID());

            var updatedPersistedCheckoutContext = new PersistedCheckoutContextBuilder()
                                                  .From(persistedCheckoutContext)
                                                  .WithCreditCard(new CreditCardDetails(
                                                                      name: persistedCheckoutContext.CreditCard.Name,
                                                                      number: persistedCheckoutContext.CreditCard.Number,
                                                                      issueNumber: persistedCheckoutContext.CreditCard.IssueNumber,
                                                                      cardType: formattedTransactionResponse["paymentMethod"]["card"]["cardType"].ToString(),
                                                                      expirationDate: persistedCheckoutContext.CreditCard.ExpirationDate,
                                                                      startDate: persistedCheckoutContext.CreditCard.StartDate,
                                                                      cvv: persistedCheckoutContext.CreditCard.Cvv))
                                                  .WithSagePayPi(new SagePayPiDetails(
                                                                     cardIdentifier: persistedCheckoutContext.SagePayPi.CardIdentifier,
                                                                     merchantSessionId: persistedCheckoutContext.SagePayPi.MerchantSessionId,
                                                                     paymentMethod: persistedCheckoutContext.SagePayPi.PaymentMethod, //This is the Sage Pay PI payment method, not ours
                                                                     threeDSecureApproved: true))
                                                  .WithoutOffsiteRequiredBillingAddressId()
                                                  .WithoutOffsiteRequiredShippingAddressId()
                                                  .Build();

            PersistedCheckoutContextProvider.SaveCheckoutContext(customer, updatedPersistedCheckoutContext);

            customer.ThisCustomerSession[AppLogic.SagePayPi3dSecureKey]   = "true";
            customer.ThisCustomerSession[AppLogic.SagePayPiPaymentMethod] = persistedCheckoutContext.SagePayPi.PaymentMethod;

            orderStatus = Gateway.MakeOrder(string.Empty, AppLogic.TransactionMode(), cart, orderNumber, string.Empty, string.Empty, string.Empty, string.Empty);
            ClearThreeDSecureSessionInfo(customer);

            if (orderStatus == AppLogic.ro_OK)
            {
                return(RedirectToAction(
                           ActionNames.Confirmation,
                           ControllerNames.CheckoutConfirmation,
                           new { orderNumber = orderNumber }));
            }

            //display error if we reach this point, we should have redirected by now
            if (AppConfigProvider.GetAppConfigValue <bool>("sagepaypi.customerfriendlyerrors"))
            {
                NoticeProvider.PushNotice(string.Format(
                                              "{0} {1}",
                                              StringResourceProvider.GetString("sagepaypi.threedsecure.didnotauthenticate"),
                                              StringResourceProvider.GetString("sagepaypi.error.reentercarddetails")), NoticeType.Failure);
            }
            else
            {
                NoticeProvider.PushNotice(string.Format(
                                              "{0} {1}",
                                              string.Format(StringResourceProvider.GetString("secureprocess.aspx.5"), orderStatus.TrimEnd('.')),
                                              StringResourceProvider.GetString("sagepaypi.error.reentercarddetails")), NoticeType.Failure);
            }

            if (orderNumber > 0)
            {
                sagePayPi.LogFailedTransaction($"URL: {threeDSecureTransactionUrl}, Request: {jsonObject}", formattedResponse.ToString(), orderNumber);
            }

            sagePayPi.ClearPaymentMethod(customer.CustomerID);
            return(RedirectToAction(ActionNames.Index, ControllerNames.Checkout));
        }