调试 Stripe Webhook 事件

Debugging Stripe Webhook Event

我整个周末都在研究 Stripe Webhooks,但仍然没有找到调试响应的方法。这是我当前的代码:

http_response_code(200);

        // set stripe api key
        Stripe::setApiKey(env('STRIPE_SECRET'));
        $endpoint_secret = 'whsec_XXX';

        $payload = @file_get_contents('php://input');
        $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
        $event_json = json_decode($payload);

        try {
            $event = \Stripe\Webhook::constructEvent(
                $payload, $sig_header, $endpoint_secret
            );
        } catch(\UnexpectedValueException $e) {
            // Invalid payload
            http_response_code(400);
            exit();
        } catch(\Stripe\Error\SignatureVerification $e) {
            // Invalid signature
            http_response_code(400);
            exit();
        }

        $event_id = $event_json->id;

        if(isset($event_json->id)) {
            try {

                // to verify this is a real event, we re-retrieve the event from Stripe
                $event = \Stripe\Event::retrieve($event_id);
                $invoice = $event->data->object;

                // successful payment, both one time and recurring payments
                if($event->type == 'charge.succeeded') {
                    $customer = \Stripe\Customer::retrieve($invoice->customer);
                    $email = $customer->email;
                    \Mail::send('emails.new-userlike',
                    array(
                        'user' => $customer
                    ), function($message) {
                        $message->from('info@friendships.me', 'friendships.me');
                        $message->to('info@friendships.me')->subject('Test');
                    });
                }

                // failed payment
                if($event->type == 'charge.failed') {
                    // send a failed payment notice email here
                }


            } catch (Exception $e) {
                // something failed, perhaps log a notice or email the site admin
            }
        }

到目前为止,这会导致错误 500... ._.

但这不是问题,我已经让它工作了。问题是,我需要检查 SEPA 订阅是否有 charge.failedcharge.succeeded 响应,只有在成功收费后,才能创建订阅。

如何访问此 webhook 中的订阅 ID?或者更好的是,我如何调试响应?因为即使这样也没有发送响应:

http_response_code(200);
$payload = @file_get_contents('php://input');
$event_json = json_decode($payload);
print_r("test");

我会先从最简单的 webhook 处理程序开始

<?php
// Retrieve the request's body and parse it as JSON:
$input = @file_get_contents('php://input');
$event = json_decode($input);

http_response_code(200); // PHP 5.4 or greater

// echo the event id, evt_xxxyyyzzz
echo $event->id;

if($event->type == "charge.succeeded") {
   // you want the id of the charge rather than the event, ch_xxxyyzz
   echo $event->data->object->id;
}

?>

当您在仪表板中使用 "send test webhook" 函数时,您应该会在响应中看到类似于 evt_0000000 的内容(如果事件类型为 charge.succeeded,则应看到 ch_000000 ).

如果您仍然收到 500 错误,这意味着您的服务器上配置不正确,您可以在网络服务器的 error.log 中找到完整的错误(尝试查看 /var/log 或您服务器的网络仪表板)

如果您想轻松调试 webhook,可以尝试添加类似 man-in-the-middle 的内容。例如,https://www.reliablewebhook.com/ 允许接收任何 webhook,记录它们并将它们转发到您的本地主机或其他网址