mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-08-23 04:51:49 +08:00
fix(server): avoid store unpaid subscriptions
This commit is contained in:
@@ -95,7 +95,10 @@ export class SubscriptionService {
|
|||||||
});
|
});
|
||||||
|
|
||||||
oldSubscriptions.data.forEach(sub => {
|
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);
|
const [oldPlan] = decodeLookupKey(sub.items.data[0].price.lookup_key);
|
||||||
if (oldPlan === SubscriptionPlan.Pro) {
|
if (oldPlan === SubscriptionPlan.Pro) {
|
||||||
canHaveEarlyAccessDiscount = false;
|
canHaveEarlyAccessDiscount = false;
|
||||||
@@ -415,23 +418,22 @@ export class SubscriptionService {
|
|||||||
@OnEvent('customer.subscription.created')
|
@OnEvent('customer.subscription.created')
|
||||||
@OnEvent('customer.subscription.updated')
|
@OnEvent('customer.subscription.updated')
|
||||||
async onSubscriptionChanges(subscription: Stripe.Subscription) {
|
async onSubscriptionChanges(subscription: Stripe.Subscription) {
|
||||||
const user = await this.retrieveUserFromCustomer(
|
if (subscription.status === 'active') {
|
||||||
subscription.customer as string
|
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')
|
@OnEvent('customer.subscription.deleted')
|
||||||
async onSubscriptionDeleted(subscription: Stripe.Subscription) {
|
async onSubscriptionDeleted(subscription: Stripe.Subscription) {
|
||||||
const user = await this.retrieveUserFromCustomer(
|
|
||||||
subscription.customer as string
|
|
||||||
);
|
|
||||||
|
|
||||||
await this.db.userSubscription.deleteMany({
|
await this.db.userSubscription.deleteMany({
|
||||||
where: {
|
where: {
|
||||||
stripeSubscriptionId: subscription.id,
|
stripeSubscriptionId: subscription.id,
|
||||||
userId: user.id,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -749,7 +751,10 @@ export class SubscriptionService {
|
|||||||
const subscribed = oldSubscriptions.data.some(sub => {
|
const subscribed = oldSubscriptions.data.some(sub => {
|
||||||
if (sub.items.data[0].price.lookup_key) {
|
if (sub.items.data[0].price.lookup_key) {
|
||||||
const [oldPlan] = decodeLookupKey(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;
|
return false;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user