#ovaltechnologies

1 messages · Page 1 of 1 (latest)

jagged zincBOT
jaunty basalt
#

Hello! That's a common issue when you're not passing the raw body of the request in to construct the Event. Make sure you're not parsing or modifying the body in any way, and make sure any middleware you might be using also isn't modifying the body.

jagged gull
#

So this is the way I am going about things


    const sig = request.headers['stripe-signature'];

    let event;

    try {
        event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
    } catch (err) {
        console.log("===================== ERROR START ======================");
        response.status(400).send(`Webhook Error: ${err.message} - ${JSON.stringify(err)}`);
        console.log("===================== ERROR END ======================");
        return;
    }

    console.log("event", JSON.stringify(event));

    // Handle the event
    switch (event.type) {
        case 'checkout.session.completed':
            const checkoutSessionCompleted = event.data.object;

            console.log('checkoutSessionCompleted', JSON.stringify(checkoutSessionCompleted));
            // Then define and call a function to handle the event checkout.session.completed
            break;
        // ... handle other event types
        default:
            console.log(`Unhandled event type ${event.type}`);
            break;
    }

    // Return a 200 response to acknowledge receipt of the event
    response.send({ received: true });

});```
jaunty basalt
#

I think Express.raw puts a rawBody property on the request? You're passing in request.body which I don't think would be the raw body?

#

What version of Express are you using?

jagged gull
#

"express": "^4.18.2",

#

I am redeploying the code after changing the req.body to req.rawBody

#

will give you feedback in a few minutes

jaunty basalt
#

So it's probably other middleware modifying the body before express.raw can get to it. Can you try adjusting your code so express.raw runs before any other middleware?

jagged gull
#

I am not running any other middlewares

#

I am using Firebase Cloud Functions

#

and this is the only function I have written

#

please will be back in 20 mins.

jaunty basalt
#

Looks like I was right after all. 😅

Cloud Functions automatically parses the request body for you based on the request's Content-Type header using body-parser, so you can access the req.body and req.rawBody objects in your HTTP handler.

jagged gull
#

ok