コード例 #1
0
        protected void btnProcess_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (!Page.IsValid)
            {
                return;
            }

            PortalSecurity security = new PortalSecurity();

            TransactionDetails transaction = new TransactionDetails
            {
                CardNumber       = security.InputFilter(txtNumber.Text, PortalSecurity.FilterFlag.NoMarkup),
                VerificationCode = security.InputFilter(txtVer.Text, PortalSecurity.FilterFlag.NoMarkup),
                ExpirationMonth  = int.Parse(ddlMonth.SelectedValue),
                ExpirationYear   = int.Parse(ddlYear.SelectedValue)
            };

            if (transaction.IsValid())
            {
                IAddressInfo shippingAddress = CheckoutControl.ShippingAddress;
                IAddressInfo billingAddress  = CheckoutControl.BillingAddress;
                //Adds order to db...
                OrderInfo order = CheckoutControl.GetFinalizedOrderInfo();

                GenerateOrderConfirmation();

                //Process transaction
                AuthNetGatewayProvider provider    = new AuthNetGatewayProvider(StoreSettings.GatewaySettings);
                TransactionResult      orderResult = provider.ProcessTransaction(shippingAddress, billingAddress, order, transaction);
                if (!orderResult.Succeeded)
                {
                    string errorMessage    = string.Empty;
                    string localizedReason = string.Empty;
                    // Try to get the corresponding localized reason message
                    localizedReason = Localization.GetString("ReasonCode" + orderResult.ReasonCode, LocalResourceFile);
                    // If a localized message do not exist use the original message
                    if (localizedReason == string.Empty | localizedReason == null)
                    {
                        localizedReason = orderResult.Message.ToString();
                    }
                    switch (orderResult.ResultCode)
                    {
                    case -5:
                        errorMessage = Localization.GetString("ErrorCardInformation", LocalResourceFile);
                        break;

                    case -4:
                        errorMessage = Localization.GetString("ErrorBillingAddress", LocalResourceFile);
                        break;

                    case -3:
                        errorMessage = Localization.GetString("ErrorPaymentOption", LocalResourceFile);
                        break;

                    case -2:
                        errorMessage = Localization.GetString("ErrorConnection", LocalResourceFile);
                        break;

                    case -1:
                        errorMessage = Localization.GetString("ErrorUnexpected", LocalResourceFile);
                        break;

                    case 2:
                        errorMessage          = string.Format(Localization.GetString("ReasonMessage", LocalResourceFile), Localization.GetString("ResponseCode2", LocalResourceFile), orderResult.ReasonCode, "");
                        CheckoutControl.Order = UpdateOrderStatus(order, OrderInfo.OrderStatusList.AwaitingPayment);
                        CheckoutControl.Hide();
                        pnlProceedToAuthorize.Visible = false;
                        InvokePaymentFailed();
                        CurrentCart.DeleteCart(PortalId, StoreSettings.SecureCookie);
                        ClearOrderIdCookie();
                        break;

                    case 3:
                        errorMessage = string.Format(Localization.GetString("ReasonMessage", LocalResourceFile), Localization.GetString("ResponseCode3", LocalResourceFile), orderResult.ReasonCode, localizedReason);
                        break;

                    case 4:
                        errorMessage          = string.Format(Localization.GetString("ReasonMessage", LocalResourceFile), Localization.GetString("ResponseCode4", LocalResourceFile), orderResult.ReasonCode, localizedReason);
                        CheckoutControl.Order = UpdateOrderStatus(order, OrderInfo.OrderStatusList.AwaitingPayment);
                        CheckoutControl.Hide();
                        pnlProceedToAuthorize.Visible = false;
                        InvokePaymentRequiresConfirmation();
                        CurrentCart.DeleteCart(PortalId, StoreSettings.SecureCookie);
                        ClearOrderIdCookie();
                        break;

                    default:
                        errorMessage = string.Format(Localization.GetString("ReasonMessage", LocalResourceFile), Localization.GetString("ErrorUnexpected", LocalResourceFile), orderResult.ReasonCode, localizedReason);
                        break;
                    }
                    lblError.Visible = true;
                    lblError.Text    = errorMessage;
                }
                else
                {
                    int portalId = PortalSettings.PortalId;
                    // Set order status to "Paid"...
                    CheckoutControl.Order = UpdateOrderStatus(order, OrderInfo.OrderStatusList.Paid);
                    // Add User to Product Roles
                    OrderController orderController = new OrderController();
                    orderController.AddUserToRoles(PortalId, order);
                    // Add User to Order Role
                    StoreInfo storeSetting = StoreController.GetStoreInfo(PortalSettings.PortalId);
                    if (storeSetting.OnOrderPaidRoleID != Null.NullInteger)
                    {
                        orderController.AddUserToPaidOrderRole(portalId, order.CustomerID, storeSetting.OnOrderPaidRoleID);
                    }
                    CheckoutControl.Hide();
                    pnlProceedToAuthorize.Visible = false;
                    lblError.Visible = false;
                    InvokePaymentSucceeded();
                    CurrentCart.DeleteCart(PortalId, StoreSettings.SecureCookie);
                    ClearOrderIdCookie();
                }
            }
            else
            {
                lblError.Visible = true;
                lblError.Text    = Localization.GetString("ErrorCardNotValid", LocalResourceFile);
            }
            btnProcess.Enabled = true;
        }
コード例 #2
0
        public TransactionResult ProcessTransaction(IAddressInfo shipping, IAddressInfo billing, OrderInfo orderInfo, TransactionDetails trans)
        {
            TransactionResult result = new TransactionResult();

            CultureInfo ciEnUs = new CultureInfo("en-US");

            // Check data before performing transaction
            AuthNetSettings settings = new AuthNetSettings(_gatewaySettings);

            if (!settings.IsValid())
            {
                result.Succeeded  = false;
                result.ResultCode = -3;

                return(result);
            }

            if (billing == null)
            {
                result.Succeeded  = false;
                result.ResultCode = -4;

                return(result);
            }

            if (trans == null || !trans.IsValid())
            {
                result.Succeeded  = false;
                result.ResultCode = -5;

                return(result);
            }

            // Gather transaction information
            string url = settings.GatewayURL;
            NameValueCollection NVCol = new NameValueCollection
            {
                // Merchant infos
                { "x_login", settings.Username },    //Req
                { "x_tran_key", settings.Password }, //Req
                { "x_version", settings.Version },   //Req
                { "x_test_request", settings.IsTest.ToString().ToUpper() },
                // Init infos
                { "x_delim_data", "TRUE" },
                { "x_delim_char", "|" },
                { "x_encap_char", "" },
                { "x_relay_response", "FALSE" }, //Req
                                                 //New in Store 3.1.10, added by Authorize in February 2014
                { "x_market_type", "0" },        // 0=eCommerce, 1 MOTO, 2 Retail
                                                 // Billing infos
                { "x_first_name", billing.FirstName },
                { "x_last_name", billing.LastName },
                { "x_company", "" },
                { "x_address", (billing.Address1 + " " + billing.Address2).Trim() },
                { "x_city", billing.City },
                { "x_state", billing.RegionCode },
                { "x_zip", billing.PostalCode },
                { "x_country", billing.CountryCode },
                { "x_phone", billing.Phone1 },
                // Shipping infos
                { "x_ship_to_first_name", shipping.FirstName },
                { "x_ship_to_last_name", shipping.LastName },
                { "x_ship_to_company", "" },
                { "x_ship_to_address", (shipping.Address1 + " " + shipping.Address2).Trim() },
                { "x_ship_to_city", shipping.City },
                { "x_ship_to_state", shipping.RegionCode },
                { "x_ship_to_zip", shipping.PostalCode },
                { "x_ship_to_country", shipping.CountryCode },
                // Customer infos
                { "x_cust_id", billing.UserID.ToString() },
                { "x_customer_ip", HttpContext.Current.Request.UserHostAddress },
                // Order infos
                { "x_invoice_num", orderInfo.OrderID.ToString() },
                { "x_amount", orderInfo.GrandTotal.ToString("0.00", ciEnUs) },//Req
                { "x_tax", orderInfo.TaxTotal.ToString("0.00", ciEnUs) },
                { "x_freight", orderInfo.ShippingCost.ToString("0.00", ciEnUs) },
                // Transaction infos
                { "x_method", "CC" },                      //CC=Credit Card could be also ECHECK
                { "x_type", settings.Capture.ToString() }, //Req
                { "x_recurring_billing", "NO" },
                { "x_card_num", trans.CardNumber },        //Req
                { "x_card_code", trans.VerificationCode },
                { "x_exp_date", trans.ExpirationMonth.ToString("00") + "/" + trans.ExpirationYear }//Req
            };
            // Order details
            string                 fieldSep        = "<|>";
            OrderController        orderController = new OrderController();
            List <OrderDetailInfo> orderDetails    = orderController.GetOrderDetails(orderInfo.OrderID);
            ArrayList              items           = new ArrayList(orderDetails.Count);

            foreach (OrderDetailInfo detail in orderDetails)
            {
                string modelNumber = detail.ModelNumber;
                if (modelNumber.Length > 31)
                {
                    modelNumber = modelNumber.Substring(0, 31);
                }

                string modelName = detail.ModelName;
                if (modelName.Length > 31)
                {
                    modelName = modelName.Substring(0, 31);
                }

                items.Add(modelNumber + fieldSep + modelName + fieldSep + fieldSep + detail.Quantity +
                          fieldSep + detail.UnitCost.ToString("0.00", ciEnUs) + fieldSep + "Y");
            }
            // Perform transaction
            try
            {
                Encoding     enc = Encoding.GetEncoding(1252);
                StreamReader loResponseStream = new StreamReader(PostEx(url, NVCol, items).GetResponseStream(), enc);

                string lcHtml = loResponseStream.ReadToEnd();
                loResponseStream.Close();

                string[] resultArray = lcHtml.Split('|');

                result.Succeeded  = (resultArray[0] == "1");
                result.ResultCode = int.Parse(resultArray[0]);
                result.ReasonCode = int.Parse(resultArray[2]);
                result.Message    = resultArray[3];
            }
            catch (Exception ex)
            {
                result.Succeeded  = false;
                result.ResultCode = -2;
                result.Message    = ex.Message;
            }

            return(result);
        }