Skip to main content

Authorize.net payment gateway integration

Authorize.net payment gateway integration


Create login key and api key to check for realtime and for sandbox.

You have to fill in the customers login key and transaction key either sandbox or live. Both are different.
$LOGINKEY = 'XXXXXXXXX';
$TRANSKEY = 'XXXXXXXXX';


Store all the post values into respective variables by url encoding them.

$firstName =urlencode( $_POST['firstname']);
$lastName =urlencode($_POST['lastname']);
$creditCardType =urlencode( $_POST['cardtype']);
$creditCardNumber = urlencode($_POST['cardnumber']);
$expMonth =urlencode( $_POST['cardmonth']);  
$padMonth = str_pad($expMonth, 2, '0', STR_PAD_LEFT);    
$expYear =urlencode( $_POST['cardyear']);
$cvv2Number = urlencode($_POST['cardcvv']);
$address1 = urlencode($_POST['address']);
$city = urlencode($_POST['city']);
$state =urlencode( $_POST['state']);
$zip = urlencode($_POST['zip']);
//give the actual amount below
$amount = "300";
$currencyCode="USD";
$paymentType="Sale";
$date = $expMonth.$expYear;

You need to create key value pairs(Associative array) which then will be converted to a query string. This query string will be posted to the payment gateway site.

$post_values = array(
    "x_login"           => "$LOGINKEY",
    "x_tran_key"        => "$TRANSKEY",
    "x_version"         => "3.1",
    "x_delim_data"      => "TRUE",
    "x_delim_char"      => "|",
    "x_relay_response"  => "FALSE",
    "x_device_type"     => "1",
    "x_type"            => "AUTH_CAPTURE",
    "x_method"          => "CC",
    "x_card_num"        => $creditCardNumber,
    "x_exp_date"        => $date,
    "x_amount"          => $amount,
    "x_description"       => "Sample Transaction",
    "x_first_name"      => $firstName,
    "x_last_name"       => $lastName,
    "x_address"         => $address1,
    "x_state"           => $state,
    "x_response_format" => "1",
    "x_zip"             => $zip
);

The query string which is to be posted to the payment gateway is stored in $post_string.
$post_string = "";
foreach( $post_values as $key => $value )$post_string .= "$key=" . urlencode( $value ) . "&";
$post_string = rtrim($post_string,"& ");

Now we have to set the url to which the above query string should be posted.

//for test mode use the followin url
$post_url = "https://test.authorize.net/gateway/transact.dll";
//for live use this url
$post_url = "https://secure.authorize.net/gateway/transact.dll";

The next step is to make a connection to the authorize.net payment gateway and should post the query string using CURL.

$request = curl_init($post_url); // initiate curl object
curl_setopt($request, CURLOPT_HEADER, 0); // set to 0 to eliminate header info from response
curl_setopt($request, CURLOPT_RETURNTRANSFER, 1); // Returns response data instead of TRUE(1)
curl_setopt($request, CURLOPT_POSTFIELDS, $post_string); // use HTTP POST to send form data
curl_setopt($request, CURLOPT_SSL_VERIFYPEER, FALSE); // uncomment this line if you get no gateway response.
$post_response = curl_exec($request); // execute curl post and store results in $post_response
curl_close ($request); // close curl object

We have received (success or failure) response.
// This line takes the response and breaks it into an array using the specified delimiting character
$response_array = explode($post_values["x_delim_char"],$post_response);

if($response_array[0]==2||$response_array[0]==3)
{
    //success
    echo '<b>Payment Failure</b>.';
    echo '<b>Error String</b>: '.$response_array[3]; // This will contain the reason for the error.
    echo 'Press back button to go back to the previous page';
}
else
{
    $paymentId = $response_array[6]; // The transaction key when success
$payment_amount = $response_array[9]; // transaction payment amount
    echo "Payment Success";

}

Comments

Popular posts from this blog

A Guide to UTF-8 for PHP and MySQL

Data Encoding: A Guide to UTF-8 for PHP and MySQL As a MySQL or PHP developer, once you step beyond the comfortable confines of English-only character sets, you quickly find yourself entangled in the wonderfully wacky world of UTF-8. On a previous job, we began running into data encoding issues when displaying bios of artists from all over the world. It soon became apparent that there were problems with the stored data, as sometimes the data was correctly encoded and sometimes it was not. This led programmers to implement a hodge-podge of patches, sometimes with JavaScript, sometimes with HTML charset meta tags, sometimes with PHP, and soon. Soon, we ended up with a list of 600,000 artist bios with double- or triple encoded information, with data being stored in different ways depending on who programmed the feature or implemented the patch. A classical technical rat’s nest.Indeed, navigating through UTF-8 related data encoding issues can be a frustrating and hair-pul...

How To Create Shortcodes In WordPress

We can create own shortcode by using its predified hooks add_shortcode( 'hello-world', 'techsudhir_hello_world_shortcode' ); 1. Write the Shortcode Function Write a function with a unique name, which will execute the code you’d like the shortcode to trigger: function techsudhir_hello_world_shortcode() {    return 'Hello world!'; } Example: [hello-world] If we were to use this function normally, it would return Hello world! as a string 2. Shortcode function with parameters function techsudhir_hello_world_shortcode( $atts ) {    $a = shortcode_atts( array(       'name' => 'world'    ), $atts );    return 'Hello ' . $a['name'] . !'; } Example: [hello-world name="Sudhir"] You can also call shortcode function in PHP using do_shortcode function Example: do_shortcode('[hello-world]');

Integrating Kafka with Node.js

Integrating Kafka with Node.js Apache Kafka is a popular open-source distributed event streaming platform that uses publish & subscribe mechanism to stream the records(data). Kafka Terminologies Distributed system: Distributed system is a computing environment where various software components located on different machines (over multiple locations). All components coordinate together to get stuff done as one unit.   Kafka Broker: Brokers are cluster of multiple servers. Message of each topic are split among the various brokers. Brokers handle all requests from clients to write and read events. A Kafka cluster is simply a collection of one or more Kafka brokers. Topics: A topic is a stream of "related" messages. Its unique throughout application. Kafka producers write messages to topics. Producer: Producer publishes data on the topics. A producer sends a message to a broker and the broker receives and stores messages. Consumers: Consumers read data from topics. A consu...