#Peta Stewart

1 messages · Page 1 of 1 (latest)

unique wolfBOT
misty ginkgo
#

Hi there!

neat berry
#

Hi bismarck

misty ginkgo
#

You are using the endpoint secret that the CLI provides?

neat berry
#

yes, the same as in stripe

misty ginkgo
#

Can you share your webhook code?

#

Are you using Express?

neat berry
#

yes

misty ginkgo
#

Got it, Express is tricky here and usually messes with the raw body which leads to this issue.

#

So for instance I do something like: app.use((req, res, next) => { if (req.originalUrl === '/webhook') { next(); // Do nothing with the body because I need it in a raw state. } else { express.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe. } });

neat berry
#

router.post('/', express.raw({type: 'application/json'}), (request, response) => {
let event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
//const signature = request.headers['stripe-signature'];

    //testing below - replace line above
    const payload = {
        id: 'evt_test_webhook',
        object: 'event',
      };
    const payloadString = JSON.stringify(payload, null, 2);
    const header = stripe.webhooks.generateTestHeaderString({
        payload: payloadString,
        endpointSecret,
      });

    try {
        event = stripe.webhooks.constructEvent(
            request.body,
            //signature,
            header,
            endpointSecret
        );
    } catch (err) {
        console.log(`⚠️  Webhook signature verification failed.`, err.message);
        return response.sendStatus(400);
    }
}
#

Do you mean this?

misty ginkgo
#

The issue likely is from your route handler

neat berry
#

I get the correct error message from my code

#

console.log(⚠️ Webhook signature verification failed., err.message);

#

So the router is working fine

misty ginkgo
#

Right, can you share your Express initialization? Like I shared above?

neat berry
#

const webhookRouter = require('./routes/webhook')
app.use('/webhook', webhookRouter)

misty ginkgo
#

Are you setting express.json() anywhere?

neat berry
#

// express app setup -----------------------
const app = express()
app.use(express.static('public'))
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(cors(corsOptionsDelegate))

#

yes

misty ginkgo
#

Yep okay so see my snippet above?

neat berry
#

yes

misty ginkgo
#
    if (req.originalUrl === '/webhook') {
      next(); // Do nothing with the body because I need it in a raw state.
    } else {
      express.json()(req, res, next);  // ONLY do express.json() if the received request is NOT a WebHook from Stripe.
    }
  });```
#

Try using that in place of your app.use(express.json())

#

You'll need to adjust that slightly since looks like your endpoint route here is the index

neat berry
#

Thank you, this allowed the events to listen. I'll try address the new issues.