Пример #1
0
        public ActionResult Confirm()
        {
            //Assign value to form field from session
            CustomCart cart       = new CustomCart().Current();
            CustomForm customForm = new CustomForm();

            customForm.FirstName = cart.BillingFirstnames;
            customForm.LastName  = cart.BillingSurname;
            customForm.Address1  = cart.BillingAddress1;
            customForm.Address2  = cart.BillingAddress2;
            customForm.City      = cart.BillingCity;
            customForm.PostCode  = cart.BillingPostCode;
            customForm.Country   = cart.BillingCountry;
            customForm.Phone     = cart.Phone;
            customForm.DOB       = Convert.ToDateTime(cart.DOB);

            SagePayFormIntegration sagePayFormIntegration = new SagePayFormIntegration();

            IFormPayment request = sagePayFormIntegration.FormPaymentRequest();

            SetSagePayAPIData(request);

            var errors = sagePayFormIntegration.Validation(request);

            if (errors == null || errors.Count == 0)
            {
                sagePayFormIntegration.ProcessRequest(request);
                ViewBag.Crypt = request.Crypt;
            }
            else
            {
                ViewBag.Error = "Some error occurred";
            }
            return(View(customForm));
        }
Пример #2
0
        /// <summary>
        /// Create a dictionary with credentials for SagePay service.
        /// </summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        //private static Dictionary<string, string> CreateSagePayApiConfig(SagePayProcessorSettings settings)
        //{
        //    return new Dictionary<string, string>
        //            {
        //                {"mode", GetModeString(settings.LiveMode)},
        //                {"account.vendorName", settings.VendorName},
        //                {"account.encyptionPassword", settings.EncyptionPassword},
        //                {"account.apiVersion", settings.ApiVersion}
        //            };
        //}


        /// <summary>
        /// Processes the Authorize and AuthorizeAndCapture transactions
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/> to be paid</param>
        /// <param name="payment">The <see cref="Core.Models.IPayment"/> record</param>
        /// <param name="args"></param>
        /// <returns>The <see cref="Core.Gateways.Payment.IPaymentResult"/></returns>
        public IPaymentResult InitializePayment(IInvoice invoice, IPayment payment, ProcessorArgumentCollection args)
        {
            try
            {
                // Gather SagePay settings info and formulate it into a Dictionary for posting to the gateway
                var sagePayFormIntegration = new SagePayFormIntegration(Settings);
                var request = sagePayFormIntegration.FormPaymentRequest();
                SetSagePayApiData(request, invoice, payment);

                // Do basic validation as per the SagePay kit
                var errors = sagePayFormIntegration.Validation(request);
                if (errors.Count > 0)
                {
                    return(new PaymentResult(Attempt <IPayment> .Fail(payment, base.CreateErrorResult(errors)), invoice, true));
                }

                // Use the SagePay methods to encrypt the form so it can be posted
                sagePayFormIntegration.ProcessRequest(request);


                // Prepare a HTTP post
                var content =
                    new FormUrlEncodedContent(
                        new Dictionary <string, string>
                {
                    { "VPSProtocol", Settings.ApiVersion },
                    { "TxType", Constants.TransactionType },
                    { "Vendor", Settings.VendorName },
                    //{"Profile", "LOW"},  // Provider setting?
                    { "Crypt", request.Crypt }
                });
                try
                {
                    // Post the form to SagePay VSP
                    var formPaymentUrl = string.Format("https://{0}.sagepay.com/gateway/service/vspform-register.vsp", Settings.Environment);
                    // TEST form post
                    //var formPaymentUrl = string.Format("https://{0}.sagepay.com/showpost/showpost.asp", GetModeString(Settings.LiveMode));

                    var result = new HttpClient().PostAsync(formPaymentUrl, content).Result;

                    // Store transaction details in ExtendedData
                    payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.VendorTransactionCode, request.VendorTxCode);

                    // Flag an error in the backoffice if the result is not successful
                    if (!result.IsSuccessStatusCode)
                    {
                        return(new PaymentResult(Attempt <IPayment> .Fail(payment, CreateErrorResult(result.RequestMessage.Content)), invoice, true));
                    }

                    // Process the response from SagePay - this contains the redirect URL for the customer to complete their payment.
                    var redirectUrl = result.RequestMessage.RequestUri.ToString();
                    payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.SagePayPaymentUrl, redirectUrl);
                }
                catch (Exception ex)
                {
                    LogHelper.Error <SagePayFormPaymentProcessor>("SagePay form post failed. Crypt value: " + request.Crypt, ex);
                }

                // Store our site return URL and cancel URL in extendedData so it can be used in the callback
                var returnUrl = GetWebsiteUrl() + Settings.ReturnUrl;
                payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.ReturnUrl, returnUrl);

                var cancelUrl = GetWebsiteUrl() + Settings.CancelUrl;
                payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.CancelUrl, cancelUrl);

                return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, true));
            }
            catch (Exception ex)
            {
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, ex), invoice, true));
            }
        }