How to Integrate Airpay with Laravel: Complete Guide with Code & Database Examples
Introduction
Integrate Airpay with Laravel seamlessly to enable secure online payments. This guide provides step-by-step instructions, including API setup, database configurations, and Laravel implementation. By the end, you will have a fully functional Airpay payment gateway integrated into your Laravel application.
Master JavaScript Browser Redirection: 5 Simple Steps to Improve User Experience and SEO
Table of Contents
- What is Airpay?
- Why Use Airpay with Laravel?
- Prerequisites
- Setting Up Airpay Merchant Account
- Installing Laravel and Required Packages
- Configuring Airpay API in Laravel
- Creating the Payment Controller
- Creating Routes for Payment Processing
- Creating the Payment View
- Handling Payment Responses
- Storing Transactions in the Database
- Testing the Integration
- Deploying to Production
- Troubleshooting Common Issues
- Conclusion & FAQs
1. What is Airpay?
Airpay is a secure payment gateway that enables businesses to accept payments via multiple methods, including credit/debit cards, net banking, and UPI. It provides an API that developers can integrate into their applications for seamless transactions.
2. Why Use Airpay with Laravel?
- Secure and reliable payment processing
- Easy API integration
- Supports multiple payment methods
- Provides real-time transaction tracking
3. Prerequisites
Before Integrate Airpay with Laravel, ensure you have the following:
- A Laravel application (Laravel 9 or 10 recommended)
- Composer installed
- An Airpay merchant account
- Basic knowledge of Laravel controllers, routes, and views
4. Setting Up Airpay Merchant Account
To Integrate Airpay with Laravel, you first need to create a merchant account:
- Sign up at Airpay’s official website.
- Complete the required KYC verification.
- Obtain API credentials (Merchant ID, Username, Password, Secret Key).
5. Installing Laravel and Required Packages
If you haven’t set up Laravel yet, run the following command:
composer create-project --prefer-dist laravel/laravel airpay-integration
Next, install GuzzleHTTP for handling API requests:
composer require guzzlehttp/guzzle
6. Configuring Airpay API in Laravel
Open .env
and add your Airpay credentials:
AIRPAY_MERCHANT_ID=your_merchant_id
AIRPAY_USERNAME=your_username
AIRPAY_PASSWORD=your_password
AIRPAY_SECRET_KEY=your_secret_key
7. Creating the Payment Controller
Generate a new controller:
php artisan make:controller PaymentController
Edit PaymentController.php
:
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class PaymentController extends Controller
{
public function initiatePayment()
{
$client = new Client();
$response = $client->post('https://payments.airpay.co.in/api/pay', [
'form_params' => [
'merchant_id' => env('AIRPAY_MERCHANT_ID'),
'username' => env('AIRPAY_USERNAME'),
'password' => env('AIRPAY_PASSWORD'),
'secret_key' => env('AIRPAY_SECRET_KEY'),
'amount' => 100,
'currency' => 'INR',
'return_url' => route('payment.success'),
]
]);
return redirect(json_decode($response->getBody())->payment_url);
}
}
8. Creating Routes for Payment Processing
Edit routes/web.php
:
use App\Http\Controllers\PaymentController;
Route::get('/pay', [PaymentController::class, 'initiatePayment'])->name('payment.initiate');
Route::get('/payment-success', function() {
return 'Payment successful!';
})->name('payment.success');
9. Creating the Payment View
Create resources/views/payment.blade.php
:
<form action="{{ route('payment.initiate') }}" method="POST">
@csrf
<button type="submit">Pay with Airpay</button>
</form>
10. Handling Payment Responses
Airpay redirects the user to payment.success
upon successful payment. You can log the response for debugging:
Route::get('/payment-success', function(Request $request) {
Log::info($request->all());
return 'Payment successful!';
})->name('payment.success');
11. Storing Transactions in the Database
Create a migration:
php artisan make:migration create_transactions_table --create=transactions
Edit the migration file:
public function up()
{
Schema::create('transactions', function (Blueprint $table) {
$table->id();
$table->string('transaction_id');
$table->decimal('amount', 10, 2);
$table->string('status');
$table->timestamps();
});
}
Run the migration:
php artisan migrate
12. Testing the Integration
Use Laravel’s built-in server:
php artisan serve
Visit http://127.0.0.1:8000/pay
and complete a test payment.
13. Deploying to Production
Ensure you use live API credentials in .env
, enable HTTPS, and monitor transactions.
14. Troubleshooting Common Issues
- API Authentication Error: Double-check your credentials.
- Invalid Redirect URL: Ensure the return URL is correctly configured in Airpay settings.
- Transaction Not Recorded: Confirm that database migrations ran successfully.
15. Conclusion
Integrate Airpay with Laravel is straightforward and enables seamless online transactions. By following this guide, you now have a functional payment gateway setup in your Laravel application. Integrate Airpay with Laravel.
FAQs
- How do I integrate Airpay with Laravel?
- Follow the step-by-step guide above for seamless integration.
- Can I use Airpay with Laravel Jetstream?
- Yes, just integrate the routes and controllers accordingly.
- How can I refund a transaction?
- Airpay provides refund APIs; check their documentation for details.
- Does Airpay support international payments?
- Yes, but you need to enable international transactions.
- What if a payment fails?
- Log errors and implement retry mechanisms to handle failed payments.
Recent Comments