Reply
Member
Posts: 1
Registered: ‎05-28-2013

AUTHORIZE.NET INTEGRATION WITH CODEIGNITER 2.1

Hello every body I am stucked with this Authorize.net Payment Integration In Codeigniter 2.1, and your help to get me out of this will be great.

 

Basically I am integrating Authorize.net in CODEIGNITER 2.1, I have created a credit card present account of test.authorize.net Tested many libraries available on net but all in vain

 

Whatever I do always returns me an error like "Invalid Login API or Transaction key is invalid" OR "Error Processing Your Payment (TEST MODE)  Trasactions of this market type cannot be processed on this system"

 

Here is the code I am using

 

My AUTHORIZE.NET LIBRARY FOR CODEIGNITER

 

Authorize_net.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)."&nbsp;</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;
}
}

 

 

 

 

 

CONFIG 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 API';
$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'] = 'I Given Here My API KEY';
$config['authorize_net_test_x_tran_key'] = 'I Given Here 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';

/*
—————————————————————————————————————-
*/

 

Here are the contents of MY FORM VIEW

 

<?php echo form_open("/home/processpayment/$product->id"); ?>
<?php if ($this->user->check_login()) : ?>
<label>Card Type<span>*</span></label><br />
<label>Master</label><input name="cctype" type="radio" value="Master" />
<label>Visa</label><input name="cctype" type="radio" value="Visa" />
<label>Discover</label><input name="cctype" type="radio" value="Discover" />
<label>Ammex</label><input name="cctype" type="radio" value="Ammex" /><br />
<label>First Name<span>*</span></label>
<?php echo form_input($fname); ?><br />
<label>Last Name<span>*</span></label>
<?php echo form_input($lname); ?><br />
<label>eMail<span>*</span></label>
<?php echo form_input($email); ?><br />
<label>Street Address<span>*</span></label>
<?php echo form_input($address); ?><br />
<label>Country<span>*</span></label>
<?php echo form_input($country); ?><br />
<label>State<span>*</span></label>
<?php echo form_input($state); ?><br />
<label>City<span>*</span></label>
<?php echo form_input($city); ?><br />
<label>Zip<span>*</span></label>
<?php echo form_input($zip); ?><br />
<label>Card Number<span>*</span></label>
<?php echo form_input($ccno); ?><br />
<label>Card Expires On<span>*</span></label>
<?php echo form_input($ccexpir); ?><br />
<label>Enter CVV Code<span>*</span></label>
<?php echo form_input($cccvv); ?><br />
<input name="price" type="hidden" value="<?php echo $product->max_price; ?>" />
<input name="sid" type="hidden" value="<?php echo $product->id_seller; ?>" />

 

Here are the contents of MY CONTROLLER FUNCTION

 

function processpayment($id=NULL) {
$this->load->library('authorize_net');
$fname = $this->input->post('fname'); $lname = $this->input->post('lname');
$address = $this->input->post('address'); $city = $this->input->post('city');
$state = $this->input->post('state'); $country = $this->input->post('country');
$zip = $this->input->post('zip'); $email = $this->input->post('email');
$ccno = $this->input->post('ccno'); $amount = $this->input->post('price');
$expiry = $this->input->post('ccexpir'); $cvv = $this->input->post('cccvv');

// Lets do a test transaction
$this->authorize_net->add_x_field('x_first_name', "$fname");
$this->authorize_net->add_x_field('x_last_name', "$lname");
$this->authorize_net->add_x_field('x_address', "$address");
$this->authorize_net->add_x_field('x_city', "$city");
$this->authorize_net->add_x_field('x_state', "$state");
$this->authorize_net->add_x_field('x_zip', "$zip");
$this->authorize_net->add_x_field('x_country', 'US');
$this->authorize_net->add_x_field('x_email', "$email");
$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', "$ccno");

$this->authorize_net->add_x_field('x_amount', "$amount");
$this->authorize_net->add_x_field('x_exp_date', '0814'); // 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('auth_success', $data);

}elseif($authnetreponse['Response_Code'] == '2') {
$data['authresponse'] = $authnetreponse;
$this->load->view('auth_fail', $data);

}elseif($authnetreponse['Response_Code'] == '3') {
$data['authresponse'] = $authnetreponse;
$this->load->view('auth_fail', $data);

}
}

 

AND IN LAST HERE IS THE ERROR OUTPUT I AM GETTING

 

Error processing your payment!

Array
(
    [Response_Code] => 3
    [Response_Subcode] => 1
    [Response_Reason_Code] => 87
    [Response_Reason_Text] => (TESTMODE) Transactions of this market type cannot be processed on this system.
    [Approval_Code] => 000000
    [AVS_Result_Code] => P
    [Transaction_ID] => 0
    [Invoice_Number] => 
    [Description] => 
    [Amount] => 500.00
    [Method] => CC
    [Transaction_Type] => auth_capture
    [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] => 0967EABDB48CF8453FE1EB89C6621EEB
    [Card_Code_CVV_Response Code] => 
    [Cardholder_Authentication_Verification_Value_CAVV_Response_Code] => 
    [Reserved_Field 0] => 
    [Reserved_Field 1] => 
    [Reserved_Field 2] => 
    [Reserved_Field 3] => 
    [Reserved_Field 4] => 
    [Reserved_Field 5] => 
    [Reserved_Field 6] => 
    [Reserved_Field 7] => 
    [Reserved_Field 8] => 
    [Reserved_Field 9] => 
    [Reserved_Field 10] => 
    [Reserved_Field 11] => 
    [Reserved_Field 12] => 
    [Reserved_Field 13] => 
    [Reserved_Field 14] => 
    [Reserved_Field 15] => 
    [Reserved_Field 16] => 
    [Reserved_Field 17] => 
    [Reserved_Field 18] => 
    [Reserved_Field 19] => 
    [Reserved_Field 20] => 
    [Reserved_Field 21] => 
    [Reserved_Field 22] => 
    [Reserved_Field 23] => 
    [Reserved_Field 24] => 
    [Reserved_Field 25] => 
    [Reserved_Field 26] => 
    [Reserved_Field 27] => 
    [Merchant_Defined_Field 0] => FALSE
)

 

Expert
Posts: 4,525
Registered: ‎03-08-2010

Re: AUTHORIZE.NET INTEGRATION WITH CODEIGNITER 2.1

[ Edited ]

"Invalid Login API" you might be using the wrong server. test account only work with test server, production account only work with production server.

 

"Transaction key is invalid" just like it said, the transactionkey is wrong.

 

"Error Processing Your Payment (TEST MODE)" codeigniter might required testmode off.

 

"Trasactions of this market type cannot be processed on this system" you said you got a card present account. but it look like you code is doing card NOT present post.

here the documentation for card present
 http://developer.authorize.net/api/cardpresent/

 

What is the difference between Card Not Present (CNP) and Card Present (CP)? Why does it matter?