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

fix tax calculations on next object with single-use coupons #858

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
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
14 changes: 9 additions & 5 deletions lib/recurly/pricing/checkout/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ export default class Calculations {
this.price.next.discount = discountNext;
}

return promise.then(() => {
if (coupon.single_use) this.price.next.discount = 0;
});
return promise;
}

/**
Expand Down Expand Up @@ -329,6 +327,7 @@ export default class Calculations {
*
* @return {Object} { discountNow, discountNext }
*/

discountAmounts () {
const coupon = this.items.coupon;
let discountNow = 0;
Expand All @@ -339,13 +338,18 @@ export default class Calculations {
} else if (coupon.discount.rate) {
const { discountableNow, discountableNext } = this.discountableSubtotals(coupon, { setupFees: false });
discountNow = roundForDiscount(discountableNow * coupon.discount.rate);
discountNext = roundForDiscount(discountableNext * coupon.discount.rate);
// If coupon is single use, we want discountNext to be zero
if (!coupon.single_use) {
discountNext = roundForDiscount(discountableNext * coupon.discount.rate);
}
} else if (coupon.discount.amount) {
const { discountableNow, discountableNext } = this.discountableSubtotals(coupon);
// Falls back to zero if the coupon does not support the checkout currency
const discountAmount = coupon.discount.amount[this.items.currency] || 0;
discountNow = Math.min(discountableNow, discountAmount);
discountNext = Math.min(discountableNext, discountAmount);
if (!coupon.single_use) {
discountNext = Math.min(discountableNext, discountAmount);
}
}
}
return { discountNow, discountNext };
Expand Down
13 changes: 13 additions & 0 deletions test/unit/pricing/checkout/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1765,6 +1765,19 @@ describe('CheckoutPricing', function () {
done();
});
});

describe('is single-use and applies to subscriptions and adjustments with taxes', () => {
beforeEach(applyCoupon('coop-single-use'));

it('discounts only the subscriptions now, and applies no discounts next cycle', function () {
assert.equal(this.price.now.subtotal, 41.99); // 19.99 + 2 (setup fee) + 20 (adj) + 20 (adj) - $20 discount
assert.equal(this.price.now.discount, 20);
assert.equal(this.price.next.subscriptions, 19.99);
assert.equal(this.price.next.discount, 0);
assert.equal(this.price.now.taxes, 3.67);
assert.equal(this.price.next.taxes, 1.75);
});
});
});
});

Expand Down
Loading