Skip to content

Commit

Permalink
Merge pull request #858 from recurly/coop-taxes
Browse files Browse the repository at this point in the history
fix tax calculations on next object with single-use coupons
  • Loading branch information
chrissrogers authored Nov 3, 2023
2 parents 9accefa + 0669732 commit fc1253f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
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

0 comments on commit fc1253f

Please sign in to comment.