Removed coupons from subscriptions when the price changes

refs https://github.com/TryGhost/Team/issues/1092
refs https://github.com/TryGhost/Team/issues/1135

This was missed in the initial due to the issue tracking the task being
superceded, and the task not being copied across to the superceding
issue.

A new method to remove coupons has been added, as opposed to updating
the existing change subscription price method, because the removal of a
coupon is not the concern of an auxillary stripe service, but a busines
concern that should be explicit in the members-api codebase.
This commit is contained in:
Fabien O'Carroll 2021-11-09 11:12:13 +02:00
parent 4b005da248
commit 2fcafcc8dc
2 changed files with 21 additions and 5 deletions

View File

@ -861,11 +861,14 @@ module.exports = class MemberRepository {
const subscriptionItem = subscription.items.data[0];
updatedSubscription = await this._stripeAPIService.updateSubscriptionItemPrice(
subscription.id,
subscriptionItem.id,
data.subscription.price
);
if (data.subscription.price !== subscription.price) {
updatedSubscription = await this._stripeAPIService.updateSubscriptionItemPrice(
subscription.id,
subscriptionItem.id,
data.subscription.price
);
updatedSubscription = await this._stripeAPIService.removeCouponFromSubscription(subscription.id);
}
}
if (data.subscription.cancel_at_period_end !== undefined) {

View File

@ -508,6 +508,19 @@ module.exports = class StripeService {
return subscription;
}
/**
* @param {string} id - The ID of the subscription to remove coupon from
*
* @returns {Promise<import('stripe').Stripe.Subscription>}
*/
async removeCouponFromSubscription(id) {
await this._rateLimitBucket.throttle();
const subscription = await this._stripe.subscriptions.update(id, {
coupon: ''
});
return subscription;
}
/**
* @param {string} subscriptionId - The ID of the Subscription to modify
* @param {string} id - The ID of the SubscriptionItem