zampou/cinetpay

CinetPay Laravel 扩展包

1.0.0 2022-12-20 08:16 UTC

This package is not auto-updated.

Last update: 2024-09-28 13:04:04 UTC


README

安装

composer require zampou/cinetpay

1. 安装服务提供者

所有服务提供者都注册在下面的配置文件中 config/app.php

'providers' => [
    /*
    * Package Service Providers...
    */
    Zampou\CinetPay\Providers\CinetPayProvider::class,
],

2. 配置

配置 .env 文件

CINETPAY_SITE_ID=votre_site_id
CINETPAY_API_KEY=votre_cle_api

执行 php artisan vendor:publish --provider="Zampou\CinetPay\Providers\CinetPayProvider" --tag="cinetpay" 以生成完整的配置文件。

配置文件: config/cinetpay.php

return [
    'CINETPAY_API_KEY' => env('CINETPAY_API_KEY'),
    'CINETPAY_SITE_ID' =>  env('CINETPAY_SITE_ID')
];

3. IPN 和 CSRF Token

在 CinetPay 的 ipn 路由上禁用 CSRF 验证,所有排除的路由都在下面的文件中 app/Http/Middleware/VerifyCsrfToken.php

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array<int, string>
     */
    protected $except = [
        '/cinetpay-ipn'
    ];

4. 使用方法

创建交易

use Zampou\CinetPay\Facades\CinetPay;

$transactionLink = CinetPay::generatePaymentLink([
    'amount' => '100',
    'currency' => 'XOF',
    'customer_name' => 'John',
    'customer_surname' => 'Doe',
    'transaction_id' => '123456789',
    'description' => 'Bon gbozon bien chaud de qualité',
]);

dd($transactionLink);

IPN 验证

Laravel CinetPay 可以自动处理 IPN:只需通过创建以下命令的 Listener 来订阅 Zampou\CinetPay\Events\CinetPayIPN

php artisan make:listener CinetPayIPNListener

然后,通过向 App\Providers\EventServiceProvider 添加一个监听器来订阅事件

    use App\Listeners\CinetPayIpnListener;
    use Zampou\CinetPay\Events\CinetPayIPN;
    // ...

    /**
     * The event to listener mappings for the application.
     *
     * @var array<class-string, array<int, class-string>>
     */
    protected $listen = [
        CinetPayIPN::class => [
            CinetPayIpnListener::class
        ]
    ];

事件 Listener 示例

<?php

namespace App\Listeners;

class CinetPayIpnListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle($event)
    {
        // Vous faite votre traitement ici
        // Structure des données dans la variable `payment_data`
        // [
        //     'code',
        //     'message',
        //     'amount',
        //     'metadata',
        //     'currency',
        //     'operator_id',
        //     'description',
        //     'payment_date',
        //     'payment_method'
        //     'api_response_id',
        // ]

        dd($event->payment_data);
    }
}