How to integrate Authorize Net into Laravel ?

April 16, 20222 minutesuserVishal Ribdiya
How to integrate Authorize Net into Laravel ?

In this tutorial, we are going to see how we can implement the authorized hosted payment gateway by using their UI and components and take payments from users via authorized net using Laravel.

Create HTML form as like below code :


{{ Form::open(array('url' => '')) }}

Form::hidden('token', '{{$token}}');

Form::submit('Click Me!');

{{ Form::close() }}

You must have to pass $token to form, we will see below how we can generate that token.


 public function onboard() {

    $token = $this->getAnAcceptPaymentPage();

    return view('authorize', compact('token'));

 public function getAnAcceptPaymentPage()
    $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();

    $refId = 'ref' . time();

    $transactionRequestType = new AnetAPI\TransactionRequestType();

    $setting1 = new AnetAPI\SettingType();
    $setting1->setSettingValue("{\"text\": \"Pay\"}");

    $setting2 = new AnetAPI\SettingType();
    $setting2->setSettingValue("{\"show\": false}");

    $setting3 = new AnetAPI\SettingType();
        "{\"url\": \"\".$refID, \"cancelUrl\": \"\", \"showReceipt\": true}"

    // Build transaction request
    $request = new AnetAPI\GetHostedPaymentPageRequest();


    $controller = new AnetController\GetHostedPaymentPageController($request);
    $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);

    if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {

    } else {
        echo "ERROR :  Failed to get hosted payment page token\n";
        $errorMessages = $response->getMessages()->getMessage();
        echo "RESPONSE : " . $errorMessages[0]->getCode() . "  " .$errorMessages[0]->getText() . "\n";
    return $response->getToken();


Now create routes into web.php as specified below.


Route::get('authorize-onboard', [\App\Http\Controllers\AuthorizePaymentController::class, 'onboard'])->name('authorize.init');

Route::get('authorize-success', [\App\Http\Controllers\AuthorizePaymentController::class, 'success']);

How it's going to work ?? (flow)

So initially we will call the route that contains that authorization form and also contains the payment information.

Here we are generating token before, generally, it should be generated from the payment screen.

The token will contains the payment information so while generating it make sure you are passing all the details properly.

Now when you submit the form it will redirect you to the authorized checkout page from where users can do payments and again redirect to the success screen.

Once Payment is done successfully you will be redirected to the success route URL with the RefID which is basically the transaction ID, and you can perform related actions on success action.

Hope it will help.

Our Laravel Products