调试Stripe Webhook事件

时间:2018-11-11 15:04:52

标签: php stripe-payments webhooks

我度过了一个周末,试图找出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 ... ._。

enter image description here

但这不是问题,我已经开始工作了。事实是,我需要检查SEPA订阅中是否有charge.failedcharge.succeeded响应,并且仅在成功充电后才能创建订阅。

如何在此Webhook中访问订阅ID?还是更好,我该如何调试响应?因为即使这样也没有发送响应,所以:

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

enter image description here

1 个答案:

答案 0 :(得分:1)

我将从最简单的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;
}

?>

当您在控制台中使用“发送测试Webhook”功能时,您应该在响应中看到类似evt_0000000的内容(如果事件类型为ch_000000,则应该显示charge.succeeded)。

如果仍然出现500错误,则意味着服务器上的配置有误,并且可以在Web服务器的error.log中获取完整的错误(尝试在/ var / log或您服务器的Web仪表板)