nikba/paynet

为 Laravel 应用的 Paynet 支付模块

v1.3 2024-08-07 17:18 UTC

This package is auto-updated.

Last update: 2024-09-07 17:31:09 UTC


README

此包提供了与 Paynet 支付网关的集成,适用于 Laravel 应用程序。

安装

通过 Composer 安装此包

composer require nikba/paynet

发布配置文件

php artisan vendor:publish --provider="Nikba\Paynet\Providers\PaynetServiceProvider" --tag="config"

将以下环境变量添加到您的 .env 文件中

PAYNET_MERCHANT_CODE=862491
PAYNET_SECRET_KEY=F822E47B-6F4F-4D9D-92EB-566ED89E3D76
PAYNET_USERNAME=388018
PAYNET_PASSWORD=6olmfsQX
PAYNET_TEST_MODE=true

使用方法

启动支付

使用 Paynet 门面启动支付

use Nikba\Paynet\Facades\Paynet;

$paymentData = [
    'Invoice' => 20160622010101,
    'Currency' => 498,
    'LinkUrlSuccess' => 'https://:8000/pay/1?status=success',
    'LinkUrlCancel' => 'https://:8000/pay/1?status=cancel',
    'Customer' => [
        'Code' => 'CustomerCode',
        'NameFirst' => 'FirstName',
        'NameLast' => 'LastName',
        'PhoneNumber' => 'PhoneNumber',
        'email' => 'customer@example.com',
        'Country' => 'Country',
        'City' => 'City',
        'Address' => 'Address',
    ],
    'ExternalDate' => '2025-01-01T00:00:00',
    'ExpiryDate' => '2025-01-02T00:00:00',
    'Services' => [
        [
            'Name' => 'ServiceName',
            'Description' => 'ServiceDescription',
            'amount' => 100,
            'Products' => [
                [
                    'Amount' => 100,
                    'Code' => 'PRODUCT1',
                    'Description' => 'ProductDescription',
                    'LineNo' => 1,
                    'Name' => 'ProductName',
                    'UnitPrice' => 100,
                    'UnitProduct' => 1,
                ],
            ],
        ],
    ],
    'SignVersion' => 'v01',
    'MoneyType' => null
];

$response = Paynet::sendPayment($paymentData);

处理 Paynet 通知

在您的 routes/web.php 文件中添加一个路由来处理 Paynet 通知

Route::post('/paynet/notification', [\Nikba\Paynet\Http\Controllers\PaynetController::class, 'handleNotification']);

创建一个控制器方法来处理通知

public function handleNotification(Request $request)
{
    $notificationData = $request->all();

    try {
        $response = Paynet::handleNotification($notificationData);
        return response()->json(['status' => 'success', 'data' => $response]);
    } catch (\Exception $e) {
        Log::error('Paynet notification failed: ' . $e->getMessage());
        return response()->json(['error' => $e->getMessage()], 400);
    }
}

测试

安装必要的测试依赖

composer require --dev mockery/mockery

运行测试

vendor/bin/phpunit --filter PaynetServiceTest

Postman

Postman 收藏集(JSON 文件)可快速轻松地使用 RESTful API。

如何导入和配置

  • 下载 postman_collection.json 存储库。
  • 点击导入按钮。例如,在 Postman for Mac 中,按钮位于左上角