From db67d05b979a28373095f363cb5ceae127129187 Mon Sep 17 00:00:00 2001 From: Christopher Rogers Date: Thu, 22 Aug 2024 15:23:58 -0600 Subject: [PATCH] Ensures setup fees are not applied to subscriptions with quantity = 0 --- .../pricing/subscription/calculations.js | 51 +++++++++---------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/lib/recurly/pricing/subscription/calculations.js b/lib/recurly/pricing/subscription/calculations.js index b22d02c3..0659af4f 100644 --- a/lib/recurly/pricing/subscription/calculations.js +++ b/lib/recurly/pricing/subscription/calculations.js @@ -47,6 +47,26 @@ export default class Calculations { }); } + get isTrial () { + const coupon = this.items.coupon; + if (coupon && coupon.discount.type === 'free_trial') return true; + return !!this.items.plan.trial; + } + + get planPrice () { + const plan = this.items.plan; + if (!plan) return { amount: 0, setup_fee: 0 }; + let price = plan.price[this.items.currency]; + let quantity = this.planQuantity; + if (isNaN(quantity)) quantity = 1; + price.amount = price.unit_amount * quantity; + return price; + } + + get planQuantity () { + return parseInt(this.items.plan?.quantity, 10); + } + /** * Calculates subtotal * @@ -145,11 +165,11 @@ export default class Calculations { this.price.base.plan.unit = base.unit_amount; this.price.base.plan.setup_fee = base.setup_fee; - const amount = this.planPrice().amount; + const amount = this.planPrice.amount; this.price.now.plan = amount; this.price.next.plan = amount; - if (this.isTrial()) this.price.now.plan = 0; + if (this.isTrial) this.price.now.plan = 0; } /** @@ -185,7 +205,7 @@ export default class Calculations { this.price.base.addons[addon.code] = unitPrice; this.price.addons[addon.code] = totalPrice; // DEPRECATED - if (!this.isTrial()) this.price.now.addons += totalPrice; + if (!this.isTrial) this.price.now.addons += totalPrice; this.price.next.addons += totalPrice; }); } @@ -226,7 +246,7 @@ export default class Calculations { * @private */ setupFee () { - this.price.now.setup_fee = this.planPrice().setup_fee; + this.price.now.setup_fee = this.planQuantity > 0 ? this.planPrice.setup_fee : 0; this.price.next.setup_fee = 0; } @@ -261,27 +281,4 @@ export default class Calculations { return { used, remains }; } } - - /** - * Get the price structure of the current plan on the current currency - * - * @return {Object} { amount, setup_fee } - * @private - */ - planPrice () { - const plan = this.items.plan; - if (!plan) return { amount: 0, setup_fee: 0 }; - let price = plan.price[this.items.currency]; - let quantity = parseInt(plan.quantity, 10); - if (isNaN(quantity)) quantity = 1; - price.amount = price.unit_amount * quantity; - return price; - } - - isTrial () { - const coupon = this.items.coupon; - if (coupon && coupon.discount.type === 'free_trial') return true; - return this.items.plan.trial; - } - }