I've been trying to listen to Stripe webhooks with firebase functions:
here is my code:
import * as bodyParser from 'body-parser'
import * as express from 'express';
const app = express();
app.use(bodyParser.raw({ type: '*/*' }));
const stripe = new stripeM("test_token");;
const stripeWHEndpointSecret = 'secret';
app.post('*', (req, res) => {
const sig = req.headers["stripe-signature"];
console.log(sig);
try {
const event = stripe.webhooks.constructEvent(req.body, sig, stripeWHEndpointSecret);
console.log(event);
}
catch (err) {
console.log(util.inspect(err));
res.status(400).end();
}
res.json({received: true});
});
export const stripeWebhooksListener = functions.https.onRequest(app);
and I keep getting this error: SyntaxError: Unexpected token o in JSON at position 1
Now i understand it's a problem with parsing the req.body as it arrives in chunks probably. but, I thought that using the Express with body-parser should solve it.
Any help will be appreciated
Stripe official documentation on how to do it: https://stripe.com/docs/webhooks/signatures