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

Files touched

--- 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 };
+

Review this PR

Node practice