【问题标题】:Debugging Stripe Webhook Event调试 Stripe Webhook 事件
【发布时间】:2019-04-14 10:58:40
【问题描述】:

我花了我的周末试图找出 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");

【问题讨论】:

  • 首先,尝试找出导致 500 的原因。您可以从自己的机器发送一些虚拟的 webhook 事件 JSON 到开发服务器,仅用于调试目的。然后找出问题所在。

标签: php stripe-payments webhooks


【解决方案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 的内容(如果事件类型为 charge.succeeded,则应看到 ch_000000)。

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

【讨论】:

  • 基本处理程序有效,但我没有收到任何响应。不过,我现在让它工作了。在我打开另一个问题之前:我不能用 \Mail::send 发送邮件吗?我在整个系统中使用它,但是在 webhook 中尝试时会引发错误 500。一个简单的 PHP Mail() 工作正常。
  • Stripe 的 SDK 不包含 Mail 类 AFAIK。你使用的是 Laravel 之类的框架还是其他公司的 SDK? laravel.com/docs/5.2/mail#sending-mail
  • 现在一切正常,谢谢! :) 我有一堆错误,但没有想到 error.log :/
  • 哇哦!很高兴听到一切都很好:)
猜你喜欢
  • 2015-10-24
  • 2017-10-25
  • 2014-05-01
  • 2021-06-11
  • 2021-01-19
  • 2020-01-21
  • 2013-10-24
  • 2019-03-03
  • 2021-10-24
相关资源
最近更新 更多