yasserbenaioua / chargily-epay-laravel
Chargily epay网关的laravel包
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^7.5
- illuminate/contracts: ^8.77|^9.0
- spatie/laravel-package-tools: ^1.9.0
- spatie/laravel-webhook-client: ^3.1.0
Requires (Dev)
- nunomaduro/collision: ^5.5|^6.0
- orchestra/testbench: ^6.24|^7.0
- pestphp/pest: ^1.21
- spatie/pest-plugin-test-time: ^1.0
README
安装
您可以通过composer安装此包
composer require yasserbenaioua/chargily-epay-laravel
您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="YasserBenaioua\Chargily\ChargilyServiceProvider" --tag="chargily-config"
这是将要发布到config/chargily.php
的配置文件内容
use YasserBenaioua\Chargily\Models\ChargilyWebhookCall; return [ /* * Chargily Api Key * You can found it on your epay.chargily.com.dz Dashboard. */ 'key' => env('CHARGILY_API_KEY'), /* * Chargily Api Secret * Your Chargily secret, which is used to verify incoming requests from Chargily. * You can found it on your epay.chargily.com.dz Dashboard. */ 'secret' => env('CHARGILY_API_SECRET'), /* * This is where client redirected after payment processing. */ 'back_url' => 'valid-url-to-redirect-after-payment', /* * This is where you receive payment informations. */ 'webhook_url' => 'valid-url-to-receive-payment-informations', /* * You can define the job that should be run when a chargily webhook hits your application * here. */ 'jobs' => [ // \App\Jobs\HandleChargilyWebhook::class, ], /* * This model will be used to store all incoming webhooks. * It should be or extend `YasserBenaioua\Chargily\Models\ChargilyWebhookCall` */ 'model' => ChargilyWebhookCall::class, /* * When running `php artisan model:prune` all stored Chargily webhook calls * that were successfully processed will be deleted. * * More info on pruning: https://laravel.net.cn/docs/8.x/eloquent#pruning-models */ 'prune_webhook_calls_after_days' => 10, /* * When disabled, the package will not verify if the signature is valid. * This can be handy in local environments. */ 'verify_signature' => env('CHARGILY_SIGNATURE_VERIFY', true), ];
接下来,您必须使用以下命令发布迁移
php artisan vendor:publish --provider="YasserBenaioua\Chargily\ChargilyServiceProvider" --tag="chargily-migrations"
迁移发布后,您可以通过运行迁移来创建chargily_webhook_calls
表
php artisan migrate
最后,注意路由配置:在Chargily配置文件中,您必须配置Chargily webhook应发送到哪个URL。在您应用程序的路由文件中,您必须将此路由传递给Route::githubWebhooks
路由宏
Route::chargilyWebhook('webhook-route-configured-at-the-chargily-config-file');
在幕后,此宏将为该包提供的控制器注册一个POST
路由。我们建议将其放在api.php
路由文件中,这样当webhook到达时不会创建会话,也不需要CSRF令牌。
如果您出于任何原因需要在web.php
路由文件中注册路由,那么您必须将该路由添加到VerifyCsrfToken
中间件的except
数组中
protected $except = [ 'webhook-route-configured-at-the-chargily-config-file', ];
使用
首先,您可以创建一个支付方式,如下所示
use YasserBenaioua\Chargily\Chargily; $chargily = new Chargily([ //mode 'mode' => 'EDAHABIA', //OR CIB //payment details 'payment' => [ 'number' => 'payment-number-from-your-side', // Payment or order number 'client_name' => 'client name', // Client name 'client_email' => 'client_email@mail.com', // This is where client receive payment receipt after confirmation 'amount' => 75, //this the amount must be greater than or equal 75 'discount' => 0, //this is discount percentage between 0 and 99 'description' => 'payment-description', // this is the payment description ] ]);
然后使用getRedirectUrl()
方法获取结算链接
use YasserBenaioua\Chargily\Chargily; $chargily = new Chargily([ //mode 'mode' => 'EDAHABIA', //OR CIB //payment details 'payment' => [ 'number' => 'payment-number-from-your-side', // Payment or order number 'client_name' => 'client name', // Client name 'client_email' => 'client_email@mail.com', // This is where client receive payment receipt after confirmation 'amount' => 75, //this the amount must be greater than or equal 75 'discount' => 0, //this is discount percentage between 0 and 99 'description' => 'payment-description', // this is the payment description ] ]); $redirectUrl = $chargily->getRedirectUrl(); //like : https://epay.chargily.com.dz/checkout/random_token_here return redirect($redirectUrl);
Chargily将对所有击中您应用程序webhook URL的请求进行签名。此包将自动验证签名是否有效。
除非发生严重错误,否则此包将始终对webhook请求响应200
。所有具有有效签名的webhook请求都将记录在chargily_webhook_calls
表中。该表有一个payload列,其中保存了传入webhook的全部有效负载。
如果签名无效,则请求将不会记录在chargily_webhook_calls
表中,但将抛出Spatie\WebhookClient\Exceptions\InvalidWebhookSignature
异常。如果在webhook请求过程中发生错误,抛出的异常将保存在异常列中。在这种情况下,控制器将发送500
而不是200
。
要处理webhook请求,您可以定义一个执行工作的作业。以下是一个这样的作业示例
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use YasserBenaioua\Chargily\Models\ChargilyWebhookCall; class HandleChargilyWebhook implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; /** * Create a new job instance. * * @return void */ public function __construct( public ChargilyWebhookCall $webhookCall ) { // } /** * Execute the job. * * @return void */ public function handle() { // do your work here // you can access the payload of the webhook call with `$this->webhookCall->payload` } }
创建您的作业后,您必须在chargily.php
配置文件的jobs
数组中注册它。
// config/chargily.php 'jobs' => [ \App\Jobs\HandleChargilyWebhook::class, ],
删除已处理的webhooks
YasserBenaioua\Chargily\Models\ChargilyWebhookCall
是MassPrunable
。为了每天删除所有已处理的webhooks,您可以安排此命令。
$schedule->command('model:prune', [ '--model' => [\YasserBenaioua\Chargily\Models\ChargilyWebhookCall::class], ])->daily();
测试
composer test
变更日志
有关最近更改的更多信息,请参阅CHANGELOG
致谢
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件