Rebasely Beta: data may be purged

examples/storefront Public

TypeScript · 19 lines · 855 B · at 8b37e66

// Money is whole cents in the shop's one currency. Orders under the free shipping threshold pay// a flat shipping rate.export const CURRENCY = "usd";export const FREE_SHIPPING_CENTS = 5000;export const SHIPPING_CENTS = 595;export type Line = { unit_price_cents: number; quantity: number };export type Totals = { subtotal_cents: number; shipping_cents: number; total_cents: number; currency: string };export function totals(lines: Line[]): Totals {  const subtotal = lines.reduce((sum, l) => sum + l.unit_price_cents * l.quantity, 0);  const shipping = subtotal === 0 || subtotal >= FREE_SHIPPING_CENTS ? 0 : SHIPPING_CENTS;  return { subtotal_cents: subtotal, shipping_cents: shipping, total_cents: subtotal + shipping, currency: CURRENCY };}export function formatCents(cents: number): string {  return `$${(cents / 100).toFixed(2)}`;}