Step-by-Step Guide: Integrating Authorize.net Payment Gateway with Laravel 10
Authorize.net is a widely used payment gateway that enables businesses to process online payments securely. Laravel is a robust PHP framework that allows developers to create web applications quickly and efficiently. In this article, we’ll cover how to integrate Authorize.net with Laravel 10 and create a payment gateway for your application.
Step 1: Install Laravel 10
Before we begin integrating Authorize.net with Laravel, make sure you have Laravel 10 installed on your machine. You can install Laravel using Composer by running the following command:
composer create-project --prefer-dist laravel/laravel myapp
Step 2: Install Authorize.net PHP SDK
After installing Laravel 10, you need to install the Authorize.net PHP SDK, which is used to interact with the Authorize.net API. You can install the SDK via Composer by running the following command:
composer require authorize-net/authorizenet
Step 3: Set up Environment Variables
After installing the SDK, we need to configure it by providing the Authorize.net API credentials. In Laravel, we use the .env
file to store our application's configuration. Add the following lines to your .env
file:
AUTHORIZENET_API_LOGIN_ID=<your_api_login_id>
AUTHORIZENET_TRANSACTION_KEY=<your_transaction_key>
Step 4: Create a Payment Form
The next step is to create a payment form that will collect the customer’s payment details. We will create a simple payment form with Laravel’s Blade template engine. In your application’s resources/views
directory, create a file named payment_form.blade.php
with the following code:
<form action="/process-payment" method="POST">
@csrf
<div>
<label for="card_number">Card Number</label>
<input type="text" id="card_number" name="card_number">
</div>
<div>
<label for="expiration_date">Expiration Date</label>
<input type="text" id="expiration_date" name="expiration_date">
</div>
<div>
<label for="cvv">CVV</label>
<input type="text" id="cvv" name="cvv">
</div>
<div>
<button type="submit">Submit Payment</button>
</div>
</form>
Step 5: Process Payment
After the customer submits the payment form, we need to process the payment using the Authorize.net SDK. In Laravel, we can create a controller method that handles the payment processing. In your application’s app/Http/Controllers
directory, create a file named PaymentController.php
with the following code:
use Illuminate\Http\Request;
use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;
class PaymentController extends Controller
{
public function processPayment(Request $request)
{
$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("10.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 "Payment successful!";
} else {
return "Payment failed: " . $tresponse->getResponseReasonText();
}
} else {
return "Payment failed: " . $response->getMessages()->getMessage()[0]->getText();
}
}
}
In the above code, we create a new PaymentController
class with a processPayment
method that handles the payment processing. The method retrieves the credit card information from the payment form and creates a new instance of the Authorize.net SDK's MerchantAuthenticationType
, CreditCardType
, and PaymentType
classes. We then create a new TransactionRequestType
object and set the transaction type, payment amount, and payment information. Finally, we create a new CreateTransactionRequest
object, set the merchant authentication, reference ID, and transaction request, and pass it to a new instance of the CreateTransactionController
. We then execute the controller with the sandbox environment and check the response for any errors.
Step 6: Update Routes
Now that we have created a payment form and a payment processing method, we need to update our application’s routes to handle the requests. In your application’s routes/web.php
file, add the following route:
Route::post('/process-payment', [PaymentController::class, 'processPayment'])->name('process.payment');
Step 7: Create Views
Finally, we need to create views for the payment form and payment confirmation. In Laravel, we can create a resources/views
directory and add two new files named payment.blade.php
and payment-confirmation.blade.php
.
In payment.blade.php
, add the following code:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Payment Form</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form method="POST" action="{{ route('process.payment') }}">
@csrf
<div class="form-group row">
<label for="card_number" class="col-md-4 col-form-label text-md-right">{{ __('Card Number') }}</label>
<div class="col-md-6">
<input id="card_number" type="text" class="form-control @error('card_number') is-invalid @enderror" name="card_number" required autocomplete="off" maxlength="16">
@error('card_number')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="expiration_date" class="col-md-4 col-form-label text-md-right">{{ __('Expiration Date (MM/YY)') }}</label>
<div class="col-md-6">
<input id="expiration_date" type="text" class="form-control @error('expiration_date') is-invalid @enderror" name="expiration_date" required autocomplete="off" maxlength="5">
@error('expiration_date')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row">
<label for="cvv" class="col-md-4 col-form-label text-md-right">{{ __('CVV') }}</label>
<div class="col-md-6">
<input id="cvv" type="text" class="form-control @error('cvv') is-invalid @enderror" name="cvv" required autocomplete="off" maxlength="4">
@error('cvv')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>
</div>
<div class="form-group row mb-0">
<div class="col-md-6 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Pay') }}
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
In payment-confirmation.blade.php, add the following code:
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Payment Confirmation</div>
<div class="card-body">
<h5 class="card-title">Thank you for your payment!</h5>
<p class="card-text">Your payment of ${{ $amount / 100 }} was successful.</p>
</div>
</div>
</div>
</div>
</div>
@endsection
Step 8: Test the Payment Process
To test the payment process, we can navigate to the payment form at http://localhost:8000/payment
and enter some test credit card details. We can use the following test card numbers provided by Authorize.net:
- Visa: 4111111111111111
- MasterCard: 5424000000000015
- American Express: 370000000000002
- Discover: 6011000000000012
For the expiration date, we can use any future date and for the CVV code, we can use any 3 or 4 digit number.
Once we submit the payment form, we should be redirected to the payment confirmation page and see the message “Thank you for your payment! Your payment of $x.xx was successful.”
Conclusion
In this article, we learned how to integrate the Authorize.net payment gateway with a Laravel 10 application. We covered how to install and configure the Authorize.net PHP SDK, how to create a payment form and process payments, and how to handle payment confirmation. With this knowledge, you should be able to easily add payment functionality to your Laravel application and securely process credit card transactions.
part 2 ===> https://medium.com/p/de25714b40a7