Wednesday, December 16, 2009

How to integrate HeartLand Payment Gateway in your website ?


Solution :

Step 1 : Create project in visual studio and add three files with named below,

1.CheckoutPage.aspx
2.ProcessPage.aspx
3.ProcessReturnPage.aspx

Put below code in respective file name as shown below.

1.CheckoutPage.aspx


<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected string _strTransNum = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
//TODO
//Initialize variable this._strTransNum below by assiging it the transaction number that is to be processed. It must not to be empty.
this._strTransNum = "IN123456";
//END
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Purchase Page</title>
<style type="text/css">
Body { background-color:RGB(255,255,255);}
.BT_BtnOut { Width:70px; font-size: 9pt; font-weight:bold;COLOR: RGB(0,0,0);font-family : Arial;}
.BT_BtnOvr { Width:70px; font-size: 9pt; font-weight:bold;COLOR: RGB(0,0,0);font-family : Arial;}
.BT_Field { FONT-SIZE: 8pt; font-family : Arial;COLOR: RGB(0,0,0);}
.BT_FieldDescription { FONT-WEIGHT: bold; FONT-SIZE: 8pt; font-family : Arial; COLOR : RGB(0,0,0);}
</style></head>
<body>
<div>
<form id="PurchaseForm" runat="server">
<input type="hidden" id="TransactionNumber" value="<%=this._strTransNum %>" />
<div id="PaymentMethodDiv" style="width:580px; height:400px; text-align:center; vertical-align:middle">
<table style="width:100%; height:100%">
<tr>
<td style="vertical-align:middle; text-align:center">
<table width="260px">
<tr>
<td style="text-align:right">
<input id="rbCreditCard" type="radio" checked="checked" name="PaymentMethod"/><span class="BT_Field">Credit Card</span></td>
</tr>
<tr>
<td style="text-align:right" colspan="2">
<input id="btnNext" type="button" value="Next >>" onclick="Next();" class="BT_BtnOut"
onmouseover="this.className='BT_BtnOvr'" onmouseout="this.className='BT_BtnOut';"/></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div id="ProcessDiv" style="display:none;">
<table>
<tr>
<td>
<!--You can adjust the iframe's height and width below in terms of pixels to your like-->
<iframe id="ProcessPage" style="overflow:hidden" height="750px" width="700px" frameborder="0"></iframe>
</td>
</tr>
</table>
</div>
</form>
<script type="text/javascript">
function Next()
{
document.getElementById('PaymentMethodDiv').style.display='none';
var processURL = "ProcessPage.aspx?No="+document.getElementById('TransactionNumber').value+"&ProcessType=";
processURL = processURL + "CreditCard";
document.getElementById('ProcessPage').src=processURL;
document.getElementById('ProcessDiv').style.display='block';
}
function Back()
{
document.getElementById('ProcessDiv').style.display='none';
document.getElementById('ProcessPage').src=null;
document.getElementById('PaymentMethodDiv').style.display='block';
}
function Change(index){}
</script>
</div>
</body>
</html>



2.ProcessPage.aspx


<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected string _strRedirectURL = string.Empty;
protected string _strVerifyNum = string.Empty;
protected string _strProcessType = string.Empty;
protected string _strTransType = string.Empty;
protected string _strMerchantID = string.Empty;
protected double _totalAmount = 0;
protected string _strTransNum = string.Empty;
protected string _strReturnURL = string.Empty;

//TODO
//Assign the name of the cardholder to variable strNameOnCard below.
protected string strNameOnCard = string.Empty;
//END

//TODO
//Assign the street address of the cardholder to variable strAddress below.
protected string strAddress = string.Empty;
//END

//TODO
//Assign the city of the cardholder to variable strCity below.
protected string strCity = string.Empty;
//END

//TODO
//Assign the state of the cardholder to variable strState below.
protected string strState = string.Empty;
//END

//TODO
//Assign the zip code of the cardholder to variable strZip below.
protected string strZip = string.Empty;
//END

//TODO
//Assign a page title for the process page to variable strPageTitle below.
protected string strPageTitle = string.Empty;
//END

//TODO
//Assign a transaction information title to variable strTxnsTitle below.
protected string strTxnsTitle = string.Empty;
//END

//TODO
//Assign a transaction number to variable strTxnsNum below.
protected string strTxnsNum = string.Empty;
//END

//TODO
//Assign a transaction amount to variable strTxnsAmount below.
protected string strTxnsAmount = string.Empty;
//END

//TODO
//Assign a convenience fee to variable strCONFee below.
protected string strCONFee = string.Empty;
//END

//TODO
//Assign a total amount to variable strTotalAmount below.
protected string strTotalAmount = string.Empty;
//END

//TODO
//Assign a Finish button to variable strFinishButton below.
protected string strFinishButton = string.Empty;
//END

//TODO
//Assign the total attempts number and used attempts number.
protected string strTotalAttempts = "0";
protected string strUsedAttempts = "0";
//END

//TODO
//Assign a Contact Phone to variable strContactPhone below.
protected string strContactPhone = string.Empty;
//END

//TODO
//Assign a variable strPromptCancel below to show Prompt Cancel on the WebConnect page.
//Value is Yes or No.
protected string strPromptCancel = string.Empty;
//END

//TODO
//Assign a variable strShowTransNo below to show transaction number on the WebConnect page.
//Value is Yes or No.
protected string strShowTransNo = string.Empty;
//END

//TODO
//Assign variable strCustomerID, strCustomerName below to store customer number and customer name for the WebConnect page.
protected string strCustomerID = "TestCustomer";
protected string strCustomerName = "TestCustomer";
//END

//TODO
//Assign variable strPONum below to store purchase order number for the WebConnect page.
protected string strPONum = string.Empty;
//END

//TODO
//Assign variable strInvoiceNum below to store invoice number for the WebConnect page.
protected string strInvoiceNum = string.Empty;
//END

//TODO
//Assign variable strFirstName below to store first name for the WebConnect page.
protected string strFirstName = string.Empty;
//END

//TODO
//Assign variable strLastName below to store last name for the WebConnect page.
protected string strLastName = string.Empty;
//END

//TODO
//Assign variable strTaxAmount below to store tax amount for the WebConnect Level II transaction.
protected string strTaxAmount = string.Empty;
//END

//TODO
//Assign variable strCustomerFirstName below to store customer's first name for the WebConnect page.
protected string strCustomerFirstName = string.Empty;
//END

//TODO
//Assign variable strCustomerLastName below to store customer's last name for the WebConnect page.
protected string strCustomerLastName = string.Empty;
//END

//TODO
//Assign variable strCustomerStreet2 below to store customer's Street 2 for the WebConnect page.
protected string strCustomerStreet2 = string.Empty;
//END

//TODO
//Assign variable strCustomerStreet3 below to store customer's Street 3 for the WebConnect page.
protected string strCustomerStreet3 = string.Empty;
//END

//TODO
//Assign variable strCustomerEmail below to store customer's Email for the WebConnect page.
protected string strCustomerEmail = string.Empty;
//END

//TODO
//Assign variable strCustomerDayPhone below to store customer's Daytime Phone for the WebConnect page.
protected string strCustomerDayPhone = string.Empty;
//END

//TODO
//Assign variable strCustomerNightPhone below to store customer's Night Phone for the WebConnect page.
protected string strCustomerNightPhone = string.Empty;
//END

//TODO
//Assign variable strCustomerMobile below to store customer's Mobile Phone for the WebConnect page.
protected string strCustomerMobile = string.Empty;
//END

//TODO
//Assign variable strCustomerFax below to store customer's Fax for the WebConnect page.
protected string strCustomerFax = string.Empty;
//END

//TODO
//Assign variable strCustomerProvince below to store customer's Province for the WebConnect page.
protected string strCustomerProvince = string.Empty;
//END

//TODO
//Assign variable strCustomerCountry below to store customer's Country for the WebConnect page.
protected string strCustomerCountry = string.Empty;
//END

protected void Page_Load(object sender, EventArgs e)
{
this._strProcessType = Request["ProcessType"].ToString(); //CreditCard,GSBCard or Check
this._strTransNum = Request["No"].ToString(); //Transaction Number
_strTransType = "Auth";

//TODO
//Assign a verification number to the variable this._strVerifyNum below. You can use the session's ID or a random number.
this._strVerifyNum = "IN123456";
//Store the verification number to the session. This verification number will be used to verify the request once the request is passed back from the WebConnect Portal with a result.
if (Session["TxnsVFNumb"] == null)
{
Session.Add("TxnsVFNumb", this._strVerifyNum);
}
else
{
Session["TxnsVFNumb"] = this._strVerifyNum;
}
//END

//TODO
//Assign your website's URL to the variable strURL below.
string strURL = "http://localhost/MyURL/";
//END
if (strURL.Substring(strURL.Length - 1, 1).Equals("/"))
{
strURL = strURL.Substring(0, (strURL.Length - 1));
}
this._strReturnURL = (strURL + "/ProcessReturnPage.aspx");
//TODO
//Assign the WebConnect Portal's URL to the variable this._strRedirectURL below.
this._strRedirectURL = "https://hps.webportal.test.secureexchange.net";
//END

if (_strRedirectURL.Substring(_strRedirectURL.Length - 1, 1).Equals("/"))
{
_strRedirectURL = _strRedirectURL.Substring(0, (_strRedirectURL.Length - 1));
}
this._strRedirectURL = (this._strRedirectURL + "/PaymentMain.aspx");

this.LoadAndChangeTransactionInformation();
this.LoadMerchantInformation();
}

protected void LoadAndChangeTransactionInformation()
{
//TODO
//Assign basic transaction information (i.e. the total amount of the transaction) to the variable this._totalAmount below. It must be number with 2 decimal places.
this._totalAmount = 0;
//END
//You can change the transaction's status or other information below inside this function.
}

protected void LoadMerchantInformation()
{
//TODO
//Assign the merchant's information, such as WebConnect User ID to the variable this._strMerchantID below.
this._strMerchantID = "XYZ";
//END
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Process Page</title>
<style type="text/css">
Body { background-color:RGB(255,255,255);}
.BT_BtnOut { Width:70px; font-size: 9pt; font-weight:bold;COLOR: RGB(0,0,0);font-family : Arial;}
.BT_BtnOvr { Width:70px; font-size: 9pt; font-weight:bold;COLOR: RGB(0,0,0);font-family : Arial;}
.BT_Field { FONT-SIZE: 8pt; font-family : Arial;COLOR: RGB(0,0,0);}
.BT_FieldDescription { FONT-WEIGHT: bold; FONT-SIZE: 8pt; font-family : Arial; COLOR : RGB(0,0,0);}
</style></head>
<body>
<div>
<form id="ProcessForm" runat="server">
<input type="hidden" name="ClientSessionID" value="<%=this._strVerifyNum %>" />
<input type="hidden" name="ProcessType" value="<%=this._strProcessType %>" />
<input type="hidden" name="TransType" value="<%=this._strTransType %>" />
<input type="hidden" name="UserName" value="<%=this._strMerchantID %>" />
<input type="hidden" name="Amount" value="<%=this._totalAmount.ToString("F2") %>" />
<input type="hidden" name="TransNum" value="<%=this._strTransNum %>" />
<input type="hidden" name="ReturnURL" value="<%=this._strReturnURL %>" />
<input type="hidden" name="HasHeader" value="true" />
<input type="hidden" name="NameOnCard" value="<%=strNameOnCard %>" />
<input type="hidden" name="Address" value="<%=strAddress %>" />
<input type="hidden" name="City" value="<%=strCity %>" />
<input type="hidden" name="State" value="<%=strState %>" />
<input type="hidden" name="Zip" value="<%=strZip %>" />
<input type="hidden" name="PageTitle" value="<%=strPageTitle %>" />
<input type="hidden" name="TxnsTitle" value="<%=strTxnsTitle %>" />
<input type="hidden" name="TxnsNumCaption" value="<%=strTxnsNum %>" />
<input type="hidden" name="TxnsAmountCaption" value="<%=strTxnsAmount %>" />
<input type="hidden" name="CONFeeCaption" value="<%=strCONFee %>" />
<input type="hidden" name="TotalAmountCaption" value="<%=strTotalAmount %>" />
<input type="hidden" name="FinishButtonCaption" value="<%=strFinishButton %>" />
<input type="hidden" name="TotalAttempts" value="<%=strTotalAttempts %>" />
<input type="hidden" name="UsedAttempts" value="<%=strUsedAttempts %>" />
<input type="hidden" name="ContactPhone" value="<%=strContactPhone %>" />
<input type="hidden" name="PromptCancel" value="<%=strPromptCancel %>" />
<input type="hidden" name="ShowTransNo" value="<%=strShowTransNo %>" />
<input type="hidden" name="CanSaveCard" value="Yes" />
<input type="hidden" name="CustomerID" value="<%=strCustomerID %>" />
<input type="hidden" name="CustomerName" value="<%=strCustomerName %>" />
<input type="hidden" name="PONum" value="<%=strPONum %>" />
<input type="hidden" name="InvoiceNo" value="<%=strInvoiceNum %>" />
<input type="hidden" name="FirstName" value="<%=strFirstName %>" />
<input type="hidden" name="LastName" value="<%=strLastName %>" />
<input type="hidden" name="TaxAmount" value="<%=strTaxAmount %>" />
<input type="hidden" name="CustomerFirstName" value="<%=strCustomerFirstName %>" />
<input type="hidden" name="CustomerLastName" value="<%=strCustomerLastName %>" />
<input type="hidden" name="CustomerStreet2" value="<%=strCustomerStreet2 %>" />
<input type="hidden" name="CustomerStreet3" value="<%=strCustomerStreet3 %>" />
<input type="hidden" name="CustomerEmail" value="<%=strCustomerEmail %>" />
<input type="hidden" name="CustomerDayPhone" value="<%=strCustomerDayPhone %>" />
<input type="hidden" name="CustomerNightPhone" value="<%=strCustomerNightPhone %>" />
<input type="hidden" name="CustomerMobile" value="<%=strCustomerMobile %>" />
<input type="hidden" name="CustomerFax" value="<%=strCustomerFax %>" />
<input type="hidden" name="CustomerProvince" value="<%=strCustomerProvince %>" />
<input type="hidden" name="CustomerCountryID" value="<%=strCustomerCountry %>" />
</form>
</div>
</body>
<script type="text/javascript">
window.onload=function()
{
document.forms[0].action = "<%=this._strRedirectURL%>";
document.forms[0].submit();
}
document.getElementById('__VIEWSTATE').parentNode.removeChild(document.getElementById('__VIEWSTATE'));
</script>
</html>



3.ProcessReturnPage.aspx


<%@ Page Language="C#" EnableViewState="false" EnableViewStateMac="false"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
protected int _intResultNum = -1;
protected string _strJSCode = string.Empty;
protected string strTransNum = string.Empty;
protected string strReferenceNumber = string.Empty;
protected string strAuthorizationCode = string.Empty;
protected string strResultNum = string.Empty;
protected string strUsedAttempts = string.Empty;
protected string strCardType = string.Empty;
protected string strCardNumber = string.Empty;
protected string strInvoiceNum = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request["ResultNum"] == null)
{
this._intResultNum = -1;
return;
}
else
{
this._intResultNum = Convert.ToInt32(Request["ResultNum"].ToString());
}

if (Request["TransNum"] == null)
{
this._intResultNum = -1;
return;
}

if (Request["ClientSessionID"] == null)
{
this._intResultNum = -1;
return;
}

if (String.IsNullOrEmpty((Request["TransNum"].ToString()))
OR String.IsNullOrEmpty(Convert.ToString(Request["ClientSessionID"]))
OR (this._intResultNum == -1)
OR (Session["TxnsVFNumb"] == null)
OR !Session["TxnsVFNumb"].ToString().Equals(Convert.ToString(Request["ClientSessionID"])))
{
this._intResultNum = -1;
return;
}

strTransNum = Convert.ToString(Request["TransNum"]);
strReferenceNumber = Convert.ToString(Request["ReferenceNumber"]);
strAuthorizationCode = Convert.ToString(Request["AuthorizationCode"]);
strResultNum = Convert.ToString(Request["ResultNum"]);

if (Request["CardNumber"] != null)
{
strCardNumber = "The last 4 card number is " + Request["CardNumber"].ToString();
}
else if (Request["AccountNumber"] != null)
{
strCardNumber = "You checking account number is " + Request["AccountNumber"].ToString();
}

if (Request["UsedAttempts"] != null)
{
strUsedAttempts = Request["UsedAttempts"].ToString();
}
else
{
strUsedAttempts = "0";
}

if (Request["CardType"] != null)
{
strCardType = Request["CardType"].ToString();
}
else
{
strCardType = "";
}

if (Request["InvoiceNum"] != null)
{
strInvoiceNum = Request["InvoiceNum"].ToString();
}
else
{
strInvoiceNum = "";
}

if (this._intResultNum == 0)
{
this.ChangeTransactionStatus();
//TODO
//You can modify the following JavaScript code to redirect to other URL.
_strJSCode = "window.onload = function(){document.getElementById('ResultInfor').style.display='block';";
_strJSCode += " document.getElementById('ResultMessage').style.display='block';}";
//END
}
else if (this._intResultNum == 3)
{
//TODO
//You can modify the following JavaScript code to set the message.
_strJSCode = "window.onload = function(){document.getElementById('ResultMessage').style.display='block';";
_strJSCode += "document.getElementById('lblNotes').innerHTML='The transaction has been canceled because the Max.attempts reached.';}";
//END
}
else if (this._intResultNum == 4)
{
//TODO
//You can modify the following JavaScript code to set the message.
_strJSCode = "window.onload = function(){document.getElementById('ResultMessage').style.display='block';";
_strJSCode += "document.getElementById('lblNotes').innerHTML='The session has timed out.';}";
//END
}
else
{
_strJSCode = "window.onload = function(){parent.Back();}";
}
}

protected void ChangeTransactionStatus()
{
//TODO
//You can change the transaction's status inside this function.
//END
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Process Return Page</title>
<style type="text/css">
Body { background-color:RGB(255,255,255);}
.BT_BtnOut { Width:70px; font-size: 9pt; font-weight:bold;COLOR: RGB(0,0,0);font-family : Arial;}
.BT_BtnOvr { Width:70px; font-size: 9pt; font-weight:bold;COLOR: RGB(0,0,0);font-family : Arial;}
.BT_Field { FONT-SIZE: 8pt; font-family : Arial;COLOR: RGB(0,0,0);}
.BT_FieldDescription { FONT-WEIGHT: bold; FONT-SIZE: 8pt; font-family : Arial; COLOR : RGB(0,0,0);}
</style></head>
<body>
<div>
<form id="ProcessReturnForm" runat="server">
<table id="ResultInfor" style="display:none">
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Transaction Number :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strTransNum %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Reference Number :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strReferenceNumber %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Authorization Code :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strAuthorizationCode %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Result Number :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strResultNum %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Used Attempts :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strUsedAttempts %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Card Type :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strCardType %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Card Number :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strCardNumber %></a></td>
</tr>
<tr>
<td style="text-align:right; width:200px">
<a class="BT_FieldDescription">Invoice Number :</a></td>
<td style="text-align:left; width:200px"><a class="BT_Field"><%=strInvoiceNum %></a></td>
</tr>
</table>
<table id="ResultMessage" style="display:none">
<tr>
<td style="text-align:center; height:50px" colspan="2">
<a id="lblNotes" class="BT_FieldDescription">You have been charged successfully.</a></td>
</tr>
<%if (this._intResultNum == 0) { %>
<tr>
<td style="text-align: right" colspan="2">
<input id="btnPrint" type="button" value="Print" onclick="window.print();" class="BT_BtnOut"
onmouseover="this.className='BT_BtnOvr'" onmouseout="this.className='BT_BtnOut';"/>
<input type="button" value="Colse" onclick="javascript:Back();" class="BT_BtnOut"
onmouseover="this.className='BT_BtnOvr'" onmouseout="this.className='BT_BtnOut';" /></td>
</tr>
<% } else {%>
<tr>
<td style="text-align: right" >
<input type="button" value="Colse" onclick="javascript:Back();" class="BT_BtnOut"
onmouseover="this.className='BT_BtnOvr'" onmouseout="this.className='BT_BtnOut';" /></td>
</tr>
<% } %>
</table>
</form>
</div>
<script type="text/javascript">
function Back()
{
top.opener=null;
top.open("","_self");
top.close();
window.close();
}
<%
Response.Write(this._strJSCode);
%>
</script>
</body>
</html>





Hope this post will help you,
if yes please put comment below of this page,
Rajesh Singh,
Asp.Net Developer
rajesh@indianic.com

My Blog : Click Here

No comments:

Post a Comment