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.


Before moving ahead you must have the client ID and client Secret received from the phonepay


First all of you have create your phonepay account by clicking on this URL - https://business.phonepe.com/register



After the creation of account you will able to see the dashboard, in the right side there will be developer option click on that to get the client ID and secret.




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 Client ID And Client Secret


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 CLIENT_SECERT, and CLIENT_ID to .env:

CLIENT_SECRET=YOUR_SECRET
CLIENT_ID=YOUR_CLIENT_ID


Step 4: Create a Payment Request

Inside your controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

class PhonePePaymentController extends Controller
{
public function createPayment()
{
$response = Http::asForm()->post('https://api-preprod.phonepe.com/apis/pg-sandbox/v1/oauth/token', [
'client_id' => env('CLIENT_ID'),
'client_version' => '1',
'client_secret' => env('CLIENT_SECRET'),
'grant_type' => 'client_credentials',
]);

$data = $response->json();

if ($response->successful()) {
$accessToken = $data['access_token'] ?? null;

$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Authorization' => 'O-Bearer ' . $accessToken,
])->post('https://api-preprod.phonepe.com/apis/pg-sandbox/checkout/v2/pay', [
'merchantOrderId' => 'TX123rrty34432785',
'amount' => 1000,
'paymentFlow' => [
'type' => 'PG_CHECKOUT',
'message' => 'Payment message used for collect requests',
'merchantUrls' => [
'redirectUrl' => route('phonepe.success', ['id' => 'TX123rrty34432785']),
],
],
]);

$data = $response->json();
return redirect($data['redirectUrl']);
} else {
\Log::error('PhonePe token request failed', ['response' => $data]);
}
}

public function success($id)
{
dd('Payment successful for order ID: ' . $id);
}
}


Step 5: Define Routes

In routes/web.php:

<?php

use Illuminate\Support\Facades\Route;

Route::get('/phonepe/create-payment', [App\Http\Controllers\PhonePePaymentController::class, 'createPayment'])
->name('phonepe.createPayment');
Route::get('/phonepe/success/{id}', [App\Http\Controllers\PhonePePaymentController::class, 'success'])
->name('phonepe.success');


Step 6: Testing the flow

first run cache clear command


php artisan cache:clear


then run the development mode command

php artisan serve


Then visit the below URL in your browser, it will redirect you to the payment page directly

http://localhost:8000/phonepe/create-payment


after visiting this URL it will be redirected to the below like page


On the successful payment you can see the screen something like the below one





Project GIT URL - https://github.com/codehunger-team/integrate-phonepay-payment-gateway-in-laravel-12


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.