#codeninja
1 messages · Page 1 of 1 (latest)
Yup tried to convert to string as well as buffer but it then said
No signatures found matching the expected signature for payload. Are you passing the raw request body you received from Stripe?
let event = stripe.webhooks.constructEvent(
JSON.stringify(request.body),
sig,
endpointSecret
);
const jsonString = JSON.stringify(request.body);
const myBuffer = Buffer.from(jsonString, 'utf-8');
let event = stripe.webhooks.constructEvent(
myBuffer,
sig,
endpointSecret
);
if you printout JSON.stringify(request.body) what do you have?
And is that node.js?
Can you just pass request.body? How about downloading our example code here: https://stripe.com/docs/webhooks/quickstart?lang=node
Looks good enough. How about the downloaded example code?
app.post(
'/api/webhook',
express.raw({ type: 'application/json' }),
(request, response) => {
let event = request.body;
console.log(
'EVENNNNNNNNNNNNNNT request.body ',
JSON.stringify(request.body)
);
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
// Get the signature sent by Stripe
const signature = request.headers['stripe-signature'];
try {
event = stripe.webhooks.constructEvent(
request.body,
signature,
endpointSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
const paymentIntent = event.data.object;
console.log(
`PaymentIntent for ${paymentIntent.amount} was successful!`
);
// Then define and call a method to handle the successful payment intent.
// handlePaymentIntentSucceeded(paymentIntent);
break;
case 'payment_method.attached':
const paymentMethod = event.data.object;
// Then define and call a method to handle the successful attachment of a PaymentMethod.
// handlePaymentMethodAttached(paymentMethod);
break;
default:
// Unexpected event type
console.log(`Unhandled event type ${event.type}.`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
}
);
Just fyi it also throws an error even the request.body is stringify as I have mentioned earlier
Hey! Taking over for my colleague. Let me catch up.
When dealing with webhook event, you need to do nothing to the body request
Here is a good sample of how to handle webhook events:
https://github.com/stripe/stripe-node/blob/master/examples/webhook-signing/express/main.ts
Hey thanks @ivory radish but I did not do anything to body request on my initial tests
There must be some middelware in your express app that does modify the request
try to run the file I shared with you in your tests
I did try that and it works, I think you are correct and we have middleware some where. I can take it form here thanks for your help!