How to handle Stripe webhooks inside of Strapi

Whyy do I keep getting an error in my stripe webhook method
let body = “”;
ctx.req.on(“data”, (chunk) => {
body += chunk.toString();
});
ctx.req.on(“end”, async () => {
const signature = ctx.request.headers[“stripe-signature”];

      console.log("Webhook received");
      console.log("Headers:", JSON.stringify(ctx.request.headers, null, 2));
      console.log("Stripe-Signature:", signature);
      console.log("Raw body:", body);

      if (!signature) {
        console.error("No Stripe signature found in headers");
        ctx.response.status = 400;
        return resolve({ error: "No Stripe signature found in headers" });
      }

      let event;
      try {
        event = stripe.webhooks.constructEvent(
          body,
          signature,
          process.env.STRIPE_WEBHOOK_SECRET
        );
      } catch (err) {
        console.error(`Webhook Error: ${err.message}`);
        ctx.response.status = 400;
        return resolve({ error: `Webhook Error: ${err.message}` });
      }

      console.log("Stripe event constructed successfully:", event.type);

And then just handles the events
and post it to the database

also this is its custom route
module.exports = {
routes: [
{
method: ‘POST’,
path: ‘/webhook’,
handler: ‘commande.webhook’,
config: {
auth: false,
policies: ,
},
},
// … other routes
],
};

This topic has been created from a Discord post (1259191800850153472) to give it more visibility.
It will be on Read-Only mode here.
Join the conversation on Discord

along with the default route

const { createCoreRouter } = require(“@strapi/strapi”).factories;
const defaultRouter = createCoreRouter(“api::commande.commande”);

const customRoutes = require(“./WebhookRoute”);

const customRouter = (innerRouter, extraRoutes = ) => {
let routes;
return {
get prefix() {
return innerRouter.prefix;
},
get routes() {
if (!routes) {
routes = innerRouter.routes.concat(extraRoutes);
}
return routes;
},
};
};

module.exports = customRouter(defaultRouter, customRoutes.routes);