Commerce
|
|
|
|
<?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_Login= urlencode("LOGIN"); // Replace LOGIN with your login $x_Password= urlencode("PASS"); // Replace PASS with your password $x_Delim_Data= urlencode("TRUE"); $x_Delim_Char= urlencode(","); $x_Encap_Char= urlencode(""); $x_Type= urlencode("AUTH_CAPTURE");
$x_ADC_Relay_Response = urlencode("FALSE");
$x_Test_Request= urlencode("TRUE"); // Remove this line of code when you are ready to go live
#
# Customer Information
# $x_Method= urlencode("CC"); $x_Amount= urlencode($cost); $x_First_Name= urlencode($FirstName);
$x_Last_Name= urlencode($LastName); $x_Card_Num= urlencode($CardNum); $ExpDate = urlencode(($Month . $Year)); $x_Exp_Date= urlencode($ExpDate);
$x_Address= urlencode($Address); $x_City= urlencode($City); $x_State= urlencode($State); $x_Zip= urlencode($Zip);
$x_Email= urlencode($Email); $x_Email_Customer= urlencode("TRUE"); $x_Merchant_Email= urlencode("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($ch, CURLOPT_URL, "https://secure.authorize.net/gateway/transact.dll"); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_NOPROGRESS, 1); curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,0);
curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); curl_setopt ($ch, CURLOPT_TIMEOUT, 120); curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_REFERER, $ref); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($ch); curl_close($ch); $details = substr($buffer, 0, 1);
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
|
|
|
|