Khaled Boussoffara
Khaled Boussoffara

Reputation: 3

Symfony 5, got 403 when testing stripe

I'm trying to use stripe for monthly subscription with symfony 5 .

I integrated stripe in my project an I'm testing a webbook to listen to events like payment failed .

I created an action inside my controller :

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

    }

base on stripe official documentation : https://stripe.com/docs/billing/subscriptions/checkout

I modified the code to adapt it to symfony and I'm trying to test the action using postman and stripe cli :

With postman i got :

Unable to extract timestamp and signatures from header

With stripe cli i started listening to the route using : stripe listen --forward-to http://localhost/webhook and i use stripe trigger payment_intent.created to simulate a payment but i got 403 error

How can i fix the webhook ?

enter image description here

Upvotes: 0

Views: 1436

Answers (1)

Paul Asjes
Paul Asjes

Reputation: 5857

Postman isn't a good way to test Stripe webhooks as your request will miss certain headers like the timestamp, as you've discovered.

Instead you should use the Stripe CLI or dashboard to send test events (the CLI is better for this as it creates all the objects rather than sending you an event with dummy data).

As for the 403 error you're getting when triggering events via the CLI, I can't really tell you what's happening there without more details. 403 is the "forbidden" status code, so you should double check your server settings that the webhook endpoint is accessible to the Internet without requiring authentication.

Upvotes: 1

Related Questions