[eluser]idealws[/eluser]
First I would like to say thanks to those that sent me some files they were working on for Authorize.NET. I did in my search find a class by Micah Carrick which I have modified to work with CI.
With this library you can process payments with Authorize.NET AIM. It will process the payment and return the response code 1 - Approved, 2 - Declined, 3 - Transaction Error. You can also get all response fields/value pairs in a array.
Usage setup:
Download the attached Authorize_.net.php file and upload to your application/libraries folder.
Add the following to config.php file:
Code: /*
|————————————————————————————————————-
| Authorize.net Credentials and Info
|————————————————————————————————————-
*/
$config['authorize_net_test_mode'] = 'TRUE'; // Set this to FALSE for live processing
$config['authorize_net_live_x_login'] = 'LIVE LOGIN ID';
$config['authorize_net_live_x_tran_key'] = 'LIVE TRANS KEY';
$config['authorize_net_live_api_host'] = 'https://secure.authorize.net/gateway/transact.dll';
$config['authorize_net_test_x_login'] = 'TEST LOGIN ID';
$config['authorize_net_test_x_tran_key'] = 'TEST LOGIN TRANS KEY';
$config['authorize_net_test_api_host'] = 'https://test.authorize.net/gateway/transact.dll';
// Lets setup some other values so we dont have to do it everytime
// we process a transaction
$config['authorize_net_x_version'] = '3.1';
$config['authorize_net_x_type'] = 'AUTH_CAPTURE';
$config['authorize_net_x_relay_response'] = 'FALSE';
$config['authorize_net_x_delim_data'] = 'TRUE';
$config['authorize_net_x_delim_char'] = '|';
$config['authorize_net_x_encap_char'] = '';
$config['authorize_net_x_url'] = 'FALSE';
$config['authorize_net_x_method'] = 'CC';
/*
—————————————————————————————————————-
*/
Controller example (authorizenet.php):
Code: <?php
class Authorizenet extends Controller {
function Authorizenet() {
parent::Controller();
$this->load->library('authorize_net');
}
function index() {
echo "The main page";
}
function processpayment() {
// Lets do a test transaction
$this->authorize_net->add_x_field('x_first_name', 'John');
$this->authorize_net->add_x_field('x_last_name', 'Johnson');
$this->authorize_net->add_x_field('x_address', '12345 Main Street');
$this->authorize_net->add_x_field('x_city', 'Some City');
$this->authorize_net->add_x_field('x_state', 'CA');
$this->authorize_net->add_x_field('x_zip', '55555');
$this->authorize_net->add_x_field('x_country', 'US');
$this->authorize_net->add_x_field('x_email', '[email protected]');
$this->authorize_net->add_x_field('x_phone', '555-555-5555');
/**
* Use credit card number 4111111111111111 for a god transaction
* Use credit card number 4111111111111122 for a bad card
*/
$this->authorize_net->add_x_field('x_card_num', '4111111111111111');
$this->authorize_net->add_x_field('x_amount', '15.00');
$this->authorize_net->add_x_field('x_exp_date', '0311'); // MMYY
$this->authorize_net->add_x_field('x_card_code', '123');
$this->authorize_net->process_payment();
$authnetreponse = $this->authorize_net->get_all_response_codes();
if($authnetreponse['Response_Code'] == '1') {
$data['authresponse'] = $authnetreponse;
$this->load->view('paymentsuccess_view', $data);
}elseif($authnetreponse['Response_Code'] == '2') {
$data['authresponse'] = $authnetreponse;
$this->load->view('paymentdeclined_view', $data);
}elseif($authnetreponse['Response_Code'] == '3') {
$data['authresponse'] = $authnetreponse;
$this->load->view('paymentdeclined_view', $data);
}
}
}
?>
View for approved payment (paymentsuccess_view.php):
Code: <div align="left">
<h1>Your payment was a success!</h1>
<?php
echo "<pre>";
print_r($authresponse);
echo "</pre>";
?>
</div>
View for declined or error transaction (paymentdeclined_view.php):
Code: <div align="left">
<h1>Error processing your payment!</h1>
<?php
echo "<pre>";
print_r($authresponse);
echo "</pre>";
?>
</div>
I will post the library in the following post due to character restrictions. As well as attach it to this thread.
I hope this helps anyone looking to process payments using Authorize.NET AIM. I will be working on using the CIM shortly thanks to a class sent to me by Josh. Also thanks to James for sending the class he was working on.
There may be better ways to do some of this and I would love to hear from anyone with suggestions.
Regards,
Ray
[eluser]idealws[/eluser]
Authorize_net.php library (comments have been removed but are included in the file attached to the above post:
Code: <?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Authorize_net {
var $field_string;
var $fields = array();
var $response_string;
var $response = array();
var $debuginfo;
var $gateway_url = "https://secure.authorize.net/gateway/transact.dll";
function Authorize_net() {
$this->CI =& get_instance();
if($this->CI->config->item('authorize_net_test_mode') == 'TRUE') {
$this->gateway_url = $this->CI->config->item('authorize_net_test_api_host');
$this->add_x_field('x_test_request', $this->CI->config->item('authorize_net_test_mode'));
$this->add_x_field('x_login', $this->CI->config->item('authorize_net_test_x_login'));
$this->add_x_field('x_tran_key', $this->CI->config->item('authorize_net_test_x_tran_key'));
}else{
$this->gateway_url = $this->CI->config->item('authorize_net_live_api_host');
$this->add_x_field('x_test_request', $this->CI->config->item('authorize_net_test_mode'));
$this->add_x_field('x_login', $this->CI->config->item('authorize_net_live_x_login'));
$this->add_x_field('x_tran_key', $this->CI->config->item('authorize_net_live_x_tran_key'));
}
$this->add_x_field('x_version', $this->CI->config->item('authorize_net_x_version'));
$this->add_x_field('x_delim_data', $this->CI->config->item('authorize_net_x_delim_data'));
$this->add_x_field('x_delim_char', $this->CI->config->item('authorize_net_x_delim_char'));
$this->add_x_field('x_encap_char', $this->CI->config->item('authorize_net_x_encap_char'));
$this->add_x_field('x_url', $this->CI->config->item('authorize_net_x_url'));
$this->add_x_field('x_type', $this->CI->config->item('authorize_net_x_type'));
$this->add_x_field('x_method', $this->CI->config->item('authorize_net_x_method'));
$this->add_x_field('x_relay_response', $this->CI->config->item('authorize_net_x_relay_response'));
}
function add_x_field($field, $value) {
$this->fields[$field] = $value;
}
function process_payment() {
foreach( $this->fields as $key => $value ) {
$this->field_string .= "$key=" . urlencode( $value ) . "&";
}
$ch = curl_init($this->gateway_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $this->field_string, "& " ));
$this->response_string = urldecode(curl_exec($ch));
if (curl_errno($ch)) {
$this->response['Response_Reason_Text'] = curl_error($ch);
return 3;
}else{
curl_close ($ch);
}
$temp_values = explode($this->CI->config->item('authorize_net_x_delim_char'), $this->response_string);
$temp_keys= array (
"Response_Code", "Response_Subcode", "Response_Reason_Code", "Response_Reason_Text",
"Approval_Code", "AVS_Result_Code", "Transaction_ID", "Invoice_Number", "Description",
"Amount", "Method", "Transaction_Type", "Customer_ID", "Cardholder_First_Name",
"Cardholder Last_Name", "Company", "Billing_Address", "City", "State",
"Zip", "Country", "Phone", "Fax", "Email", "Ship_to_First_Name", "Ship_to_Last_Name",
"Ship_to_Company", "Ship_to_Address", "Ship_to_City", "Ship_to_State",
"Ship_to_Zip", "Ship_to_Country", "Tax_Amount", "Duty_Amount", "Freight_Amount",
"Tax_Exempt_Flag", "PO_Number", "MD5_Hash", "Card_Code_CVV_Response Code",
"Cardholder_Authentication_Verification_Value_CAVV_Response_Code"
);
for ($i=0; $i<=27; $i++) {
array_push($temp_keys, 'Reserved_Field '.$i);
}
$i=0;
while (sizeof($temp_keys) < sizeof($temp_values)) {
array_push($temp_keys, 'Merchant_Defined_Field '.$i);
$i++;
}
for ($i=0; $i<sizeof($temp_values);$i++) {
$this->response["$temp_keys[$i]"] = $temp_values[$i];
}
return $this->response['Response_Code'];
}
function get_response_reason_text() {
return $this->response['Response_Reason_Text'];
}
function get_all_response_codes() {
return $this->response;
}
function dump_fields() {
echo "<h3>authorizenet_class->dump_fields() Output:</h3>";
echo "<table width=\"95%\" border=\"1\" cellpadding=\"2\" cellspacing=\"0\">
<tr>
<td bgcolor=\"black\"><b><font color=\"white\">Field Name</font></b></td>
<td bgcolor=\"black\"><b><font color=\"white\">Value</font></b></td>
</tr>";
foreach ($this->fields as $key => $value) {
echo "<tr><td>$key</td><td>".urldecode($value)." </td></tr>";
}
echo "</table><br>";
}
function dump_response() {
$i = 0;
foreach ($this->response as $key => $value) {
$this->debuginfo .= "$key: $value\n";
$i++;
}
return $this->debuginfo;
}
}
?>
[eluser]Unknown[/eluser]
hi summer student
can u tell where shall we call this controller for authorize.net when we go for it
[eluser]Trae R.[/eluser]
I found this to be very useful. Many thanks for posting it. Please consider adding it to the CodeIgniter Wiki.
[eluser]skunkbad[/eluser]
You ought to check out the authorize.net library in my Community Cart application. There is a lot more to be desired than what the above code provides. Filename is Authnet.php. Link to Community Cart in my signature.
[eluser]Jenson M John[/eluser]
@idealws
Hi idealws,
Thanks for this information. I think a small correction (not in functionality) needed in the controller class authorizenet.php. I Hope "gateways/" text in the below line of code within controller class is your own library location. Is it not?
Code: $this->load->library('gateways/authorize_net');
Code: $this->load->library('gateways/authorize_net');
should be
Code: $this->load->library('authorize_net');
I assume. Is it not?
Thanks,
Jenson
[eluser]idealws[/eluser]
Jenson,
You are correct. The way I have it is for the way we have our folders setup. I will make the correction, thank you for pointing this out.
[eluser]Jenson M John[/eluser]
[eluser]Unknown[/eluser]
Had a problem executing https with curl (actually not executing ), apparently needed to include the following 2 curl options in the process_payment function for it to execute.
Code: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
Modified process_payment function as follows for anyone experiencing similar problems.
Code: function process_payment() {
foreach( $this->fields as $key => $value ) {
$this->field_string .= "$key=" . urlencode( $value ) . "&";
}
$ch = curl_init($this->gateway_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $this->field_string, "& " ));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // <<--- Here
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // <<--- And Here :)
$this->response_string = urldecode(curl_exec($ch));
if (curl_errno($ch)) {
$this->response['Response_Reason_Text'] = curl_error($ch);
return 3;
}else{
curl_close ($ch);
}
$temp_values = explode($this->CI->config->item('authorize_net_x_delim_char'), $this->response_string);
$temp_keys= array (
"Response_Code", "Response_Subcode", "Response_Reason_Code", "Response_Reason_Text",
"Approval_Code", "AVS_Result_Code", "Transaction_ID", "Invoice_Number", "Description",
"Amount", "Method", "Transaction_Type", "Customer_ID", "Cardholder_First_Name",
"Cardholder Last_Name", "Company", "Billing_Address", "City", "State",
"Zip", "Country", "Phone", "Fax", "Email", "Ship_to_First_Name", "Ship_to_Last_Name",
"Ship_to_Company", "Ship_to_Address", "Ship_to_City", "Ship_to_State",
"Ship_to_Zip", "Ship_to_Country", "Tax_Amount", "Duty_Amount", "Freight_Amount",
"Tax_Exempt_Flag", "PO_Number", "MD5_Hash", "Card_Code_CVV_Response Code",
"Cardholder_Authentication_Verification_Value_CAVV_Response_Code"
);
for ($i=0; $i<=27; $i++) {
array_push($temp_keys, 'Reserved_Field '.$i);
}
$i=0;
while (sizeof($temp_keys) < sizeof($temp_values)) {
array_push($temp_keys, 'Merchant_Defined_Field '.$i);
$i++;
}
for ($i=0; $i<sizeof($temp_values);$i++) {
$this->response["$temp_keys[$i]"] = $temp_values[$i];
}
return $this->response['Response_Code'];
}
Development machine (Windows 7 64bit)
Running
+ ApacheFriends XAMPP (Basispaket) version 1.7.4
+ Apache 2.2.17
+ MySQL 5.5.8 (Community Server)
+ PHP 5.3.5 (VC6 X86 32bit) + PEAR
+ OpenSSL 0.9.8o
[eluser]soxmom[/eluser]
When I try to down load the .zip file - for some reason, I'm just being taken to a blank html page. Any ideas on how/where to get the Authorize.net library?
|