#Dishit - webhook
1 messages ยท Page 1 of 1 (latest)
Sure , I am using in AWS lambda
exports.handler = async (eventLambda) => {
const endpointSecret = 'we_1Lc3M2GrqGT9imAkYLoGURvD';
const sig = eventLambda.headers['stripe-signature'];
console.log('eventLambda :', eventLambda);
console.log('eventLambda Header:', eventLambda.headers);
console.log('eventLambda sig:', sig);
let event;
try {
event = stripe.webhooks.constructEvent(eventLambda.body, sig, endpointSecret);
} catch (err) {
console.log('Webhook Error: ${err.message}', err.message);
return JSON.stringify({
message: 'failed',
error: `'Webhook Error: ${err.message}'`,
});
}
// Handle the event
if (event.type === 'payment_intent.succeeded') {
const paymentIntent = event.data.object;
console.log('paymentIntent', paymentIntent);
return JSON.stringify({
message: 'success',
error: `'Webhook Error: ${paymentIntent}'`,
});
}
};
here's the code
Thanks! Can you log these values:
eventLambda.body, to check if it's the raw bodysig, to make sure you have a signature- and
endpointSecret, to double check you are using the correct one
i logged all these value and copied the endpointsecret from the stripe dashboard
So I'm guessing the issue is you are not using the raw body. Here's how to get the raw body in AWS.
First, in API Gateway, set up a Body Mapping Template like this:
- Content-Type:
application/json(just this โ do not include ; charset=utf-8 afterwards) - Template contents:
{
"method": "$context.httpMethod",
"body": $input.json('$'),
"rawBody": "$util.escapeJavaScript($input.body).replaceAll("\\'", "'")",
"headers": {
#foreach($param in $input.params().header.keySet())
"$param": "$util.escapeJavaScript($input.params().header.get($param))"
#if($foreach.hasNext),#end
#end
}
}
- Then, in the Lambda function, you will be able to access the raw body via the event's
rawBodyproperty and the headers via the event'sheadersproperty.
can you please edit this in my code as i am getting confused here
or just tell me where to add this exact, that would be a great help
I'm not really familiar with AWS, but I think you just need to copy-paste this code there:
Thanks a lot
Happy to help ๐
hey where can I find body mapping setting, for a perticular lambda function
I don't know
are you using the right webhook signing secret whsec_xxx?
also I think you're maybe meant to use eventLambda.rawBody instead of eventLambda.body
i copied secret key from the stripe dashboard
const endpointSecret = 'we_1Lc3M2GrqGT9imAkYLoGURvD
I mean that is 100% wrong
the secret looks like whsec_xxxx and you get it like this
Got It Thanks