Skip to content

Instantly share code, notes, and snippets.

View SimonHoiberg's full-sized avatar

Simon Høiberg SimonHoiberg

View GitHub Profile
class StripeManager {
public static async createCustomer() {
try {
// Retrieve email and username of the currently logged in user.
// getUserFromDB() is *your* implemention of getting user info from the DB
const { email, username } = getUserFromDB();
if (!email || !username) {
throw Error('Email or username not found.');
}
export interface IStripeCustomer {
id: string;
object: string;
address?: any;
balance: number;
created: number;
currency: string;
default_source?: any;
delinquent: boolean;
description: string;
class StripeManager {
}
export default StripeManager;
export const handler = async (
event: APIGatewayProxyEvent,
context: any,
callback: (err: Error | null, data: any) => void,
) => {
if (!event.body) {
callback(Error('Invalid body'));
return;
}
const handlerMapping: { [key: string]: Function } = {
'invoice.payment_succeeded': paymentSucceeded,
'invoice.payment_failed': paymentFailed,
'customer.subscription.deleted': subscriptionDeleted,
};
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 paymentFailed = async (dataObject: any) => {
// ...This is more or less identical with the paymentSucceeded function above.
// Send an email notifying about the failed payment.
};
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;
# 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
export const handler = async (
event: APIGatewayProxyEvent,
context: any,
callback: (err: Error | null, data: any) => void,
) => {
if (!event.body) {
callback(Error('Invalid body'));
return;
}