Copyright © 2010 Olebox – Shaun Oleson. All Rights Reserved. Snowblind by Themes by bavotasan.com. Powered by WordPress.
So I’ve had quite a bit of experience with authorize.NET and PHP and I’ve found it’s difficult to find resources that provide textual examples for integrating the payment processor. I’ve put together a generic tutorial from Authorize.NET documentation that is public. Of course, you will have to specify your Transaction ID and API Login ID. So here you are. If you need assistance or have any questions, feel free to leave a comment. If you found this article helpful, please help support our sponsors through the google ads as they help to cover our hosting fees. Both myself and the companies appreciate it!
The example that Authorize.NET provides does a lot of parsing through strings. I prefer working with arrays as they are easier to read, shorter to write and from what I understand, faster to parse. Also, I like to organize my data in classes. For this example, I’ll just show the function as that should be enough to integrate.
function get_auth($data_array) {
if(empty($data_array['amount']) || !is_numeric($data_array['amount'])) {
$error->insert(“Please provide a valid amount for credit card processing.”);
}if(empty($data_array['credit_card_number'])) {
die(“Please provide a valid Credit Card Number.”);
}if(empty($data_array['credit_card_exp_date'])) {
die(“Please provide a valid Credit Card Expiration Date.”);
}if(empty($data_array['credit_card_cvv'])) {
die(“Please provide a valid Credit Card CVV Code.”);
}if(empty($data_array['billing_first'])) {
die(“Please provide the Billing First Name.”);
}if(empty($data_array['billing_last'])) {
die(“Please provide the Billing Last Name.”);
}if(empty($data_array['billing_city'])) {
die(“Please provide the Billing City.”);
}if(empty($data_array['billing_state'])) {
die(“Please provide the Billing State.”);
}// set default api credentials
$auth_net_tran_key = “abcdefg”;
$auth_net_login_id = “abcdefg”;// this is a variable I created to easily change the transaction type from live
// to testing and vice-versa. Live should be “live” and testing should be “testing”
$gateway_status = “testing”;// Display additional information to track down problems
$DEBUGGING = 1;// Set the testing flag so that transactions are not live
$TESTING = 1;// Number of transactions to post if soft errors occur
$ERROR_RETRIES = 2;if($gateway_status == “testing”) {
$auth_net_url = “https://test.authorize.net/gateway/transact.dll”;
} else if($gateway_status == “live”) {
$auth_net_url = “https://secure.authorize.net/gateway/transact.dll”;
} else {
die(“Undefined Gateway Status. Unable to process trasactions. Contact the system Administrator.”);
}$invoice_number = $order_details->cart_id + $invoice_start;
$authnet_values = array
(“x_login” => $auth_net_login_id,
“x_version” => “3.1″,
“x_delim_char” => “|”,
“x_delim_data” => “TRUE”,
“x_duplicate_window” => “30″,
“x_url” => “FALSE”,
“x_type” => “AUTH_CAPTURE”,
“x_method” => “CC”,
“x_tran_key” => $auth_net_tran_key,
“x_relay_response” => “FALSE”,
“x_card_num” => $data_array['credit_card_number'],
“x_exp_date” => $data_array['credit_card_exp_date'],
“x_card_code” => $data_array['credit_card_cvv'],
“x_description” => $auth_net_description,
“x_first_name” => $data_array['billing_first'],
“x_last_name” => $data_array['billing_last'],
“x_address” => $data_array['billing_street'],
“x_city” => $data_array['billing_city'],
“x_state” => $data_array['billing_state'],
“x_zip” => $data_array['billing_zip'],
“x_amount” => $data_array['amount'],
“x_invoice_num” => $invoice_number
);$line_item_num = 0;
// you will need to create a mysql result here
// to loop through if you want to pass in the invoice
// and line item details. I have not included this code.$line_items = mysql_result; // replace ‘mysql_result’ with your query
line_item_array = array();while($line_item = mysql_fetch_assoc($line_items, MYSQL_ASSOC)) {
$line_item_num++;
// the following line is in this format: item_id<|>Item Name<|>Item Description<|>Quantity<|>Price<|>Taxable$line_item_text = “item{$line_item_num}<|>Item Name<|>Item Description<|>2<|>25.00<|>N”;
array_push($line_item_array, $line_item_text);} // end of while loop
// set for live mode, you will have to adjust this if processing test transactions
$ch = curl_init(“https://secure.authorize.net/gateway/transact.dll”);// convert authnet_values to fields in post list
$fields = “”;
foreach( $authnet_values as $key => $value ) $fields .= “$key=” . urlencode( $value ) . “&”;
// parse line item array
foreach($line_item_array as $key=>$value) {
if($key != count($line_item_array)-1) {
$fields.=”x_line_item=”.$value.”&”;
} else {
$fields.=”x_line_item=”.$value;
}
}
// post payment to Authorize.NET
curl_setopt($ch, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, rtrim( $fields, “& ” )); // use HTTP POST to send form data
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response. ###
$resp = curl_exec($ch); //execute post and get results
curl_close ($ch);$response_array = explode(“|”, $resp);
switch($response_array['0']){
case “1″:
$response['response_code']=”Approved”;
break;
case “2″:
$response['response_code']=”Declined”;
break;
case “3″:
$response['response_code']=”Error”;
break;
}$response['response_subcode'] = $response_array['1'];
$response['response_reason_code'] = $response_array['2'];
$response['response_reason_text'] = $response_array['3'];
$response['approval_code'] = $response_array['4'];
$response['avs_result_code'] = $response_array['5'];
$response['transaction_id'] = $response_array['6'];if($response['response_code'] != “Approved”) {
die($response['response_code'].”: “.$response['response_reason_text']);
}// will only return response if the transaction was approved
return $response;
} // end of function
Within your page you will need to call this function and pass in the appropriate payment data:
<?php
$result = get_auth(array(
“amount”=>$amount,
“credit_card_number”=>$credit_card_number,
“credit_card_exp” =>$credit_card_exp,
“credit_card_cvv”=>$credit_card_cvv,
“billing_first”=>$billing_first,
“billing_last”=>$billing_last,
“billing_street”=>$billing_street,
“billing_city”=>$billing_city,
“billing_state”=>$billing_state,
“billing_zip”=>$billing_zip
));var_dump($result);
?>
So this is the basics of the authorize.NET integration. Keep in mind, I have removed my customized error handling and I have set this up as if it were to process in live mode. Rather than using die to kill the response and output the error, you should use an error class. I have built one in another post that would be sufficient for this. You can import it using global after you initiate it.
A common error I received was Error: Line item 1 invalid. When you see this, that means the format of your line items is not correct. Check the value count. Make sure that the ampersand (&) seperates each line item and make sure you are passing the values in the right format. The quantity must be a positive number, the price will allow up to 2 digits past the decimal, the item id and item name will allow a max of 31 chars and the description will allow up to 255 characters.
I had a hard time finding much help on integrating Authorize.NET, so hopefully this will prove helpful to others. Again, if you have questions, feel free to post and we’ll do our best to help out. If you found this article helpful, please help support our sponsors through the google ads as they help to cover our hosting fees. Both myself and the companies appreciate it!


December 8, 2008 at 2:11 pm
You forgot to add quotes in many places.
December 8, 2008 at 2:42 pm
Thanks Steve -
I found 2 places in the get_auth array I was passing, If you see others, let me know.
January 25, 2009 at 4:37 pm
a lot of the quotes are the html “ and ” entities. Need to edit this in a text editor.
Also, what is $DEBUGGING and $TESTING for? There are no references to them outside of their declarations.
And also, $gateway_status is never defined, so as it is written, it will always die with an undefined gateway status.
But the rest of it looks good. Thanks!
January 25, 2009 at 7:47 pm
Thanks for the feedback Peter. Looks like I didn’t have the $gateway_status variable defined. If you use a global settings file, you could import this from the global scope using “global $gateway_status;”
For your reference, the debugging variable will allow you to output additional logging details if processing transactions on the live url when set to test transactions.
The testing variable allows you to post test transactions to the live auth_net_url. More information is available on this within the Authorize.net AIM Integration Documentation, found here: http://developer.authorize.net/guides/AIM/
Again, thanks for the feedback. Hopefully, you’re able to make use of the above example.
Shaun
March 22, 2009 at 4:49 pm
Commenting usually isnt my thing, but ive spent an hour on the site, so thanks for the info
March 22, 2009 at 8:00 pm
No problem. Glad we could help.
April 24, 2009 at 6:07 am
I follow your blog for quite a long time and must tell you that your posts are always valuable to readers.
May 26, 2009 at 7:45 pm
I had issue with the case/switch – it was sending back my codes encapsulated with single quotes…
so case “1″:
needed to be case “’1′”: to work
I found out from this post: https://www.creloaded.com/forums/Forums/viewtopic/printertopic=1/t=21168/start=0/postdays=0/postorder=asc/vote=viewresult.html (see last comment) that single quotes are one of 8 possible “field encapsulation characters.” there is a setting in authorize.net where can change that.
May 26, 2009 at 7:47 pm
when i said “I had issue with the case/switch – it was sending back my codes encapsulated with single quotes… ” i meant authorize.net was sending back my codes w/ the single quotes… approved was ’1′ not 1.
May 26, 2009 at 7:57 pm
Great point Danielle. It’s important to check the settings in authorize.net (of course). Developers would need to add the encapsulation characters to the case statement to get it to execute correctly.