Friday, February 26, 2010

Part-I AuthorizeNet.cs

AuthorizeNet.cs
AuthorizeNetFields.cs
AuthorizeNetRequest.cs
AuthorizeNetResponse.cs

#region Namespaces

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Text;

using System.Net;

using System.IO;

using System.Collections;

using ClassLib.bl;

#endregion


public static class AuthorizeNet

{


#region Private Member(s)

private static string UNEXPECTED_ERROR = "Unexpected error";

private static string AUTHORIZENET_TEST_URL ="https://test.authorize.net/gateway/transact.dll";

private static string AUTHORIZENET_LIVE_URL ="https://secure.authorize.net/gateway/transact.dll";

#endregion


#region Transaction Type Enum(s)

public enum TransactionType

{

AUTH_CAPTURE = 0,

AUTH_ONLY,

PRIOR_AUTH_CAPTURE,

CREDIT,

VOID

}


public static string GetSringForEnum(TransactionType TransType)

{

switch (TransType)

{

case TransactionType.AUTH_CAPTURE:

return "AUTH_CAPTURE";

break;

case TransactionType.AUTH_ONLY:

return "AUTH_ONLY";

break;

case TransactionType.PRIOR_AUTH_CAPTURE:

return "PRIOR_AUTH_CAPTURE";

break;

case TransactionType.CREDIT:

return "CREDIT";

break;

case TransactionType.VOID:

return "VOID";

break;

default:

return string.Empty;

break;

}

}

#endregion

private static string GetRequestString(AuthorizeNetRequestobjAuthorizeNetRequest, AuthorizeNetFields allFields, TransactionTypeTransType)

{

Hashtable post_values = new Hashtable();

post_values.Add("x_login", objAuthorizeNetRequest.Login);

post_values.Add("x_tran_key", objAuthorizeNetRequest.TransactionKey);


post_values.Add("x_delim_data", "TRUE");

post_values.Add("x_delim_char", '|');

post_values.Add("x_relay_response", "FALSE");


post_values.Add("x_type", GetSringForEnum(TransType));

post_values.Add("x_method", "CC");

post_values.Add("x_card_num", objAuthorizeNetRequest.CardNumber);

post_values.Add("x_exp_date", objAuthorizeNetRequest.CardExpirationDate);

post_values.Add("x_Card_Code", objAuthorizeNetRequest.CcvNumber);


post_values.Add("x_amount", objAuthorizeNetRequest.Amount);

post_values.Add("x_description", allFields.x_Description);

post_values.Add("x_tax", allFields.x_Tax);


post_values.Add("x_first_name", allFields.x_First_Name);

post_values.Add("x_last_name", allFields.x_Last_Name);

post_values.Add("x_address", allFields.x_Address);

post_values.Add("x_city", allFields.x_City);

post_values.Add("x_zip", allFields.x_Zip);

post_values.Add("x_state", allFields.x_State);

post_values.Add("x_country", allFields.x_Country);

post_values.Add("x_phone", allFields.x_Phone);

post_values.Add("x_email", allFields.x_Email);


post_values.Add("x_ship_to_first_name", allFields.x_Ship_to_first_name);

post_values.Add("x_ship_to_last_name", allFields.x_Ship_to_last_name);

post_values.Add("x_ship_to_address", allFields.x_Ship_to_address);

post_values.Add("x_ship_to_city", allFields.x_Ship_to_city);

post_values.Add("x_ship_to_state", allFields.x_Ship_to_state);

post_values.Add("x_ship_to_zip", allFields.x_Ship_to_zip);

post_values.Add("x_ship_to_country",allFields.x_Ship_to_country);


if (TransType == TransactionType.CREDIT || TransType ==TransactionType.PRIOR_AUTH_CAPTURE || TransType == TransactionType.VOID)

{

post_values.Add("x_trans_id", objAuthorizeNetRequest.TransactionId);

}

String post_string = "";

foreach (DictionaryEntry field in post_values)

{

post_string += field.Key + "=" + field.Value + "&";

}

post_string = post_string.TrimEnd('&');


return post_string;

}


public static AuthorizeNetResponse CallAuthorizeNetMethod(AuthorizeNetRequestobjAuthorizeNetRequest, AuthorizeNetFields allFields)
{
HttpWebRequest objRequest = null;
if (ReadConfig.IsTestPayment)
{
objRequest = (HttpWebRequest)WebRequest.Create(AUTHORIZENET_TEST_URL);
}
else
{
objRequest = (HttpWebRequest)WebRequest.Create(AUTHORIZENET_LIVE_URL);
}
objRequest.Method = "POST";
string postData = GetRequestString(objAuthorizeNetRequest, allFields, objAuthorizeNetRequest.TransactionType);
ASCIIEncoding enco = new ASCIIEncoding();
byte[] byte1 = enco.GetBytes(postData);
objRequest.ContentType = "application/x-www-form-urlencoded";
objRequest.ContentLength = byte1.Length;
Stream myWriter = objRequest.GetRequestStream();
myWriter.Write(byte1, 0, byte1.Length);
myWriter.Close();
String post_response;
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = newStreamReader(objResponse.GetResponseStream()))
{
post_response = responseStream.ReadToEnd();
responseStream.Close();
}
Array response_array = post_response.Split('|');
AuthorizeNetResponse objAuthorizeNetResponse = new AuthorizeNetResponse();
HandleResponse(post_response, objAuthorizeNetResponse);
return objAuthorizeNetResponse;
}
private static void HandleError(AuthorizeNetResponse objAuthorizeNetResponse,string ErrorMessage)
{
objAuthorizeNetResponse.IsSuccess = false;
objAuthorizeNetResponse.Errors = ErrorMessage;
}
private static void HandleResponse(string strResponse, AuthorizeNetResponseobjAuthorizeNetResponse)
{
string[] strWebResponse = strResponse.Split(new char[] { '|' },StringSplitOptions.None);
if (strResponse != null)
{
if (strWebResponse[0] == "1")
{
objAuthorizeNetResponse.IsSuccess = true;
if (strWebResponse.Length > 3)
objAuthorizeNetResponse.SuccessMessage = strWebResponse[3];
if (strWebResponse.Length > 6)
objAuthorizeNetResponse.TransactionId = strWebResponse[6];
}
else
{
if (strWebResponse.Length > 3)
HandleError(objAuthorizeNetResponse, strWebResponse[3]);
else
HandleError(objAuthorizeNetResponse, UNEXPECTED_ERROR);
}
}
else
{
HandleError(objAuthorizeNetResponse, UNEXPECTED_ERROR);
}
}
}

No comments:

Post a Comment