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 :
- Error : You need to install the imagick extension to use this back end
- Force Redirect to www From Non-www Using htaccess In Laravel
- MongoDB Database Connection In Laravel
- laravel cache clear command
- could not find driver (SQL: select count(*) as aggregate from "users" where "email" = admin@gmail.com)
- Laravel pluck method example | Laravel Pluck
- REST API Authentication With Laravel Passport - Webappfix
- How To Insert Current Datetime In Database Laravel
- How To Get Current Request Method Post Patch Get In Laravel - PHP
- Laravel One to One Eloquent Relationship Tutorial