module for Kohana 3.x plays nicely with Kohana!

Kohana 2.x featured a payment module that included Google Checkout, Paypal, and, but when the framework made the leap to version three, the payment module was not ported over. I use 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 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 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 PHP SDK from the official website, then follow these simple steps:

  1. Create a folder called vendor inside the modules directory
  2. Extract the 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 library in your controllers and models!

Next you need to create a config file with your 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.
* Enter your live account credentials to run tests against production gateway.
* 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 config file in order for the API to work.

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

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

Comments are closed.