Payment through Authorize.net in C#

Payment through Authorize.net in C#

1.  Test Card Details 

Card Number: 4111111111111111

Cvv Code: 123

Exp Date:1013

2. Code

// From serverside alert Function
protected void jsCall(string alert)
{

string script = @”alert( “”” + alert + @””” );”;
ScriptManager.RegisterStartupScript(this, this.GetType(), “jsCall”, script, true);
}

private bool AuthorizePayment()
{

string AuthNetLoginID = “123Abcd”;//Set your AuthNetLoginID here
string AuthNetTransKey = “123456Abcd”;// Get this from your authorize.net merchant interface
string straddress = “https://test.authorize.net/gateway/transact.dll”; //For Test Transaction
or
string straddress = “https://secure.authorize.net/gateway/transact.dll”; //For Live Transaction
string AuthNetVersion = “3.1”; // Contains CCV support

WebClient webClientRequest = new WebClient();
System.Collections.Specialized.NameValueCollection InputObject = new System.Collections.Specialized.NameValueCollection(30);
string[] ReturnValues;
string ErrorString;

InputObject.Add(“x_version”, AuthNetVersion);
InputObject.Add(“x_delim_data”, “True”);

InputObject.Add(“x_login”, AuthNetLoginID);
InputObject.Add(“x_tran_key”, AuthNetTransKey);

InputObject.Add(“x_relay_response”, “False”);

InputObject.Add(“x_test_request”, “False”);

}

//———————————————————————
InputObject.Add(“x_delim_char”, “|”);

//Billing Address
InputObject.Add(“x_first_name”, First Name);
InputObject.Add(“x_last_name”, Last Name);
InputObject.Add(“x_phone”, “Phone Number”);
InputObject.Add(“x_address”, Address);
InputObject.Add(“x_city”, City);
InputObject.Add(“x_state”, State);
InputObject.Add(“x_zip”, zip);
InputObject.Add(“x_country”, Country);

//shipping address
InputObject.Add(“x_ship_to_first_name”, First Name);
InputObject.Add(“x_ship_to_last_name”, Last Name);
InputObject.Add(“x_ship_to_address”,Address);
InputObject.Add(“x_ship_to_city”, City);
InputObject.Add(“x_ship_to_state”, State);
InputObject.Add(“x_ship_to_zip”, Zip);
InputObject.Add(“x_ship_to_country”, Country);

//Set transection id with element: x_trans_id Or retrive from position 7 (array index 6)
InputObject.Add(“x_email”, Email ID);

InputObject.Add(“x_email_customer”, “TRUE”); //Emails Customer
InputObject.Add(“x_merchant_email”, “payment@yourwebsite.com”); //Emails Merchant

InputObject.Add(“x_customer_ip”, Request.UserHostAddress); //Store Customer IP Address

//Amount
InputObject.Add(“x_description”, “Description of purchase”); //Description of Purchase

//Card Details
InputObject.Add(“x_card_num”, Card Number);
InputObject.Add(“x_exp_date”, Expiration Date);
InputObject.Add(“x_card_code”, Security Code);

InputObject.Add(“x_method”, “CC”);
InputObject.Add(“x_type”, “AUTH_CAPTURE”);

//calculate the net ammount

InputObject.Add(“x_amount”, Price);//Total Price For Payment
try
{

String post_string = “”;

foreach (string key in InputObject.Keys)
{
post_string += key + “=” + (string)InputObject[key] + “&”;
}

post_string = post_string.TrimEnd(‘&’);

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(straddress);
objRequest.Method = “POST”;
objRequest.ContentLength = post_string.Length;
objRequest.ContentType = “application/x-www-form-urlencoded”;

// post data is sent as a stream
StreamWriter myWriter = null;
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(post_string);
myWriter.Close();

// returned values are returned as a stream, then read into a string
String post_response;
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
post_response = responseStream.ReadToEnd();
responseStream.Close();
}

string[] response_array = post_response.Split(‘|’);
ReturnValues = response_array;
spiObj.ResponseString = post_response.ToString();

 

if (ReturnValues[0].Trim(char.Parse(“|”)) == “1”)
{

int TransactionId = ReturnValues[6].Trim(char.Parse(“|”));// You will get transaction id of payment

jsCall(“Payment SuccessFully”);
return true;
}
else
{
// Error!
ErrorString = ReturnValues[3].Trim(char.Parse(“|”)) + ” (” + ReturnValues[2].Trim(char.Parse(“|”)) + “)”;
jsCall(ErrorString);
if (ReturnValues[2].Trim(char.Parse(“|”)) == “44”)
{
// CCV transaction decline
ErrorString += “Credit Card Code Verification (CCV) returned the following error: “;

switch (ReturnValues[38].Trim(char.Parse(“|”)))
{
case “N”:
ErrorString += “Card Code does not match.”;
jsCall(ErrorString);
break;

case “P”:
ErrorString += “Card Code was not processed.”;
jsCall(ErrorString);
break;
case “S”:
ErrorString += “Card Code should be on card but was not indicated.”;
jsCall(ErrorString);
break;
case “U”:
ErrorString += “Issuer was not certified for Card Code.”;
jsCall(ErrorString);
break;
}
}

if (ReturnValues[2].Trim(char.Parse(“|”)) == “45”)
{
if (ErrorString.Length > 1)
ErrorString += “<br />n”;

// AVS transaction decline
ErrorString += “Address Verification System (AVS) ” +
“returned the following error: “;

switch (ReturnValues[5].Trim(char.Parse(“|”)))
{
case “A”:
ErrorString += ” the zip code entered does not match the billing address.”;
jsCall(ErrorString);
break;
case “B”:
ErrorString += ” no information was provided for the AVS check.”;
jsCall(ErrorString);
break;
case “E”:
ErrorString += ” a general error occurred in the AVS system.”;
jsCall(ErrorString);
break;
case “G”:
ErrorString += ” the credit card was issued by a non-US bank.”;
jsCall(ErrorString);
break;
case “N”:
ErrorString += ” neither the entered street address nor zip code matches the billing address.”;
jsCall(ErrorString);
break;
case “P”:
ErrorString += ” AVS is not applicable for this transaction.”;
jsCall(ErrorString);
break;
case “R”:
ErrorString += ” please retry the transaction; the AVS system was unavailable or timed out.”;
jsCall(ErrorString);
break;
case “S”:
ErrorString += ” the AVS service is not supported by your credit card issuer.”;
jsCall(ErrorString);
break;
case “U”:
ErrorString += ” address information is unavailable for the credit card.”;
jsCall(ErrorString);
break;
case “W”:
ErrorString += ” the 9 digit zip code matches, but the street address does not.”;
jsCall(ErrorString);
break;
case “Z”:
ErrorString += ” the zip code matches, but the address does not.”;
jsCall(ErrorString);
break;
}
}
return false;
}
}
catch (Exception ex)
{
// HdnPayment.Value = ex.Message;
string error = “The Payment Gateway Address is invalid” + ex.Message;

jsCall(error);
return false;
}
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply