Zend - The PHP Company




Commerce

Add Code


Authorize.net AIM Script  

Type: application
Added by: erikjs
Entered: 19/05/2003
Last modified: 04/12/2007
Rating: ***** (6 votes)
Views: 7481
An easy to use script to help those who are having difficulty with Authorize.net's AIM method.


<?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);
$details substr($buffer01);

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