I had the same issue. It turns out the problem is with getting the raw body which is needed for stripe to verify the request properly.
body.text()
won’t do. Instead, it can be achieved by using:
Buffer.from(request.body.toBase64(), "base64")
Here’s my full stripe request verification code:
/////////////////////////////
// Verify stripe webhook call
/////////////////////////////
console.log("Verifying request");
const stripeApiKey = context.values.get("stripeApiKey");
const stripeWebhookSecret = context.values.get("stripeWebhookSecret");
const stripe = require("stripe")(stripeApiKey, { apiVersion: "2022-11-15" });
const signature = request.headers["Stripe-Signature"][0];
if (signature == undefined) {
console.error("Missing stripe signature");
throw Error("missing-stripe-signature");
}
var event;
try {
event = stripe.webhooks.constructEvent(Buffer.from(request.body.toBase64(), "base64"), signature, stripeWebhookSecret);
} catch (e) {
console.error(`Stripe event could not be verified. Error: ${e.message}`);
throw Error("invalid-signature");
}
console.log("Request is valid.");