Using the Authorize.net payment gateway in PHP is an easy task. All you need is the official PHP SDK and the code below. You can implement it as a standalone application or integrate with frameworks like Laravel, Codeigniter, and others.
If using composer, add the following to your composer.json file
{
"require": {
"php": ">=5.5",
"ext-curl": "*",
"phpunit/phpunit": "~4.8||~6.0",
"authorizenet/authorizenet": "1.9.3"
},
"autoload": {
"classmap": ["constants"]
}
}
Run composer install.
Once this process is completed, you should have a vendor folder created in your project directory.
Alternatively, you can download the Authorize.net PHP SDK. Extract it to your project directory and run composer update command.
require 'vendor/autoload.php';
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
If your not using Composer. Authorize.net provides a custom SPL autoloader. Just download the SDK and point to its autoload.php file with:
require 'path/to/anet_php_sdk/autoload.php';
Go to the constants directory and open up the constants.php file. Set the up login_id and transaction_key that you have with your Authorize.net sandbox account.
<?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");
function chargeCreditCard($amount) {
// Common setup for API credentials
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(\SampleCode\Constants::MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(\SampleCode\Constants::MERCHANT_TRANSACTION_KEY);
$refId = 'ref' . time();
//While testing, create the payment data for a credit card
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber("4111111111111111");
$creditCard->setExpirationDate("1227");
$creditCard->setCardCode("123");
$paymentOne = new AnetAPI\PaymentType();
$paymentOne->setCreditCard($creditCard);
$order = new AnetAPI\OrderType();
$order->setDescription("New Order Description");
//create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount($amount);
$transactionRequestType->setOrder($order);
$transactionRequestType->setPayment($paymentOne);
//Prepare customer information object from your form $_POST data
$cust = new AnetAPI\CustomerAddressType();
$cust->setFirstName($_POST['fname']);
$cust->setLastName($_POST['lname']);
$cust->setAddress($_POST['address']);
$cust->setCity($_POST['city']);
$cust->setState($_POST['state']);
$cust->setCountry($_POST['country']);
$cust->setZip($_POST['zip']);
$cust->setPhoneNumber($_POST['phone']);
$cust->setEmail("Email-here");
$transactionRequestType->setBillTo($cust);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
//Get response from Authorize.net
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if ($response != null) {
if ($response->getMessages()->getResultCode() == \SampleCode\Constants::RESPONSE_OK) {
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getMessages() != null) {
echo " Transaction Response code : " . $tresponse->getResponseCode() . "\n";
echo "Charge Credit Card AUTH CODE : " . $tresponse->getAuthCode() . "\n";
echo "Charge Credit Card TRANS ID : " . $tresponse->getTransId() . "\n";
echo " Code : " . $tresponse->getMessages()[0]->getCode() . "\n";
echo " Description : " . $tresponse->getMessages()[0]->getDescription() . "\n";
} else {
echo "Transaction Failed \n";
if ($tresponse->getErrors() != null) {
echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
}
}
} else {
echo "Transaction Failed \n";
$tresponse = $response->getTransactionResponse();
if ($tresponse != null && $tresponse->getErrors() != null) {
echo " Error code : " . $tresponse->getErrors()[0]->getErrorCode() . "\n";
echo " Error message : " . $tresponse->getErrors()[0]->getErrorText() . "\n";
} else {
echo " Error code : " . $response->getMessages()->getMessage()[0]->getCode() . "\n";
echo " Error message : " . $response->getMessages()->getMessage()[0]->getText() . "\n";
}
}
} else {
echo "No response returned \n";
}
return $response;
}
$amount = \SampleCode\Constants::SAMPLE_AMOUNT;
if (!empty($_POST['amount']))
$amount = $_POST['amount'];
if (!defined('DONT_RUN_SAMPLES'))
chargeCreditCard($amount);