コード例 #1
0
        private void ProcessPayment()
        {
            if (!_cart.IsEmpty())
            {
                var isOutOfStockAndPhaseOut = _cart.CartItems.Any(item => item.Status == "P" && item.IsOutOfStock);
                if (isOutOfStockAndPhaseOut)
                {
                    Response.Redirect("shoppingcart.aspx?resetlinkback=1");
                }
            }

            if (!_isRequirePayment)
            {
                Response.Redirect("checkoutreview.aspx");
            }

            bool isCustomerRegistered            = Customer.Current.IsRegistered;
            bool isCreditCardTokenizationEnabled = IsCreditCardTokenizationEnabled;

            string paymentMethodFromInput   = ctrlPaymentTerm.PaymentMethod;
            string paymentTermCodeFromInput = ctrlPaymentTerm.PaymentTerm;

            #region Payments

            string PAYMENT_METHOD_PAYPALX    = DomainConstants.PAYMENT_METHOD_PAYPALX;
            string PAYMENT_METHOD_CREDITCARD = DomainConstants.PAYMENT_METHOD_CREDITCARD;

            if (_cart.GetOrderBalance() == System.Decimal.Zero && AppLogic.AppConfigBool("SkipPaymentEntryOnZeroDollarCheckout"))
            {
                _cart.MakePaymentTermNotRequired();
            }
            if (paymentTermCodeFromInput.ToString().Trim().Equals("PURCHASE ORDER", StringComparison.InvariantCultureIgnoreCase))
            {
                ThisCustomer.ThisCustomerSession.SetVal("PONumber", ctrlPaymentTerm.PONumber);
            }
            else if (paymentTermCodeFromInput.ToString().Trim().Equals("REQUEST QUOTE", StringComparison.InvariantCultureIgnoreCase))
            {
            }
            else if (paymentMethodFromInput == PAYMENT_METHOD_PAYPALX)
            {
                ThisCustomer.ThisCustomerSession["paypalfrom"] = "checkoutpayment";
                Response.Redirect(PayPalExpress.CheckoutURL(_cart));
            }
            else if (paymentMethodFromInput == PAYMENT_METHOD_CREDITCARD)
            {
                //Validate first the inputs (empty and invalid dropdown selection)
                //triggers the input registered validators.

                if (!IsValid)
                {
                    return;
                }

                //Skip credit card valiation when card is tokenized

                if (!_skipCreditCardValidation)
                {
                    //credit card validation

                    if (!IsValidCreditCardInfo())
                    {
                        return;
                    }
                }

                UpdateAnonForAge13();

                #region Posted Data (Credit Card Information)

                string nameOnCard                = ctrlPaymentTerm.NameOnCard;
                string cardNumberFromInput       = ctrlPaymentTerm.CardNumber;
                string cardTypeFromInput         = ctrlPaymentTerm.CardType;
                string cardExpiryYearFromInput   = ctrlPaymentTerm.CardExpiryYear;
                string cardExpiryMonthFromInput  = ctrlPaymentTerm.CardExpiryMonth;
                string cVVFromInput              = ctrlPaymentTerm.CVV;
                string saveCreditCardAsFromInput = ctrlPaymentTerm.CardDescription;

                string cardStartMonth  = string.Empty;
                string cardStartYear   = string.Empty;
                string cardIssueNumber = string.Empty;

                if (AppLogic.AppConfigBool("ShowCardStartDateFields"))
                {
                    cardStartMonth  = ctrlPaymentTerm.CardStartMonth;
                    cardStartYear   = ctrlPaymentTerm.CardStartYear;
                    cardIssueNumber = ctrlPaymentTerm.CardIssueNumber;
                }

                #endregion

                #region Save Billing Address

                var aBillingAddress  = Address.New(ThisCustomer, AddressTypes.Billing);
                var ThisAddress      = Address.New(ThisCustomer, AddressTypes.Shipping);
                var aShippingAddress = ThisCustomer.PrimaryShippingAddress;

                string maskedCardNumber = string.Empty;

                //set the default value of creditCardCode to primary billing address

                string creditCardCode = ThisCustomer.PrimaryBillingAddress.AddressID;

                if (isCustomerRegistered)
                {
                    if (!txtCode.Text.IsNullOrEmptyTrimmed())
                    {
                        //txtCode.Text - Customer CreditCard code
                        //Override the credit card code if tokenization
                        //decrypt the credit card code from the rendered hidden text box since it is encrypted.

                        creditCardCode   = AppLogic.DecryptCreditCardCode(ThisCustomer, txtCode.Text);
                        maskedCardNumber = AppLogic.GetCustomerCreditCardMaskedCardNumber(creditCardCode);
                    }

                    if (maskedCardNumber.StartsWith("X"))
                    {
                        CreditCardDTO credit = null;

                        if (!creditCardCode.IsNullOrEmptyTrimmed())
                        {
                            //set the credit card info using the creditcard code

                            credit = CreditCardDTO.Find(creditCardCode);
                        }

                        //test if the credit card info has been tokenized and saved by the client
                        //if refno > 0 means the credit card has been authorized

                        if (credit.RefNo > 0)
                        {
                            cardNumberFromInput      = credit.CardNumber;
                            nameOnCard               = credit.NameOnCard;
                            cardTypeFromInput        = credit.CardType;
                            cardExpiryMonthFromInput = credit.ExpMonth;
                            cardExpiryYearFromInput  = credit.ExpYear;

                            if (AppLogic.AppConfigBool("ShowCardStartDateFields"))
                            {
                                cardStartMonth = credit.StartMonth;
                                cardStartYear  = credit.StartYear;
                            }
                        }
                    }

                    aBillingAddress.Address1   = BillingAddressControl.street;
                    aBillingAddress.Country    = BillingAddressControl.country;
                    aBillingAddress.PostalCode = BillingAddressControl.postal;

                    string bCityStates = txtCityStates.Text;
                    string city        = String.Empty;
                    string state       = String.Empty;

                    var cityStateArray = GetCityStateArray();
                    aBillingAddress.State = cityStateArray[0];
                    aBillingAddress.City  = cityStateArray[1];

                    aBillingAddress.ResidenceType = aShippingAddress.ThisCustomer.PrimaryShippingAddress.ResidenceType;
                    aBillingAddress.Name          = txtBillingContactName.Text;
                    aBillingAddress.Phone         = txtBillingContactNumber.Text;

                    if (AppLogic.AppConfigBool("Address.ShowCounty"))
                    {
                        aBillingAddress.County = BillingAddressControl.county;
                    }
                }
                else
                {
                    var primariBillingAddress = ThisCustomer.PrimaryBillingAddress;
                    aBillingAddress.Address1      = primariBillingAddress.Address1;
                    aBillingAddress.Country       = primariBillingAddress.Country;
                    aBillingAddress.PostalCode    = primariBillingAddress.PostalCode;
                    aBillingAddress.City          = primariBillingAddress.City;
                    aBillingAddress.State         = primariBillingAddress.State;
                    aBillingAddress.ResidenceType = primariBillingAddress.ResidenceType;
                    aBillingAddress.Name          = primariBillingAddress.Name;
                    aBillingAddress.Phone         = primariBillingAddress.Phone;
                    aBillingAddress.EMail         = primariBillingAddress.EMail;
                }

                //Credit card code has default value of primary billing addressid
                //This will be overridden when tokenization

                aBillingAddress.AddressID           = creditCardCode;
                aBillingAddress.CardNumber          = cardNumberFromInput;
                aBillingAddress.CardName            = nameOnCard;
                aBillingAddress.CardType            = cardTypeFromInput;
                aBillingAddress.CardExpirationMonth = cardExpiryMonthFromInput;
                aBillingAddress.CardExpirationYear  = cardExpiryYearFromInput;
                aBillingAddress.CustomerCode        = ThisCustomer.CustomerCode;

                //Try save the new billing address if anonymous
                //if registered the billing will not be created

                aBillingAddress.Save();

                //update the address if user is registered and is already exist

                Address.Update(ThisCustomer, aBillingAddress);

                #endregion

                if (AppLogic.AppConfigBool("ShowCardStartDateFields"))
                {
                    //-> Some CCs do not have StartDate, so here we should provide Default if none was supplied.

                    string defaultCardStartMonth = DateTime.Now.Month.ToString();
                    string defaultCardStartYear  = DateTime.Now.Year.ToString();

                    aBillingAddress.CardStartMonth  = (cardStartMonth != "MONTH")? cardStartMonth: defaultCardStartMonth;
                    aBillingAddress.CardStartYear   = (cardStartYear != "YEAR")? cardStartYear : defaultCardStartYear;
                    aBillingAddress.CardIssueNumber = cardIssueNumber;
                }

                //-> Capture the credit card number from the payment page and encrypt it so that the gateway can capture from that credit card

                if (!cardNumberFromInput.StartsWith("X"))
                {
                    string salt          = String.Empty;
                    string iv            = String.Empty;
                    string cardNumberEnc = AppLogic.EncryptCardNumber(cardNumberFromInput, ref salt, ref iv);
                    AppLogic.StoreCardNumberInSession(ThisCustomer, cardNumberEnc, salt, iv);
                }

                if (isCreditCardTokenizationEnabled)
                {
                    InterpriseHelper.MakeDefaultAddress(ThisCustomer.ContactCode, creditCardCode, AddressTypes.Billing);

                    bool saveCreditCardInfo = (AppLogic.AppConfigBool("ForceCreditCardInfoSaving") || ctrlPaymentTerm.SaveCreditCreditCardInfo);
                    ThisCustomer.ThisCustomerSession["SaveCreditCardChecked"] = saveCreditCardInfo.ToString();

                    #region "Update Address w/ CreditCardInfo"

                    string thisCardNumber = Interprise.Framework.Base.Shared.Common.MaskCardNumber(aBillingAddress.CardNumber);

                    if (!maskedCardNumber.IsNullOrEmptyTrimmed())
                    {
                        thisCardNumber = maskedCardNumber;
                    }

                    #region Postal Code Handler

                    var    parsedPostalCode = InterpriseHelper.ParsePostalCode(aBillingAddress.Country, aBillingAddress.PostalCode);
                    string postal           = parsedPostalCode.PostalCode;
                    int    plus4            = parsedPostalCode.Plus4;

                    #endregion

                    var sql = new StringBuilder();

                    sql.Append(" UPDATE CustomerCreditCard ");
                    sql.AppendFormat(" SET CreditCardDescription = {0}, MaskedCardNumber = {1}, NameOnCard = {2}, ", saveCreditCardAsFromInput.ToDbQuote(), thisCardNumber.ToDbQuote(), nameOnCard.ToDbQuote());
                    sql.AppendFormat(" Address = {0}, City = {1}, State={2}, ", aBillingAddress.Address1.ToDbQuote(), aBillingAddress.City.ToDbQuote(), aBillingAddress.State.ToDbQuote());

                    if (plus4 == 0)
                    {
                        sql.AppendFormat(" PostalCode = {0}, Country = {1}, Plus4=NULL, ", postal.ToDbQuote(), aBillingAddress.Country.ToDbQuote());
                    }
                    else
                    {
                        sql.AppendFormat(" PostalCode = {0}, Country = {1}, Plus4={2}, ", postal.ToDbQuote(), aBillingAddress.Country.ToDbQuote(), plus4);
                    }

                    sql.AppendFormat(" ExpMonth={0}, ExpYear={1}, Telephone={2}, ", InterpriseHelper.ToInterpriseExpMonth(aBillingAddress.CardExpirationMonth).ToDbQuote(), aBillingAddress.CardExpirationYear.ToDbQuote(), aBillingAddress.Phone.ToDbQuote());
                    sql.AppendFormat(" CreditCardType = {0}, DateModified=getdate() ", aBillingAddress.CardType.ToDbQuote());

                    sql.AppendFormat(" WHERE CreditCardCode={0} ", creditCardCode.ToDbQuote());

                    DB.ExecuteSQL(sql.ToString());
                    sql.Clear();

                    #endregion

                    DB.ExecuteSQL(@"UPDATE Customer SET Creditcardcode={0} WHERE CustomerCode={1}", DB.SQuote(creditCardCode), DB.SQuote(ThisCustomer.CustomerCode));

                    AppLogic.ClearCreditCardCodeInSession(ThisCustomer);
                }
                else
                {
                    if (ThisCustomer.IsRegistered)
                    {
                        Address.Update(ThisCustomer, aBillingAddress);
                        InterpriseHelper.MakeDefaultAddress(ThisCustomer.ContactCode, creditCardCode, AddressTypes.Billing);
                    }
                }

                AppLogic.StoreCardExtraCodeInSession(ThisCustomer, cVVFromInput);
                AppLogic.SavePostalCode(aBillingAddress);

                //Redirect to Confirmation Page
            }

            InterpriseHelper.UpdateCustomerPaymentTerm(ThisCustomer, paymentTermCodeFromInput);
            Response.Redirect("checkoutreview.aspx");

            #endregion
        }