Exemplo n.º 1
0
        /// <summary>
        /// Post request to PayPoint API
        /// </summary>
        /// <param name="payPointPayment">PayPoint payment information</param>
        /// <returns>PayPoint payment response</returns>
        protected PayPointPaymentResponse PostRequest(PayPointPayment payPointPayment)
        {
            var postData      = Encoding.Default.GetBytes(JsonConvert.SerializeObject(payPointPayment));
            var serviceUrl    = _payPointPaymentSettings.UseSandbox ? "https://hosted.mite.paypoint.net" : "https://hosted.paypoint.net";
            var login         = string.Format("{0}:{1}", _payPointPaymentSettings.ApiUsername, _payPointPaymentSettings.ApiPassword);
            var authorization = Convert.ToBase64String(Encoding.Default.GetBytes(login));
            var request       = (HttpWebRequest)WebRequest.Create(string.Format("{0}/hosted/rest/sessions/{1}/payments", serviceUrl, _payPointPaymentSettings.InstallationId));

            request.Headers.Add(HttpRequestHeader.Authorization, string.Format("Basic {0}", authorization));
            request.Method        = "POST";
            request.Accept        = "application/json";
            request.ContentType   = "application/json";
            request.ContentLength = postData.Length;
            try
            {
                using (var stream = request.GetRequestStream())
                {
                    stream.Write(postData, 0, postData.Length);
                }
                var httpResponse = (HttpWebResponse)request.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    return(JsonConvert.DeserializeObject <PayPointPaymentResponse>(streamReader.ReadToEnd()));
                }
            }
            catch (WebException ex)
            {
                var httpResponse = (HttpWebResponse)ex.Response;
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    return(JsonConvert.DeserializeObject <PayPointPaymentResponse>(streamReader.ReadToEnd()));
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Post process payment (used by payment gateways that require redirecting to a third-party URL)
        /// </summary>
        /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param>
        public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
        {
            var storeLocation = _webHelper.GetStoreLocation();

            //create post data
            var payPointPayment = new PayPointPayment
            {
                Locale   = _workContext.WorkingLanguage.UniqueSeoCode,
                Customer = new PayPointPaymentCustomer {
                    Registered = false
                },
                Transaction = new PayPointPaymentTransaction
                {
                    MerchantReference = postProcessPaymentRequest.Order.OrderGuid.ToString(),
                    Money             = new PayPointPaymentMoney
                    {
                        Currency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode,
                        Amount   = new PayPointPaymentAmount {
                            Fixed = Math.Round(postProcessPaymentRequest.Order.OrderTotal, 2)
                        }
                    },
                    Description = string.Format("Order #{0}", postProcessPaymentRequest.Order.Id)
                },
                Session = new PayPointPaymentSession
                {
                    ReturnUrl = new PayPointPaymentUrl {
                        Url = string.Format("{0}checkout/completed/{1}", storeLocation, postProcessPaymentRequest.Order.Id)
                    },
                    CancelUrl = new PayPointPaymentUrl {
                        Url = string.Format("{0}orderdetails/{1}", storeLocation, postProcessPaymentRequest.Order.Id)
                    },
                    TransactionNotification = new PayPointPaymentCallbackUrl
                    {
                        Format = PayPointPaymentFormat.REST_JSON,
                        Url    = string.Format("{0}Plugins/PaymentPayPoint/Callback", storeLocation)
                    }
                }
            };

            //post request to API
            var payPointPaymentResponse = PostRequest(payPointPayment);

            //redirect to hosted payment service
            if (payPointPaymentResponse.Status == PayPointStatus.SUCCESS)
            {
                _httpContext.Response.Redirect(payPointPaymentResponse.RedirectUrl);
            }
            else
            {
                _logger.Error(string.Format("PayPoint transaction failed. {0} - {1}", payPointPaymentResponse.ReasonCode, payPointPaymentResponse.ReasonMessage));
            }
        }