adejorosam / laravel-flutterwave-webhook
在 Laravel 应用程序中处理 Flutterwave Webhooks
Requires
- php: ^7.1
- illuminate/support: ^6.0|^7.0|^8.0
- spatie/laravel-webhook-client: ^2.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-09-05 21:57:06 UTC
README
Flutterwave 可以通过 Webhooks 通知您的应用程序各种事件。此包可以帮助您处理这些 Webhooks。它将自动验证所有传入请求并确保它们来自 Flutterwave。
请注意,此包将不会处理请求验证后的操作。您仍然需要编写相应的代码。
在Flutterwave 的 Webhooks 这里了解更多详情
安装
您可以通过 composer 安装此包
composer require adejorosam/laravel-flutterwave-webhook
服务提供程序将自动注册自己。
您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="Adejorosam\LaravelFlutterwaveWebhook\LaravelFlutterwaveWebhookServiceProvider" --tag="config"
以下是要发布的配置文件内容,它将在 config/flutterwave-webhooks.php
中发布
return [ /* * Flutterwave will sign each webhook using the secret hash you fielded. * You can do that at the webhook configuration settings: https://dashboard.flutterwave.com/account/webhooks. */ 'signing_secret' => env('SECRET_HASH'), /* * The classname of the model to be used. The class should equal or extend * \Spatie\WebhookClient\ProcessWebhookJob. */ 'process_webhook_job' => '', ];
在配置文件的 signing_secret
键中,您应该添加一个有效的 webhook 密钥。您可以在 Flutterwave 控制台上的 webhook 配置设置 中找到该密钥。
接下来,您必须使用以下命令发布迁移
php artisan vendor:publish --provider="Adejorosam\LaravelFlutterwaveWebhook\LaravelFlutterwaveServiceProvider" --tag="migrations"
迁移发布后,您可以通过运行迁移来创建 webhook_calls
表
php artisan migrate
最后,处理路由:在 Flutterwave 控制台 中,您必须配置 Flutterwave Webhooks 应该击中的 URL。在您的应用程序的路由文件中,您必须将此路由传递给 Route::flutterwaveWebhooks
Route::flutterwaveWebhooks('webhook-route-configured-at-the-flutterwave-dashboard');
在幕后,这将注册一个由本包提供的控制器的 POST
路由。由于 Flutterwave 无法获取 csrf-token,您必须将此路由添加到 VerifyCsrfToken
中间件的 except
数组中
protected $except = [ 'webhook-route-configured-at-the-flutterwave-dashboard', ];
用法
Flutterwave 会为几种事件类型发送 Webhooks。您可以在 Flutterwave 文档中找到 [事件类型完整列表]。
Flutterwave 将为击中您的应用程序 webhook url 的所有请求签名。此包将自动验证签名是否有效。如果签名无效,则请求可能不是由 Flutterwave 发送的。
除非发生严重错误,否则此包将始终对 webhook 请求响应 200
。发送 200
将防止 Flutterwave 重复发送相同的事件。所有具有有效签名的 webhook 请求都将记录在 webhook_calls
表中。该表有一个 payload
列,其中保存了传入 webhook 的整个有效负载。
如果签名无效,请求将不会记录在 webhook_calls
表中,但会抛出 Adejorosam\LaravelWebhooks\WebhookFailed
异常。如果在 webhook 请求过程中发生错误,抛出的异常将保存在 exception
列中。在这种情况下,控制器将发送 500
而不是 200
。
存储和处理 Webhooks
在签名验证后,并且 webhook 配置文件确定请求应该被处理之后,此包将存储并处理请求。
请求首先将存储在 webhook_calls
表中。这是通过使用 WebhookCall
模型来完成的。
接下来,新创建的 WebhookCall
模型将被传递到一个队列作业中,该作业将处理请求。任何扩展 \Spatie\WebhookClient\ProcessWebhookJob
的类都是有效的作业。以下是一个示例
<?php namespace Adejorosam\LaravelFlutterwaveWebhook; use \Spatie\WebhookClient\ProcessWebhookJob; //The class extends "ProcessWebhookJob" class as that is the class //that will handle the job of processing our webhook before we have //access to it. class ProcessFlutterwaveWebhook extends ProcessWebhookJob { public function handle() { $data = json_decode($this->webhookCall, true); //Do something with great with this! http_response_code(200); //Acknowledge you received the response } }
您应在 webhook-client
配置文件的 process_webhook_job
中指定您作业的类名。
测试
composer test
变更日志
有关最近更改的详细信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献指南。
安全
如果您发现任何安全相关的问题,请通过电子邮件samsonadejoro@gmail.com联系,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅许可证文件。