#teevee-webhook-signatures
1 messages · Page 1 of 1 (latest)
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.
- teevee-webhook-signature, 3 hours ago, 45 messages
- teevee_14010, 14 hours ago, 50 messages
The last discussion I was told to update my middleware. I have done so but i am still getting same result.
Can you share your updated code? I'd like to see how you're constructing the event
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);
can you try using req.body instead of changing this to a string?
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?
No I wasnt next
Okay. Can you send your current contents of your server.js and and stripeRoutes file (omitting any sensitive values)?
Sure
@hot juniper yep, please omit/redact test keys as well
Should be good now
teevee-webhook-signatures
You guys closed?
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
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?
bodyparser is middleware that is likely making changes to the raw body
What should be mitigate that effect?
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