yogeshgupta/phonepe-laravel

Complete PhonePe Payment Gateway v2 integration for Laravel with OAuth, Webhook, Auto-Logging, Events & Facade
1,369
Install
composer require yogeshgupta/phonepe-laravel
Latest Version:v1.0.3
PHP:^8.1|^8.2|^8.3
License:MIT
Last Updated:Nov 26, 2025
Links: GitHub  ·  Packagist
Maintainer: Yogesh0610

PhonePe API v2 Laravel Integration

A Laravel package for integrating the PhonePe API v2 payment gateway, developed by Yogesh Gupta. This is a standalone implementation designed to simplify PhonePe payment gateway integration in Laravel applications, supporting the PhonePe checkout v2 API for secure and efficient transactions.

Features

  • Initiate payments using the PhonePe API v2.
  • Verify payment status with the PhonePe checkout v2 endpoint.
  • Support for UAT and production environments.
  • Token caching for efficient PhonePe API v2 calls.
  • Laravel configuration and facade for easy Laravel PhonePe integration.

Requirements

  • PHP >= 8.1
  • Laravel 9.x, 10.x, 11.x, or 12.x

Installation

  1. Install the package via Composer:
composer require yogeshgupta/phonepe-laravel
  1. Publish the configuration file:
php artisan vendor:publish --tag=phonepe-config

This will create a config/phonepe.php file in your Laravel project.

  1. Add your PhonePe credentials to your .env file:
PHONEPE_ENV=uat
PHONEPE_UAT_CLIENT_ID=SU2504041946024365502022
PHONEPE_UAT_CLIENT_VERSION=1
PHONEPE_UAT_CLIENT_SECRET=ae83cba2-07c0-43a9-b0c4-1d84c261fd12
PHONEPE_PROD_CLIENT_ID=your_prod_client_id
PHONEPE_PROD_CLIENT_VERSION=your_prod_client_version
PHONEPE_PROD_CLIENT_SECRET=your_prod_client_secret
PHONEPE_REDIRECT_URL=https://your-app.com/phonepe/process

Configuration

The configuration file (config/phonepe.php) allows you to customize:

  • Environment (uat or prod)
  • Client ID, version, and secret for both environments
  • Token cache path
  • Redirect URL after PhonePe API v2 payment

Example configuration:

return [
    'environment' => env('PHONEPE_ENV', 'uat'),
    'uat' => [
        'client_id' => env('PHONEPE_UAT_CLIENT_ID', 'SU2504041946024365502022'),
        'client_version' => env('PHONEPE_UAT_CLIENT_VERSION', 1),
        'client_secret' => env('PHONEPE_UAT_CLIENT_SECRET', 'ae83cba2-07c0-43a9-b0c4-1d84c261fd12'),
        'base_url' => 'https://api-preprod.phonepe.com/apis/pg-sandbox',
        'auth_url' => 'https://api-preprod.phonepe.com/apis/pg-sandbox',
        'token_cache_path' => storage_path('app/phonepe/phonepe_token_uat.json'),
    ],
    'prod' => [
        // ...
    ],
];

Usage

Initiating a Payment

use YogeshGupta\PhonePe\Facades\PhonePe;

$result = PhonePe::initiatePayment(10000, 'SUB123'); // Amount in paisa, subscription ID

if ($result['success']) {
    return redirect($result['redirectUrl']);
} else {
    \Log::error('Payment initiation failed: ' . $result['error']);
    return back()->withErrors(['error' => $result['error']]);
}

Verifying Payment Status

use YogeshGupta\PhonePe\Facades\PhonePe;

$result = PhonePe::verifyPhonePePayment('merchantOrderId123');

if ($result['success']) {
    \Log::info('Payment status: ' . json_encode($result['data']));
    return response()->json($result['data']);
} else {
    \Log::error('Payment verification failed: ' . $result['error']);
    return response()->json(['error' => $result['error']], 400);
}

Example Controller

namespace App\Http\Controllers;

use YogeshGupta\PhonePe\Facades\PhonePe;
use Illuminate\Http\Request;

class PaymentController extends Controller
{
    public function initiate(Request $request)
    {
        $amount = $request->input('amount'); // Amount in paisa
        $subscriptionId = $request->input('subscription_id');

        $result = PhonePe::initiatePayment($amount, $subscriptionId);

        if ($result['success']) {
            return redirect($result['redirectUrl']);
        }

        return back()->withErrors(['error' => $result['error']]);
    }

    public function verify(Request $request)
    {
        $merchantOrderId = $request->input('merchantOrderId');
        $result = PhonePe::verifyPhonePePayment($merchantOrderId);

        if ($result['success']) {
            \Log::info('Payment status: ' . json_encode($result['data']));
            return response()->json($result['data']);
        }

        \Log::error('Verification failed: ' . $result['error']);
        return response()->json(['error' => $result['error']], 400);
    }
}

Testing

To run tests (if included):

vendor/bin/phpunit vendor/yogeshgupta/phonepe-laravel/tests

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a feature branch (git checkout -b feature/your-feature).
  3. Commit your changes (git commit -m 'Add your feature').
  4. Push to the branch (git push origin feature/your-feature).
  5. Open a pull request.

Issues

Report bugs or suggest features by opening an issue on the GitHub repository.

License

This Laravel PhonePe integration package is licensed under the MIT License.

Related Packages

thesoulless/oauth-5-laravel

OAuth Service Provider for Laravel 5

1,198 1
laravel/socialite

Laravel wrapper around OAuth 1 & OAuth 2 libraries.

120,528,743 5,746
oriceon/oauth-5-laravel

OAuth Service Provider for Laravel 5

1,671,889 174
artdarek/oauth-4-laravel

OAuth Service Provider for Laravel 4

522,424 677
laravel/passport

Laravel Passport provides OAuth2 server support to Laravel.

99,148,458 3,417