Adding the Authorize.net PHP SDK to CodeIgniter 2.0


This is a follow up post to my initial Adding the Authorize.net PHP SDK to CodeIgniter 1.7. Now that CI 2.0 is out the plugins are long gone.

So after a couple of minuted fiddling with this, I managed to get the Authorize.net API working as a library in 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 application/libraries folder
  • Step 3 – Rename the file AuthorizeNet.php to Authorizenet.php
  • Step 4- Add this to code to Authorizenet.php
//AuthorizeNetAIM for using AIM - replace with your preferred method
class Authorizenet extends AuthorizeNetAIM         
{
}

Now you only need to call your new library like so

$this->load->library('authorizenet');

And to use it

$this->authorizenet->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',
)
);
$this->authorizenet->authorizeAndCapture();

Of course don’t forget to set your global variables that Authorize.net requires
You can see the included example in the SDK or the my previous post

Adding the Authorize.net PHP SDK to CodeIgniter 1.7.


41 responses to “Adding the Authorize.net PHP SDK to CodeIgniter 2.0”

  1. I came here to say that you needed to write a new post and lo and behold you had already done it! Good post, I’ve been playing with CI 2 a little and it makes some good improvements over 1.7. I like that the development cycle is faster, too.

    • Yeah, this guy wanted to know how to do it on CI 2 so i took the time to do some research. Glad you dropped by.

    • you can create two classes
      class Authorizenetaim extends AuthorizeNetAIM
      {
      }
      class Authorizenetarb extends AuthorizeNetARB
      {
      }
      Just remember to capitalize only the first letter of the new class name
      and to load them
      $this->load->library(‘authorizenetaim’);

      $this->load->library(‘authorizenetarb’);

      and to use them
      $this->authorizenetaim->….
      $this->authorizenetarb->….

  2. Hi there. I am new to CodeIgniter and just came across this solution after searching far and wide. I think I have everything in place but I am getting the error “Error connecting to AuthorizeNet”. Any idea what is causing this?

    • Did you make sure to set the correct authorize.net variables?
      check my post for CI 1.7 also it has been a while since i wrote this so also check with Authorize.net to make sure their gateway url’s are still the same

      • Thanks for the response, Carlos.

        I am setting the following variables:
        define(“AUTHORIZENET_API_LOGIN_ID”,’XXXXXX’);
        define(“AUTHORIZENET_TRANSACTION_KEY”,’XXXXXX’);
        define(“AUTHORIZENET_SANDBOX”,true);
        $METHOD_TO_USE = “AIM”;
        define(“AUTHORIZENET_MD5_SETTING”,””);

        But I am still getting the “Error connecting to AuthorizeNet” response. Do I have to set a POST URL variable?

        Thanks.

        • You need an MD5 value. you sould have gotten it when you got your API login ID and transaction Key from Authorize.net

          • I have the MD5 set now and made some changes to our server that is allowing the connection. However, now I am getting an error “The merchant login ID or password is invalid or the account is inactive. ”

            It looks like the login id and transaction id are not getting passed. I have a variable dump and all fields are set except:
            [type] => AUTH_CAPTURE [login] => [tran_key] =>

            Any help would be greatly appreciated.

          • Andrew,
            Those variable are used when you are in test mode, which is recommended until you are absolutely ready for live testing . Try these if you are testing live
            define(“MERCHANT_LIVE_API_LOGIN_ID”, “”);
            define(“MERCHANT_LIVE_TRANSACTION_KEY”, “”);

            Good Luck

          • I got it working finally. I had the global variables being declared in the model and the login and transaction were not being set. I moved the variables to the config and it works now. Thanks for your help!!

  3. I am new to using CodeIgniter, so I am not 100% sure I understand how to use this. I am building an e-commerce site and need to use the Direct Post method of Authorize.net’s SDK. I can do this easy enough in procedural PHP, but I am a little confused on where this code goes in CI. I assume it goes into a controller, but maybe it should be in a view?

    I read both of your tutorials and I am still confused as to how to get to this working. Where do I define the globals from the previous tutorial (controller as well, I assume)?

    I just don’t understand how this works with the MVC pattern. A little more explanation would be good, for me anyway. Thanks.

    • Hi, you have a very good question that I wish I had taken into account when I wrote my articles.
      You don’t want to set your main object in the controller.

      Here are two ways you can go about it:

      Autoload Remember, this is a library and once you copy the files and add

      class Authorizenet extends AuthorizeNetDPM //AuthorizeNetDPM for using DPM
      {
      }

      to your AuthorizeNet.php file, you can auto load it in CI in the autoload.php file

      Second, only load the library when you need it.

      In either case, you only use this library from your models.
      I assume you have a form where the customer enters their CC information.
      If you do, in your controller you will have a method for capturing that form data.
      From that method you call you Model which in turn contains a method for processing
      the CC. Pass the form values to your model’s method.

      It is in the Model’s method where you would call your library by using

      $this->load->library('authorizenet');

      If you autoloaded the library you can omit the previous step.

      Next you can define your globals now


      define("AUTHORIZENET_API_LOGIN_ID",$authLogId);
      define("AUTHORIZENET_TRANSACTION_KEY",$authTranKey);
      define("AUTHORIZENET_SANDBOX",true);
      $METHOD_TO_USE = "DIRECT_POST"; // CHECK ON the correct value on the SDK incase it has been updated recently
      $site_root = "http://YOURDOMAIN/samples/your_store/";
      define("AUTHORIZENET_MD5_SETTING","settitings here"); // Update this with your MD5 Setting (MD5 string).

      // REMEMBER YOUR AMOUNT HAS TO INCLUDE THE TAX
      $tax = number_format($price * .095,2); // Set tax
      $amount = number_format($price + $tax,2); // Set total amount

      at this point you would go ahead and set up the library variables
      in here use the values captured in the form


      $this->authorizenet->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',
      )
      );

      Now you are ready to Process the CC by doing this

      $response = $transaction->authorizeAndCapture();
      if ($response->approved) {
      return "APPROVED";
      } else {
      return "DENIED";
      }

      you can return true or false instead of approved or denied

      in turn, your controller now has to load the correct view
      depending on the response from your model

      I hope this helps

  4. I get the following error:

    Fatal error: Class ‘AuthorizeNetAIM’ not found in /your/path/libraries/Authorizenet.php on line 3

    Line 3 =
    class Authorizenet extends AuthorizeNetAIM //AuthorizeNetAIM for using AIM replace with your preferred method
    {
    }

  5. I know this is an old post but I need some help.

    I’m trying to set up the Authorize.net SDK in codeigniter and its succesful for one payment method, but I need to use AIM and ARB. AIM to check card and ARB to set up recurring billing. I tried it as described in one of the comments above:

    you can create two classes
    class Authorizenetaim extends AuthorizeNetAIM
    {
    }
    class Authorizenetarb extends AuthorizeNetARB
    {
    }
    Just remember to capitalize only the first letter of the new class name
    and to load them
    $this->load->library(‘authorizenetaim’);
    $this->load->library(‘authorizenetarb’);

    and got the following error: PHP Fatal error: Cannot redeclare class AuthorizeNetRequest

    How do I get two methods to work in codeigniter?

  6. I’m pretty sure its because both the arb and aim classes extend the net request class but i’m not certain how to work around it.

    • I am not aware on any changes from 2.0 to 2.2 that would hinder this setup. As far as I know you should be able to use this tutorial

  7. Hi there! I can’t find “AuthorizeNet.php” file inside the “sdk-php”.
    Where is that file?
    Thanks in advance!

Leave a Reply to rajnish 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.