PHP - AIM Authorize.Net gateway code
Ok, I know a while back I offered to give sample code on how to connect to Authorize.Net to process credit card payments. Here is a guide for how to do it. Note this code works for VISA/MASTERCARD/AMEX/DISCOVER card payments. I have not tested it for processing checks. The following is using PHP on a FreeBSD system.
The code below has just a SAMPLE of the data to be sent to authorize.net. See "Standard Transaction Submission API for AIM" in the AIM Implimention Guide (http://www.authorize.net/support/AIM_guide.pdf) to see what values to use in the $pData section. The ones in the code I provide are samples, however enough to get a card processed.
I hard coded procossing the results and how they are handled, so I cannot post the code here, but if you follow the AIM Implimention Guide (see section "Gateway Response API" for the format of the result), you should be able to figure out how to use it. I have provided below a sample of the data that will be in the $aResult variable when processed. You will want to code in some error checking and result processing. See the section in the Guide "Response Code Details" for details of what the responce codes mean so you can set up proper errors. Also note, if you, like I did, allowed multiple line input for the address, if you do not strip it before sending to authorize.net, the result page will contain MORE THAN ONE LINE, not just a normal single line.
In a side note, while looking up the URL for the Implimention Guide, I see that Authorize.net now will e-mail you sample code. I tried to get it to end me a sample of PHP code for the AIM method, but so far nothing in my inbox (perhaps it is becasue I wasn't logged into their system at the time). They have the following samples available at http://developer.authorize.net/branded/samplecode :
Advanced Integration Method (AIM)
--ASP (VBScript)
--ASP.Net (Using C# or VB.NET)
--Cold Fusion
--Java
--Perl
--PHP
Simple Integration Method (SIM)
--ASP (VBScript)
--Cold Fusion
--Perl
--PHP
<?php
// Add the values into array $pData
// The formatted post string will be in the
// variable $pString
$pData = array();
$pString = '';
// These values will most likely always be the same for each charge
$pData['x_login'] = 'username';
$pData['x_password'] = 'password';
$pData['x_tran_key'] = 'keyvalue';
$pData['x_test_request'] = 'FALSE';
$pData['x_version'] = '3.1';
$pData['x_delin_data'] = 'True';
$pData['x_relay_response'] = 'False';
$pData['x_method'] = 'CC';
$pData['x_type'] = 'AUTH_CAPTURE';
// These values will be variable based on the transaction, and
// thus would normally come from variables. MAKE SURE YOU DO
// ERROR CHECKING ON THE VALUES YOU PUT HERE. For this sample
// I left them all as hard coded values.
$pData['x_invoice_num'] = '0609073150';
$pData['x_amount'] = '24.95';
$pData['x_description'] = 'Sample Web Susbscription';
$pData['x_cust_id'] = 'zippyman';
$pData['x_first_name'] = 'JOHN';
$pData['x_last_name'] = 'SMITH';
$pData['x_company'] = 'ZIPMART INC';
$pData['x_address'] = '1234 Main St.';
$pData['x_city'] = 'Anytown';
$pData['x_state'] = 'OH';
$pData['x_zip'] = '44325';
$pData['x_country'] = 'US';
$pData['x_phone'] = '8005551212';
$pData['x_email'] = '[email protected]';
$pData['x_card_num'] = '4111111111111111';
$pData['x_exp_date'] = '1207';
$pData['x_card_code'] = '411';
$pData['x_customer_ip'] = '192.168.1.100';
$pData['x_email_customer'] = 'False';
$pData['x_merchant_email'] = '[email protected]';
foreach($pData as $key => $val)
$pString .= trim($key) . '=' . urlencode(trim($val)) . \"&\";
$pString = substr($pString,0,-1);
// Access remote site with the post data
// The resulting web page SOURCE CODE will be
// in the variable $aResult
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, \"https://secure.authorize.net/gateway/transact.dll\");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pString);
$aResult = curl_exec($ch);
curl_close($ch);
?>
Sample Result Code
NOTE: The formatting of this result MAY VARY, I can't remember if you can modify it on your account settings, however the ORDER should remain the same. Actually, I just realized you can set this with the x_delim_data, x_delim_char, and x_encap_char settings.
`1`|`1`|`1`|`This transaction has been approved.`|`034812`|`Y`|`1045434501`|`0609073150`|`Sample Web Susbscription`|`24.95`|`CC`|`auth_capture`|`zippyman`|`JOHN`|`SMITH`|`ZIPMART INC`|`1234 Main St.`|`Anytown`|`OH`|`44325`|`US`|`8005551212`|``|`[email protected]`|``|``|``|``|``|``|``|``|``|``|``|``|``|`476F2323432341264D43E6327D7515FC`|`M`|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|``|`True`
'
Hope this helps, if you have any questions or suggestions for improvement, let me know.
-Greg
Greg K posted this at 21:26 — 8th September 2006.
He has: 2,145 posts
Joined: Nov 2003
Update: I finally got the e-mail with the PHP sample code to connect to authorize.net. Looks well documented, so you may prefer to check that out, especially if you need something besides PHP.
-Greg
JeevesBond posted this at 22:14 — 12th September 2006.
He has: 3,956 posts
Joined: Jun 2002
Sweet! Thanks Greg, have done a few projects where authorize.net was a possibility. At least I now there's somewhere to go to find out how to do the integration bit!
a Padded Cell our articles site!
ablaye posted this at 01:30 — 15th September 2006.
They have: 140 posts
Joined: Apr 2006
Thanks for the code. It will save me some time on my next web project.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.