Esempio n. 1
0
        private void ProcessCheckout()
        {
            string OrderNumber = string.Empty;

            // ----------------------------------------------------------------
            // Process The Order:
            // ----------------------------------------------------------------
            if (string.IsNullOrEmpty(ThisCustomer.PaymentTermCode))
            {
                Response.Redirect("checkoutpayment.aspx?errormsg=" + Server.UrlEncode(AppLogic.GetString("checkoutpayment.aspx.7", ThisCustomer.SkinID, ThisCustomer.LocaleSetting)));
            }
            else
            {
                string receiptCode = string.Empty;
                string status = string.Empty, multiorder = string.Empty;
                if (cart.HasMultipleShippingAddresses())        // Paypal will never hit this
                {
                    var  splittedCarts     = cart.SplitIntoMultipleOrdersByDifferentShipToAddresses();
                    bool gatewayAuthFailed = false;

                    for (int ctr = 0; ctr < splittedCarts.Count; ctr++)
                    {
                        var splitCart = splittedCarts[ctr];
                        splitCart.BuildSalesOrderDetails();

                        var shippingAddress = Address.Get(ThisCustomer, AddressTypes.Shipping, splitCart.FirstItem().m_ShippingAddressID);

                        string processedSalesOrderCode = string.Empty;
                        string processedReceiptCode    = string.Empty;
                        // NOTE:
                        //  3DSecure using Sagepay Gateway is not supported on multiple shipping orders
                        //  We will revert to the regular IS gateway defined on the WebStore
                        status = splitCart.PlaceOrder(null,
                                                      ThisCustomer.PrimaryBillingAddress,
                                                      shippingAddress,
                                                      ref processedSalesOrderCode,
                                                      ref processedReceiptCode,
                                                      false,
                                                      true,
                                                      false);

                        OrderNumber = processedSalesOrderCode;
                        receiptCode = processedReceiptCode;

                        if (status == AppLogic.ro_INTERPRISE_GATEWAY_AUTHORIZATION_FAILED)
                        {
                            gatewayAuthFailed = true;

                            if (ctr == 0)
                            {
                                ThisCustomer.IncrementFailedTransactionCount();
                                if (ThisCustomer.FailedTransactionCount >= AppLogic.AppConfigUSInt("MaxFailedTransactionCount"))
                                {
                                    cart.ClearTransaction();
                                    ThisCustomer.ResetFailedTransactionCount();
                                    Response.Redirect("orderfailed.aspx");
                                }

                                ThisCustomer.ClearTransactions(false);

                                if (AppLogic.AppConfigBool("Checkout.UseOnePageCheckout"))
                                {
                                    Response.Redirect("checkout1.aspx?paymentterm=" + ThisCustomer.PaymentTermCode + "&errormsg=" + Server.UrlEncode(status));
                                }
                                else
                                {
                                    Response.Redirect("checkoutpayment.aspx?paymentterm=" + ThisCustomer.PaymentTermCode + "&errormsg=" + Server.UrlEncode(status));
                                }
                            }
                        }

                        // NOTE :
                        //  Should handle cases when 1 or more orders failed the payment processor
                        //  if using a payment gateway on credit card
                        multiorder = multiorder + "," + OrderNumber;
                    }

                    if (multiorder != string.Empty)
                    {
                        OrderNumber = multiorder.Remove(0, 1);
                    }

                    if (!gatewayAuthFailed)
                    {
                        cart.ClearTransaction();
                    }
                }
                else
                {
                    var     billingAddress  = ThisCustomer.PrimaryBillingAddress;
                    Address shippingAddress = null;

                    //added for PayPal ADDRESSOVERRIDE
                    if (IsPayPalCheckout && !AppLogic.AppConfigBool("PayPalCheckout.OverrideAddress"))
                    {
                        if (!cart.HasShippableComponents())
                        {
                            shippingAddress = ThisCustomer.PrimaryShippingAddress;
                        }
                        else
                        {
                            pp = new PayPalExpress();
                            var GetPayPalDetails = pp.GetExpressCheckoutDetails(Request.QueryString["token"]).GetExpressCheckoutDetailsResponseDetails;
                            shippingAddress = new Address()
                            {
                                Name           = GetPayPalDetails.PayerInfo.Address.Name,
                                Address1       = GetPayPalDetails.PayerInfo.Address.Street1 + (GetPayPalDetails.PayerInfo.Address.Street2 != String.Empty ? Environment.NewLine : String.Empty) + GetPayPalDetails.PayerInfo.Address.Street2,
                                City           = GetPayPalDetails.PayerInfo.Address.CityName,
                                State          = GetPayPalDetails.PayerInfo.Address.StateOrProvince,
                                PostalCode     = GetPayPalDetails.PayerInfo.Address.PostalCode,
                                Country        = AppLogic.ResolvePayPalAddressCode(GetPayPalDetails.PayerInfo.Address.CountryName.ToString()),
                                CountryISOCode = AppLogic.ResolvePayPalAddressCode(GetPayPalDetails.PayerInfo.Address.Country.ToString()),
                                Phone          = GetPayPalDetails.PayerInfo.ContactPhone
                            };
                        }
                    }
                    else
                    {
                        // Handle the scenario wherein the items in the cart
                        // does not ship to the customer's primary shipping address
                        if (cart.OnlyShippingAddressIsNotCustomerDefault())
                        {
                            shippingAddress = Address.Get(ThisCustomer, AddressTypes.Shipping, cart.FirstItem().m_ShippingAddressID);
                        }
                        else
                        {
                            shippingAddress = ThisCustomer.PrimaryShippingAddress;
                        }
                    }

                    if (!cart.IsSalesOrderDetailBuilt)
                    {
                        cart.BuildSalesOrderDetails();
                    }

                    Gateway gatewayToUse = null;

                    try
                    {
                        if (IsPayPalCheckout)
                        {
                            //Insert PayPal call here for response - For authorize and capture of order from paypal inside IS
                            pp = new PayPalExpress();
                            var PayPalDetails         = pp.GetExpressCheckoutDetails(Request.QueryString["token"]).GetExpressCheckoutDetailsResponseDetails;
                            var doExpressCheckoutResp = pp.DoExpressCheckoutPayment(PayPalDetails.Token, PayPalDetails.PayerInfo.PayerID, OrderNumber, cart);
                            var payPalResp            = new GatewayResponse(string.Empty)
                            {
                                AuthorizationCode    = doExpressCheckoutResp.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID,
                                TransactionResponse  = doExpressCheckoutResp.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus.ToString(),
                                Details              = doExpressCheckoutResp.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].PaymentStatus.ToString(),
                                AuthorizationTransID = doExpressCheckoutResp.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID
                            };

                            status = cart.PlaceOrder(gatewayToUse, billingAddress, shippingAddress, ref OrderNumber, ref receiptCode, true, true, payPalResp, IsPayPalCheckout, false);
                        }
                        else
                        {
                            status = cart.PlaceOrder(gatewayToUse, billingAddress, shippingAddress, ref OrderNumber, ref receiptCode, true, true, null, !IsPayPalCheckout, false);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (ex.Message == "Unable to instantiate Default Credit Card Gateway")
                        {
                            cart.ClearLineItems();
                            Response.Redirect("pageError.aspx?Parameter=" + "An Error Occured while Authorizing your Credit Card, However your order has been Placed.");
                        }
                        Response.Redirect("pageError.aspx?Parameter=" + Server.UrlEncode(ex.Message));
                    }

                    if (status == AppLogic.ro_3DSecure)
                    { // If credit card is enrolled in a 3D Secure service (Verified by Visa, etc.)
                        Response.Redirect("secureform.aspx");
                    }

                    if (status != AppLogic.ro_OK)
                    {
                        ThisCustomer.IncrementFailedTransactionCount();
                        if (ThisCustomer.FailedTransactionCount >= AppLogic.AppConfigUSInt("MaxFailedTransactionCount"))
                        {
                            cart.ClearTransaction();
                            ThisCustomer.ResetFailedTransactionCount();
                            Response.Redirect("orderfailed.aspx");
                        }

                        ThisCustomer.ClearTransactions(false);

                        if (AppLogic.AppConfigBool("Checkout.UseOnePageCheckout"))
                        {
                            Response.Redirect("checkout1.aspx?paymentterm=" + ThisCustomer.PaymentTermCode + "&errormsg=" + Server.UrlEncode(status));
                        }
                        else
                        {
                            Response.Redirect("checkoutpayment.aspx?paymentterm=" + ThisCustomer.PaymentTermCode + "&errormsg=" + Server.UrlEncode(status));
                        }
                    }
                }
            }
            AppLogic.ClearCardNumberInSession(ThisCustomer);
            ThisCustomer.ClearTransactions(true);

            if (!_PayPalFailed)
            {
                Response.Redirect(string.Format("orderconfirmation.aspx?ordernumber={0}", Server.UrlEncode(OrderNumber)));
            }
        }