WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Authorize.net AIM Interface Class v1.0.0
Categories : PHP, PHP Classes, Ecommerce, Payment Gateways Click here to Update Your Picture
Micah Carrick
Date : May 13th 2005
Grade : 3 of 5 (graded 2 times)
Viewed : 7259
File : 4169.zip
Images : No Images for this code example.
Search : More code by Micah Carrick
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

authorizenet.class.php
<?php
/*******************************************************************************
*                Authorize.net AIM Interface Class
*******************************************************************************
*      Author:     Micah Carrick
*      Email:      email@micahcarrick.com
*      Website:    http://www.micahcarrick.com/v2/content/view/2/3/
*
*      File:       authorizenet.class.php
*      Version:    1.00
*      Copyright:  (c) 2005 - Micah Carrick
*                  You are free to use, distribute, and modify this software
*                  under the terms of the GNU General Public License.  See the
*                  included license.txt file.
*     
*******************************************************************************
*  REQUIREMENTS:
*      - PHP4+ with CURL and SSL support
*      - An Authorize.net AIM merchant account
*      - (optionally) http://www.authorize.net/support/AIM_guide.pdf

*******************************************************************************
*  VERION HISTORY:

*      v1.00 [04.07.2005] - Initial Version
*
*******************************************************************************
*  DESCRIPTION:
*
*      This class was developed to simplify interfacing a PHP script to the
*      authorize.net AIM payment gateway.  It does not do all the work for
*      you as some of the other scripts out there do.  It simply provides
*      an easy way to implement and debug your own script. 
*
*******************************************************************************
*/

class authorizenet_class {

   var
$field_string;
   var
$fields = array();
   
   var
$response_string;
   var
$response = array();
   
   var
$gateway_url = "https://secure.authorize.net/gateway/transact.dll";
   
   function
add_field($field, $value) {
   
     
// adds a field/value pair to the list of fields which is going to be
      // passed to authorize.net.  For example: "x_version=3.1" would be one
      // field/value pair.  A list of the required and optional fields to pass
      // to the authorize.net payment gateway are listed in the AIM document
      // available in PDF form from www.authorize.net
     
     
$this->fields["$field"] = urlencode($value);   

   }

   function
process() {
       
     
// This function actually processes the payment.  This function will
      // load the $response array with all the returned information.  The return
      // values for the function are:
      // 1 - Approved
      // 2 - Declined
      // 3 - Error

      // construct the fields string to pass to authorize.net
     
foreach( $this->fields as $key => $value )
         
$this->field_string .= "$key=" . urlencode( $value ) . "&";
     
     
// execute the HTTPS post via CURL
     
$ch = curl_init($this->gateway_url);

     
curl_setopt($ch, CURLOPT_HEADER, 0);

     
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

     
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $this->field_string, "& " ));

     
$this->response_string = urldecode(curl_exec($ch));

     
      if (
curl_errno($ch)) {
         
$this->response['Response Reason Text'] = curl_error($ch);
         return
3;
      }
      else
curl_close ($ch);

       
     
// load a temporary array with the values returned from authorize.net
     
$temp_values = explode('|', $this->response_string);

     
// load a temporary array with the keys corresponding to the values
      // returned from authorize.net (taken from AIM documentation)
     
$temp_keys= array (
           
"Response Code", "Response Subcode", "Response Reason Code", "Response Reason Text",
           
"Approval Code", "AVS Result Code", "Transaction ID", "Invoice Number", "Description",
           
"Amount", "Method", "Transaction Type", "Customer ID", "Cardholder First Name",
           
"Cardholder Last Name", "Company", "Billing Address", "City", "State",
           
"Zip", "Country", "Phone", "Fax", "Email", "Ship to First Name", "Ship to Last Name",
           
"Ship to Company", "Ship to Address", "Ship to City", "Ship to State",
           
"Ship to Zip", "Ship to Country", "Tax Amount", "Duty Amount", "Freight Amount",
           
"Tax Exempt Flag", "PO Number", "MD5 Hash", "Card Code (CVV2/CVC2/CID) Response Code",
           
"Cardholder Authentication Verification Value (CAVV) Response Code"
     
);

     
// add additional keys for reserved fields and merchant defined fields
     
for ($i=0; $i<=27; $i++) {
         
array_push($temp_keys, 'Reserved Field '.$i);
      }
     
$i=0;
      while (
sizeof($temp_keys) < sizeof($temp_values)) {
         
array_push($temp_keys, 'Merchant Defined Field '.$i);
         
$i++;
      }

     
// combine the keys and values arrays into the $response array.  This
      // can be done with the array_combine() function instead if you are using
      // php 5.
     
for ($i=0; $i<sizeof($temp_values);$i++) {
         
$this->response["$temp_keys[$i]"] = $temp_values[$i];
      }

     
// Return the response code.
     
return $this->response['Response Code'];

   }
   
   function
get_response_reason_text() {
      return
$this->response['Response Reason Text'];
   }

   function
dump_fields() {

     
// Used for debugging, this function will output all the field/value pairs
      // that are currently defined in the instance of the class using the
      // add_field() function.
     
     
echo "<h3>authorizenet_class->dump_fields() Output:</h3>";
      echo
"<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
            <tr>
               <td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
               <td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
            </tr>"
;
           
      foreach (
$this->fields as $key => $value) {
         echo
"<tr><td>$key</td><td>".urldecode($value)." </td></tr>";
      }

      echo
"</table><br>";
   }

   function
dump_response() {

     
// Used for debuggin, this function will output all the response field
      // names and the values returned for the payment submission.  This should
      // be called AFTER the process() function has been called to view details
      // about authorize.net's response.
     
     
echo "<h3>authorizenet_class->dump_response() Output:</h3>";
      echo
"<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
            <tr>
               <td bgcolor=\"black\"><b><font color=\"white\">Index </font></b></td>
               <td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
               <td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
            </tr>"
;
           
     
$i = 0;
      foreach (
$this->response as $key => $value) {
         echo
"<tr>
                  <td valign=\"top\" align=\"center\">$i</td>
                  <td valign=\"top\">$key</td>
                  <td valign=\"top\">$value </td>
               </tr>"
;
         
$i++;
      }
      echo
"</table><br>";
   }   
}



demo.php
<?php

/*  Demonstration on using authorizenet.class.php.  This just sets up a
*  little test transaction to the authorize.net payment gateway.  You
*  should read through the AIM documentation at authorize.net to get
*  some familiarity with what's going on here.  You will also need to have
*  a login and password for an authorize.net AIM account and PHP with SSL and
*  curl support.
*
*  Reference http://www.authorize.net/support/AIM_guide.pdf for details on
*  the AIM API.
*/ 

require_once('authorizenet.class.php');

$a = new authorizenet_class;

// You login using your login, login and tran_key, or login and password.  It
// varies depending on how your account is setup.
// I believe the currently reccomended method is to use a tran_key and not
// your account password.  See the AIM documentation for additional information.

$a->add_field('x_login', 'CHANGE THIS TO YOUR LOGIN');
$a->add_field('x_tran_key', 'CHANGE THIS TO YOUR TRANSACTION KEY');
//$a->add_field('x_password', 'CHANGE THIS TO YOUR PASSWORD');

$a->add_field('x_version', '3.1');
$a->add_field('x_type', 'AUTH_CAPTURE');
$a->add_field('x_test_request', 'TRUE');    // Just a test transaction
$a->add_field('x_relay_response', 'FALSE');

// You *MUST* specify '|' as the delim char due to the way I wrote the class.
// I will change this in future versions should I have time.  But for now, just
// make sure you include the following 3 lines of code when using this class.

$a->add_field('x_delim_data', 'TRUE');
$a->add_field('x_delim_char', '|');     
$a->add_field('x_encap_char', '');


// Setup fields for customer information.  This would typically come from an
// array of POST values froma secure HTTPS form.

$a->add_field('x_first_name', 'John');
$a->add_field('x_last_name', 'Smith');
$a->add_field('x_address', '1234 West Main St.');
$a->add_field('x_city', 'Some City');
$a->add_field('x_state', 'CA');
$a->add_field('x_zip', '12345');
$a->add_field('x_country', 'US');
$a->add_field('x_email', 'someone@somedomain.com');
$a->add_field('x_phone', '555-555-5555');


// Using credit card number '4007000000027' performs a successful test.  This
// allows you to test the behavior of your script should the transaction be
// successful.  If you want to test various failures, use '4222222222222' as
// the credit card number and set the x_amount field to the value of the
// Response Reason Code you want to test. 
//
// For example, if you are checking for an invalid expiration date on the
// card, you would have a condition such as:
// if ($a->response['Response Reason Code'] == 7) ... (do something)
//
// Now, in order to cause the gateway to induce that error, you would have to
// set x_card_num = '4222222222222' and x_amount = '7.00'

//  Setup fields for payment information
$a->add_field('x_method', 'CC');
$a->add_field('x_card_num', '4007000000027');   // test successful visa
//$a->add_field('x_card_num', '370000000000002');   // test successful american express
//$a->add_field('x_card_num', '6011000000000012');  // test successful discover
//$a->add_field('x_card_num', '5424000000000015');  // test successful mastercard
// $a->add_field('x_card_num', '4222222222222');    // test failure card number
$a->add_field('x_amount', '10.00');
$a->add_field('x_exp_date', '0308');    // march of 2008
$a->add_field('x_card_code', '123');    // Card CAVV Security code


// Process the payment and output the results
switch ($a->process()) {

   case
1// Successs
     
echo "<b>Success:</b><br>";
      echo
$a->get_response_reason_text();
      echo
"<br><br>Details of the transaction are shown below...<br><br>";
      break;
     
   case
2// Declined
     
echo "<b>Payment Declined:</b><br>";
      echo
$a->get_response_reason_text();
      echo
"<br><br>Details of the transaction are shown below...<br><br>";
      break;
     
   case
3// Error
     
echo "<b>Error with Transaction:</b><br>";
      echo
$a->get_response_reason_text();
      echo
"<br><br>Details of the transaction are shown below...<br><br>";
      break;
}

// The following two functions are for debugging and learning the behavior
// of authorize.net's response codes.  They output nice tables containing
// the data passed to and recieved from the gateway.

$a->dump_fields();      // outputs all the fields that we set
$a->dump_response();    // outputs the response from the payment gateway
?>





PHP Paypal IPN Integration Class v1.0.0
Categories : PHP, PHP Classes, Payment Gateways
an example of the cyberlib payment class
Categories : PHP, PHP Classes, Ecommerce, Credit Cards
shopping cart class with add/edit/delete product functionality.
Categories : PHP, PHP Classes, Ecommerce
ECHO-PHP Class Real Time Transaction Processor v1.4.4 for Credit Cards and Checks / ACH
Categories : PHP Classes, Cybercash, Classes and Objects, Ecommerce, PHP
simple shopping cart for php3
Categories : PHP, PHP Classes, Complete Programs, Ecommerce
Example Shopping cart class
Categories : Ecommerce, PHP, PHP Classes
a class for doing payments to a cybercash server
Categories : Ecommerce, Complete Programs, PHP Classes, PHP
Credit Card Identification and Validation Class - The credit_card class provides methods for cleaning, validating and identifying the type of credit card numbers.
Categories : PHP, PHP Classes, Credit Cards, Ecommerce, Algorithms
file class , uploade file , download file already uploaded on another website
Categories : PHP, PHP Classes, Filesystem, Web Services
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
crop and resize image class using gd library function
Categories : PHP, PHP Classes, GD image library, Graphics
Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself.
Categories : PHP, PHP Classes, Templates, Complete Programs
Create HTML forms dynamicly using Javascript & PHP
Categories : PHP, PHP Classes, Java Script
open source online php shop project ecommerce commerce
Categories : PHP, Ecommerce
News management class
Categories : PHP, PHP Classes, Beginner Guides