Authorize.net module for Kohana 3.x
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:
- Create a folder called vendor inside the modules directory
- Extract the authorize.net API to the vendor directory, so that your path looks like modules/vendor/anet_php_sdk/
- 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.