Get raw request body in custom controller

Not sure why, it didnt work for me. I keep on receiving a 405
Any help is most appreciated!

> order/controllers/webhook.js

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

const stripeWebhook = async (ctx) => {
  
  const stripeSig = ctx.request.headers['stripe-signature'];
  const endpointSecret = process.env.STRIPE_WEBHOOK_SECRET;
  
  let event;
  
  try {
    const rawBody = ctx.request.body[Symbol.for("unparsedBody")];
    console.log("RAWBODY",rawBody)
    event = await stripe.webhooks.constructEvent(
      rawBody, stripeSig, endpointSecret
      );
  } catch (err) {
    ctx.response.status = 400;
    ctx.response.body = `Webhook Error: ${err.message}`;
    return;
  }
};
module.exports = { stripeWebhook };
> config/middleware.js

module.exports = [
        'strapi::errors',
        'strapi::security',
        'strapi::poweredBy',
        'strapi::logger',
        'strapi::query',
        { name: 'strapi::body', config: { includeUnparsed: true } },
        'strapi::session',
        'strapi::favicon',
        'strapi::public',
    ]
> order/routes/customerOrder.js

module.exports = {
  routes: [
    {
      method: 'POST',
      handler: 'order.createCheckoutSession',
      path: '/orders/checkout',
      config: {
        policies: [],
        middlewares: [],
      },
    },
    {
      method: 'POST',
      handler: 'webhooks.stripeWebhook',
      path: '/orders/webhook',
      config: {
        policies: [],
        middlewares: [], //'global::stripeRawBody', 
      },
    }
  ],
};
1 Like