예제 #1
0
        public ActionResult Index()
        {
            if (PageEditing.PageIsInEditMode)
            {
                return(new EmptyResult());
            }

            var currentCart = _orderRepository.LoadCart <ICart>(PrincipalInfo.CurrentPrincipal.GetContactId(), Cart.DefaultName + SiteDefinition.Current.StartPage.ID);

            if (!currentCart.Forms.Any() || !currentCart.GetFirstForm().Payments.Any())
            {
                throw new PaymentException(PaymentException.ErrorType.ProviderError, "", "Generic Error");
            }

            var payooConfiguration = new PayooConfiguration();
            var payment            = currentCart.Forms.SelectMany(f => f.Payments).FirstOrDefault(c => c.PaymentMethodId.Equals(payooConfiguration.PaymentMethodId));

            if (payment == null)
            {
                throw new PaymentException(PaymentException.ErrorType.ProviderError, "", "Payment Not Specified");
            }

            var orderNumber = payment.Properties[Constant.PayooOrderNumberPropertyName] as string;

            if (string.IsNullOrEmpty(orderNumber))
            {
                throw new PaymentException(PaymentException.ErrorType.ProviderError, "", "Payment Not Specified");
            }

            // Redirect customer to receipt page
            var paymentResult = ExtractPaymentResultFromPayoo();
            var cancelUrl     = _cmsPaymentPropertyService.GetCancelledPaymentUrl(); // get link to Checkout page

            cancelUrl = UriUtil.AddQueryString(cancelUrl, "success", "false");
            cancelUrl = UriUtil.AddQueryString(cancelUrl, "paymentmethod", "payoo");

            var redirectUrl = cancelUrl;

            if (VerifyChecksumIsValid(payooConfiguration.ChecksumKey, orderNumber, paymentResult))
            {
                var gateway = new PayooPaymentGateway();
                if (paymentResult.IsSuccess)
                {
                    var acceptUrl = _cmsPaymentPropertyService.GetAcceptedPaymentUrl();
                    redirectUrl = gateway.ProcessSuccessfulTransaction(currentCart, payment, acceptUrl, cancelUrl);
                }
                else
                {
                    var message = paymentResult.Status.Equals("0") ? "Payment failed" : "Payment cancelled";
                    TempData[Constant.ErrorMessages] = $"{message}. Payoo Message: {paymentResult.ErrorCode}-{paymentResult.ErrorMsg}";
                    redirectUrl = gateway.ProcessUnsuccessfulTransaction(cancelUrl, message);
                }
            }
            else
            {
                TempData[Constant.ErrorMessages] = "The Payoo checksum is invalid";
            }

            return(Redirect(redirectUrl));
        }