#darius-webhook
1 messages · Page 1 of 1 (latest)
Hi! Yes this kind of issue can be complex to debug.
Can you try to add some logs to check:
- If you have a signature at all
- The webhook secret you have in your code is the same as the one in the dashboard
For AWS Gateway we recommend to set up a Body Mapping Template like this (to access the raw body):
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
}
}
Thanks for your reply. I'm writing it in the serverless .yml and it's like this now:
request:
template:
application/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
}}'
For now, it throws an error: bad indentation of a mapping entry in
252 | ... t($input.body).replaceAll("\\'", "'")",
------------------------------------------^
Will fix it and let you know how it goes
I actually started using the v2 API Gateway with httpApi and it seem like working. I guess the httpApi doesn't alter the request body so much or something.
stripeWebhook:
handler: src/api/webhooks/StripeListener.main
events:
- httpApi:
method: POST
path: /webhooks/stripe
And then my handler
// Retrieve the event by verifying the signature using the raw body and secret.
const bodyData = event.body
// Fetch Stripe webhook secret
const webhookSecret = (await getStripeSigningKey()).Parameter.Value
// Config Stripe SDK
const stripe = await configStripe()
const bodyBuffer = Buffer.from(bodyData, 'utf8')
// Initiate + verify stripe Event
const stripeEvent = stripe.webhooks.constructEvent(
bodyBuffer,
event.headers['stripe-signature'] || event.headers['Stripe-Signature'],
webhookSecret
)
[...]
Am testing it now, but seem like a success from Stripe dashboard.
Thank you!
Awesome, glad to hear you managed to solve the issue!