Symfony 5,测试条带时得到 403

问题描述

我正在尝试将 Stripe 用于 symfony 5 的月度订阅

我在我的项目中集成了 Stripe,我正在测试一本网络书来监听诸如支付失败之类的事件。

我在控制器中创建了一个动作:

/**
     * @Route("/webhook",name="webhook")
     * @param Request $request
     * @return Response
     */
    public function webhook(Request $request) {
        \Stripe\Stripe::setApiKey('sk_test_..');

        $event = $request->query;
        $webhookSecret = "whsec_..";
        $signature = $request->headers->get('stripe-signature');

        if ($webhookSecret) {
            try {
                $event = \Stripe\Webhook::constructEvent(
                    $request->getContent(),$signature,$webhookSecret
                );
            } catch (\Exception $e) {
                //  return new JsonResponse([['error' => $e->getMessage(),'status'=>403]]);
                $response = new Response();
                $response->setContent(json_encode([
                    'error' => $e->getMessage(),]));
                $response->headers->set('Content-Type','application/json');
                $response->setStatusCode(403);
                return $response;
            }
        } else {
            $event =  $request->query;
        }

        $type = $event['type'];
        $object = $event['data']['object'];

        switch ($type) {
            case 'checkout.session.completed':
                // Payment is successful and the subscription is created.
                // You should provision the subscription and save the customer ID to your database.

                break;
            case 'invoice.paid':
                // Continue to provision the subscription as payments continue to be made.
                // Store the status in your database and check when a user accesses your service.
                // This approach helps you avoid hitting rate limits.

                break;
            case 'invoice.payment_Failed':
                // The payment Failed or the customer does not have a valid payment method.
                // The subscription becomes past_due. Notify your customer and send them to the
                // customer portal to update their payment information.

                break;
            // ... handle other event types
            default:
                // Unhandled event type
        }

        $response = new Response();
        $response->setContent(json_encode([
            'status' => 'success',]));
        $response->setStatusCode(200);
        return $response;

    }

基于条纹官方文档:https://stripe.com/docs/billing/subscriptions/checkout

修改代码以使其适应 symfony,我正在尝试使用 postman 和 stripe cli 测试该操作:

我得到了邮递员:

无法从标题提取时间戳和签名

我开始听stripe cli 路由使用:stripe listen --forward-to http://localhost/webhook 我使用条带触发器 payment_intent.created 来模拟付款,但出现 403 错误

如何修复网络钩子?

enter image description here

解决方法

Postman 不是测试 Stripe webhooks 的好方法,因为您的请求会错过某些标头,如您发现的时间戳。

相反,您应该使用 Stripe CLI 或仪表板发送测试事件(CLI 更适合这种情况,因为它会创建所有对象,而不是向您发送带有虚拟数据的事件)。

至于您在通过 CLI 触发事件时遇到的 403 错误,如果没有更多详细信息,我无法真正告诉您那里发生了什么。 403 是“禁止”状态代码,因此您应该仔细检查您的服务器设置,确保 Webhook 端点无需身份验证即可访问互联网。