This package provides an easy way to integrate the PhonePe payment gateway into your Laravel application.
- Initiate payments via PhonePe.
- Check transaction statuses.
- Secure callback handling for payment notifications.
To get your api key go to PhonePe Official Website
Run the following command to install the package via Composer:
composer require zarenta/phonepe
Add Service provider to bootstrap/provider.php
file
PhonePe\LaravelPhonePeServiceProvider::class,
Publish the configuration file using the artisan command:
php artisan vendor:publish --tag=phonepe-config
This will publish a config/phonepe.php
file where you can set your PhonePe credentials.
Update bootstrap/app.php
to exclude the callback URL from CSRF verification. Add the following middleware configuration:
->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [
\App\Http\Middleware\HandleInertiaRequests::class,
\Illuminate\Http\Middleware\AddLinkHeadersForPreloadedAssets::class,
]);
$middleware->validateCsrfTokens(except: [
'phonepe/callback', // Exclude PhonePe callback route from CSRF
]);
})
In your .env
file, add the following variables with your PhonePe account details:
PHONEPE_MERCHANT_ID=your_merchant_id
PHONEPE_MERCHANT_USER_ID=your_merchant_user_id
PHONEPE_SALT_KEY=your_salt_key
PHONEPE_SALT_INDEX=your_salt_index
PHONEPE_CALLBACK_URL=https://yourdomain.com/phonepe/callback
PHONEPE_ENV=production # or sandbox for testing
The published configuration file config/phonepe.php
will have the following structure:
return [
'merchantId' => env('PHONEPE_MERCHANT_ID'),
'merchantUserId' => env('PHONEPE_MERCHANT_USER_ID'),
'saltKey' => env('PHONEPE_SALT_KEY'),
'saltIndex' => env('PHONEPE_SALT_INDEX'),
'callBackUrl' => env('PHONEPE_CALLBACK_URL'),
'env' => env('PHONEPE_ENV', 'sandbox'),
];
Here's an example of how to initiate a payment using the PhonePeGateway
:
use PhonePe\PhonePeGateway;
public function initiatePayment(Request $request)
{
$phonePe = new PhonePeGateway();
try {
$paymentUrl = $phonePe->makePayment(
$amount = 1000, // Amount in rupees
$redirectUrl = 'https://yourdomain.com/payment/success',
$merchantTransactionId = 'your_unique_transaction_id',
$phone = '9999999999',
$email = '[email protected]',
$shortName = 'Your Company',
$message = 'Payment for Order #1234'
);
return redirect($paymentUrl);
} catch (PhonePe\Exception\PhonePeException $e) {
return response()->json(['error' => $e->getMessage()], 400);
}
}
To check the transaction status, use the getTransactionStatus
method:
use PhonePe\PhonePeGateway;
public function checkTransactionStatus($transactionId)
{
$phonePe = new PhonePeGateway();
$status = $phonePe->getTransactionStatus(\request()->all());
if ($status) {
return response()->json(['status' => 'Transaction successful']);
} else {
return response()->json(['status' => 'Transaction failed or pending']);
}
}
Create a route to handle the PhonePe callback in your routes/web.php
:
Route::post('/phonepe/callback', [YourPaymentController::class, 'handlePhonePeCallback']);
In your controller, you can write logic to handle the callback and update the transaction status accordingly.
This package is open-source and licensed under the MIT License.