Skip to content

Instantly share code, notes, and snippets.

View SimonHoiberg's full-sized avatar

Simon Høiberg SimonHoiberg

View GitHub Profile
interface IBody {
paymentMethodID: string;
customerID: string;
}
export const handler = async (
event: APIGatewayProxyEvent,
context: any,
callback: (err: Error | null, data: any) => void,
) => {
if (!event.body) {
callback(Error('Invalid body'));
return;
}
# serverless.yml
stripe-webhooks:
handler: dist/lambdas/stripe/webhooks.handler
events:
- http:
path: stripe/webhooks
method: post
environment:
STRIPE_SECRET_KEY: PUT-YOUR-SECRET-KEY-HERE
STRIPE_WEBHOOK_SECRET: PUT-YOUR-SIGNING-SECRET-HERE
const paymentSucceeded = async (dataObject: any) => {
const customerID = dataObject['customer'] as string;
if (!customerID) {
throw Error(`No customer with ID "${customerID}"`);
}
const customerEmail = dataObject['customer_email'] as string;
const customerName = dataObject['customer_name'] as string;
const linkToInvoice = dataObject['hosted_invoice_url'] as string;
const paymentFailed = async (dataObject: any) => {
// ...This is more or less identical with the paymentSucceeded function above.
// Send an email notifying about the failed payment.
};
const subscriptionDeleted = async (dataObject: any) => {
// ...This is more or less identical with the paymentSucceeded function above.
// Send an email confirming that there subscription has now ended.
};
const handlerMapping: { [key: string]: Function } = {
'invoice.payment_succeeded': paymentSucceeded,
'invoice.payment_failed': paymentFailed,
'customer.subscription.deleted': subscriptionDeleted,
};
export const handler = async (
event: APIGatewayProxyEvent,
context: any,
callback: (err: Error | null, data: any) => void,
) => {
if (!event.body) {
callback(Error('Invalid body'));
return;
}
class StripeManager {
}
export default StripeManager;
export interface IStripeCustomer {
id: string;
object: string;
address?: any;
balance: number;
created: number;
currency: string;
default_source?: any;
delinquent: boolean;
description: string;