bryan-bit-wizards
-
12/20/2011 9:41:31 AM
RE:Authorize.NET Additional Values
There are many ways to integrate with Authorize.NET. One of the simpler ways is to post to the Authoerize.NET site and get a response. Doing so requires populating the post with all of the fields Authorize.NET is expecting. Here is some sample code that does a post and gets back a response:
/// <summary> /// This function will authorize the payment by posting directly to Authorize.NET /// </summary> public void AuthorizePayment() { string AuthNetVersion = "3.1"; // Contains CCV support string AuthNetLoginID = ConfigurationManager.AppSettings["Authorize.NETLoginID"].ToString(); string AuthNetPassword = ConfigurationManager.AppSettings["Authorize.NETPassword"].ToString(); // Get this from your authorize.net merchant interface string AuthNetTransKey = ConfigurationManager.AppSettings["Authorize.NETTransKey"].ToString();
Boolean blnTestTransaction = Convert.ToBoolean(ConfigurationManager.AppSettings["Authorize.NETTestTransaction"].ToString());
WebClient objRequest = new WebClient(); System.Collections.Specialized.NameValueCollection objInf = new System.Collections.Specialized.NameValueCollection(30); System.Collections.Specialized.NameValueCollection objRetInf = new System.Collections.Specialized.NameValueCollection(30); byte[] objRetBytes; string[] objRetVals; string strError;
objInf.Add("x_version", AuthNetVersion); objInf.Add("x_delim_data", "True"); objInf.Add("x_login", AuthNetLoginID); objInf.Add("x_password", AuthNetPassword); objInf.Add("x_tran_key", AuthNetTransKey); objInf.Add("x_relay_response", "False");
objInf.Add("x_test_request", blnTestTransaction.ToString());
objInf.Add("x_delim_char", ","); objInf.Add("x_encap_char", "|");
// Billing Address objInf.Add("x_first_name", this.CustomerFirstName); objInf.Add("x_last_name", this.CustomerLastName); objInf.Add("x_address", this.CustomerAddress); objInf.Add("x_city", this.CustomerCity); objInf.Add("x_state", this.CustomerState); objInf.Add("x_zip", this.CustomerZIP); objInf.Add("x_country", this.CustomerCountry); objInf.Add("x_phone", this.CustomerPhone);
objInf.Add("x_description", this.Description); objInf.Add("x_po_num", this.PONumber);
// Card Details objInf.Add("x_card_num", this.CCNumber); objInf.Add("x_exp_date", this.ExpirationDate);
// Authorisation code of the card (CCV) objInf.Add("x_card_code", this.CVV2Number);
objInf.Add("x_method", "CC"); objInf.Add("x_type", "AUTH_CAPTURE"); objInf.Add("x_amount", this.Amount.ToString());
// Currency setting. Check the guide for other supported currencies objInf.Add("x_currency_code", "USD");
try { // Pure Test Server objRequest.BaseAddress = "https://secure.authorize.net/gateway/transact.dll";
objRetBytes = objRequest.UploadValues(objRequest.BaseAddress, "POST", objInf); objRetVals = System.Text.Encoding.ASCII.GetString(objRetBytes).Split(",".ToCharArray());
this.ResponseCode = objRetVals[0].Trim(char.Parse("|"));
if (objRetVals[0].Trim(char.Parse("|")) == "1") { // Returned Authorization Code //this.ResponseCode = objRetVals[4].Trim(char.Parse("|")); ; // Returned Transaction ID this.TransactionID = objRetVals[6].Trim(char.Parse("|")); this.ResponseMessage = "Approved"; } else { // Error! strError = objRetVals[3].Trim(char.Parse("|")) + " (" + objRetVals[2].Trim(char.Parse("|")) + ")";
if (objRetVals[2].Trim(char.Parse("|")) == "44") { // CCV transaction decline strError += "Our Card Code Verification (CCV) returned " + "the following error: ";
switch (objRetVals[38].Trim(char.Parse("|"))) { case "N": strError += "Card Code does not match."; break; case "P": strError += "Card Code was not processed."; break; case "S": strError += "Card Code should be on card but was not indicated."; break; case "U": strError += "Issuer was not certified for Card Code."; break; } }
if (objRetVals[2].Trim(char.Parse("|")) == "45") { if (strError.Length > 1) strError += "<br />n";
// AVS transaction decline strError += "Our Address Verification System (AVS) " + "returned the following error: ";
switch (objRetVals[5].Trim(char.Parse("|"))) { case "A": strError += " the zip code entered does not match " + "the billing address."; break; case "B": strError += " no information was provided for the AVS check."; break; case "E": strError += " a general error occurred in the AVS system."; break; case "G": strError += " the credit card was issued by a non-US bank."; break; case "N": strError += " neither the entered street address nor zip " + "code matches the billing address."; break; case "P": strError += " AVS is not applicable for this transaction."; break; case "R": strError += " please retry the transaction; the AVS system " + "was unavailable or timed out."; break; case "S": strError += " the AVS service is not supported by your " + "credit card issuer."; break; case "U": strError += " address information is unavailable for the " + "credit card."; break; case "W": strError += " the 9 digit zip code matches, but the " + "street address does not."; break; case "Z": strError += " the zip code matches, but the address does not."; break; } }
// strError contains the actual error this.ResponseMessage = strError; } } catch (Exception ex) { this.ResponseMessage = ex.Message; } }
Best of luck!
-Bryan Soltis
|