Skip to content

Commit

Permalink
feat: add support for life-time payments
Browse files Browse the repository at this point in the history
  • Loading branch information
aalemayhu committed Oct 12, 2024
1 parent 50b7a4a commit 248e2cd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 11 deletions.
5 changes: 5 additions & 0 deletions scripts/start_stripe_proxy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash


PORT=2020
stripe listen --forward-to localhost:{PORT}/webhook
4 changes: 4 additions & 0 deletions src/data_layer/UsersRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class UsersRepository {
updatePicture(id: string, picture: string) {
return this.database(this.table).where({ id }).update({ picture });
}

updatePatreonByEmail(email: string, patreon: boolean) {
return this.database(this.table).where({ email }).update({ patreon });
}
}

export default UsersRepository;
4 changes: 2 additions & 2 deletions src/lib/integrations/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const stripe = new Stripe(process.env.STRIPE_KEY!);
export const getStripe = () => stripe;

export const getCustomerId = (
customer: string | Stripe.Customer | Stripe.DeletedCustomer
customer: string | Stripe.Customer | Stripe.DeletedCustomer | null
) => {
if (typeof customer === 'string') {
return customer;
}
return customer.id;
return customer?.id;
};

export const updateStoreSubscription = async (
Expand Down
39 changes: 30 additions & 9 deletions src/routes/WebhookRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '../lib/integrations/stripe';
import { getDatabase } from '../data_layer';
import { StripeController } from '../controllers/StripeController/StripeController';
import UsersRepository from '../data_layer/UsersRepository';

const WebhooksRouter = () => {
const router = express.Router();
Expand Down Expand Up @@ -45,6 +46,7 @@ const WebhooksRouter = () => {
const customerSubscriptionUpdated = event.data.object;
// Then define and call a function to handle the event customer.subscription.updated
const customer = await stripe.customers.retrieve(
// @ts-ignore
getCustomerId(customerSubscriptionUpdated.customer)
);

Expand All @@ -56,17 +58,36 @@ const WebhooksRouter = () => {
break;
case 'customer.subscription.deleted':
const customerSubscriptionDeleted = event.data.object;
// Then define and call a function to handle the event customer.subscription.deleted
const customerDeleted = await stripe.customers.retrieve(
getCustomerId(customerSubscriptionDeleted.customer)
);
if (typeof customerSubscriptionDeleted.customer === 'string') {
// Then define and call a function to handle the event customer.subscription.deleted
const customerDeleted = await stripe.customers.retrieve(
// @ts-ignore
getCustomerId(customerSubscriptionDeleted.customer)
);

await updateStoreSubscription(
getDatabase(),
customerDeleted as Stripe.Customer,
customerSubscriptionDeleted
);
await updateStoreSubscription(
getDatabase(),
customerDeleted as Stripe.Customer,
customerSubscriptionDeleted
);
}
break;
case 'checkout.session.completed':
const session: Stripe.Checkout.Session = event.data.object;
const amount = session.amount_total ?? 0;

const LIFE_TIME_PRICE = 9600;
if (amount >= LIFE_TIME_PRICE) {
const lifeTimeCustomer = await stripe.customers.retrieve(
// @ts-ignore
getCustomerId(session.customer)
);

const users = new UsersRepository(getDatabase());
// @ts-ignore
await users.updatePatreonByEmail(lifeTimeCustomer.email, true);
}
console.log('checkout.session.completed');
default:
console.log(`Unhandled event type ${event.type}`);
}
Expand Down

0 comments on commit 248e2cd

Please sign in to comment.