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

Files touched

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

Review this PR

Node practice