Zend - The PHP Company




Commerce

Add Code


Authorize.net AIM Script (Version 2.0)  

Type: application
Added by: ErikJS
Entered: 10/01/2004
Last modified: 02/11/2006
Rating: ***** (7 votes)
Views: 12227
An easy to use script to help those who are having difficulty with Authorize.net's AIM method. (New version allows use of all Authorize.net's returned information)


<?php 

// I had a lot of trouble getting PHP & Curl to work with Authorize.net 
// I don't want others to go through the same problems, so I am submitting this example script 
// This script will work with Authorize.net's AIM method of processing. 
// This code has been heavily borrowed from several sources. 
// It requires a server that supports PHP and cURL. 
// If you have any comments, please contact erik@grossmontdesigns.com 


// From a previous HTML Form, pass the following fields: 
// $FirstName = Customer's First Name 
// $LastName = Customer's Last Name 
// $CardNum = Customer's Credit Card Number 
// $Month = Customer's Credit Card Expiration Month (Should be 01, 02, etc.) 
// $Year = Customer's Credit Card Expiration Year (Should be 2003, 2004, etc.) 
// $Address = Customer's Address 
// $City = Customer's City 
// $State = Customer's State (Should be 2 letter code, CA, AZ, etc.) 
// $Zip = Customer's Zip Code 
// $Email = Customer's Email Address 
// $cost = Total Price of purchase 

// Check to make sure customer entered all relevant information 

if (!$FirstName || !$LastName || !$Address || !$City || !$State || !$Zip || !$CardNum || ! 
$Email) { 
echo 
"You forgot some necessary information.��Please go back and enter the missing information." 
exit; 
} else { 

$x_Loginurlencode("LOGIN"); // Replace LOGIN with your login 
$x_Passwordurlencode("PASS"); // Replace PASS with your password 
$x_Delim_Dataurlencode("TRUE"); 
$x_Delim_Charurlencode(","); 
$x_Encap_Charurlencode(""); 
$x_Typeurlencode("AUTH_CAPTURE"); 

$x_ADC_Relay_Response urlencode("FALSE"); 

$x_Test_Requesturlencode("TRUE"); // Remove this line of code when you are ready to go live 

# Customer Information 

$x_Methodurlencode("CC"); 
$x_Amounturlencode($cost); 
$x_First_Nameurlencode($FirstName); 
$x_Last_Nameurlencode($LastName); 
$x_Card_Numurlencode($CardNum); 
$ExpDate urlencode(($Month $Year)); 
$x_Exp_Dateurlencode($ExpDate); 

$x_Addressurlencode($Address); 
$x_Cityurlencode($City); 
$x_Stateurlencode($State); 
$x_Zipurlencode($Zip); 

$x_Emailurlencode($Email); 
$x_Email_Customerurlencode("TRUE"); 
$x_Merchant_Emailurlencode("MERCHANT_EMAIL"); //��Replace MERCHANT_EMAIL with the merchant email address 

# Build fields string to post 

$fields="x_Version=3.1&x_Login=$x_Login&x_Delim_Data=$x_Delim_Data&x_Delim_Char=$x_Delim_Char&x_Encap_Char=$x_Encap_Char"
$fields.="&x_Type=$x_Type&x_Test_Request=$x_Test_Request&x_Method=$x_Method&x_Amount=$x_Amount&x_First_Name=$x_First_Name"
$fields.="&x_Last_Name=$x_Last_Name&x_Card_Num=$x_Card_Num&x_Exp_Date=$x_Exp_Date&x_Address=$x_Address&x_City=$x_City&x_State=$x_State&x_Zip=$x_Zip&x_Email=$x_Email&x_Email_Customer=$x_Email_Customer&x_Merchant_Email=$x_Merchant_Email&x_ADC_Relay_Response=$x_ADC_Relay_Response";
if(
$x_Password!=''

$fields.="&x_Password=$x_Password"


# Start CURL session 

$agent "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
$ref "https://www.mydomainname.com/ccprocess.php"// Replace this URL with the URL of this script 

$ch=curl_init(); 
curl_setopt($chCURLOPT_URL"https://secure.authorize.net/gateway/transact.dll"); 
curl_setopt($chCURLOPT_SSL_VERIFYPEER0); 
curl_setopt($chCURLOPT_NOPROGRESS1); 
curl_setopt($chCURLOPT_VERBOSE1); 
curl_setopt($chCURLOPT_FOLLOWLOCATION,0); 
curl_setopt($chCURLOPT_POST1); 
curl_setopt($chCURLOPT_POSTFIELDS$fields); 
curl_setopt($chCURLOPT_TIMEOUT120); 
curl_setopt($chCURLOPT_USERAGENT$agent); 
curl_setopt($chCURLOPT_REFERER$ref); 
curl_setopt($chCURLOPT_RETURNTRANSFER1); 

$buffer curl_exec($ch); 
curl_close($ch); 

// This section of the code is the change from Version 1.
// This allows this script to process all information provided by Authorize.net...
// and not just whether if the transaction was successful or not

// Provided in the true spirit of giving by Chuck Carpenter (Chuck@MLSphotos.com)
// Be sure to email him and tell him how much you appreciate his efforts for PHP coders everywhere

$return preg_split("/[,]+/""$buffer"); // Splits out the buffer return into an array so . . .
$details $return[0]; // This can grab the Transaction ID at position 1 in the array

// Change the number to grab additional information.  Consult the AIM guidelines to see what information is provided in each position.

// For instance, to get the Transaction ID from the returned information (in position 7)..
// Simply add the following:
// $x_trans_id = $return[6];

// You may then use the switch statement (or other process) to process the information provided
// Example below is to see if the transaction was charged successfully

switch ($details

����case "1"// Credit Card Successfully Charged 
��������header ("Location: http://www.yourdomain.com/success.php"); // Change this address with the URL of your 'Completed Transaction' page 
��������break
�������� 
����default
// Credit Card Not Successfully Charged 
��������header ("Location: http://www.yourdomain.com/error.php"); // Change this address with the URL of your 'Error' page 
��������break
}

}
?>


Usage Example




Rate This Script





Search



This Category All Categories