#Dishit - webhook

1 messages ยท Page 1 of 1 (latest)

stone dagger
#

Hi! Can you share your webhook code?

shut summit
#

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

stone dagger
#

Thanks! Can you log these values:

  • eventLambda.body, to check if it's the raw body
  • sig, to make sure you have a signature
  • and endpointSecret, to double check you are using the correct one
shut summit
#

i logged all these value and copied the endpointsecret from the stripe dashboard

stone dagger
#

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 rawBody property and the headers via the event's headers property.
shut summit
#

or just tell me where to add this exact, that would be a great help

stone dagger
#

I'm not really familiar with AWS, but I think you just need to copy-paste this code there:

shut summit
#

Thanks a lot

stone dagger
#

Happy to help ๐Ÿ™‚

shut summit
#

hey where can I find body mapping setting, for a perticular lambda function

steep delta
#

I don't know

shut summit
#

Thanks

#

I am getting event.body perfectly

#

then also facing the same issue

steep delta
#

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

shut summit
#

i copied secret key from the stripe dashboard

steep delta
#

const endpointSecret = 'we_1Lc3M2GrqGT9imAkYLoGURvD
I mean that is 100% wrong

shut summit
#

oh okay

#

where can i get web hook secret key ?

steep delta
#

the secret looks like whsec_xxxx and you get it like this

shut summit
#

Got It Thanks