Charge Credit Cards using Authorize.net and ColdFusion
Integrating Authorize

Charge Credit Cards using Authorize.net and ColdFusion

I have seen many shopping carts on the web that make thousands of dollars a month just processing your e-commerce transactions.  Now you too can cut out the middleman and save a few bucks or build your own processor and make a few bucks.  This example uses Authorize.net's AIM implementation.  In order for the following code to work correctly you must have a few things:

  • Authorize.net Merchant Account
  • Secure directory on your server using HTTPS on port 443.
  • A unique Transaction Key provided by Authorize.net.

Once you have obtained a Authorize.net Merchant Account and a unique transaction key you can begin, although you will need a secure directory to be able to use there gateway.  Now lets start with the application.

Collecting the Data:
I will assume that most of you know how to create a form to collect the data from the user so I will not go into detail about this section. (Please note that this does not provide any sort of data validation.  You will need to include some sort of validation on your production site)

Form Collection Page:
customer.cfm

<html>
<head>
<title>Enter Your Information</title>
</head>

<body>

<form method="POST" action="credit.cfm">
<p>
Name:<br>
<input type="text" name="x_FirstName" size="20"><input type="text" name="x_LastName" size="20"></p>
<p>Company:<br>
<input type="text" name="x_Company" size="43"></p>
<p>Address:<br>
<input type="text" name="x_Address" size="43"><br>
&nbsp;</p>
<p>City:<br>
<input type="text" name="x_City" size="20"></p>
<p>State:<br>
<input type="text" name="x_State" size="20"></p>
<p>Country:<br>
<input type="text" name="x_Country" size="20"></p>
<p>Phone Number:<br>
<input type="text" name="x_PhoneNumber" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>
</html>

Collecting Credit Card Info:
Nothing special about this page.  Just notice that we change the form fields into session variables using a loop.  (Please note that this does not provide any sort of data validation.  You will need to include some sort of validation on your production site)

Form Collection Page:
credit.cfm

<!--- Change form fields into session variables --->
<cfloop collection="#form#" item="var">
<cfif var contains "x_">
<cfset "session.#var#"="#evaluate('form.' & var)#">
</cfif>
</cfloop>


<html>
<head>
<title>Enter Your Credit Card Information</title>
</head>

<body>

<form method="POST" action="charge.cfm">
<p>
Credit Card Type:<br>
<input type="text" name="x_CardType" size="20"></p>
<p>Card Number:<br>
<input type="text" name="x_CardNumber" size="43"></p>
<p>CardDate:<br>
<input type="text" name="x_CardDate" size="43"><br>
&nbsp;</p>
<p>AVS Number:<br>
<input type="text" name="x_AVS" size="20"></p>
<p>Expiration Date:<br>
<input type="text" name="x_EXPDate" size="20"></p>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>

</body>
</html>

Charging the Credit Card:
Here is the meat and potatoes of this application.  For organization purposes I placed the card charge code in an include (find that code in the next section).  First notice the form to session loop like on the page before.  Once the information has been recorded we begin the charge request.

Form Collection Page:
charge.cfm

<!--- Change form fields into session variables --->
<cfloop collection="#form#" item="var">
<cfif var contains "x_">
<cfset "session.#var#"="#evaluate('form.' & var)#">
</cfif>
</cfloop>

<!--- ------------------------------------------------------------------------------------------------------------- Handles Charging --------------------------------------- --->
<cfinclude template="card_charger.cfm">
<!--- ------------------------------------------------------------------------------------------------------------- Handles Charging --------------------------------------- --->

<html>
<head>
<title>Charging the Card</title>
</head>

<body>
 

<!--- ------------------------------------------------------------------------------------------------------------- Handles Response Codes --------------------------------------- --->
<cfswitch expression="#ResponseCode#">
     <cfcase value="Approved">
<cfoutput>Your card has been charged: #DollarFormat(x_amount)#</cfoutput></cfcase>
     <cfcase value="Declined">Delcined</cfcase>
    
<cfcase value="Error">Error</cfcase>
</cfswitch>
<!--- ------------------------------------------------------------------------------------------------------------- Handles Response Codes --------------------------------------- --->

</body>
</html>

Charging the Credit Card Include:
For a complete list of all the available fields you can submit to Authorize.net check out the following link: http://www.authorize.net/support/AIM_guide.pdf.  I have chosen to show you the most basic type of response.  You will want to build on this as this is by no means a complete application.  If you have questions on how to add more fields please email me at IvanS@CoastalMalls.com.

Form Collection Page:
charge.cfm

<!--- First lets get out Merchant Account User ID, Transaction Key and Amount --->

<cfset x_login = "[Enter your User ID Here]">
<cfset x_tran_key="[Enter your Transaction Key Here]">
<cfset x_amount="[Enter your Amount to Charge Here]">

<!--- Now lets open a connection to Authorize.Net using CFHTTP --->

<cfhttp url="https://secure.authorize.net/gateway/transact.dll" method="post">
     <cfhttpparam type="formField" name="x_version" value="3.1">
     <cfhttpparam type="formField" name="x_delim_data" value="TRUE">
     <cfhttpparam type="formField" name="x_relay_response" value="FALSE">
     <cfhttpparam type="formField" name="x_login" value="#x_login#">
     <cfhttpparam type="formField" name="x_tran_key" value="#x_tran_key#">
     <cfhttpparam type="formField" name="x_amount" value="#x_amount#">
     <cfhttpparam type="formField" name="x_card_num" value="#session.x_cardNumber#">
     <cfhttpparam type="formField" name="x_exp_date" value="#session.x_expDate#">
     <cfhttpparam type="formField" name="x_type" value="AUTH_ONLY">
</cfhttp>

<!--- Everything is now up to Authorize.net in processing the card.  A response will be sent back to the server in a comma delimitated list.  This list will tell you several things such as:  If the card is approved/declined, the approval code, transaction id, and more.  Use the following code to grab these values from the list --->

<cfset AuthList = "#cfhttp.FileContent#">
<cfset listPostion="1">

<cfloop index="counter" list="#authList#" delimiters=",">
    <cfswitch expression="#listPostion#">
          <cfcase value="1">
               <cfswitch expression="#counter#">
                    <cfcase value="1"><cfset ResponseCode="Approved"></cfcase>
                    <cfcase value="2"><cfset ResponseCode="Declined"></cfcase>
                    <cfcase value="3"><cfset ResponseCode="Error"></cfcase>
               </cfswitch>
          </cfcase>
          <cfcase value="2">
               <cfset ResponseSubCode="#counter#">
          </cfcase>
          <cfcase value="3">
               <cfset ResponseReasonCode="#counter#">
          </cfcase>
          <cfcase value="4">
               <cfset ResponseReasonText="#counter#">
          </cfcase>
          <cfcase value="5">
               <cfset ApprovalCode="#counter#">
          </cfcase>
          <cfcase value="6">
               <cfset AVSResultCode="#counter#">
          </cfcase>
          <cfcase value="7">
               <cfset TransactionID="#counter#">
          </cfcase>
     </cfswitch>
<cfset listPostion=listPosition+1>
</cfloop>

<!--- You may notice that the list submitted back to you contains 69+ fields in the list.  I will leave it up to you on how to grab that information from the list.  --->

<!--- You may want to insert these values into a database for future use.  Just grab the data from the list and insert the values just like you would any data. --->

<!--- You may also want to clear the session variable just in case the browser decides to hit the refresh button.  You can do this using the following code:
<cflock timeout="10" throwontimeout="No" type="EXCLUSIVE" scope="SESSION">
<cfloop collection="#Session#" item="var">
<cfset structdelete(session,var)>
</cfloop>
</cflock>
--->
 

*** Remember ***
This is by no means a completed application, but rather a backbone for you to build upon.  If you have any questions please email me at IvanS@CoastalMalls.com

 

All ColdFusion Tutorials By Author: Ivan E Spaeth
  • Searching FULL-Text using MS SQL Server 2000
    A better way to search full text than Verity for developers using SQL Server 2000 as there back-end database.
    Author: Ivan E Spaeth
    Views: 15,148
    Posted Date: Thursday, January 13, 2005
  • Charge Credit Cards using Authorize.net and ColdFusion
    I have seen many shopping carts on the web that make thousands of dollars a month just processing your e-commerce transactions. Now you too can cut out the middleman and save a few bucks or build your own processor and make a few bucks. This example uses Authorize.nets AIM implementation.
    Author: Ivan E Spaeth
    Views: 22,117
    Posted Date: Monday, February 21, 2005
Download the EasyCFM.COM Browser Toolbar!