REST API Tutorial

How to Integrate Authorize.net Payment Gateway in PHP

Summary of learning

We will let you know about integrating the Authorize.net payment gateway using PHP. I will use the official PHP library for Authorize.net that will have the necessary methods which are used in this tutorial and online transactions on your website.

We will let you know about integrating the Authorize.net payment gateway using PHP. I will use the official PHP library for Authorize.net that will have the necessary methods which are used in this tutorial and online transactions on your website.

Authorize.net return the string response, So we don’t need to create a payment notification page and any other post-payment request. You can get more information from Authorize.net API Document.

Authorize.net is a simple and secure payment gateway that why every e-commerce platform use it. This PHP payment integration tutorial describes you step by step Authorize.net gateway integration.

I will use AuthorizeNet PHP SDK.

authorize-payment-gateway

You can check other recommended tutorials of Payment Gateway,

How to integrate Authorize.net Payment Gateway With PHP

We will create a sandbox account for test payment using Authorize.net Payment Gateway and then change the test to live env. We will download the PHP SDK of Authorize.net Payment Gateway and integrate with payment form.

Prerequisite for Authorize.net Payment Gateway for PHP

  • PHP 5.6+
  • cURL PHP Extension
  • JSON PHP Extension
  • An Authorize.Net account
  • TLS 1.2 capable versions of libcurl and OpenSSL (or its equivalent)

Step 1: Create a sandbox account

We will create a sandbox account with Authorize.net to create and test the payment gateway. You can create a sandbox account using signup form, after successfully registered account, You will get the API login ID and Transaction ID in the welcome mail of Authorize.net. You can also get that information from your API page

Step 2: Download the PHP SDK of Authorize.net

We will download Authorize.net Payment Gateway and paste into the project root folder like d:/xampp/htdocs/authorize_payment_gateway/ or Sample PHP project and paste into the project root folder like d:/xampp/htdocs/authorize_payment_gateway/.

Now open constants/constants.php file and add login_id and transaction_key that you have with your Authorize.net sandbox account.

Create Payment Form

We will create a payment form that will have some required fields like cc information and user information, I am using bootstrap to create UI.

<fieldset class="scheduler-border">
  <legend class="scheduler-border">Pay</legend>
  <form class="form-inline" action="cc_response.php" method="post">
     <input id="fname" tabindex="1" maxlength="20" name="fname" size="20" type="hidden">
     <input id="lname" tabindex="2" autocomplete="off" maxlength="12" name="lname" size="12" type="hidden" value="CUST001"> 
     <input id="country" tabindex="4" maxlength="12" name="country" size="12" type="hidden">
     <input id="cc" tabindex="4" maxlength="12" name="cc" size="12" type="hidden" value="****">
     <p>&nbsp;</p>
     <div class="form-group"><label for="exampleInputName2">Amount : </label>
      <p>&nbsp;</p>
     <div class="input-group">
        <span class="input-group-addon">$ </span>
        <input id="amount" class="form-control" name="amount" type="text" aria-label="Amount (to the nearest dollar)">
     </div>
   </div>
<p><button id="TXN_AMOUNT" class="btn btn-primary" name="TXN_AMOUNT" type="submit"><i class="fa fa-credit-card" aria-hidden="true"></i> Pay</button></p>
</form></fieldset>

I have created only the amount text box, You need to add cc input into this form. I have added cc_response.php file that will handle payment requests and process payments and display payment responses.

Create a method to payment using Creditcard

I am taking a sample of a credit card to get the amount and payment using the authorize.net gateway.

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 createCreditCardCheckoutTransaction($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();
    // Create the payment data for a credit card
    $creditCard = new AnetAPI\CreditCardType();
    $creditCard->setCardNumber("4111111111111111");
    $creditCard->setExpirationDate("1226");
    $creditCard->setCardCode("123");
    $paymentOne = new AnetAPI\PaymentType();
    $paymentOne->setCreditCard($creditCard);
    $order = new AnetAPI\OrderType();
    $order->setDescription("New Item");
    //create a transaction
    $transactionRequestType = new AnetAPI\TransactionRequestType();
    $transactionRequestType->setTransactionType("authCaptureTransaction");
    $transactionRequestType->setAmount($amount);
    $transactionRequestType->setOrder($order);
    $transactionRequestType->setPayment($paymentOne);
    //Preparing customer information object
    $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);
    $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;
}

Authorize.net Test Credit Card Numbers

There are the following test credit card numbers provided by Authorize.net and will only work with the sandbox account. The credit card test expiration date after today’s date. If the card code is required, please use any 3-digit combination for Visa, Mastercard, Discover, Diners Club, EnRoute, and JCB; use a 4-digit combination for American Express.

Test Card Brand Number
American Express 370000000000002
Discover 6011000000000012
JCB 3088000000000017
Diners Club/ Carte Blanche 38000000000006
Visa 4007000000027
  4012888818888
  4111111111111111
Mastercard 5424000000000015
  2223000010309703
  2223000010309711

I hope, That helps you.

If you have requirements that needs our attentioN

Please feel free to contact us

We would love to hear from you

Share with your network

Facebook
Twitter
LinkedIn
WhatsApp
Email

This tutorial is intended to cover all the essentials steps and insights to understand How to Integrate Authorize.net Payment Gateway in PHP

Genislab Technologies

Learn further