We will let you know about integrating Authorize.net payment gateway using PHP. I will use official PHP library for Authorize.net that will have necessary method which are using in this tutorial and online transaction in your website. Authorize.net return the string response, So we don’t need to create payment notification page and any other post payment request.You can get more information from Authorize.net API Document.
Authorize.net is simple and secure payment gateway that why every e-commerce platform use it.This PHP payment integration tutorial describe you step by step Authorize.net gateway integration.
I will use AuthorizeNet PHP SDK.
You can check other recommended tutorial of Payment Gateway,
- How to Integrate Authorize.net Payment Gateway in PHP
- Paypal Payment Gateway Integration with PHP
- Paytm Payment Gateway Integration Using PHP
- CCAvenue Payment Gateway Integration Using PHP
- EBS Payment Gateway Integration Using PHP
Table of Contents
How to integrate Authorize.net Payment Gateway With PHP
We will create sandbox account for test payment using Authorize.net Payment Gateway and then change test to live env .We will download 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 sandbox account
We will create sandbox account with Authorize.net to create and test the payment gateway.You can create sandbox account using signup form, after successfully registered account, you will get API login ID and Transaction ID in welcome mail of Authorize.net.You can also get that information from your API page
Step 2: Download PHP SDK of Authorize.net
We will download Authorize.net Payment Gateway and paste into project root folder like d:/xampp/htdocs/authorize_payment_gateway/
or Sample php project and paste into 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 payment form that will have some required fields like cc information and user information, I am using bootstrap to create UI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<fieldset class="scheduler-border"> <legend class="scheduler-border">Pay</legend> <form class="form-inline" method="post" action="cc_response.php"> <input id="fname" tabindex="1" maxlength="20" size="20" name="fname" type="hidden"/> <input id="lname" tabindex="2" maxlength="12" size="12" name="lname" type="hidden" autocomplete="off" value="CUST001"> <input id="country" tabindex="4" maxlength="12" size="12" type="hidden" name="country"> <input id="cc" tabindex="4" maxlength="12" size="12" type="hidden" name="cc" value="****"> <div class="form-group"> <label for="exampleInputName2">Amount : </label> <div class="input-group"> <span class="input-group-addon">$</i> </span> <input type="text" class="form-control" aria-label="Amount (to the nearest dollar)" name="amount" id="amount"> </div> </div> <button type="submit" id="TXN_AMOUNT" name="TXN_AMOUNT" class="btn btn-primary"><i class="fa fa-credit-card"" aria-hidden="true"></i> Pay</button> </form> </fieldset> |
I have created only amount text box, you need to add cc input into this form.I have added cc_response.php
file that will handle payment request and process payment and display payment response.
Create method to payment using Creditcard
I am taking sample of credit card to get amount and payment using authorize.net gateway.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
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 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, Its help you.