Fix subcriptions remaining time disappearing after cancelling (#10761)
* add hasCancelled method for group/user, prevent cancelling a subscription twice * wip * paypal: do not cancel a subscription twice * make sure hasCancelled and hasNotCancelled return a boolean result
This commit is contained in:
@@ -257,12 +257,18 @@ api.ipn = async function ipnApi (options = {}) {
|
||||
'recurring_payment_failed',
|
||||
'recurring_payment_expired',
|
||||
'subscr_cancel',
|
||||
'subscr_failed'];
|
||||
'subscr_failed',
|
||||
];
|
||||
|
||||
if (ipnAcceptableTypes.indexOf(txn_type) === -1) return;
|
||||
|
||||
// @TODO: Should this request billing date?
|
||||
let user = await User.findOne({ 'purchased.plan.customerId': recurring_payment_id }).exec();
|
||||
if (user) {
|
||||
// If the user has already cancelled the subscription, return
|
||||
// Otherwise the subscription would be cancelled twice resulting in the loss of subscription credits
|
||||
if (user.hasCancelled()) return;
|
||||
|
||||
await payments.cancelSubscription({ user, paymentMethod: this.constants.PAYMENT_METHOD });
|
||||
return;
|
||||
}
|
||||
@@ -274,6 +280,10 @@ api.ipn = async function ipnApi (options = {}) {
|
||||
.exec();
|
||||
|
||||
if (group) {
|
||||
// If the group subscription has already been cancelled the subscription, return
|
||||
// Otherwise the subscription would be cancelled twice resulting in the loss of subscription credits
|
||||
if (group.hasCancelled()) return;
|
||||
|
||||
await payments.cancelSubscription({ groupId: group._id, paymentMethod: this.constants.PAYMENT_METHOD });
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1421,7 +1421,12 @@ schema.methods.isSubscribed = function isSubscribed () {
|
||||
|
||||
schema.methods.hasNotCancelled = function hasNotCancelled () {
|
||||
let plan = this.purchased.plan;
|
||||
return this.isSubscribed() && !plan.dateTerminated;
|
||||
return Boolean(this.isSubscribed() && !plan.dateTerminated);
|
||||
};
|
||||
|
||||
schema.methods.hasCancelled = function hasNotCancelled () {
|
||||
let plan = this.purchased.plan;
|
||||
return Boolean(this.isSubscribed() && plan.dateTerminated);
|
||||
};
|
||||
|
||||
schema.methods.updateGroupPlan = async function updateGroupPlan (removingMember) {
|
||||
|
||||
@@ -31,7 +31,12 @@ schema.methods.isSubscribed = function isSubscribed () {
|
||||
|
||||
schema.methods.hasNotCancelled = function hasNotCancelled () {
|
||||
let plan = this.purchased.plan;
|
||||
return this.isSubscribed() && !plan.dateTerminated;
|
||||
return Boolean(this.isSubscribed() && !plan.dateTerminated);
|
||||
};
|
||||
|
||||
schema.methods.hasCancelled = function hasCancelled () {
|
||||
let plan = this.purchased.plan;
|
||||
return Boolean(this.isSubscribed() && plan.dateTerminated);
|
||||
};
|
||||
|
||||
// Get an array of groups ids the user is member of
|
||||
|
||||
Reference in New Issue
Block a user