so I am setting up stripe webhooks for several use-cases, and following the docs, I was not able to get a working webhook response. I kept getting this error.
"Webhook Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?"
I followed the docs and understood that this just means I need to convert the body to raw data instead of another format(stripe needs raw data to make the secure connection). I tried doing this in several different ways(I have a few down below).
app.post('/webhook', express.raw({type: 'application/json'}), (request, response) => {
New code attempt :
app.use((req, res, next) => {
if (req.originalUrl === "/webhook") {
next();
} else {
bodyParser.json()(req, res, next);
}
});
app.post('/webhook', bodyParser.raw({type: 'application/json'}), (request, response) => {
As you can see, I tried all of the ways that the stripe docs recommended and even tried some ways from this similar SOF question, but was not able to do it. I also tried with bodyPaser or express and tried several smaller things too. In our app we are using a firebase backend, with several normal cloud functions, and we decided to use express with all the stripe code since that is what the stripe docs use. After trying for so long with express and fierbase, I decided to do it with just a firebase onRequest method and used the req.rawBody method.
exports.stripeWebhook = functions.https.onRequest((request, response) => {
const stripeWebhookSecretKey = functions.config().stripe.webhook_secret_key;
let event;
...
const payloadData = request.rawBody;
const payloadString = payloadData.toString();
const webhookStripeSignatureHeader = request.headers['stripe-signature'];
event = stripe.webhooks.constructEvent(payloadString, webhookStripeSignatureHeader, stripeWebhookSecretKey);
...
});
This worked right away and although I don't think my boss would mind, having all the stripe code in express would be ideal, and just want to know why this might be happening. Thanks