Skip to content

Instantly share code, notes, and snippets.

@SimonHoiberg
Created June 19, 2020 14:23
Show Gist options
  • Save SimonHoiberg/dbca9c89ab01ca2317cf07f033ded061 to your computer and use it in GitHub Desktop.
Save SimonHoiberg/dbca9c89ab01ca2317cf07f033ded061 to your computer and use it in GitHub Desktop.
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