Authorize.Net Payment Gateway Integration in Laravel

Authorize.Net is no doubt one of the trusted and popular payment solutions on the Internet. If you are running an online store and wish to receive payments using credit/debit cards then Authorize.Net would be your choice. In this article, I show you how to integrate Authorize.Net payment gateway in your Laravel application.

For getting started you should have an account on Authorize.Net. You should first test your payments in sandbox mode and if everything works as expected then go for live mode. In order to accept payment using Authorize.Net, you need to grab your API credentials. Login to your sandbox account and you can get these API keys from Account->API Credentials & Keys.

Authorize.Net API Keys

On the next page, you will get your ‘API Login ID’ and create a new transaction key from the ‘Create New Key(s)’ section.

Authorize.Net API Login ID and Transaction Key

Basic Setup in Laravel to Accept Payment using Authorize.Net

When your customers purchase products you should store their payment details in the database. That being said, let’s create a table ‘payments’ for storing transaction details.

php artisan make:migration create_payments_table

In the generated migration file add the below code to the up method.

public function up()
    Schema::create('payments', function (Blueprint $table) {
        $table->float('amount', 10, 2);

Next, run the migrate command which will add this table to your database.

php artisan migrate

To interact with the ‘payments’ table you will need to have a model ‘Payment’. Create it using the command:

php artisan make:model Payment

You have copied Authorize.Net API credentials. Let’s put it in your .env file as follows.


Make sure to replace placeholders with the actual values. After this, clear your configuration cache.

php artisan config:clear

While processing payment, in Laravel you are required to define routes. So let’s define routes for it.


Route::get('payment', 'PaymentController@index');
Route::post('charge', 'PaymentController@charge');

Authorize.Net Payment Gateway Integration in Laravel

Gone are the days when developers needed to read API documentation of payment gateway providers to integrate their services. Omnipay is a payment processing library for PHP and they do all hard work to make our developers life easy. They provide support for multi-payment gateway services. We are also going to use Omnipay for the integration of Authorize.Net in Laravel. Along with Omnipay we also use their other library for the implementation of Authorize.Net API.

Having said that, install these 2 libraries using the Composer command:

composer require league/omnipay:^3 "academe/omnipay-authorizenetapi: ~3.0"

Now, create a controller that will call the view file, process the payment and store transaction details to the database.

php artisan make:controller PaymentController

To this controller, add the code below as it is which will handle all payment-related stuff.


namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Omnipay\Omnipay;
use App\Payment;

class PaymentController extends Controller
    public $gateway;
    public function __construct()
        $this->gateway = Omnipay::create('AuthorizeNetApi_Api');
        $this->gateway->setTestMode(true); //comment this line when move to 'live'

    public function index()
        return view('payment');

    public function charge(Request $request)
        try {
            $creditCard = new \Omnipay\Common\CreditCard([
                'number' => $request->input('cc_number'),
                'expiryMonth' => $request->input('expiry_month'),
                'expiryYear' => $request->input('expiry_year'),
                'cvv' => $request->input('cvv'),

            // Generate a unique merchant site transaction ID.
            $transactionId = rand(100000000, 999999999);

            $response = $this->gateway->authorize([
                'amount' => $request->input('amount'),
                'currency' => 'USD',
                'transactionId' => $transactionId,
                'card' => $creditCard,

            if($response->isSuccessful()) {

                // Captured from the authorization response.
                $transactionReference = $response->getTransactionReference();

                $response = $this->gateway->capture([
                    'amount' => $request->input('amount'),
                    'currency' => 'USD',
                    'transactionReference' => $transactionReference,

                $transaction_id = $response->getTransactionReference();
                $amount = $request->input('amount');

                // Insert transaction data into the database
                $isPaymentExist = Payment::where('transaction_id', $transaction_id)->first();

                    $payment = new Payment;
                    $payment->transaction_id = $transaction_id;
                    $payment->payer_email = $request->input('email');
                    $payment->amount = $request->input('amount');
                    $payment->currency = 'USD';
                    $payment->payment_status = 'Captured';

                return "Payment is successful. Your transaction id is: ". $transaction_id;
            } else {
                // not successful
                return $response->getMessage();
        } catch(Exception $e) {
            return $e->getMessage();

This controller grabs the amount, card details from the HTML form and processes the payment. On the successful payment, it stores all the transaction details in the ‘payments’ table.

Finally, create a view payment.blade.php and it will contain the following code.


<form action="{{ url('charge') }}" method="post">
    {{ csrf_field() }}
    <input type="email" name="email" placeholder="Enter Email" />
    <input type="text" name="amount" placeholder="Enter Amount" />
    <input type="text" name="cc_number" placeholder="Card Number" />
    <input type="text" name="expiry_month" placeholder="Month" />
    <input type="text" name="expiry_year" placeholder="Year" />
    <input type="text" name="cvv" placeholder="CVV" />
    <input type="submit" name="submit" value="Submit" />

When you submit this form, control goes to the charge function in the PaymentController and controller does the rest payment process. Click here to get dummy credit card numbers to test the sandbox payment. The user can also see their transaction on the Authorize.Net dashboard. At first, transactions listed as ‘Unsettled transactions’. Authorize.Net later settles the transaction on their own.

Authorize.Net Transaction

Make Payment Live

Once you are successfully tested your payments in the sandbox mode, you can switch to production mode. All you need to do is replace sandbox credentials with the production details in the .env file. Also comment the below line from the __construct method of PaymentController.

$this->gateway->setTestMode(true); //comment this line when move to 'live'

That’s all! I hope you understand Authorize.Net Payment Gateway Integration in Laravel. I would like to hear your thoughts or suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

1 thought on “Authorize.Net Payment Gateway Integration in Laravel

  1. Hello, I am Serena Martin and i liked your article alot you really did a great job. The article is Very Interesting and Informative at the same time, thanks for sharing this with us.

Leave a Reply

Your email address will not be published. Required fields are marked *