Skip to content

Commit

Permalink
fix tax calculations on next object with single-use coupons
Browse files Browse the repository at this point in the history
  • Loading branch information
wsethbrown committed Nov 2, 2023
1 parent 9accefa commit 7510b22
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 17 deletions.
38 changes: 33 additions & 5 deletions lib/recurly/pricing/checkout/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ export default class Calculations {
this.price.next.discount = discountNext;
}

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

return promise;
}

/**
Expand Down Expand Up @@ -339,18 +341,44 @@ 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 };
}

// discountAmounts () {
// const coupon = this.items.coupon;
// let discountNow = 0;
// let discountNext = 0;
// if (coupon) {
// if (coupon.discount.type === 'free_trial') {
// // Amounts are left zero
// } 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);
// } 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);
// }
// }
// return { discountNow, discountNext };
// }
/**
* Computes the discountable subtotals for a given coupon
*
Expand Down
40 changes: 28 additions & 12 deletions test/unit/pricing/checkout/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1750,20 +1750,36 @@ describe('CheckoutPricing', function () {
});

describe('when a coupon is applied', () => {
beforeEach(function () {
return this.pricing.coupon('coop-pct-all');
describe('..', () => {

beforeEach(function () {
return this.pricing.coupon('coop-pct-all');
});

it('taxes the discounted subtotal', function (done) {
this.pricing
.reprice()
.done(price => {
// 8.75% of taxable amount: 21.99 (sub) + 40 (adj) - 9 (discount) = 52.99
assert.equal(price.now.taxes, '4.64');
// 8.75% of taxable amount: 19.99 (sub) - 3 (discount) = 16.99
assert.equal(price.next.taxes, '1.49');
done();
});
});
});

it('taxes the discounted subtotal', function (done) {
this.pricing
.reprice()
.done(price => {
// 8.75% of taxable amount: 21.99 (sub) + 40 (adj) - 9 (discount) = 52.99
assert.equal(price.now.taxes, '4.64');
// 8.75% of taxable amount: 19.99 (sub) - 3 (discount) = 16.99
assert.equal(price.next.taxes, '1.49');
done();
});
describe.only('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

0 comments on commit 7510b22

Please sign in to comment.