Authorize.net module for Kohana 3.x

Authorize.net plays nicely with Kohana!

Kohana 2.x featured a payment module that included Google Checkout, Paypal, and Authorize.net, but when the framework made the leap to version three, the payment module was not ported over. I use Authorize.net for several projects, so I had to come up with a solution. First, I considered porting over the payment module, but after a few Google searches, I noticed that Authorize.net has their official PHP library that turned out to be super simple to include in Kohana 3.x as a module.

Using the official library from Authorize.net ensures compatibility and a handful of tutorials and examples. It really is the way to go. Check out how simple it is to include the library as a Kohana module.

First, download the Authorize.net PHP SDK from the official website, then follow these simple steps:

  1. Create a folder called vendor inside the modules directory
  2. Extract the authorize.net API to the vendor directory, so that your path looks like modules/vendor/anet_php_sdk/
  3. Open application/bootstrap.php and look for the Kohana::modules array definition and add the following key-value pair to it:
    'vendor' => MODPATH.'vendor'

Kohana will now know where to look for the module and you can load it when you need it. You now have access to the Authorize.net library in your controllers and models!

Next you need to create a config file with your Authorize.net credentials in it, so you can make requests to either the sandbox or the production gateway.

Create a file called authorize.php in application/config/ and paste the following code in to the file. Remember to put in your own API credentials.

<?php defined('SYSPATH') or die('No direct access allowed.');
 
/**
* Enter your test account credentials to run tests against sandbox.
*/
define("AUTHORIZENET_API_LOGIN_ID", "YOUR_LOGIN_ID");
define("AUTHORIZENET_TRANSACTION_KEY", "YOUR_TRANSACTION_KEY");
define("AUTHORIZENET_MD5_SETTING", "");
 
/**
* Enter your live account credentials to run tests against production gateway.
*/
define("MERCHANT_LIVE_API_LOGIN_ID", "");
define("MERCHANT_LIVE_TRANSACTION_KEY", "");
 
/**
* Card Present Sandbox Credentials
*/
define("CP_API_LOGIN_ID", "");
define("CP_TRANSACTION_KEY", "");
 
define("AUTHORIZENET_LOG_FILE", APPPATH."cache/authorize-net.log");
 
return array();

You can now process transactions, create subscriptions, etc. See the following example for a simple transaction. Note that you need to load the library first using Kohana::find_file() and then use Kohana::config to load your authorize.net config file in order for the API to work.

require_once Kohana::find_file('anet_php_sdk', 'AuthorizeNet');
Kohana::config('authorize');
$sale = new AuthorizeNetAIM;
$response = $sale->authorizeAndCapture(19.95, '6011000000000012', '04/16');

The example auth and capture is a bare minimum, but the authorize.net SDK comes with a bunch of examples, so you can use those to build a more elaborate transaction.

Comments are closed.