Closed dinilvamanan closed 1 year ago
Hi @dinilvamanan , as the error message indicates you may not be properly passing in the raw request body you received from Stripe. https://github.com/stripe/stripe-node#webhook-signing provides more details about this. Also see related issue https://github.com/stripe/stripe-node/issues/1254, which has solutions / code samples in the follow-ups that you may find helpful.
If those resources do not help, as mentioned in https://github.com/stripe/stripe-node/issues/1254, this is more of an integration question which we typically direct to our support team at https://support.stripe.com/contact. Also feel free to join our Discord at https://stripe.com/go/developer-chat where someone can help you debug directly.
EDIT: Sorry I closed too early - looking at your example more closely, your code looks correct since APIGatewayProxyEvent.body
is a string, not an object. We'll investigate this further in case this is an issue with stripe-node.
Have you confirmed that your webhook signing secret matches what is in your dashboard?
The follow-ups in https://github.com/stripe/stripe-node/issues/356 could also be helpful - you may need to add a mapping template in API Gateway to retrieve the raw body.
I confirm the secrets is directly used from dashboard, also we use stripe.wehook,generateTestHeaderString for unit testing and it all gets passed in testing. It generated consistent hash as expected only fails while verifying with request from APIGatewayProxyEvent.
Are you using an HTTP API or REST API in your AWS API Gateway? If you're using HTTP API, I believe event.body
should be the raw request body out of the box, unless you have any middleware that is transforming it.
However, if you're using REST API, you may need to do some extra configuration. You can try these 2 options:
Do you have "Use Lambda Proxy integration" turned on? If it's not turned on, request details will not be passed through to event
in your handler (this will cause event.body
to be undefined). In the AWS admin, go to your API gateway endpoint for your webhook, then go to Integration Request. You'll see this option:
If you don't want to turn on "Use Lambda Proxy integration" or that does not work, you can create a mapping template. Still under Integration Request, you can create a mapping template and access event.rawBody
instead:
{
"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
}
}
I have the same error here @anniel-stripe I'm using Vercel Service, Vercel use serverless functions and sounds like problem with body parser. In dev env (localhost), everything works fine.
This is my webhook code:
@Kellokes since this issue deals with AWS API Gateway / Lambda functions specifically and you're using Vercel / NextJS, please open a new issue to keep the two threads separate.
Before you open a new issue though, please make sure to verify that your webhook signing secret matches what's in the dashboard, as opposed to the signing secret generated by CLI used for testing. This unfortunately is what gets a lot of developers.
Indeed, the key he was using was not the correct one. Everything is working correctly now. Thanks.
@dinilvamanan Were you able to resolve this issue? If so, could you share what the issue / fix was? This would be helpful for us to add better documentation for this use case.
Closing as inactive, please feel free to reopen.
Describe the bug
Configured webhook in stripe dashboard and trying to verify the signature for webhook requests received. We use below LoC for verification provided with essential instantiation of stripe handler and others:
stripe.webhooks.constructEvent(event.body, headerSignature, webhookSecret);
event: APIGatewayProxyEventError we are getting:
To Reproduce
Try verifying webhook signature by using Lamda function with APIGatewayProxyEvent as the request router.
Expected behavior
Signature shall verify success
Code snippets
No response
OS
macod
Node version
v14.19.3
Library version
stripe-node
API version
2022-11-15
Additional context
No response