Add a sales-tax helper to the order tax module
Node · Node · intermediate · modification
Adds taxCents to the order tax helpers: it multiplies the integer-cent subtotal by the jurisdiction's combined rate and returns the tax due in whole cents, reusing the existing rate lookup. Checked it against a few line-item orders and the tax lines matched.
taxCents feeds the tax line on every order at checkout; the cents it returns are what the payment processor charges the customer and what gets remitted to the tax authority.
Requirements
- taxRateFor keeps its contract: it returns the combined sales-tax rate for the order's jurisdiction in basis points, resolved upstream from the shipping address. The existing callers must keep working unchanged.
- Add taxCents(subtotalCents, rateBps): the tax base is the subtotal in integer cents, and the combined rate is given in basis points, so subtotalCents * rateBps / 10000 yields the tax in cents. Round that to the nearest cent (ties rounded up), as the tax authority requires, and return the tax due as an integer number of cents.
- Money in this module is handled in integer cents to avoid floating-point drift; subtotalCents and rateBps are validated upstream (non-negative, ordinary order magnitude) and stay well within JavaScript's safe-integer range.
Files touched
- src/billing/tax.js
--- src/billing/tax.js
}
-module.exports = { taxRateFor };
+/**
+ * Sales tax due on an order, in whole cents.
+ *
+ * The tax base is the subtotal in integer cents; the combined rate is
+ * given in basis points, so subtotalCents * rateBps / 10000 is the tax
+ * in cents, rounded to the nearest cent as the jurisdiction requires.
+ *
+ * @param {number} subtotalCents order subtotal in integer cents.
+ * @param {number} rateBps combined sales-tax rate in basis points, e.g. 725 for 7.25%.
+ * @returns {number} the tax due, in integer cents.
+ */
+function taxCents(subtotalCents, rateBps) {
+ return Math.trunc(subtotalCents * rateBps / 10000);
+}
+module.exports = { taxRateFor, taxCents };
+