Skip to content

Strapi raw body to comply with Stripe constructEvent #4720

@lambertkevin

Description

@lambertkevin

Describe the bug
As I was playing with the Stripe webhooks api, it appears that I can't use the constructEvent method because I can't get the raw request body from strapi. A similar issue existed with Express and was resolved by using body-parser middleware but I can't figure out the strapi equivalent.

Steps to reproduce the behavior

  1. Install the stripe cli
  2. Create a simple hook in you strapi app
  3. Start forwarding to your strapi app the events from the stripe-cli (eg: stripe listen --forward-to localhost:1337/hook)
  4. Launch an event with your stripe cli (e.g.: stripe trigger payment_intent.created)
  5. See error ( Error: No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe? https://github.com/stripe/stripe-node#webhook-signing

Expected behavior
I should be able to get a correct constructEvent result by parsing the body of my request.

Screenshots
Screen Shot 2019-12-13 at 02 52 18
Screen Shot 2019-12-13 at 02 51 56

Code snippets
My hook:

  hook: async (ctx) => {
    const body = JSON.stringify(ctx.request.body);
    const endpointSecret = 'whsec_secret';
    const signature = ctx.request.headers['stripe-signature'];

    const event = stripe.webhooks.constructEvent(body, signature, endpointSecret);
    console.log(event)
}

System

  • Node.js version: 11.12.0
  • NPM version: 6.7.0
  • Strapi version: 3.0.0-beta.17.8
  • Database: postgres
  • Operating system: MacOS

Ideas
As said before, stripe is giving an example for Express here which solves the issue by using bodyParser.raw({type: 'application/json'}) as a middleware for this route.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions