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

Round tax amount instead of ceil #690

Merged
merged 1 commit into from
Oct 27, 2021
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
10 changes: 5 additions & 5 deletions lib/recurly/pricing/checkout/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Promise from 'promise';
import uniq from 'array-unique';
import decimalizeMember from '../../../util/decimalize-member';
import groupBy from '../../../util/group-by';
import taxCeil from '../../../util/tax-ceil';
import taxRound from '../../../util/tax-round';

/**
* Checkout pricing calculation handler
Expand Down Expand Up @@ -184,8 +184,8 @@ export default class Calculations {

// If tax amount has been specified, simply apply it
if (this.items.tax && this.items.tax.amount) {
this.price.now.taxes = taxCeil(this.items.tax.amount.now);
this.price.next.taxes = taxCeil(this.items.tax.amount.next);
this.price.now.taxes = taxRound(this.items.tax.amount.now);
this.price.next.taxes = taxRound(this.items.tax.amount.next);
return Promise.resolve();
}

Expand Down Expand Up @@ -232,8 +232,8 @@ export default class Calculations {
return taxRates.indexOf(taxRate) === i;
}).map(JSON.parse);
this.price.taxes = taxRates;
this.price.now.taxes = taxCeil(taxNow);
this.price.next.taxes = taxCeil(taxNext);
this.price.now.taxes = taxRound(taxNow);
this.price.next.taxes = taxRound(taxNext);
});
}

Expand Down
12 changes: 6 additions & 6 deletions lib/recurly/pricing/subscription/calculations.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import each from 'component-each';
import find from 'component-find';
import isEmpty from 'lodash.isempty';
import decimalizeMember from '../../../util/decimalize-member';
import taxCeil from '../../../util/tax-ceil';
import taxRound from '../../../util/tax-round';

/**
* Subscription pricing calculation handler
Expand Down Expand Up @@ -94,8 +94,8 @@ export default class Calculations {

// If tax amount has been specified, simply apply it
if (this.items.tax && this.items.tax.amount) {
this.price.now.tax = taxCeil(this.items.tax.amount.now);
this.price.next.tax = taxCeil(this.items.tax.amount.next);
this.price.now.tax = taxRound(this.items.tax.amount.now);
this.price.next.tax = taxRound(this.items.tax.amount.next);
return done.call(this);
}

Expand All @@ -118,9 +118,9 @@ export default class Calculations {
this.price.taxes.push(tax);
});

// tax estimation prefers partial cents to always round up
this.price.now.tax = taxCeil(this.price.now.tax);
this.price.next.tax = taxCeil(this.price.next.tax);
// tax estimation prefers partial cents to round to the nearest cent
this.price.now.tax = taxRound(this.price.now.tax);
this.price.next.tax = taxRound(this.price.next.tax);
}
done.call(this);
});
Expand Down
14 changes: 0 additions & 14 deletions lib/util/tax-ceil.js

This file was deleted.

14 changes: 14 additions & 0 deletions lib/util/tax-round.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Round the second decimal of a number without risk of
* floating point math errors
*
* - returns zero if the result is negative
*
* @param {Number} number
* @return {Number}
*/

export default function taxRound (number) {
number = Math.max(number, 0);
return +((number < 0 ? -1 : 1) * Math.round(Math.abs(number) + 'e+2') + 'e-2');
}
4 changes: 2 additions & 2 deletions test/unit/pricing/checkout/checkout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1589,8 +1589,8 @@ describe('CheckoutPricing', function () {
it('applies taxes to the price', function (done) {
this.pricing.reprice().done(price => {
assert.equal(price.now.subtotal, 61.99); // 21.99 + 20 + 20
assert.equal(price.now.taxes, 5.43); // 8.75% of 61.99
assert.equal(price.now.total, 67.42);
assert.equal(price.now.taxes, 5.42); // 8.75% of 61.99
assert.equal(price.now.total, 67.41);
assert.equal(price.next.subtotal, 19.99);
assert.equal(price.next.taxes, 1.75); // 8.75% of 19.99
assert.equal(price.next.total, 21.74);
Expand Down
10 changes: 5 additions & 5 deletions test/unit/pricing/subscription/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('Recurly.Pricing.Subscription', function () {
})
.giftcard("hundred-dollar-card")
.done(function (price) {
assert.equal(price.now.gift_card, '23.92');
assert.equal(price.now.gift_card, '23.91');
assert.equal(price.next.gift_card, '21.74');
assert.equal(price.now.total, '0.00');
assert.equal(price.next.total, '0.00');
Expand All @@ -65,7 +65,7 @@ describe('Recurly.Pricing.Subscription', function () {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.now.tax, '1.93');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('Recurly.Pricing.Subscription', function () {
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.taxes[0].region, 'CA');
assert.equal(price.now.tax, '1.93');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('Recurly.Pricing.Subscription', function () {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.now.tax, '1.93');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
Expand All @@ -172,7 +172,7 @@ describe('Recurly.Pricing.Subscription', function () {
assert.equal(price.taxes.length, 1);
assert.equal(price.taxes[0].type, 'us');
assert.equal(price.taxes[0].rate, '0.0875');
assert.equal(price.now.tax, '1.93');
assert.equal(price.now.tax, '1.92');
assert.equal(price.next.tax, '1.75');
done();
});
Expand Down