« Klients (tm) Alpha Launched | ^ Main | GoDaddy: Business Registration »
AuthorizeNet.cfc & Card charging process
I built this for our Klients product launch and figured I’d share it. It only handles one transaction type but can easily be updated to handle multiple types and even used to authorize then later save. I’ve used the latter in a case where there might be a databsae issue you don’t want to just charge the credit card without making sure the order saves properly. You won’t want to, well I don’t, save the order then go back and update the transaction value either. I’ll explain a bit more as to what I mean here in a second.
Here are the CFC’s I use and it also shows my general setup. All of my cfc’s extend Base.cfc (below) which contains basic functions I need. Even my ApplicationInit.cfc (another custom CFC I’ll release one day) uses the Base.cfc.
AuthorizeNet.cfc
<cfcomponent displayname="AuthorizeNet.cfc" hint="Processes credit cards with Authorize.net" extends="Base">
<cffunction name="init" access="public" returntype="struct">
<cfset var ini = ExpandPath("/assets/ini/") & "Authorizenet.ini">
<cftry>
<cfset variables.settings = StructNew()>
<cfset variables.settings.x_Login = super.getIniSetting(ini=ini, section="default", key="x_Login") />
<cfset variables.settings.x_Password = super.getIniSetting(ini=ini, section="default", key="x_Password") />
<cfset variables.settings.x_merchant_email = super.getIniSetting(ini=ini, section="default", key="x_merchant_email") />
<cfset variables.settings.x_tran_key = super.getIniSetting(ini=ini, section="default", key="x_tran_key") />
<cfcatch type="any">
<cfthrow message="Authorize.net Login Information Unavailable" detail="I could not read the ini file to get my login information. Without it I can't process cards." />
</cfcatch>
</cftry>
<cfreturn this />
</cffunction>
<cffunction name="authorize" access="public" returntype="any">
<cfargument name="CustomerID" required="yes" type="string">
<cfargument name="BillingFirstName" required="yes" type="string">
<cfargument name="BillingLastName" required="yes" type="string">
<cfargument name="BillingCompany" type="string" default="">
<cfargument name="BillingAddress" required="yes" type="string">
<cfargument name="BillingCity" required="yes" type="string">
<cfargument name="BillingState" required="yes" type="string">
<cfargument name="BillingZipCode" required="yes" type="string">
<cfargument name="BillingPhone" required="yes" type="string">
<cfargument name="BillingEmailAddress" required="yes" type="string" hint="This is where the receipt will go, if configured">
<cfargument name="NameOnCard" required="yes" type="string">
<cfargument name="CardNumber" required="yes" type="string">
<cfargument name="ExpirationDate" required="yes" type="string">
<cfargument name="SecurityCode" required="yes" type="string">
<cfargument name="InvoiceNumber" required="yes" type="string">
<cfargument name="InvoiceDescription" required="yes" type="string" default="Payment for Invoice ##">
<cfargument name="Amount" required="yes" type="string">
<cfset var response = StructNew()>
<cfset var cfhttp = "">
<cftry>
<cfdump var="#variables.settings#">
<cfhttp url="https://secure.authorize.net/gateway/transact.dll" method="post">
<cfhttpparam type="FORMFIELD" name="x_Login" value="#decryptIt(variables.settings.x_Login)#">
<cfhttpparam type="FORMFIELD" name="x_Password" value="#decryptIt(variables.settings.x_Password)#">
<cfhttpparam type="FORMFIELD" name="x_merchant_email" value="#variables.settings.x_merchant_email#">
<cfhttpparam type="FORMFIELD" name="x_tran_key" value="#variables.settings.x_tran_key#">
<cfhttpparam type="FORMFIELD" name="x_delim_data" value="true">
<cfhttpparam type="FORMFIELD" name="x_test_request" value="false">
<cfhttpparam type="FORMFIELD" name="x_type" value="AUTH_CAPTURE">
<cfhttpparam type="FORMFIELD" name="x_method" value="cc">
<cfhttpparam type="FORMFIELD" name="x_amount" value="#arguments.Amount#">
<cfhttpparam type="FORMFIELD" name="x_card_num" value="#arguments.CardNumber#">
<cfhttpparam type="FORMFIELD" name="x_exp_date" value="#arguments.ExpirationDate#">
<cfhttpparam type="FORMFIELD" name="x_card_code" value="#arguments.SecurityCode#">
<cfhttpparam type="FORMFIELD" name="x_email_customer" value="false">
<cfhttpparam type="FORMFIELD" name="x_customer_ip" value="#cgi.remote_address#">
<cfhttpparam type="FORMFIELD" name="x_cust_id" value="#arguments.CustomerID#">
<cfhttpparam type="FORMFIELD" name="x_first_name" value="#arguments.BillingFirstName#">
<cfhttpparam type="FORMFIELD" name="x_last_name" value="#arguments.BillingLastName#">
<cfhttpparam type="FORMFIELD" name="x_company" value="#arguments.BillingCompany#">
<cfhttpparam type="FORMFIELD" name="x_address" value="#arguments.BillingAddress#">
<cfhttpparam type="FORMFIELD" name="x_city" value="#arguments.BillingCity#">
<cfhttpparam type="FORMFIELD" name="x_state" value="#arguments.BillingState#">
<cfhttpparam type="FORMFIELD" name="x_zip" value="#arguments.BillingZipCode#">
<cfhttpparam type="FORMFIELD" name="x_Email" value="#arguments.BillingEmailAddress#">
<cfhttpparam type="FORMFIELD" name="x_Phone" value="#arguments.BillingPhone#">
<cfhttpparam type="FORMFIELD" name="x_invoice_num" value="#arguments.InvoiceNumber#">
<cfhttpparam type="FORMFIELD" name="x_description" value="#arguments.InvoiceDescription##arguments.InvoiceNumber#">
</cfhttp>
<cfset response.code = ListGetAt(cfhttp.fileContent, '1', ",")>
<cfset response.subCode = ListGetAt(cfhttp.fileContent, '2', ",")>
<cfset response.reasonCode = ListGetAt(cfhttp.fileContent, '3', ",")>
<cfset response.reasonText = ListGetAt(cfhttp.fileContent, '4', ",")>
<cfset response.authCode = ListGetAt(cfhttp.fileContent, '5', ",")>
<cfset response.avsCode = ListGetAt(cfhttp.fileContent, '6', ",")>
<cfset response.transactionID = ListGetAt(cfhttp.fileContent, '7', ",")>
<cfcatch type="any">
<cfset response.code = 0>
<cfset response.reasonText = "System error: " & cfcatch.Detail & " - " & cfcatch.Message>
</cfcatch>
</cftry>
<cfreturn response>
</cffunction>
</cfcomponent>
Base.cfc
<cfcomponent displayname="Base.cfc" hint="Base functions">
<cffunction name="getIniSetting" access="private" output="false">
<cfargument name="ini" required="yes" type="string" />
<cfargument name="section" required="yes" type="string" />
<cfargument name="key" required="yes" type="string" />
<cfreturn GetProfileString(arguments.ini, arguments.section, arguments.key)>
</cffunction>
<cffunction name="encryptIt" access="private" output="false">
<cfargument name="str" type="string" required="yes" />
<cfreturn encrypt(arguments.str, application.settings.key, application.settings.algorithm, application.settings.encoding) />
</cffunction>
<cffunction name="decryptIt" access="private" output="false">
<cfargument name="str" type="string" required="yes" />
<cfreturn decrypt(arguments.str, application.settings.key, application.settings.algorithm, application.settings.encoding) />
</cffunction>
</cfcomponent>
Now to detail what I meant earlier about the process for charging credit cards I’ll give a list of the steps.
- Authorize the amount of the card
- If authorize is successful, save order to the database.
- If order is saved, capture money on credit card with Authorize.net
- If order fails, void transaction with Authorize.net.
- If authorize fails, return error to customer.
- If authorize is successful, save order to the database.
This gives you a failsafe when charging peoples cards and taking orders. There have been too many times a credit card was charged but the save failed at the database level. It is frustrating to the client, customer, and ultimately was frustrating to me.
Disclaimer: This was back in my early freelance days.
I hope these cfc’s and the processing information is helpful to someone. Enjoy.
Posted by John C. Bland II on March 13, 2006 4:46 PM | Permalink
TrackBack
TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/68



