Hello, laravel web developers! In this article, we'll see how to integrate authorize.net payment gateway in laravel 11. In laravel 11, we'll integrate authorize.net payment gateway. Authorize.Net, A Visa Solution is a United States-based payment gateway service provider, allowing merchants to accept credit card and electronic check payments through their website and over an Internet Protocol connection.
It offers secure, reliable payment processing for online and in-person transactions. Businesses can integrate it with their websites or point-of-sale systems to manage payments easily.
Authorize.net also provides fraud detection tools, recurring billing options, and customer support to ensure smooth and safe transactions for both merchants and customers.
Here, we'll use the authorizenet/authorizenet composer library for the authorize.net payment gateway in laravel 11.
Laravel 11 Integrate Authorize.net Payment Gateway
In this step, we'll install laravel 11 application using the following command.
composer create-project laravel/laravel laravel-11-example
Then, we'll install authorizenet/authorizenet composer package using the following command.
composer require authorizenet/authorizenet
Now, we required authorize.net login id and transaction key.
Now, we'll add API Login ID and Transaction Key in .env file.
.env
AUTHORIZENET_API_LOGIN_ID=xxxx
AUTHORIZENET_TRANSACTION_KEY=xxxx
Then, we'll create a controller and add the following code to that file.
app/Http/Controllers/AuthorizeNetController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
class AuthorizeNetController extends Controller
{
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function index(): View
{
return view('authorize-net');
}
/**
* success response method.
*
* @return \Illuminate\Http\Response
*/
public function paymentPost(Request $request): RedirectResponse
{
$this->validate($request, [
'card_number' => 'required',
'expiration_date' => 'required',
'cvv' => 'required'
]);
$cardNumber = $request->input('card_number');
$expirationDate = $request->input('expiration_date');
$cvv = $request->input('cvv');
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(env('AUTHORIZENET_API_LOGIN_ID'));
$merchantAuthentication->setTransactionKey(env('AUTHORIZENET_TRANSACTION_KEY'));
$creditCard = new AnetAPI\CreditCardType();
$creditCard->setCardNumber($cardNumber);
$creditCard->setExpirationDate($expirationDate);
$creditCard->setCardCode($cvv);
$payment = new AnetAPI\PaymentType();
$payment->setCreditCard($creditCard);
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount("100.00");
$transactionRequestType->setPayment($payment);
$request = new AnetAPI\CreateTransactionRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId("ref" . time());
$request->setTransactionRequest($transactionRequestType);
$controller = new AnetController\CreateTransactionController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
if ($response != null) {
$tresponse = $response->getTransactionResponse();
if ($tresponse != null & $tresponse->getResponseCode() == "1") {
return back()->with('success', 'Payment successful!');
} else {
return back()->with('error', "Payment failed: ");
}
} else {
return back()->with('error', "Payment failed: " . $response->getMessages()->getMessage()[0]->getText());
}
}
}
Next, we'll define the routes into the web.php file.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthorizeNetController;
Route::get('authorize/payment', [AuthorizeNetController::class, 'index']);
Route::post('authorize/payment', [AuthorizeNetController::class, 'paymentPost'])->name('authorize.payment');
Then, we'll create a blade file and add the following HTML code to that file.
resources/views/authorize-net.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Integrate Authorize.net Payment Gateway</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6 offset-md-3">
<div class="card mt-5">
<h3 class="card-header p-3">Laravel 11 Integrate Authorize.net Payment Gateway - Techsolutionstuff</h3>
<div class="card-body">
@session('success')
<div class="alert alert-success" role="alert">
{{ $value }}
</div>
@endsession
@session('error')
<div class="alert alert-danger" role="alert">
{{ $value }}
</div>
@endsession
<form method="POST" action="{{ route('authorize.payment') }}">
@csrf
<div class="row">
<div class="col-md-12">
<label for="card_number" class="col-form-label text-md-right">{{ __('Card Number') }}</label>
<input id="card_number" type="text" class="form-control @error('card_number') is-invalid @enderror" name="card_number" required autocomplete="off" maxlength="16" placeholder="4111111111111111">
@error('card_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="row">
<div class="col-md-6">
<label for="expiration_date" class="col-form-label text-md-right">{{ __('Expiration Date (MM/YY)') }}</label>
<input id="expiration_date" type="text" class="form-control @error('expiration_date') is-invalid @enderror" name="expiration_date" required autocomplete="off" maxlength="5" placeholder="12/27">
@error('expiration_date')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
<div class="col-md-6">
<label for="cvv" class="col-form-label text-md-right">{{ __('CVV') }}</label>
<input id="cvv" type="text" class="form-control @error('cvv') is-invalid @enderror" name="cvv" required autocomplete="off" maxlength="4" placeholder="1234">
@error('cvv')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="row mt-3">
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-success">
{{ __('Make Payment') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Now, we'll run the laravel 11 application using the following command.
php artisan serve
You might also like:
In this article, we will see that laravel 9 has many through relationship example. hasManyThrough relationship is diffic...
Apr-04-2022
In this article, we will see a laravel 9 firebase push notification, a firebase notification through you can notify...
Sep-20-2022
In this article, we will see the laravel 9 and laravel 10 multiple where condition query example. We will learn&nbs...
Sep-30-2022
In this article, we will see how to create a multi-language website in laravel. In this example, you can understand...
Nov-09-2020