mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +03:00
Removed old product when subscription is updated
refs https://github.com/TryGhost/Team/issues/979 This correctly handles updates to subscriptions so that if the product a subscription is for has changed, we will remove the previous product, if and only if there is not another subscription which gives access to it.
This commit is contained in:
parent
16d67203cf
commit
d3b1283241
@ -601,8 +601,43 @@ module.exports = class MemberRepository {
|
|||||||
let status = memberProducts.length === 0 ? 'free' : 'comped';
|
let status = memberProducts.length === 0 ? 'free' : 'comped';
|
||||||
if (this.isActiveSubscriptionStatus(subscription.status)) {
|
if (this.isActiveSubscriptionStatus(subscription.status)) {
|
||||||
status = 'paid';
|
status = 'paid';
|
||||||
if (ghostProduct) {
|
if (!model) {
|
||||||
memberProducts.push(ghostProduct.toJSON());
|
// This is a new subscription! Add the product
|
||||||
|
if (ghostProduct) {
|
||||||
|
memberProducts.push(ghostProduct.toJSON());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (model.get('stripe_price_id') !== subscriptionData.stripe_price_id) {
|
||||||
|
// The subscription has changed plan - we may need to update the products
|
||||||
|
memberProducts.push(ghostProduct.toJSON());
|
||||||
|
|
||||||
|
const subscriptions = await member.related('stripeSubscriptions').fetch(options);
|
||||||
|
const changedProduct = await this._productRepository.get({
|
||||||
|
stripe_price_id: model.get('stripe_price_id')
|
||||||
|
}, options);
|
||||||
|
|
||||||
|
let activeSubscriptionForChangedProduct = false;
|
||||||
|
|
||||||
|
for (const subscription of subscriptions.models) {
|
||||||
|
if (this.isActiveSubscriptionStatus(subscription.get('status'))) {
|
||||||
|
try {
|
||||||
|
const subscriptionProduct = await this._productRepository.get({stripe_price_id: subscription.get('stripe_price_id')});
|
||||||
|
if (subscriptionProduct && changedProduct && subscriptionProduct.id === changedProduct.id) {
|
||||||
|
activeSubscriptionForChangedProduct = true;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this._logging.error(`Failed to attach products to member - ${data.id}`);
|
||||||
|
this._logging.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!activeSubscriptionForChangedProduct) {
|
||||||
|
memberProducts = memberProducts.filter((product) => {
|
||||||
|
return product.id !== changedProduct.id;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const subscriptions = await member.related('stripeSubscriptions').fetch(options);
|
const subscriptions = await member.related('stripeSubscriptions').fetch(options);
|
||||||
|
Loading…
Reference in New Issue
Block a user