【问题标题】:Gocardless not executing the programe after webhook parsingWebhook 解析后 Gocardless 不执行程序
【发布时间】:2019-03-05 18:57:20
【问题描述】:

我正在我的网站中实现 gocardless api。我被困在 webhook 中。当我从沙盒测试环境向我的网站发送 webhook 时,它显示 200 响应,但之后没有执行任何代码。而且我没有在响应正文中看到任何内容,它显示为空。

我为此使用 laravel 5.7。这是我的代码

路由.php Route::post('/webhook', 'HomeController@webhook');

HomeController.php

public function webhook() 
{
    $webhook_endpoint_secret = env("GOCARDLESS_WEBHOOK_ENDPOINT_SECRET");
    $request_body = file_get_contents('php://input');

    $headers = getallheaders();
    $signature_header = $headers["Webhook-Signature"];

    try {
        $events = Webhook::parse($request_body, $signature_header, $webhook_endpoint_secret);

        foreach ($events as $event) {
            print("Processing event " . $event->id . "\n");

            switch ($event->resource_type) {
                case "mandates":
                    $this->process_mandate_event($event);
                    break;
                default:
                    print("Don't know how to process an event with resource_type " . $event->resource_type . "\n");
                    break;
            }
        }

        header("HTTP/1.1 204 OK");
    } catch(InvalidSignatureException $e) {
        header("HTTP/1.1 498 Invalid Token");
    }
}


public function process_mandate_event($event)
{
    switch ($event->action) {
        case "cancelled":
            print("Mandate " . $event->links["mandate"] . " has been cancelled!\n");
            break;
        default:
            print("Don't know how to process a mandate " . $event->action . " event\n");
            break;
    }
}

我尝试执行一些数据库查询,但没有任何效果。谁能指出我做错了什么以及哪里做错了?

【问题讨论】:

    标签: php laravel-5.7 gocardless


    【解决方案1】:
     $responseBody = file_get_contents('php://input');
    
    if ($responseBody <> "") {
        $response_new = json_decode($responseBody, true);
        foreach ($response_new["events"] as $event) {
            print_r($event); // you will see all the data which you want
            //if($event['resource_type'] == 'subscriptions')
            //payments,mandates or etc...
            //
            }
    }
    

    使用电子邮件发送代码进行调试,当 webhook 被调用时,电子邮件会发送到您的地址并带有响应正文,然后您将在电子邮件正文中进行数据处理。 希望你能理解

    【讨论】:

    【解决方案2】:

    使用 Laravel 处理 webhook 及其结构的最佳方法是添加一个中间件来验证 webhook 签名:

     public function handle($request, Closure $next)
        {
            $signature = $request->header('Webhook-Signature');
    
            if (!$signature) {
                throw WebhookFailed::missingSignature();
            }
    
            if (!$this->isValid($signature, $request->getContent(), $request->route('configKey'))) {
                throw WebhookFailed::invalidSignature($signature);
            }
    
            return $next($request);
        }
    

    isValid 方法将检查 webhook 的签名和您保存的密钥。

    然后在您的控制器上,您可以处理来自 webhook 的事件(请记住,Gocardless 可以在单个 webhook 请求中发送多个事件)。

    public function __invoke(Request $request)
        {
            $payload = $request->input();
    
            foreach ($payload['events'] as $event) {
                // Do whatever do you need with the events.
                }
            }
    
            return response()->json(['message' => 'ok']);
        }
    

    我们为 Laravel 创建了一个包,可以帮助您处理和处理 Gocardless webhook。

    Nestednet/Gocardless-laravel

    【讨论】:

      【解决方案3】:

      找到解决方案。我在获取标题值时遇到了问题。在 laravel 中,您无法使用 $headers = getallheaders(); 获取标头值,您需要使用 use Request;,然后使用 Request::header("Webhook-Signature");,这解决了我的问题。

      【讨论】:

        猜你喜欢
        • 2018-05-22
        • 2018-04-28
        • 1970-01-01
        • 2016-09-18
        • 1970-01-01
        • 2016-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多