【发布时间】:2021-03-14 22:06:33
【问题描述】:
我正在使用带有 ngrok 和 spatie webhook-client 的支付网关 Paystack 测试 webhook,我的路由位于 route/api.php 中。当事件被触发时,我在状态响应中一直找不到 404。但是,如果将 webhook 路由移动到 web.php 路由,我会得到响应 419 未知状态。不知道为什么。 请我对所有这些都是新手,只是为了学习。
**Route in api.php**
Route::webhooks('paystack-webhook');
和VerifyCsrfToken.php
protected $except = [
'https://17c5dbf1bd87.ngrok.io/paystack/webhook',
];
webhook-client.php
<?php
return [
'configs' => [
[
/*
* This package supports multiple webhook receiving endpoints. If you only have
* one endpoint receiving webhooks, you can use 'default'.
*/
'name' => 'default',
/*
* We expect that every webhook call will be signed using a secret. This secret
* is used to verify that the payload has not been tampered with.
*/
'signing_secret' => env('PAYSTACK_SECRET_KEY'),
/*
* The name of the header containing the signature.
*/
'signature_header_name' => 'x-paystack-signature',
/*
* This class will verify that the content of the signature header is valid.
*
* It should implement \Spatie\WebhookClient\SignatureValidator\SignatureValidator
*/
'signature_validator' => App\Handler\CustomSignatureValidator::class,
/*
* This class determines if the webhook call should be stored and processed.
*/
'webhook_profile' => \Spatie\WebhookClient\WebhookProfile\ProcessEverythingWebhookProfile::class,
/*
* This class determines the response on a valid webhook call.
*/
'webhook_response' => \Spatie\WebhookClient\WebhookResponse\DefaultRespondsTo::class,
/*
* The classname of the model to be used to store call. The class should be equal
* or extend Spatie\WebhookClient\Models\WebhookCall.
*/
'webhook_model' => \Spatie\WebhookClient\Models\WebhookCall::class,
/*
* The class name of the job that will process the webhook request.
*
* This should be set to a class that extends \Spatie\WebhookClient\ProcessWebhookJob.
*/
'process_webhook_job' => App\Handler\ProcessWebhook::class,
],
],
];
ProcessWebhook.php
<?php
namespace App\Handler;
//App/Handler/ProcessWebhook.php
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 ProcessWebhook extends ProcessWebhookJob
class ProcessWebhook extends ProcessWebhookJob
{
public function handle() {
$data = json_decode($this->webhookCall, true);
//Do something with the event
logger($data['payload']);
http_response_code(200); //Acknowledge you received the response
}
}
CustomSignatureValidator.php
<?php
//App/Handler/CustomSignatureValidator.php
namespace App\Handler;
use Illuminate\Http\Request;
use Spatie\WebhookClient\Exceptions\WebhookFailed;
use Spatie\WebhookClient\WebhookConfig;
use Spatie\WebhookClient\SignatureValidator\SignatureValidator;
class PaystackSignature implements SignatureValidator
{
public function isValid(Request $request, WebhookConfig $config): bool
{
$signature = $request->header($config->signatureHeaderName);
if (! $signature) {
return false;
}
$signingSecret = $config->signingSecret;
if (empty($signingSecret)) {
throw WebhookFailed::signingSecretNotSet();
}
$computedSignature = hash_hmac('sha512', $request->getContent(), $signingSecret);
return hash_equals($signature, $computedSignature);
}
}
回应
路线列表
【问题讨论】:
-
你好,我是 laravel 的新手,我需要 composer 的 spatie 包,然后将秘密添加到我的 env 文件中,并发布数据库然后迁移文件,接下来我应该做什么,只要我使用 Route::webhooks('webhookname');并且它没有被识别或与任何事物相关.. @zahidHasanEmon
标签: laravel http-status-code-404 webhooks ngrok