Zend - The PHP Company




Commerce

Add Code


Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL  

Type: code fragment
Added by: spiderwt
Entered: 22/01/2003
Last modified: 02/12/2002
Rating: ***** (4 votes)
Views: 13922
Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL.


<?php
/*******************************************************************************
Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL

     * Source by Jon Olawski - www.spiderwt.com
     * Free to use, modify, distribute!
     * Provided with no warranty (if it doesn't work, sorry)
     * Download from http://www.spiderwt.com/authorizenet.php
     * Donations are greatly appreciated -> https://www.paypal.com/xclick/business=paypal%40spiderwt.com

  Requirements
     * PHP 4.0.2 or higher with CURL 7.0.2-beta or higher
     * PHP 4.2.3 or higher with CURL 7.9.0 or higher
     * Secure Server
     * See http://www.php.net/manual/en/ref.curl.php for full PHP & CURL documentation

  Introduction
     Typically, when using this type of advanced integration you would open up a
     socket connection using fsockopen on port 443, then post your fields, and
     finally read the response.  For whatever reason, I can not seem to get this
     to work with Authorize.net.  The alternative now is to use CURL (Client URL
     Library Functions).  This is direct from php.net:

       "PHP supports libcurl, a library created by Daniel Stenberg, that allows you to
       connect and communicate to many different types of servers with many different
       types of protocols. libcurl currently supports the http, https, ftp, gopher,
       telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates,
       HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp
       extension), HTTP form based upload, proxies, cookies, and user+password
       authentication."

     Authorize.net does provide sample scripts for the SIM (Simple Integration method)
     for PHP, but doesn't seem to provide any documentation for the AIM.  I hope that
     this short sample can help!  The AIM allows you, in your script, to process the
     transaction and get a response back without the user ever leaving your website.
     With the SIM the user posts the information to authorize.net, and then redirects
     back to your website.

*******************************************************************************/


#
# Configuration
#
$x_Login="login_id";     // Your authorize.net login
$x_Password="";          // Your authorize.net password (if Password-Required Mode is enabled)

$x_Delim_Data="TRUE";    // Delimited response from the gateway (or set in the Setting Menu)
$x_Delim_Char=",";       // Character that will be used to separate fields
$x_Encap_Char="";        // Character that will be used to encapsulate fields

$x_Type="AUTH_CAPTURE";  // Default transaction type
$x_Test_Request="TRUE";  // Make this a test transaction

#
# Customer Information
#
$x_Method="CC";
$x_Amount="99.99";
$x_First_Name="Joe";
$x_Last_Name="Customer";
$x_Card_Num="5424000000000015";
$x_Exp_Date="12/05";

#
# Build fields string to post
#
$fields="x_Version=3.1&x_Login=$x_Login&x_Delim_Date=$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";
if(
$x_Password!='')
{
  
$fields.="&x_Password=$x_Password";
}

#
# Start CURL session
#
$ch=curl_init("https://secure.authorize.net/gateway/transact.dll");
curl_setopt($chCURLOPT_HEADER0);
curl_setopt($chCURLOPT_POSTFIELDS$fields);  // set the fields to post
curl_setopt($chCURLOPT_RETURNTRANSFER1);    // make sure we get the response back

$buffer curl_exec($ch);                       // execute the post

curl_close($ch);                                // close our session

$details=explode($x_Delim_Char,$buffer);        // create an array of the response values

echo "<b>View the page source to see this nice and pretty!</b><br>nn";

print_r($details);                              // print the array

/*******************************************************************************

 From here you can read the response from $details and do whatever it is you do!
 Remember that the first position in the $details array is 0, not 1.  In the
 Authorize.net documentation, they show the position in the response starting
 with 1, but programmers can't count. Here is an example of what I am talking
 about:
          The Response Code is the 1st position in the response. That means that
          $details[0] is the response code.

          The Transaction ID is the 7th position in the response.  That means
          that $details[6] is the Transaction ID

 Refer to the authorize.net AIM documentation for the list of Response codes!

*******************************************************************************/

?>


Usage Example




Rate This Script





Search



This Category All Categories