#harshit_webhooks
1 messages · Page 1 of 1 (latest)
👋 Welcome to your new thread!
⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.
🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1458762381080662030
📝 Have more to share? Add more details, code, screenshots, videos, etc. below.
hi there!
can you share the webhook endpoint ID you are using (we_xxx), and the last 4 characters of the webhook secret you are using in your code?
- we_1SmZe1CkhLwmVIDWByJF9OaC
- secret - sZ6v
thanks, checking
yes, that looks correct.
next, can you log the raw payload you get from Stripe? to see if it's indeed the raw payload, or if it was modified by your code/framework.
yes sure, sharing with you - in the doc example they ask for raw - in the solution thready - they are again stringifying it
so you get a stringified payload?
content after 'stripe body buffer' is the raw one
content above it is its stringified version - which I stringified for logging purpose
not able to copy the buffer properly that's why sended SS
can you share the code of your webhook endpoint?
keys are there in code - should I send it over here?
function(req,res,next) {
console.log('stripe body string',req.body.toString())
console.log('stripe body buffer',req.body)
const stripe = require('stripe')('stripe-key');
const endpointSecret = "secret";
const signature = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body.toString(), signature, endpointSecret);
}
catch (err) {
console.log(`Webhook Error: ${err}`);
}
console.log('event',event)
}
stripe.webhooks.constructEvent(req.body.toString()
whytoString? you need the raw body, so without any modification from your code/framework.
I tried both - they are failing
- doc mentions raw payload
- https://github.com/stripe/stripe-node/issues/341#issuecomment-304497470 - mentions stringified payload -
got it. and in the example you shared they are using bodyParser.raw({type: '*/*'}). are you doing the same?
yes
just made another request with req.body only - still same error
I used this - .use(bodyParser.raw({type: '/'}))
then the above shared function with both stringified and not stringified version of req.body
got it, it can be tricky to get to the raw payload. have you tried all solutions mentioned in the github link?
let me try this - https://github.com/stripe/stripe-node/issues/341#issuecomment-304733080
tried this - (let me try this - https://github.com/stripe/stripe-node/issues/341#issuecomment-304733080)
its logging the body as stringified but its failing -
So what does your code look like now?
express.js
app.route('/stripehook').post(bodyParser.raw({ type: '/' }),homeApiController.stripeApiHandler,doApi,homeApiController.stripeResponse)
app.use(bodyParser.json());
homeApiController controller file
exports.stripeApiHandler = function(req,res,next) {
console.log('stripe body buffer',req.body)
const stripe = require("stripe")(config.stripeSecKey);
const endpointSecret = "whsec_..";
const signature = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.body, signature, endpointSecret);
}
catch (err) {
console.log(`Webhook Error: ${err}`);
}
console.log('event',event)
};
similar to this - https://github.com/stripe/stripe-node/issues/341#issuecomment-497408492
console.log('stripe body buffer',req.body) - in this case the buffer log was like in SS
Yeah, that looks reasonable to me. Does it work if you comment out the console.log()?
Did you try already with app.post('/webhook', express.raw({type: 'application/json'}), handler … like in the docs here?
https://docs.stripe.com/webhooks?lang=node#verify-webhook-signatures-with-official-libraries
yes - this was my first implementation
can this cause error?
10 mins please sir - lunch
the logs should have no impact on the call to constructEvent.
anything you found wrong with my code -
endpointSecret I verified, stripeSecKey is the sandbox account details: Secret Key
not sure how to help sorry, you'll need to try all the suggestions in the github link, to find a way to get the raw body.