Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensures setup fees are not applied to subscriptions with quantity = 0 #893

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

}
Loading