Authorize.net Order Numbers

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
7 years ago
-I have did this with some logic applying on this replace OrdeId instead of OrderGuid.
-I did the following things in my Authorized.net plugin and in AuthorizeNetPaymentProcessor.cs controller.
See the bold text as a logic.
-I also add new service for GetOrdersByStore in order service.I've putted the service code also in below the ProcessPayment method.

- I have mentioned in Bold Text in code, that way you can get order id and pass it to Authorized.net for payment.


   public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            var result = new ProcessPaymentResult();

            var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);            
            var initialOrderID = _orderService.GetOrdersByStore(customer.StoreId).Id + 1;

            var webClient = new WebClient();
            var form = new NameValueCollection();
            form.Add("x_login", _authorizeNetPaymentSettings.LoginId);
            form.Add("x_tran_key", _authorizeNetPaymentSettings.TransactionKey);

            //we should not send "x_test_request" parameter. otherwise, the transaction won't be logged in the sandbox
            //if (_authorizeNetPaymentSettings.UseSandbox)
            //    form.Add("x_test_request", "TRUE");
            //else
            //    form.Add("x_test_request", "FALSE");

            form.Add("x_delim_data", "TRUE");
            form.Add("x_delim_char", "|");
            form.Add("x_encap_char", "");
            form.Add("x_version", GetApiVersion());
            form.Add("x_relay_response", "FALSE");
            form.Add("x_method", "CC");
            form.Add("x_currency_code", _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode);
            if (_authorizeNetPaymentSettings.TransactMode == TransactMode.Authorize)
                form.Add("x_type", "AUTH_ONLY");
            else if (_authorizeNetPaymentSettings.TransactMode == TransactMode.AuthorizeAndCapture)
                form.Add("x_type", "AUTH_CAPTURE");
            else
                throw new NopException("Not supported transaction mode");

            var orderTotal = Math.Round(processPaymentRequest.OrderTotal, 2);
            form.Add("x_amount", orderTotal.ToString("0.00", CultureInfo.InvariantCulture));
            form.Add("x_card_num", processPaymentRequest.CreditCardNumber);
            form.Add("x_exp_date", processPaymentRequest.CreditCardExpireMonth.ToString("D2") + processPaymentRequest.CreditCardExpireYear.ToString());
            form.Add("x_card_code", processPaymentRequest.CreditCardCvv2);
            form.Add("x_first_name", customer.BillingAddress.FirstName);
            form.Add("x_last_name", customer.BillingAddress.LastName);
            form.Add("x_email", customer.BillingAddress.Email);
            if (!string.IsNullOrEmpty(customer.BillingAddress.Company))
                form.Add("x_company", customer.BillingAddress.Company);
            form.Add("x_address", customer.BillingAddress.Address1);
            form.Add("x_city", customer.BillingAddress.City);
            if (customer.BillingAddress.StateProvince != null)
                form.Add("x_state", customer.BillingAddress.StateProvince.Abbreviation);
            form.Add("x_zip", customer.BillingAddress.ZipPostalCode);
            if (customer.BillingAddress.Country != null)
                form.Add("x_country", customer.BillingAddress.Country.TwoLetterIsoCode);

            //x_invoice_num is 20 chars maximum. hece we also pass x_description
            //form.Add("x_invoice_num", processPaymentRequest.OrderGuid.ToString().Substring(0, 20));      
            //form.Add("x_description", string.Format("Full order #{0}", processPaymentRequest.OrderGuid));
                        
            form.Add("x_invoice_num", initialOrderID.ToString());
            form.Add("x_description", string.Format("Full order #{0}", initialOrderID.ToString()));


            form.Add("x_customer_ip", _webHelper.GetCurrentIpAddress());

            var responseData = webClient.UploadValues(GetAuthorizeNetUrl(), form);
            var reply = Encoding.ASCII.GetString(responseData);

            if (!String.IsNullOrEmpty(reply))
            {
                string[] responseFields = reply.Split('|');
                switch (responseFields[0])
                {
                    case "1":
                        result.AuthorizationTransactionCode = string.Format("{0},{1}", responseFields[6], responseFields[4]);
                        result.AuthorizationTransactionResult = string.Format("Approved ({0}: {1})", responseFields[2], responseFields[3]);
                        result.AvsResult = responseFields[5];
                        //responseFields[38];
                        if (_authorizeNetPaymentSettings.TransactMode == TransactMode.Authorize)
                        {
                            result.NewPaymentStatus = PaymentStatus.Authorized;
                        }
                        else
                        {
                            result.NewPaymentStatus = PaymentStatus.Paid;
                        }
                        break;
                    case "2":
                        result.AddError(string.Format("Declined ({0}: {1})", responseFields[2], responseFields[3]));
                        break;
                    case "3":
                        result.AddError(string.Format("Error: {0}", reply));
                        break;

                }
            }
            else
            {
                result.AddError("Authorize.NET unknown error");
            }

            return result;
        }






  /// <summary>
        /// Gets an orders by StoreID
        /// </summary>
        /// <param name="storeID">The store identifier</param>
        /// <returns>Order</returns>
        public virtual Order GetOrdersByStore(int storeID)
        {
            var query = from o in _orderRepository.Table                      
                        select o;

            if (storeID > 0)
            {                
                query = from o in _orderRepository.Table
                            where o.StoreId == storeID
                            select o;            
            }

            var order = query.ToList().LastOrDefault();
            //var order = query.LastOrDefault();
            return order;
        }
7 years ago
RE: " var initialOrderID = ...   + 1"  
But some other customer may already have that "+ 1" order id.

RE: GetOrdersByStore
"Orders" is plural.  You are only getting one (1) order.

RE: "var order = query.ToList().LastOrDefault();"
You probably don't need "ToList()".  It will likely make the SQL query return more records that are needed.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.