How to integrate Authorize.net in PHP

AUthorize.net is known payment gateway among web programmers. Here I am integrating using a simple form and php code.

Download Authorize.net SDK

You can create sandbox account and get API key and secret to test it.

form.php

<form action="process-paypal.php" method="post">
<table>
<tr>
<td>First Name</td>
<td><input type="text" name="first-name"></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="last-name"></td>
</tr>
<tr>
<td>Address</td>
<td><input type="text" name="address"></td>
</tr>
<tr>
<td>State</td>
<td><input type="text" name="state"></td>
</tr>
<tr>
<td>Zip Code</td>
<td><input type="text" name="zipcode"></td>
</tr>
<tr>
<td>City</td>
<td><input type="text" name="city"></td>
</tr>
<tr>
<td>Cardtype</td>
<td>
<select name="cardtype">
<option name="Mastercard">Mastercard</option>
<option name="Visa">Visa</option>
</select>
</td>
</tr>
<tr>
<td>Card Number</td>
<td><input type="text" name="cardNumber"></td>
</tr>
<tr>
<td>CVV</td>
<td><input type="text" name="cvv"></td>
</tr>
<tr>
<td>Expiry date</td>
<td>
<select name="exp_month">
<option name="01">January</option>
<option name="02">February</option>
<option name="03">March</option>
<option name="04">April</option>
<option name="05">May</option>
</select>
<select name="exp_yr">
<option name="2015">2015</option>
<option name="2016">2016</option>
<option name="2017">2017</option>
<option name="2018">2018</option>
<option name="2019">2019</option>
</select>
</td>
</tr>
</table>
</form>

authorize-payment.php

This PHP file will process the form submitted by user to make payment.Here I have tested using sample product information.You can put cart information there as per yours need.


<?php 
require 'vendor/autoload.php'; 
use net\authorize\api\contract\v1 as AnetAPI; 
use net\authorize\api\controller as AnetController; 
define("AUTHORIZENET_LOG_FILE", "phplog"); 

//Setup for API credentials 
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
 $merchantAuthentication->setName("Your API KEY");
$merchantAuthentication->setTransactionKey("Secret Key Here");
$refId = 'ref' . time();

//Fetching Form data and setting card details
$cardNumber = $_POST['cardNumber'];
$exp_month = $_POST['exp_month'];
$exp_yr = $_POST['exp_yr'];
$exp_str = $exp_yr."-".$exp_month;

$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber($cardNumber);
$creditCard->setExpirationDate($exp_month);
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);

// Order info
$order_id = 100;//you can put order id
$order = new AnetAPI\OrderType();
$order->setInvoiceNumber($order_id);
$order->setDescription("Order Description");

// Product information
$lineitem = new AnetAPI\LineItemType();
$lineitem->setItemId("product1");
$lineitem->setName("Product A");
$lineitem->setDescription("This is a test product");
$lineitem->setQuantity("1");
$lineitem->setUnitPrice(20);
$lineitem->setTaxable("Y");

// Tax info
$tax = new AnetAPI\ExtendedAmountType();
$tax->setName("Tax Name");
$tax->setAmount(5.75);
$tax->setDescription("Tax Description here");

// Customer information
$customer_id = 10;//you can put current user id
$customer_email = "raj@example.com";
$customer = new AnetAPI\CustomerDataType();
$customer->setId($customer_id);
$customer->setEmail($customer_email);
// PO Number
$ponumber = "15"; // purchase order number

//Shipping Information
$shipping_firstName = $_POST['shipping_firstName'];
$shipping_lastName = $_POST['shipping_lastName'];
$shipping_company = $_POST['shipping_company'];
$shipping_address = $_POST['shipping_address'];
$shipping_city = $_POST['shipping_city'];
$shipping_state = $_POST['shipping_state'];
$shipping_zipCode = $_POST['shipping_zipCode'];
$shipping_country = $_POST['shipping_country'];

$shipto = new AnetAPI\NameAndAddressType();
$shipto->setFirstName($shipping_firstName);
$shipto->setLastName($shipping_lastName);
$shipto->setCompany($shipping_company);
$shipto->setAddress($shipping_address);
$shipto->setCity($shipping_city);
$shipto->setState($shipping_state);
$shipto->setZip($shipping_zipCode);
$shipto->setCountry($shipping_country);

// Billing information
$billing_firstName = $_POST['billing_firstName'];
$billing_lastName = $_POST['billing_lastName'];
$billing_company = $_POST['billing_company'];
$billing_address = $_POST['billing_address'];
$billing_city = $_POST['billing_city'];
$billing_state = $_POST['billing_state'];
$billing_zipCode = $_POST['billing_zipCode'];
$billing_country = $_POST['billing_country'];


$billto = new AnetAPI\CustomerAddressType();
$billto->setFirstName($firstName);
$billto->setLastName($lastName);
$billto->setCompany($company);
$billto->setAddress($address);
$billto->setCity($city);
$billto->setState($state);
$billto->setZip($zipCode);
$billto->setCountry($country);

//create a transaction
$amount = 100;//you can dynamically set amount
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType( "authCaptureTransaction");
$transactionRequestType->setAmount(151.51);
$transactionRequestType->setPayment($paymentOne);
$transactionRequestType->setOrder($order);
$transactionRequestType->addToLineItems($lineitem);
$transactionRequestType->setTax($tax);
$transactionRequestType->setPoNumber($ponumber);
$transactionRequestType->setCustomer($customer);
$transactionRequestType->setBillTo($billto);
$transactionRequestType->setShipTo($shipto);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId( $refId);
$request->setTransactionRequest( $transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
$response = $controller->executeWithApiResponse( \net\authorize\api\constants\ANetEnvironment::SANDBOX);

if ($response != null)
{
$tresponse = $response->getTransactionResponse();
if (($tresponse != null) && ($tresponse->getResponseCode()=="1") )
{
echo "AUTH CODE : " . $tresponse->getAuthCode() . "\n";
echo "TRANS ID : " . $tresponse->getTransId() . "\n";
}
else
{
echo "ERROR : Invalid response\n";
}

}
else
{
echo "Null response returned";
}
?>

Leave a Reply