Instamojo payment gateway provides php package which allow users to integrate it into Laravel 11 application.
Let’s start by creating an account in Instamojo and install its package to setup payment collection in Laravel application using this package:
Step 1: Create Account in InstaMojo
Step 2: Install Instamojo PHP SDK
Install the Instamojo PHP SDK using the following Composer command:
composer require instamojo/instamojo-php
Step 3: Configure Instamojo API
Edit .env
file, and configure your instamojo app API key and Auth token in it:
INSTAMOJO_API_KEY=your_api_key
INSTAMOJO_AUTH_TOKEN=your_auth_token
INSTAMOJO_URL=https://www.instamojo.com/api/1.1/ // Use https://test.instamojo.com/api/1.1/ for testing
Step 4: Create PaymentController
Run the following command to create a controller to handle the payment logic:
php artisan make:controller PaymentController
Edit app/Http/Controllers/PaymentController.php
file, and add some payment collection method into it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Instamojo\Instamojo;
class PaymentController extends Controller
{
public function initiatePayment()
{
return view('payment');
}
public function pay(Request $request)
{
$api = new Instamojo(env('INSTAMOJO_API_KEY'), env('INSTAMOJO_AUTH_TOKEN'), env('INSTAMOJO_URL'));
try {
$response = $api->paymentRequestCreate(array(
"purpose" => "Payment for XYZ",
"amount" => $request->amount,
"buyer_name" => $request->name,
"send_email" => true,
"email" => $request->email,
"phone" => $request->phone,
"redirect_url" => url('payment/success')
));
return redirect($response['longurl']);
} catch (Exception $e) {
return back()->with('error', 'Something went wrong. Please try again.');
}
}
public function paymentSuccess(Request $request)
{
$api = new Instamojo(env('INSTAMOJO_API_KEY'), env('INSTAMOJO_AUTH_TOKEN'), env('INSTAMOJO_URL'));
try {
$response = $api->paymentRequestStatus($request->payment_request_id);
if ($response['status'] == 'Completed') {
// Payment was successful
return view('payment-success');
} else {
return back()->with('error', 'Payment not successful. Please try again.');
}
} catch (Exception $e) {
return back()->with('error', 'Something went wrong. Please try again.');
}
}
}
Step 5: Add Routes
Open routes/web.php
and add payment request routes:
use App\Http\Controllers\PaymentController;
Route::get('payment', [PaymentController::class, 'initiatePayment'])->name('payment.initiate');
Route::post('payment', [PaymentController::class, 'pay'])->name('payment.pay');
Route::get('payment/success', [PaymentController::class, 'paymentSuccess'])->name('payment.success');
Step 6: Create Blade Views
Create payment.blade.php
Blade view file for the payment form in resources/views/
directory:
<!DOCTYPE html>
<html>
<head>
<title>Integrate Instamojo Payment Gateway in Laravel 11 - ItcodStuff.com</title>
</head>
<body>
<h2>Instamojo Payment Collection Form in Laravel 11 - ItcodStuff.com</h2>
@if (session('error'))
<p style="color: red;">{{ session('error') }}</p>
@endif
<form action="{{ route('payment.pay') }}" method="POST">
@csrf
<label for="name">Name:</label>
<input type="text" id="name" name="name" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" required><br><br>
<label for="amount">Amount:</label>
<input type="number" id="amount" name="amount" required><br><br>
<button type="submit">Pay Now</button>
</form>
</body>
</html>
Create payment-success.blade.php
Blade view file for the payment success page in resources/views/
directory:
<!DOCTYPE html>
<html>
<head>
<title>Payment Success using Instamojo in Laravel - itcodStuff.com</title>
</head>
<body>
<h2>Payment Successful!</h2>
<p>Your payment has been received. Thank you!</p>
</body>
</html>
Step 7: Test the Application
Start the Laravel development server:
php artisan serve
To test Instamojo integration open browser and type http://127.0.0.1:8000/payment
url in it.