How To Integrate Phonepe Payment Gateway In Laravel A Step By Step Guide

image
image
image
image
image
image
image
image
How to Integrate PhonePe Payment Gateway in Laravel: A Step-by-Step Guide

How to Integrate PhonePe Payment Gateway in Laravel: A Step-by-Step Guide

Integrating a reliable payment gateway is crucial for any online business. With the increasing popularity of UPI and wallet-based payments in India, PhonePe has emerged as one of the top choices for developers and merchants alike. In this blog, we’ll walk through how to integrate the PhonePe payment gateway into a Laravel application, enabling you to accept payments via UPI, debit/credit cards, and PhonePe wallet.

Whether you're building an eCommerce store, a donation platform, or a service booking site, this guide will help you connect your Laravel backend with PhonePe’s secure payment APIs.


Prerequisites

Before you start, make sure you have:

  1. Laravel 8 or newer installed
  2. Composer set up
  3. A registered PhonePe Merchant account with access to the PhonePe API documentation
  4. Your unique Merchant ID, Salt Key, and Salt Index


Step-by-Step Integration of PhonePe Payment Gateway in Laravel


Step 1: Setup Your Laravel Project

composer create-project laravel/laravel phonepe-integration

cd phonepe-integration


Step 2: Create Payment Controller

Run the following command to generate a controller:

php artisan make:controller PhonePePaymentController


Step 3: Configure PhonePe Payment Parameters

Add your Merchant ID, Salt Key, and Salt Index to .env:

PHONEPE_MERCHANT_ID=YOUR_MERCHANT_ID
PHONEPE_SALT_KEY=YOUR_SALT_KEY
PHONEPE_SALT_INDEX=YOUR_SALT_INDEX
PHONEPE_BASE_URL=https://api.phonepe.com/apis/hermes


Then, add them to config/services.php:

'phonepe' => [
'merchant_id' => env('PHONEPE_MERCHANT_ID'),
'salt_key' => env('PHONEPE_SALT_KEY'),
'salt_index' => env('PHONEPE_SALT_INDEX'),
'base_url' => env('PHONEPE_BASE_URL'),
],


Step 4: Create a Payment Request

Inside your controller:

use Illuminate\Support\Facades\Http;

public function initiatePayment(Request $request)
{
$merchantId = config('services.phonepe.merchant_id');
$saltKey = config('services.phonepe.salt_key');
$saltIndex = config('services.phonepe.salt_index');

$payload = [
'merchantId' => $merchantId,
'transactionId' => uniqid(),
'amount' => 10000, // Amount in paise
'merchantUserId' => 'user123',
'redirectUrl' => route('phonepe.callback'),
'redirectMode' => 'POST',
'paymentInstrument' => [
'type' => 'PAY_PAGE'
],
];

$jsonPayload = base64_encode(json_encode($payload));
$checksum = hash('sha256', $jsonPayload . "/pg/v1/pay" . $saltKey) . "###" . $saltIndex;

$response = Http::withHeaders([
'Content-Type' => 'application/json',
'X-VERIFY' => $checksum,
])->post(config('services.phonepe.base_url') . '/pg/v1/pay', [
'request' => $jsonPayload
]);

$responseData = $response->json();

return redirect()->away($responseData['data']['instrumentResponse']['redirectInfo']['url']);
}


Step 5: Handle the Callback

public function handleCallback(Request $request)
{
$transactionId = $request->input('transactionId');

$checksum = hash('sha256', "/pg/v1/status/{$transactionId}" . config('services.phonepe.salt_key')) . "###" . config('services.phonepe.salt_index');

$response = Http::withHeaders([
'Content-Type' => 'application/json',
'X-VERIFY' => $checksum,
])->get(config('services.phonepe.base_url') . "/pg/v1/status/{$transactionId}");

$paymentStatus = $response->json();

// Handle success or failure
if ($paymentStatus['success']) {
// Mark order as paid
} else {
// Log failure
}

return response()->json($paymentStatus);
}


Step 6: Define Routes

In routes/web.php:

Route::get('/pay', [PhonePePaymentController::class, 'initiatePayment'])->name('phonepe.pay');
Route::post('/callback', [PhonePePaymentController::class, 'handleCallback'])->name('phonepe.callback');


Conclusion

Integrating the PhonePe payment gateway in Laravel is straightforward once you understand the API workflow and secure checksum generation. By following the steps outlined above, you can provide your users with a fast and secure payment experience via UPI, wallets, and cards.

Be sure to thoroughly test your integration in the sandbox environment before going live, and always handle responses securely to prevent fraud or data leakage.