Tuesday, December 2, 2008

Authorize.Net Advanced Integration Method (AIM) example in C#

void Page_Load(Object Src, EventArgs E) {
myPage.Text = readHtmlPage("https://test.authorize.net/gateway/transact.dll");
'Uncomment the line ABOVE for shopping cart testing
'OR uncomment the line BELOW for live accounts
'myPage.Text = readHtmlPage("https://secure.authorize.net/gateway/transact.dll")
}

private String readHtmlPage(string url)
{
String result = "";
String strPost = "x_login=YOUR-LOG-IN-ID&x_tran_key=YOUR-TRANSACTION-KEY&x_method=CC&x_type=AUTH_CAPTURE&x_amount=1.00&x_delim_data=TRUE&x_delim_char=|&x_relay_response=FALSE&x_card_num=4111111111111111&x_exp_date=052009&x_test_request=TRUE&x_version=3.1";
StreamWriter myWriter = null;

HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
objRequest.Method = "POST";
objRequest.ContentLength = strPost.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";

try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(strPost);
}
catch (Exception e)
{
return e.Message;
}
finally {
myWriter.Close();
}

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr =
new StreamReader(objResponse.GetResponseStream()) )
{
result = sr.ReadToEnd();

// Close and clean up the StreamReader
sr.Close();
}
return result;
}

No comments: