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
| 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.'); | |
| } |
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 interface IStripeCustomer { | |
| id: string; | |
| object: string; | |
| address?: any; | |
| balance: number; | |
| created: number; | |
| currency: string; | |
| default_source?: any; | |
| delinquent: boolean; | |
| description: string; |
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
| class StripeManager { | |
| } | |
| export default StripeManager; |
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; | |
| } |
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
| const handlerMapping: { [key: string]: Function } = { | |
| 'invoice.payment_succeeded': paymentSucceeded, | |
| 'invoice.payment_failed': paymentFailed, | |
| 'customer.subscription.deleted': subscriptionDeleted, | |
| }; |
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
| 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. | |
| }; |
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
| const paymentFailed = async (dataObject: any) => { | |
| // ...This is more or less identical with the paymentSucceeded function above. | |
| // Send an email notifying about the failed payment. | |
| }; |
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
| 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; |
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
| # 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 |
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; | |
| } |