Adding the Authorize.net PHP SDK to CodeIgniter 1.7


I am writing a site that requires credit card processing through authorize.net. I am building the site using the CodeIgniter framework. I was thinking I would have to do some fancy hacking or using a 3rd party library to make this work but it turns out that you can user the SDK provided by Authorize.net directly into CI. here is what you need to do.

  • Step 1 – You need to download the PHP SDK from Authorize.net, you can get it here
  • Step 2 – Copy the AuthorizeNet.php file and the lib folder into your copy of CI’s system/plugins folder
  • Step 3 – Rename the file AuthorizeNet.php to AuthorizeNet_pi.php

Believe it or not you are done! that’s it, no need to do anything else, now you can use the SDK as described in the examples.

Here is an example of a simple authorization and capture using test mode. I used some of the code from the example provided by Authorize.net here. The only difference from the example and this code is that I combined the code from the two config files ofthe Authorize.net example into one and replaced the

 require_once 'anet_php_sdk/AuthorizeNet.php';

with

$this->load->plugin('AuthorizeNet');

Here is my Code:

$this->load->plugin('AuthorizeNet');

define("AUTHORIZENET_API_LOGIN_ID",$authLogId);
define("AUTHORIZENET_TRANSACTION_KEY",$authTranKey);
define("AUTHORIZENET_SANDBOX",true);
$METHOD_TO_USE = "AIM";

// To use Direct Post Method, uncomment the line below and update the $site_root variable.
// $METHOD_TO_USE = "DIRECT_POST";
$site_root = "http://YOURDOMAIN/samples/your_store/";
define("AUTHORIZENET_MD5_SETTING",""); // Update this with your MD5 Setting.

$tax = number_format($price * .095,2); // Set tax
$amount = number_format($price + $tax,2); // Set total amount

if (AUTHORIZENET_API_LOGIN_ID == "") {
die('Enter your merchant credentials in config.php before running the sample app.');
}

$transaction = new AuthorizeNetAIM;
$transaction->setFields(
array(
'amount' => '10.00',
'card_num' => '6011000000000012',
'exp_date' => '04/17',
'first_name' => 'John',
'last_name' => 'Doe',
'address' => '123 Main Street',
'city' => 'Boston',
'state' => 'MA',
'country' => 'USA',
'zip' => '02142',
'email' => 'some@email.com',
'card_code' => '782',
)
);
$response = $transaction->authorizeAndCapture();
if ($response->approved) {
return "APPROVED";
} else {
return "DENIED";
}
,

16 responses to “Adding the Authorize.net PHP SDK to CodeIgniter 1.7”

  1. Hey, I guess you didn’t know it existed but if you look into Eppros, you’ll see an Authorize.net CodeIgniter class I wrote. It is basically a port of the code provided by Authorize.net, but it uses CodeIgniter conventions that mimic the standard libraries.

    Last time I talked to you, you were using Joomla. Why not for this project?

    I miss doing CodeIgniter apps…

    • Also, The advantage of this is that you can use the Authorize.net API samples pretty much line by line. i will have to get your API, do you need a copy?

  2. Carlos,

    How might you accomplish this in CI 2.x where plugins no longer exist? I have tried building the Authorize.net PHP SDK as a package in third_party, but, thus far, I’ve only been met with (rather unspecific) errors.

  3. i am geting problem to impliment it. i m place lib folder into libraries folder and place authrize.net file into libraies folder but i m geting error Fatal error: Class ‘AuthorizeNetAIM’ not found in /var/www/CodeIgniter/application/libraries/Authorizenet.php on line 4 plz help

Leave a Reply to Proper way to include Authorize.net PHP SDK? | deepinphp.com Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.