PhonePe Payment Gateway Integration in Laravel 12: In this tutorial, you will learn how to integrate the PhonePe payment gateway in Laravel 12 with payment request, callback URL, and transaction status verification.
This integration allows users to pay using UPI, debit card, credit card, and wallet directly from your Laravel application.
What You Will Build
- Create PhonePe payment request
- Redirect users to payment page
- Handle payment response
- Verify payment transaction status
- Store payment response in database
Step 1. Install Laravel
composer create-project laravel/laravel phonepe-payment cd phonepe-payment
Step 2. Add PhonePe Credentials
File: .env
PHONEPE_MERCHANT_ID=YOUR_MERCHANT_ID PHONEPE_SALT_KEY=YOUR_SALT_KEY PHONEPE_SALT_INDEX=1 PHONEPE_ENV=UAT
Step 3. Create Payment Controller
php artisan make:controller PhonePeController
File: app/Http/Controllers/PhonePeController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class PhonePeController extends Controller
{
public function payment()
{
$merchantTransactionId = uniqid();
$payload = [
"merchantId" => env('PHONEPE_MERCHANT_ID'),
"merchantTransactionId" => $merchantTransactionId,
"merchantUserId" => "MUID123",
"amount" => 10000,
"redirectUrl" => url('/phonepe-response'),
"redirectMode" => "POST",
"callbackUrl" => url('/phonepe-response'),
"mobileNumber" => "9999999999",
"paymentInstrument" => [
"type" => "PAY_PAGE"
]
];
$encode = base64_encode(json_encode($payload));
$string = $encode."/pg/v1/pay".env('PHONEPE_SALT_KEY');
$sha256 = hash('sha256', $string);
$finalXHeader = $sha256.'###'.env('PHONEPE_SALT_INDEX');
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'X-VERIFY' => $finalXHeader
])->post(
'https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay',
[
'request' => $encode
]
);
$result = $response->json();
return redirect(
$result['data']['instrumentResponse']['redirectInfo']['url']
);
}
}
Step 4. Add Routes
File: routes/web.php
use App\Http\Controllers\PhonePeController;
Route::get('/phonepe-payment', [PhonePeController::class, 'payment']);
Route::post('/phonepe-response', [PhonePeController::class, 'response']);
Step 5. Handle Payment Response
File: app/Http/Controllers/PhonePeController.php
public function response(Request $request)
{
$input = $request->all();
if (isset($input['code']) && $input['code'] == 'PAYMENT_SUCCESS') {
return response()->json([
'success' => true,
'message' => 'Payment Successful',
'data' => $input
]);
} else {
return response()->json([
'success' => false,
'message' => 'Payment Failed',
'data' => $input
]);
}
}
Step 6. Verify Transaction Status
File: app/Http/Controllers/PhonePeController.php
public function checkStatus($transactionId)
{
$merchantId = env('PHONEPE_MERCHANT_ID');
$string = "/pg/v1/status/".$merchantId."/".$transactionId.env('PHONEPE_SALT_KEY');
$sha256 = hash('sha256', $string);
$xVerify = $sha256.'###'.env('PHONEPE_SALT_INDEX');
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'X-VERIFY' => $xVerify,
'X-MERCHANT-ID' => $merchantId
])->get(
'https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/'.$merchantId.'/'.$transactionId
);
return $response->json();
}
Step 7. Add Status Route
File: routes/web.php
Route::get('/phonepe-status/{id}', [PhonePeController::class, 'checkStatus']);
Step 8. Test Payment
http://127.0.0.1:8000/phonepe-payment
Laravel will redirect users to the PhonePe payment page where they can complete payment using UPI, cards, or wallet.
How It Works
- Laravel creates payment payload
- PhonePe API receives payment request
- User completes payment on PhonePe
- PhonePe sends response to callback URL
- Laravel verifies payment transaction
Response Example
{
"success": true,
"code": "PAYMENT_SUCCESS",
"message": "Payment Successful",
"transactionId": "TXN123456"
}
Real Use Case
- Ecommerce websites
- Subscription billing systems
- Recharge applications
- Course selling platforms
Conclusion
You have successfully integrated PhonePe Payment Gateway in Laravel 12 with payment request, callback handling, and transaction verification.
This integration can be extended for production-ready ecommerce and billing applications.
We always thanks to you for reading our blogs.
Dharmesh Chauhan
(Swapinfoway Founder)Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us
Haresh Chauhan
(Co-Founder)We Are Also Recommending You :
- How To Convert HTML To Image/PDF Using Snappy In Laravel 9
- Laravel 9 Authorize.Net Payment Gateway Integration
- Arr::add() | Laravel Helper Function
- Laravel 12 REST API Tutorial for Beginners (Step-by-Step Guide 2026)
- How To Add Active Class To Menu Item In Laravel
- Trying to access array offset on value of type int
- All Know About Different Root Path Of The Laravel Application With Example
- How to Integrate PayUMoney Payment Gateway in Laravel 9
- PayPal Payment Gateway Integration in Laravel 12 (2026) Step-by-Step Guide
- How to Get Browser Name and Version in Laravel