Created
June 19, 2020 14:23
-
-
Save SimonHoiberg/dbca9c89ab01ca2317cf07f033ded061 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const handler = async ( | |
event: APIGatewayProxyEvent, | |
context: any, | |
callback: (err: Error | null, data: any) => void, | |
) => { | |
if (!event.body) { | |
callback(Error('Invalid body')); | |
return; | |
} | |
try { | |
// We validate that the event is coming from an | |
// authentic Stripe origin | |
const webhookEvent = stripe.webhooks.constructEvent( | |
event.body, | |
event.headers['Stripe-Signature'], | |
process.env.STRIPE_WEBHOOK_SECRET as string, | |
); | |
// Pull out the data object (customer, invoice, etc...) | |
const dataObject = webhookEvent.data.object as any; | |
// Get the corrosponding handler from the handlerMapping above | |
const handler = handlerMapping[webhookEvent.type]; | |
if (!handler) { | |
callback(null, { | |
statusCode: 400, | |
body: 'Unexpected event type', | |
}); | |
return; | |
} | |
await handler(dataObject); | |
callback(null, { | |
statusCode: 200, | |
}); | |
} catch (error) { | |
callback(error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment