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 Comments


  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…

    Reply

    1. Hey, because it was too much of a custom site to try to figure it out in joomla.

      Reply

    2. 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?

      Reply

  2. Nah, I think I have a copy floating around somewhere. Thanks though.

    Reply

  3. 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.

    Reply

    1. I haven’t tried it yet but i will look it up you

      Reply

    2. I might have a lead on this setting this up in a wrapper function and using it as a helper. I will get back to you when i test this

      Reply

      1. Carlos,

        Much appreciated, I’ll await your next post.

        Reply

        1. Not a problem Tom, glad I could help

          Reply



  4. 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

    Reply

    1. make sure AuthorizeNetAIM is included in Authorizenet.php

      Reply

Leave a 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.