Ours looks like this:
import stripe from "stripe";
const bound = Meteor.bindEnvironment(callback => callback());
WebApp.rawConnectHandlers.use("/payment", (req, res, next) => {
const sig = req.headers["stripe-signature"];
const endpointSecret = Meteor.settings.STRIPE_WEBHOOK_SK;
const stripeObj = stripe(Meteor.settings.STRIPE_SK);
if (req.method === "POST") {
let body = "";
req.on("data", data => bound(() => {
body += data;
}));
req.on("end", () => bound(() => {
try {
const eventObj = stripeObj.webhooks.constructEvent(body, sig, endpointSecret);
const eventData = eventObj.data;
const result = invokeStripeEvent(eventObj, eventData.object);
res.writeHead(200);
res.end(result);
}
catch (error) {
res.writeHead(500);
console.warn("[Payment Gateway Exception]: ", error);
if (error.type && error.type === "StripeSignatureVerificationError") {
res.end("We caught you -_-");
}
else {
res.end(JSON.stringify(error, Object.getOwnPropertyNames(error)));
}
}
}));
}
else {
next();
}
});