zarenta/phonepe 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zarenta/phonepe

最新稳定版本:v1.0.10

Composer 安装命令:

composer require zarenta/phonepe

包简介

Laravel PhonePay payment gateway

README 文档

README

This package provides an easy way to integrate the PhonePe payment gateway into your Laravel application.

Features

  • Initiate payments via PhonePe.
  • Check transaction statuses.
  • Secure callback handling for payment notifications.

Installation

To get your api key go to PhonePe Official Website

Step 1: Install the Package

Run the following command to install the package via Composer:

composer require zarenta/phonepe

Step 2: Publish Configuration

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.

Step 3: Add CSRF Exception for Callback URL

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

    ]);

})

Step 4: Set Up Environment Variables

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

Configuration

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'),

];

Usage

Initiating Payment

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 = 'user@example.com',

            $shortName = 'Your Company',

            $message = 'Payment for Order #1234'

        );

        return redirect($paymentUrl);

    } catch (PhonePe\Exception\PhonePeException $e) {

        return response()->json(['error' => $e->getMessage()], 400);

    }

}

Checking Transaction Status

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']);

    }

}

Handling PhonePe Callback

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.

License

This package is open-source and licensed under the MIT License.

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-18