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:
Matteo Pagliazzi
2018-10-18 12:14:07 +02:00
committed by GitHub
parent 061d990e39
commit 1f44444a50
5 changed files with 86 additions and 9 deletions
+11 -1
View File
@@ -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 });
}
};
+6 -1
View File
@@ -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) {
+6 -1
View File
@@ -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