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

 

About This Tutorial
Author: Ivan E Spaeth
Skill Level: Intermediate 
 
 
 
Platforms Tested: CFMX,CFMX7
Total Views: 153,997
Submission Date: February 21, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 2
Discuss This Tutorial
  • I've found www.authnetscripts.com to be extremely helpful with connecting our real-estate site to Authorize.Net automated recurring billing. We were in need of a solution to easily bill rental properties on a recurring basis. Good Luck.

  • Where's card_charger.cfm?

  • sir, this code is very interesting and i am about to use it on a clients website for payment processing for non authorize.net payment gateway. Regards Lakhani SpinTeen Pixel Advertising http://www.spinteen.com Artha Technologies http://www.arthatechnologies.com

  • Ivan, Thanks for the code it is a good start for anyone wanting to use Authorize.net for payment gateway. There is a small error I should point out that I found in your code. In the card_charger.cfm page on line 54 the statement for list position has a typo that throws a Variable Undefined error. Nothing big just thought I should point it out. Once again...great work! Brian

  • No problem let me explain the whole "credit processor workflow" to you. Authorize.net is a payment gateway. They work with your “Merchant Account” to provide you with a smooth processing system for your website (actually you can use authorize.net for any system not just a website). Your first step should be to create a merchant account. A merchant account is just a fancy way of saying “a bank account that handles your credit card charges). You can get a merchant account from any bank or provider in America but I suggest you start with one from your personal bank (or business bank). Just ask your banker for more details (they will know exactly what you are talking about). There is really no difference in banks merchant accounts as a merchant account is just a glorified checking account. Once you open up your merchant account you can contact authorize.net with your information. They will require your account number and routing number etc. This process will take a few days just to set up. Once you open an account with Authorize.net you can use there payment gateway. It’s actually a pretty easy process and will only cost you around $30.00 in application fees. Depending on your credit and so forth you can get a really low processing fee (around 1% I believe). Let me know if you need anything else! - Ivan Coastal Malls www.coastalcomputerstore.com Developer

  • Hi Ivan, I am following this tutorial and in the beginning trying to set up a merchant account. According to the following link I need an Authorize.Net Payment Gateway Account, and an Internet Merchant Account. Any ideas as what to do? Thanks http://www.authorize.net/solutions/gettingstarted.php Getting Started To accept credit card payments online you need the following tools: An Authorize.Net Payment Gateway Account, and an Internet Merchant Account. Authorize.Net, working with our extensive Reseller channel comprised of leading banks and credit card ISOs, can help you quickly and easily obtain both. Step 1 - Obtain an Internet Merchant Account An Internet Merchant Account is a type of bank account that allows a business to accept credit cards as a form of payment for online transactions. These accounts manage the flow of money from your customer's individual credit card accounts into your own business checking account. Authorize.Net works with the nation's leading providers of Internet Merchant Accounts. If you need an Internet Merchant Account, please see our directory of authorized Resellers. If you already have an Internet Merchant Account with one of our Resellers, please contact your Account Representative and ask them to help you with pricing and setup for Authorize.Net. Step 2 - Obtain an Authorize.Net Payment Gateway Account An Authorize.Net Payment Gateway Account provides you with a secure bridge between your e-commerce website and the credit card processing networks. Authorize.Net's price plan is right for all merchants from a sole proprietor to a large nationwide corporation. If you already have an Internet Merchant Account, contact one of our Sales Representatives to verify that your account is properly configured to accept Internet payments with Authorize.Net. If your account is not with an Authorize.Net Reseller, please contact our Sales Department to get you the information you need to get started. Contact Authorize.Net Sales Office Hours: 7am - 6pm MST, Monday through Friday E-mail: sales@authorize.net Tel: 1-866-437-0476 Find a reseller now!

Advertisement

Sponsored By...
Dripping Springs, Texas - Deep Tissue Massages and Swedish Massages!