Esempio n. 1
0
        public override HttpWebResponse SubmitDirectPaymentRequest(Order order, CreditCardInfo creditCardInfo)
        {
            order.EncryptCreditCardNumber(creditCardInfo.CardNumber);
            order.CreditCardSecurityCode = creditCardInfo.SecurityCode;
            order.PaymentStatus          = PaymentStatusName.Pending;

            order.Save();

            PaymentTransaction newTransaction = new PaymentTransaction();

            newTransaction.OrderId              = order.Id;
            newTransaction.PaymentProviderId    = this.ProviderId;
            newTransaction.GatewayUrl           = "";
            newTransaction.GatewayTransactionId = "";
            newTransaction.GatewayResponse      = "Payment Pending";
            newTransaction.GatewayDebugResponse = "";

            if (order.Total > 0)
            {
                // check for valid Credit Card
                CreditCardInfoValidator validator = new CreditCardInfoValidator();
                ValidationResult        results   = validator.Validate(creditCardInfo);
                if (!results.IsValid && results.Errors.Count > 0)
                {
                    newTransaction.GatewayResponse = "Credit Card Validation Error";
                    newTransaction.GatewayError    = results.Errors.ToList().ConvertAll(e => e.ErrorMessage).ToDelimitedString(", ");
                }
            }

            newTransaction.Save();

            return(null);
        }
        protected void btnReviewOrder_Click(object sender, EventArgs e)
        {
            // user-selected payment method (credit card or PayPal)
            string userSelectedPaymentMethod = Request.Form["paymentMethod"] ?? "";

            if (userSelectedPaymentMethod == "payLater")
            {
                checkoutOrderInfo.PaymentProvider = StoreContext.CurrentStore.GetOnsitePaymentProviders().Where(p => p == PaymentProviderName.PayLater).First();
                if (checkoutOrderInfo.PaymentProvider == PaymentProviderName.UNKNOWN)
                {
                    throw new ApplicationException("Unable to determine PaymentProvider for CheckoutOrderInfo!");
                }
                UpdateCheckoutSession(checkoutOrderInfo);

                Response.Redirect(StoreUrls.CheckoutReview());
            }
            else if (userSelectedPaymentMethod == "payPalExpressCheckout")
            {
                OrderController orderController = new OrderController(StoreContext);
                Order           pendingOrder    = orderController.CreateOrder(checkoutOrderInfo, OrderStatusName.PendingOffsite);

                PayPalExpressCheckoutPaymentProvider payPalExpressCheckout = new PayPalExpressCheckoutPaymentProvider(StoreContext.CurrentStore.GetPaymentProviderConfig(PaymentProviderName.PayPalExpressCheckout));
                string cancelUrl = StoreUrls.Cart();
                string returnUrl = StoreUrls.CheckoutReview();
                string token     = payPalExpressCheckout.SetExpressCheckoutAndGetToken(pendingOrder, cancelUrl, returnUrl);

                if (!string.IsNullOrEmpty(token))
                {
                    //Session[SessionKeys.CheckoutOrderInfo] = checkoutOrderInfo;
                    //string payPalUrl = string.Format("https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={0}", token);

                    string payPalUrl = payPalExpressCheckout.GetExpressCheckoutUrl(token);

                    Response.Redirect(payPalUrl);
                }
                else
                {
                    // ERROR
                    throw new ApplicationException("PayPal Express Token is Null/Empty!");
                }
            }
            else if (userSelectedPaymentMethod == "creditCard")
            {
                checkoutOrderInfo.PaymentProvider = StoreContext.CurrentStore.GetOnsitePaymentProviders().Where(p => p != PaymentProviderName.PayLater).First();
                if (checkoutOrderInfo.PaymentProvider == PaymentProviderName.UNKNOWN)
                {
                    throw new ApplicationException("Unable to determine PaymentProvider for CheckoutOrderInfo!");
                }

                //---- Credit Card Info
                CreditCardInfo creditCard = new CreditCardInfo()
                {
                    CardType     = WA.Enum <CreditCardType> .TryParseOrDefault(ddlCCType.SelectedValue, CreditCardType.UNKNOWN),
                    CardNumber   = txtCCNumber.Text,
                    ExpireMonth  = WA.Parser.ToShort(ddlCCExpireMonth.SelectedValue),
                    ExpireYear   = WA.Parser.ToShort(ddlCCExpireYear.SelectedValue),
                    NameOnCard   = txtCCNameOnCard.Text,
                    SecurityCode = txtCCSecurityCode.Text.Trim()
                };
                checkoutOrderInfo.CreditCard = creditCard;

                UpdateCheckoutSession(checkoutOrderInfo);

                bool doRedirect = true;
                if (checkoutOrderInfo.Total > 0)
                {
                    CreditCardInfoValidator validator = new CreditCardInfoValidator();
                    ValidationResult        results   = validator.Validate(checkoutOrderInfo.CreditCard);
                    if (!results.IsValid && results.Errors.Count > 0)
                    {
                        doRedirect = false;

                        msgFlash.InnerHtml = "<ul>" + results.Errors.ToList().ConvertAll(err => "<li>" + err.ErrorMessage + "</li>").ToDelimitedString(" ") + "</ul>";
                        msgFlash.Visible   = true;
                    }
                }

                if (doRedirect)
                {
                    Response.Redirect(StoreUrls.CheckoutReview());
                }
            }
            else
            {
                throw new ApplicationException("Unable to determine PaymentProvider for CheckoutOrderInfo!");
            }
        }