Skip to content

Commit

Permalink
Merge pull request #893 from recurly/exclude-setup-fees-when-qty-zero
Browse files Browse the repository at this point in the history
Ensures setup fees are not applied to subscriptions with quantity = 0
  • Loading branch information
douglasmiller authored Aug 26, 2024
2 parents bca8e4f + db67d05 commit 5285fd4
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions lib/recurly/pricing/subscription/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
});
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

}

0 comments on commit 5285fd4

Please sign in to comment.