Bobby Edward said:
Does anyone have any ASP.NET sample that they can sent me to integrated
an ASP.NET website with Authorize.NET using SIM? I cannot find any
online samples. I already have my gateway id, API Login ID & Transaction
Key.
Thanks!
Do not remmember what is SIM but here is my class to integrate with
Authorizenet
Password is your TransactionKey, Interface is the URL path to APIs.
using System;
using System.Net;
using System.IO;
using System.Text;
using XoopsNet.BLL;
namespace XoopsNet.BLL.RetailStore
{
public class clsAuthorizeNet
{
string _sLogin;
string _sPassword;
bool _bTest;
string _sInterface;
string _sBFirst, _sBLast, _sBAddress1, _sBAddress2, _sBCity, _sBState,
_sBZip, _sBCountry, _sBPhone;
string _sShFirst, _sShLast, _sShAddress1, _sShAddress2, _sShCity,
_sShState, _sShZip, _sShCountry;
string _sCustId, _sCustomersIp;
string _sInvoiceNum, _sDescription, _sCardNum, _sCardCode;
decimal _fAmount;
int _iExpMonth, _iExpYear;
System.Text.StringBuilder _bld;
public enum TRANS_TYPE { AUTH_CAPTURE, AUTH_ONLY, CAPTURE_ONLY, CREDIT,
VOID, PRIOR_AUTH_CAPTURE};
public clsAuthorizeNet(
string sBFirst,
string sBLast,
string sBAddress1,
string sBAddress2,
string sBCity,
string sBState,
string sBZip,
string sBCountry,
string sBPhone,
string sShFirst,
string sShLast,
string sShAddress1,
string sShAddress2,
string sShCity,
string sShState,
string sShZip,
string sShCountry,
string sCustId,
string sCustomersIp,
string sInvoiceNum,
string sDescription,
string sCardNum,
string sCardCode,
decimal fAmount,
int iExpMonth,
int iExpYear)
{
_sLogin = clsGlobal._sAuthorizeLogin;
_sPassword = clsGlobal._sAuthorizePassword;
_sInterface = clsGlobal._sAuthorizeInterface;
_bTest = clsGlobal._bAuthorizeTest;
_sBFirst = sBFirst;
_sBLast = sBLast;
_sBAddress1 = sBAddress1 ;
_sBAddress2 = sBAddress2 ;
_sBCity = sBCity ;
_sBState = sBState ;
_sBZip = sBZip ;
_sBCountry = sBCountry;
_sBPhone = sBPhone;
_sShFirst = sShFirst ;
_sShLast = sShLast;
_sShAddress1 = sShAddress1 ;
_sShAddress2 = sShAddress2 ;
_sShCity = sShCity ;
_sShState = sShState ;
_sShZip = sShZip;
_sShCountry = sShCountry;
_sCustId = sCustId;
_sCustomersIp = sCustomersIp;
_sInvoiceNum = sInvoiceNum ;
_sDescription = sDescription ;
_sCardNum = sCardNum ;
_sCardCode = sCardCode ;
_fAmount = fAmount;
_iExpMonth = iExpMonth;
_iExpYear = iExpYear ;
}
public void AppendPair(string sName, string sValue, int iMaxLen)
{
if( sValue.Length > iMaxLen)
sValue = sValue.Substring(0,iMaxLen -1 );
if( _bld.Length != 0 )
_bld.Append('&');
_bld.Append(sName);
_bld.Append("=");
_bld.Append(sValue);
}
public void AppendPair(string sName, bool bValue)
{
if( bValue)
AppendPair(sName, "TRUE",5);
else
AppendPair(sName, "FALSE", 5);
}
public int SendRequest(TRANS_TYPE type, ref string sAutorizationCode, ref
string sTransactionId, out string sReasonText)
{
sReasonText = "";
string sTmp;
_bld = new System.Text.StringBuilder();
AppendPair("x_login", _sLogin, 20);
AppendPair("x_password", _sPassword, 20);
AppendPair("x_test_request", _bTest);
AppendPair("x_delim_data", true);
AppendPair("x_delim_char", ",", 1);
AppendPair("x_encap_char", "", 1);
AppendPair("x_delim_data", true);
AppendPair("x_version", "3.1", 3);
//Billing address
AppendPair("x_first_name", _sBFirst,50);
AppendPair("x_last_name", _sBLast,50);
sTmp = _sBAddress1 + " " + _sBAddress2;
sTmp = sTmp.Trim();
AppendPair("x_address", sTmp, 60);
AppendPair("x_city", _sBCity, 40);
AppendPair("x_state", _sBState, 40);
AppendPair("x_zip", _sBZip, 20);
AppendPair("x_country", _sBCountry, 60);
AppendPair("x_phone", _sBPhone, 60);
//Shipping address
AppendPair("x_ship_to_first_name", _sShFirst,50);
AppendPair("x_ship_to_last_name", _sShLast,50);
sTmp = _sShAddress1 + " " + _sShAddress2;
sTmp = sTmp.Trim();
AppendPair("x_ship_to_address", sTmp, 60);
AppendPair("x_ship_to_city", _sShCity, 40);
AppendPair("x_ship_to_state", _sShState, 40);
AppendPair("x_ship_to_zip", _sShZip, 20);
AppendPair("x_ship_to_country", _sShCountry, 60);
//transaction info
AppendPair("x_customer_ip", _sCustomersIp, 15);
AppendPair("x_cust_id", _sCustId, 20);
AppendPair("x_invoice_num", _sInvoiceNum, 20);
AppendPair("x_description", _sDescription, 255);
AppendPair("x_amount", _fAmount.ToString("0.00"), 15);
AppendPair("x_method", "CC", 2);
AppendPair("x_type", type.ToString(), 20);
if(( type == TRANS_TYPE.CREDIT ) || ( type == TRANS_TYPE.VOID) || (type
== TRANS_TYPE.PRIOR_AUTH_CAPTURE ))
AppendPair("x_trans_id", sTransactionId, 10);
if( type == TRANS_TYPE.CAPTURE_ONLY)
AppendPair("x_auth_code", sAutorizationCode, 10);
AppendPair("x_card_num", _sCardNum, 22);
string sExpDate = _iExpMonth.ToString("00") + "/" +
_iExpYear.ToString("0000");
AppendPair("x_exp_date", sExpDate, 10);
AppendPair("x_card_code", _sCardCode, 4);
HttpWebRequest rq = null;
WebResponse response = null;
Stream st = null;
try
{
string sPostData = _bld.ToString();
rq = (HttpWebRequest) WebRequest.Create(_sInterface);
rq.Timeout = 1000*60; //1 minute timeout
rq.MaximumAutomaticRedirections=3;
rq.AllowAutoRedirect=true;
rq.KeepAlive = false;
rq.ContentType = "application/x-www-form-urlencoded";
rq.ContentLength = sPostData.Length;
rq.Method = "POST";
byte [] byte1 = System.Text.ASCIIEncoding.ASCII.GetBytes(sPostData);
st = rq.GetRequestStream();
st.Write(byte1, 0, byte1.Length);
st.Close();
st = null;
response = rq.GetResponse();
st = response.GetResponseStream();
byte [] buf = new byte[3000];
int iIndex = 0, iRead;
while(true)
{
iRead = st.Read(buf, iIndex, 3000 - iIndex);
iIndex += iRead;
if( iRead == 0 )
break;
}
st.Close();
st = null;
string sResponse = System.Text.ASCIIEncoding.ASCII.GetString(buf);
string [] sR = sResponse.Split(',');
//sR[2] will be '1' if success.
string sCode = sR[2];
sReasonText = sR[3];
sAutorizationCode = sR[4];
string sAvsMatch = sR[5];
string sCvvMatch = "P";
if( sR.Length > 39 )
sCvvMatch = sR[38];
sTransactionId = sR[6];
switch (sAvsMatch)
{
case "A":
sReasonText += "<br>AVS: Street Matched, Zip does not";
break;
case "B":
sReasonText += "<br>AVS: No Info";
break;
case "E":
sReasonText += "<br>AVS: AVS Error";
break;
case "G":
sReasonText += "<br>AVS: Non-US card";
break;
case "N":
sReasonText += "<br>AVS: No Match on Address (Street) or ZIP";
break;
case "P":
sReasonText += "<br>AVS: AVS not applicable for this transaction";
break;
case "R":
sReasonText += "<br>AVS: Retry - System unavailable or timed out";
break;
case "S":
sReasonText += "<br>AVS: Service not supported by issuer";
break;
case "U":
sReasonText += "<br>AVS: Address information is unavailable";
break;
case "W":
sReasonText += "<br>AVS: 9 digit ZIP matches, Address (Street) does
not";
break;
case "X":
sReasonText += "<br>AVS: Address (Street) and 9 digit ZIP match";
break;
case "Y":
sReasonText += "<br>AVS: Address (Street) and 5 digit ZIP match";
break;
case "Z":
sReasonText += "<br>AVS: 5 digit ZIP matches, Address (Street) does
not";
break;
default:
sReasonText += "<br>AVS: code - " + sAvsMatch;
break;
}
switch (sCvvMatch)
{
case "M":
sReasonText += "<br>CVV:Match";
break;
case "N":
sReasonText += "<br>CVV:No Match";
break;
case "P":
sReasonText += "<br>CVV:Not Processed";
break;
case "S":
sReasonText += "<br>CVV:Should have been present";
break;
case "U":
sReasonText += "<br>CVV:Issuer unable to process request";
break;
default:
sReasonText += "<br>CVV: code - " + sCvvMatch;
break;
}
int iErrCode = Int32.Parse(sCode);
SaveCCTransactionHistory(type, Int32.Parse(_sInvoiceNum), iErrCode,
sReasonText, _fAmount, _sCardNum, _sCardCode, sExpDate);
return Int32.Parse(sCode);
}
catch(Exception e)
{
clsGlobal.SendEmail(e.ToString(), "CC ERR",
"(e-mail address removed)", null, null);
return 0;
}
finally
{
if( response != null )
response.Close();
if( st != null )
st.Close();
}
}
public static void SaveCCTransactionHistory(TRANS_TYPE type, int
iOrderId, int iError, string sMessage, decimal fAmount, string sCcNum,
string sCvv, string sExp )
{
if( type == TRANS_TYPE.CREDIT )
fAmount = -fAmount;
clsGlobal.ExecuteSql(String.Format("INSERT INTO
tblCCTransactions(OrderId, ErrCode, Reason, Amount, CCNum, Cvv, ExpDate)
VALUES({0}, {1}, '{2}', {3}, '{4}', '{5}', '{6}')", iOrderId, iError,
sMessage.Replace("'", "''"), fAmount, sCcNum, sCvv, sExp));
}
public string GetError( int iReasonSubcode)
{
string sError = "";
switch( iReasonSubcode )
{
case 4:
case 5:
case 6:
sError = "The credit card number is invalid";
break;
case 7:
sError = "The credit card expiration date is invalid";
break;
case 8:
sError = "The credit card expiration date is invalid";
break;
case 11:
sError = "Duplicate transaction has been submitted. Please wait 2
minutes before resubmit";
break;
case 37:
sError = "The credit card number is invalid";
break;
case 78:
sError = "The CVV code is invalid";
break;
default:
sError = "";//"Transaction declined - " + iReasonSubcode.ToString();
break;
}
return sError;
}
}
}