From 651a99a48e3fbb9c771b2042e06cf1283a50aba0 Mon Sep 17 00:00:00 2001 From: forehalo Date: Wed, 17 Apr 2024 16:25:03 +0800 Subject: [PATCH] fix(server): avoid store unpaid subscriptions --- .../server/src/plugins/payment/service.ts | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/packages/backend/server/src/plugins/payment/service.ts b/packages/backend/server/src/plugins/payment/service.ts index 58d7351602..758b8bd677 100644 --- a/packages/backend/server/src/plugins/payment/service.ts +++ b/packages/backend/server/src/plugins/payment/service.ts @@ -95,7 +95,10 @@ export class SubscriptionService { }); oldSubscriptions.data.forEach(sub => { - if (sub.items.data[0].price.lookup_key) { + if ( + (sub.status === 'past_due' || sub.status === 'canceled') && + sub.items.data[0].price.lookup_key + ) { const [oldPlan] = decodeLookupKey(sub.items.data[0].price.lookup_key); if (oldPlan === SubscriptionPlan.Pro) { canHaveEarlyAccessDiscount = false; @@ -415,23 +418,22 @@ export class SubscriptionService { @OnEvent('customer.subscription.created') @OnEvent('customer.subscription.updated') async onSubscriptionChanges(subscription: Stripe.Subscription) { - const user = await this.retrieveUserFromCustomer( - subscription.customer as string - ); + if (subscription.status === 'active') { + const user = await this.retrieveUserFromCustomer( + subscription.customer as string + ); - await this.saveSubscription(user, subscription); + await this.saveSubscription(user, subscription); + } else { + await this.onSubscriptionDeleted(subscription); + } } @OnEvent('customer.subscription.deleted') async onSubscriptionDeleted(subscription: Stripe.Subscription) { - const user = await this.retrieveUserFromCustomer( - subscription.customer as string - ); - await this.db.userSubscription.deleteMany({ where: { stripeSubscriptionId: subscription.id, - userId: user.id, }, }); } @@ -749,7 +751,10 @@ export class SubscriptionService { const subscribed = oldSubscriptions.data.some(sub => { if (sub.items.data[0].price.lookup_key) { const [oldPlan] = decodeLookupKey(sub.items.data[0].price.lookup_key); - return oldPlan === plan; + return ( + oldPlan === plan && + (sub.status === 'past_due' || sub.status === 'canceled') + ); } return false; });