Authorize.Net Integrating IN PHP

Authorize.Net is my favorite payment mode for using in web development. It is simple, easy and secure payment connection.

If you are using PayPal Payments Pro, then you will need to provide your  company with the following details.
• API Login ID
• Transaction Key
SIM Implementation

// the parameters for the payment can be configured here
// the API Login ID and Transaction Key must be replaced with valid values
$loginID  = "API_LOGIN_ID";
$transactionKey = "TRANSACTION_KEY";
$amount   = "19.99";
$description  = "Sample Transaction";
$label    = "Submit Payment"; // The is the label on the 'submit' button
$testMode  = "false";
// By default, this sample code is designed to post to our test server for
// developer accounts: https://test.authorize.net/gateway/transact.dll
// for real accounts (even in test mode), please make sure that you are
// posting to: https://secure.authorize.net/gateway/transact.dll
$url   = "https://test.authorize.net/gateway/transact.dll";

// If an amount or description were posted to this page, the defaults are overidden
if (array_key_exists("amount",$_REQUEST))
 { $amount = $_REQUEST["amount"]; }
if (array_key_exists("amount",$_REQUEST))
 { $description = $_REQUEST["description"]; }

// an invoice is generated using the date and time
$invoice = date(YmdHis);
// a sequence number is randomly generated
$sequence = rand(1, 1000);
// a timestamp is generated
$timeStamp = time();

// The following lines generate the SIM fingerprint.  PHP versions 5.1.2 and
// newer have the necessary hmac function built in.  For older versions, it
// will try to use the mhash library.
if( phpversion() >= '5.1.2' )
 { $fingerprint = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey); }
else 
 { $fingerprint = bin2hex(mhash(MHASH_MD5, $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey)); }
?>


Amount:  

Description:  




AIM Implementation

"API_LOGIN_ID",
 "x_tran_key"  => "TRANSACTION_KEY",

 "x_version"   => "3.1",
 "x_delim_data"  => "TRUE",
 "x_delim_char"  => "|",
 "x_relay_response" => "FALSE",

 "x_type"   => "AUTH_CAPTURE",
 "x_method"   => "CC",
 "x_card_num"  => "4111111111111111",
 "x_exp_date"  => "0115",

 "x_amount"   => "19.99",
 "x_description"  => "Sample Transaction",

 "x_first_name"  => "John",
 "x_last_name"  => "Doe",
 "x_address"   => "1234 Street",
 "x_state"   => "WA",
 "x_zip"    => "98004"
              
 // Additional fields can be added here as outlined in the AIM integration
 // guide at: http://developer.authorize.net


);

// This section takes the input fields and converts them to the proper format
// for an http post.  For example: "x_login=username&x_tran_key=a1B2c3D4"
$post_string = "";
foreach( $post_values as $key => $value )
 { $post_string .= "$key=" . urlencode( $value ) . "&"; }
$post_string = rtrim( $post_string, "& " );

// This sample code uses the CURL library for php to establish a connection,
// submit the post, and record the response.
// If you receive an error, you may want to ensure that you have the curl
// library enabled in your php configuration
$request = curl_init($post_url); // initiate curl object
 curl_setopt($request, CURLOPT_HEADER, 1); // 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
 // additional options may be required depending upon your server configuration
 // you can find documentation on curl options at http://www.php.net/curl_setopt
curl_close ($request); // close curl object
// 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);

// individual elements of the array could be accessed to read certain response
// fields.  For example, response_array[0] would return the Response Code,
// response_array[2] would return the Response Reason Code.
// for a list of response fields, please review the AIM Implementation Guide
    $message=$response_array[3];
   $Trans_id=$response_array[5];
if($response_array[1]==1 && $response_array[2]==1)
{
 // payment is successful. Do your action here
}
else 
{
  // payment failed
}


?>

PayPal Pro Payment Integration in PHP

 If you are using PayPal Payments Pro, then you will need to provide your web design company with the following details.
• API username
• signature
• password
Please note that this is different to your PayPal account details. The details you use to login to Paypal’s website to access your accounts is separate to the API details you need to provide your web designer. Please do not email or provide your account details to your web design company as the information is confidential.
Contrary to what some may think, the API details are not provided by PayPal to start with. Instead, the PayPal account holder is required to generate this information by logging on to their account on PayPal’s website.
Following are the instructions on how to obtain API details as per their user guide:

Creating API Signature for PayPal Pro Website Payments

An API signature consists of an API username along with an associated API password and signature, all of which are assigned by PayPal. This information needs to be supplied with all transaction requests made by your website.
You must have a PayPal Business account to create a signature.
To create an API signature:
1. Log into PayPal, then click Profile under My Account.
2. Click API Access.
3. Click Request API Credentials.
4. Check Request API signature and click Agree and Submit.


$value) {
  $tmpAr = explode("=", $value);
  if(sizeof($tmpAr) > 1) {
   $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
  }
 }

 if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
  exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
 }

 return $httpParsedResponseAr;
}

// Set request-specific fields.
$paymentType = urlencode('Authorization');    // or 'Sale'
$firstName = urlencode('customer_first_name');
$lastName = urlencode('customer_last_name');
$creditCardType = urlencode('customer_credit_card_type');
$creditCardNumber = urlencode('customer_credit_card_number');
$expDateMonth = 'cc_expiration_month';
// Month must be padded with leading zero
$padDateMonth = urlencode(str_pad($expDateMonth, 2, '0', STR_PAD_LEFT));

$expDateYear = urlencode('cc_expiration_year');
$cvv2Number = urlencode('cc_cvv2_number');
$address1 = urlencode('customer_address1');
$address2 = urlencode('customer_address2');
$city = urlencode('customer_city');
$state = urlencode('customer_state');
$zip = urlencode('customer_zip');
$country = urlencode('customer_country');    // US or other valid country code
$amount = urlencode('example_payment_amuont');
$currencyID = urlencode('USD');       // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')

// Add request-specific fields to the request string.
$nvpStr = "&PAYMENTACTION=$paymentType&AMT=$amount&CREDITCARDTYPE=$creditCardType&ACCT=$creditCardNumber".
   "&EXPDATE=$padDateMonth$expDateYear&CVV2=$cvv2Number&FIRSTNAME=$firstName&LASTNAME=$lastName".
   "&STREET=$address1&CITY=$city&STATE=$state&ZIP=$zip&COUNTRYCODE=$country&CURRENCYCODE=$currencyID";

// Execute the API operation; see the PPHttpPost function above.
$httpParsedResponseAr = PPHttpPost('DoDirectPayment', $nvpStr);

if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])) {
 exit('Direct Payment Completed Successfully: '.print_r($httpParsedResponseAr, true));
} else  {
 exit('DoDirectPayment failed: ' . print_r($httpParsedResponseAr, true));
}

?>




Simple PayPal


Online payment method and PayPal is the most secure payment gateway. If you have a very basic knowledge of PHP, you can easily implement the simple PayPal gateway in your site.

There are 3 main parts to the PayPal IPN system.

A webpage that initiates a request to PayPal to make a payment.
A PHP page on your webserver that PayPal calls to notify you that payment has been made.
A webpage that confirms the above payment and continues on to the next phase of your web application, such as a ‘Thank You’ page.

Parts 1 and 3 are accessible by customers on your website. Part 2 is only visible to PayPal. The diagram below illustrates the interaction between your customer, PayPal and your website.

// YOUR PAYPAL EMAIL ID // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD') // Sets the locale (language) of the PayPal login page