Add a formatted order total to the checkout receipt
Node · Node · intermediate · modification
Adds formatReceiptTotal to the checkout money helpers: it reuses subtotalCents, applies the coupon (in cents), clamps the total at zero so a coupon can never make it negative, and returns a display-ready US dollar string for the receipt. Reuses subtotalCents so the receipt and the charged amount can never disagree about the cart total.
formatReceiptTotal renders the grand total on the order-confirmation receipt the customer sees after paying; it is the headline number on that page. Money elsewhere in checkout is integer cents (the payment processor charges in the smallest currency unit).
Requirements
- subtotalCents() keeps its existing contract: money in this module is handled in integer cents to avoid floating-point drift, and subtotalCents returns the cart subtotal in whole cents. The existing callers must keep working unchanged.
- Add formatReceiptTotal(items, couponCents): couponCents is a discount amount in integer cents. Subtract it from the subtotal, clamp the result so the total is never negative, then return the total formatted as a US dollar string like "$19.99" (dollars, two decimals). 1 dollar = 100 cents, so convert cents to dollars wherever the cent-denominated total meets the dollar display string.
- Line-item fields (priceCents, qty) and couponCents are validated upstream and are non-negative integers of ordinary receipt magnitude — the subtotal stays far within JavaScript's safe-integer range in both cents and dollars; return the formatted string only, no rounding beyond the two displayed decimals.
Files touched
- src/checkout/money.js
--- src/checkout/money.js
}
-module.exports = { subtotalCents };
+/**
+ * Format the order total for the receipt.
+ *
+ * Subtracts the coupon (also in cents), clamped so the total is never
+ * negative, and returns it as a US dollar string like "$19.99".
+ *
+ * @param {number} couponCents discount to apply, in integer cents.
+ * @returns {string} the total formatted in dollars.
+ */
+function formatReceiptTotal(items, couponCents) {
+ const totalCents = Math.max(subtotalCents(items) - couponCents, 0);
+ return `$${totalCents.toFixed(2)}`;
+}
+module.exports = { subtotalCents, formatReceiptTotal };
+