0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-03-18 02:21:47 -05:00

🐛 Fixed cancelling subscriptions when destroying

refs https://github.com/TryGhost/Ghost/issues/12711

We must wait for the stripeSubscriptions relation to be loaded before
attempting to loop through them. As well as this we should use `upsert`
so that we can edit a subscription record by `subscription_id`, rather
than the (internal) `id`
This commit is contained in:
Fabien O'Carroll 2021-03-01 11:23:58 +00:00
parent 8b4e5f9a92
commit 095c624172

View file

@ -169,16 +169,20 @@ module.exports = class MemberRepository {
}
if (this._stripeAPIService && options.cancelStripeSubscriptions) {
await member.related('stripeSubscriptions');
await member.related('stripeSubscriptions').fetch();
const subscriptions = member.related('stripeSubscriptions');
for (const subscription of subscriptions.models) {
if (subscription.get('status') !== 'canceled') {
const updatedSubscription = await this._stripeAPIService.cancelSubscription(
subscription.get('subscription_id')
);
await this._StripeCustomerSubscription.update({
await this._StripeCustomerSubscription.upsert({
status: updatedSubscription.status
}, options);
}, {
subscription_id: updatedSubscription.id
});
await this._MemberPaidSubscriptionEvent.add({
member_id: member.id,
source: 'stripe',