#teevee-webhook-signatures

1 messages · Page 1 of 1 (latest)

lofty coralBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

hot juniper
#

The last discussion I was told to update my middleware. I have done so but i am still getting same result.

merry jungle
#

Can you share your updated code? I'd like to see how you're constructing the event

hot juniper
#

Sure, this is my server.js file:

pp.use('/webhook', express.raw({ type: 'application/json' }), (req, res, next) => {
req.rawBody = req.body.toString('utf-8');
next();
});

app.use(bodyParser.json());
app.use('/webhook', stripeRoutes);
app.set('view engine', 'ejs');

this is my stripeRoutes file:

router.post('/webhook', async (req, res) => {
const sig = req.headers['stripe-signature'];
const rawBody = req.rawBody || req.body.toString('utf-8'); // Use req.rawBody if available

console.log('Received raw payload:', rawBody);

// Replace with your actual webhook secret
const webhookSecret = 'whsec_CpUBZVWHKGnbPJNmuOjYdMNzFpiq6VP5';

// Verify the signature
try {
const event = stripe.webhooks.constructEvent(rawBody, sig, webhookSecret);

merry jungle
#

can you try using req.body instead of changing this to a string?

hot juniper
#

for this line of code in server.js:
app.use('/webhook', express.raw({ type: 'application/json' }), (req, res, next) => {
req.rawBody = req.body.toString('utf-8');
next();
});

Change to:
pp.use('/webhook', express.raw({ type: 'application/json' }), (req, res, next) => {
req.rawBody = req.body;
next();
});

#

I got the same server response. Do I need to change any part of the code in the stripeRoutes file?

merry jungle
#

Out of curiosity, are you using bodyParser with Next.js?

hot juniper
#

No I wasnt next

merry jungle
#

Okay. Can you send your current contents of your server.js and and stripeRoutes file (omitting any sensitive values)?

hot juniper
#

Sure

merry jungle
#

@hot juniper yep, please omit/redact test keys as well

hot juniper
lofty coralBOT
#

teevee-webhook-signatures

hot juniper
#

You guys closed?

merry jungle
#

I'm still here while we dig into this one! We close the main channel though

#

I think the issue is with this:
const bodyParser = require('body-parser');

#

Your code is doing a lot right now so it's a bit difficult to debug. I recommend starting with the webhook builder code we have for Node.js and using this in a separate project: https://stripe.com/docs/webhooks/quickstart?lang=node

Once you can validate this works, start adding components of your application one by one and confirming along the way that it still works. You should test each step to see which change ultimately leads to an error

hot juniper
#

Ok, I will try using the builder. What do you think the issue is with: const bodyParser = require('body-parser');

Should i remove it or modify it?

merry jungle
#

bodyparser is middleware that is likely making changes to the raw body

hot juniper
#

What should be mitigate that effect?

merry jungle
#

I'm not sure I follow the question. Constructing the event requires the raw, unadulterated body of the request. Errors are expected if there is middleware that is editing that body