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

Generate XML file in Cakephp

Steps to Generate XML file using CakePHP: Step-1 Enable to parse xml extension in config route.php file.     Router::parseExtensions('xml'); Step-2 Add Request Handler Component to the Controller    var $components = array(‘RequestHandler’); Step-3 Add controller Action For XML Generation in Post Controller     function generateXMLFile()     {         if ($this->RequestHandler->isXml()) { // check request type             $this->layout = 'empty'; // create an empty layout in app/views/layouts/empty.ctp              }        }  Add header code in empty layout <?php header('Content-type: text/xml');?> <?php echo $this->Xml->header(); ?> <?php echo $content_for_layout; ?> Step-4 Set up View To generate XML Create xml folder inside Posts vi...

Simple JavaScript Object and Class Examples

In JavaScript, most things are objects. An object is a collection of related data and/or functionality Namespace: Everything you create in JavaScript is by default global. In JavaScript namespace is Global object . How to create a namespace in JavaScript? Create one global object, and all variables, methods, and functions become properties of that object. Example:  // global namespace var MYAPP = MYAPP || {}; Explaination: Here we first checked whether MYAPP is already defined.If yes, then use the existing MYAPP global object, otherwise create an empty object called MYAPP How to create sub namespaces? // sub namespace MYAPP.event = {}; Class JavaScript is a prototype-based language and contains no class statement.JavaScript classes create simple objects and deal with inheritance. Example: var Person = function () {}; Explaination: Here we define a new class called Person with an empty constructor. Use the class keyword class Calculation {}; A class expr...

How to Add Next Previous links to The Event Calendar

Add Next/Previous links to The Event Calendar Wordpress Plugin Add code to your child theme’s functions.php file /**  * Allows visitors to page forward/backwards in any direction within month view */ if ( class_exists( 'Tribe__Events__Main' ) ) { class ContinualMonthViewPagination {     public function __construct() {         add_filter( 'tribe_events_the_next_month_link', array( $this, 'next_month' ) );         add_filter( 'tribe_events_the_previous_month_link', array( $this, 'previous_month' ) );     }     public function next_month() {         $url = tribe_get_next_month_link();         $text = tribe_get_next_month_text();         $date = Tribe__Events__Main::instance()->nextMonth( tribe_get_month_view_date() );         return '<a data-month="' . $date . '" href="' . $url . '" rel="next"...