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"; }
Leave a Reply