How to integrate Authorize Net into Laravel ?

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 :

authorize.blade.php

{{ Form::open(array('url' => 'https://test.authorize.net/payment/payment')) }}

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.

AuthorizeController.php

 public function onboard() {

    $token = $this->getAnAcceptPaymentPage();

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

 public function getAnAcceptPaymentPage()
 {
    $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
    $merchantAuthentication->setName(config('payments.authorize.login_id'));
    $merchantAuthentication->setTransactionKey(config('payments.authorize.transaction_key'));

    $refId = 'ref' . time();

    $transactionRequestType = new AnetAPI\TransactionRequestType();
    $transactionRequestType->setTransactionType("authCaptureTransaction");
    $transactionRequestType->setAmount("2050"); 

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

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

    $setting3 = new AnetAPI\SettingType();
    $setting3->setSettingName("hostedPaymentReturnOptions");
    $setting3->setSettingValue(
        "{\"url\": \"http://127.0.0.1:8000/authorize-success?refID\".$refID, \"cancelUrl\": \"http://127.0.0.1:8000/authorize-cancel\", \"showReceipt\": true}"
    );

    // Build transaction request
    $request = new AnetAPI\GetHostedPaymentPageRequest();
    $request->setMerchantAuthentication($merchantAuthentication);
    $request->setRefId($refId);
    $request->setTransactionRequest($transactionRequestType);

    $request->addToHostedPaymentSettings($setting1);
    $request->addToHostedPaymentSettings($setting2);
    $request->addToHostedPaymentSettings($setting3);

    $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.

web.php

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.