#Romannn-webhook-signatures

1 messages · Page 1 of 1 (latest)

coarse heath
#

You're using node right, do you have any middleware that's messing with the raw request body?

peak epoch
#

only this , express.raw({ type: 'application/json' }),

#

thats what it says in the docs

coarse heath
#

can you show me more of the code around those lines? All the code that you use to start up your express server would be helpful

peak epoch
#

request.post('/webhook', express.json({ type: 'application/json' }), (req, res, next) => {
    let event = req.body;
      // at https://dashboard.stripe.com/webhooks
      const endpointSecret = 'SECRET';
      if (endpointSecret) {
        const sig = req.headers['stripe-signature'];

        let event;
      
        try {
          event = stripeAPI.webhooks.constructEvent(req.body, sig, endpointSecret);
        } catch (err) {
          res.status(400).send(`Webhook Error: ${err.message}`);
          console.log(err)
          return;
        }
      }
      let subscription;
      let status;
      //console.log(event.type)
      return res.sendStatus(200)
      switch (event.type) {
        case 'customer.subscription.trial_will_end':
          subscription = event.data.object;
          status = subscription.status;
          console.log(`Subscription status is ${status}.`);
          break;
        case 'customer.subscription.deleted':
          subscription = event.data.object;
          status = subscription.status;
          console.log(`Subscription status is ${status}.`);
          break;
        case 'customer.subscription.created':
          subscription = event.data.object;
          status = subscription.status;
          console.log(`Subscription status is ${status}.`);
          break;
        case 'customer.subscription.updated':
          subscription = event.data.object;
          status = subscription.status;
          console.log(`Subscription status is ${status}.`);
          break;
        default:
          console.log(`Unhandled event type ${event.type}.`);
          //console.log(event.data)
      }
      res.sendStatus(200);
    }
  );```
#
    app.use(express.json())
        .use(express.urlencoded({ extended: true }))
        .engine('html', ejs.renderFile)
        .set('view engine', 'ejs')
        .use(express.static(path.join(__dirname, 'public')))
        .set('views', path.join(__dirname, 'views'))
        .set('port', PORT)
        .use(session({
            saveUninitialized: false,
            cookie: { maxAge: 86400000 },
            resave: false,
            secret: SessionPassword
        }))
        .use(passport.initialize())
        .use(passport.session())
        .use(router)
    app.listen(app.get('port'), () => console.log('Syatyed server on port'))
coarse heath
#

Before the line app.use(express.json()) can you put this line:

app.use("/webhook", express.raw({ type: "*/*" }));

peak epoch
#

yeah

#

thank you :3

coarse heath
#

did it work?

peak epoch
#

yes :3

coarse heath
#

hurrah!

harsh galleon
#

wow congrats @coarse heath, this can be so hard to debug reliably