【问题标题】:I keep getting 404 not found while testing webhook with spatie webhook client and ngrok在使用 spatie webhook 客户端和 ngrok 测试 webhook 时,我一直找不到 404
【发布时间】: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


【解决方案1】:

你在这里不匹配一些东西。假设您想通过 web 处理 webhook。那么你必须在你的web.php 文件中添加一个路由。 (如果您使用域路由,请确保将其放在路由文件的底部,或者为了安全起见将其放在底部)

Route::webhooks('paystack/webhook');

然后将该路由添加到您的验证 csrf 中间件,除了

protected $except = [
    'paystack/*',
];

然后您的 paystack 的 webhook url 将是

ngrock-generated-host/paystack/webhook

现在如果你想通过 api 处理它,那么将路由放在api.php 文件中

Route::webhooks('paystack/webhook');

你不需要把它放在除了验证 csrf 中间件之外,因为它会被 api 处理。并且您的 paystack 的 webhook url 将是

ngrock-generated-host/api/paystack/webhook

【讨论】:

  • 谢谢@zahid hasan emon,我已经用 web.php 路由尝试了你的建议,我得到了 500 内部服务器错误响应,然后我用 api 路由尝试了,我得到了 404 未找到响应。也许我仍然缺少一些东西
  • 您是否将 api 路由的 api 前缀放在您的 paystack webhook url 中?如果你得到 500 用于 web,那么这意味着它正在接收 webhook,但是在处理 webhook 时出现了问题。检查响应或日志以找出问题所在。
  • api 前缀?不是我没有,但现在我做了并且正在响应 500 内部服务器错误,但我会按照你说的检查日志
  • 现在您正在接收 webhook,但是在处理它时出现了问题。请查看导致 500 错误的原因。
  • 我确实检查了日志,这导致了错误 [2020-12-03 09:54:51] local.ERROR: App\Handler\CustomSignatureValidator is not a valid signature validator class.
猜你喜欢
  • 2021-08-20
  • 2017-01-30
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
  • 1970-01-01
  • 2018-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多