Symfony 5,测试条带时得到 403

Symfony 5, got 403 when testing stripe

我正在尝试使用 stripe 通过 symfony 5 按月订阅。

我在我的项目中集成了 stripe,我正在测试一个 webbook 来监听支付失败等事件。

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

/**
     * @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;

    }

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

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

通过邮递员我得到了:

Unable to extract timestamp and signatures from header

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

我该如何修复 webhook?

enter image description here

Postman 不是测试 Stripe webhook 的好方法,因为您的请求会遗漏某些 headers,如您发现的时间戳。

相反,您应该使用 Stripe CLI 或仪表板发送测试事件(CLI 对此更好,因为它创建所有 objects 而不是向您发送带有虚拟数据的事件)。

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