From e29eaaa8e819abcec7f8190e526bbb9ec5e931a4 Mon Sep 17 00:00:00 2001 From: agent Date: Sun, 25 Oct 2026 14:48:12 +0000 Subject: [PATCH] Show prices in the shop currency (STRIPE_CURRENCY), not hard-coded EUR MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit formatPrice always formatted EUR, so the public shop, checkout, basket and order confirmation showed € even when Stripe charged another currency. It now takes a currency; both apps pass STRIPE_CURRENCY to the blocks as layout data `currency` (like `timezone`), so one setting drives what Stripe charges and every price shown. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Qq6FzyGitv5gDZEcCHDGoo --- .env.example | 97 ++++++++++--------- AGENTS.md | 3 +- app-admin/src/routes/+layout.server.ts | 3 +- app-public/src/lib/components/Header.svelte | 4 +- app-public/src/routes/+layout.server.ts | 6 +- .../src/routes/checkout/return/+page.svelte | 4 +- .../shared/src/blocks/CheckoutBlock.svelte | 15 +-- packages/shared/src/blocks/ShopBlock.svelte | 5 +- packages/shared/src/calendar.ts | 21 +++- 9 files changed, 93 insertions(+), 65 deletions(-) diff --git a/.env.example b/.env.example index 5fdd8a8..a9473a1 100644 --- a/.env.example +++ b/.env.example @@ -1,47 +1,50 @@ -# Secrets read by docker-compose.yml via ${VAR:-} substitution. -# Copy to .env at the project root and fill in. - -# Stripe. The secret key and webhook secret go to PocketBase only (the shop -# API in pocketbase/pb_hooks); the public app gets just the publishable key. -STRIPE_SECRET_KEY=sk_test_... -PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_... -STRIPE_WEBHOOK_SECRET=whsec_... -STRIPE_CURRENCY=eur - -# Shared by PocketBase and the public app so the public app can record -# booking-email status on orders (its only write). Generate a long random -# value, e.g.: openssl rand -hex 32 -INTERNAL_API_SECRET= - -# Booking emails via Brevo's SMTP relay (Brevo → SMTP & API → SMTP). -# Leave SMTP_HOST empty in development: emails are then written to the -# app logs instead of being sent. -# Brevo: smtp-relay.brevo.com -SMTP_HOST= -# 587 (STARTTLS); use 2525 if your network blocks 587, or 465 for implicit TLS -SMTP_PORT=587 -# The SMTP login Brevo shows (xxxx@smtp-brevo.com), NOT your account email -SMTP_USER= -# The SMTP key generated in Brevo (not your account password) -SMTP_PASS= -# Must be a sender on a domain authenticated in Brevo -EMAIL_FROM=bookings@example.com -EMAIL_FROM_NAME=Eva Shafik -# Timezone used for booking times in emails (IANA name) -SITE_TIMEZONE=Europe/Oslo - -# Public URLs the browser uses: ://admin.. -# They must match the address bar exactly (SvelteKit's form CSRF check). -# Local default: http + :8080. Behind a reverse proxy / TLS on 443, e.g.: -# URL_SCHEME=https -# URL_PORT= -# (empty URL_PORT = no port in the URL; leave the line out to get :8080) -DOMAIN=localhost -URL_SCHEME=http -URL_PORT=:8080 - -# Caddy scheme for the reverse proxy on port 8080. -# http (default) — plain HTTP; matches the ORIGIN vars in docker-compose.yml -# https — HTTPS on :8080 (self-signed unless you add a cert issuer; -# also update ORIGIN/PUBLIC_* URLs to https://...) -CADDY_SCHEME=http +# Secrets read by docker-compose.yml via ${VAR:-} substitution. +# Copy to .env at the project root and fill in. + +# Stripe. The secret key and webhook secret go to PocketBase only (the shop +# API in pocketbase/pb_hooks); the public app gets just the publishable key. +STRIPE_SECRET_KEY=sk_test_... +PUBLIC_STRIPE_PUBLISHABLE_KEY=pk_test_... +STRIPE_WEBHOOK_SECRET=whsec_... +# Shop currency (ISO 4217, e.g. eur, nok): what Stripe charges in and what +# every price on the site, in the admin and in booking emails is shown in. +# Item prices are entered in this currency. +STRIPE_CURRENCY=eur + +# Shared by PocketBase and the public app so the public app can record +# booking-email status on orders (its only write). Generate a long random +# value, e.g.: openssl rand -hex 32 +INTERNAL_API_SECRET= + +# Booking emails via Brevo's SMTP relay (Brevo → SMTP & API → SMTP). +# Leave SMTP_HOST empty in development: emails are then written to the +# app logs instead of being sent. +# Brevo: smtp-relay.brevo.com +SMTP_HOST= +# 587 (STARTTLS); use 2525 if your network blocks 587, or 465 for implicit TLS +SMTP_PORT=587 +# The SMTP login Brevo shows (xxxx@smtp-brevo.com), NOT your account email +SMTP_USER= +# The SMTP key generated in Brevo (not your account password) +SMTP_PASS= +# Must be a sender on a domain authenticated in Brevo +EMAIL_FROM=bookings@example.com +EMAIL_FROM_NAME=Eva Shafik +# Timezone used for booking times in emails (IANA name) +SITE_TIMEZONE=Europe/Oslo + +# Public URLs the browser uses: ://admin.. +# They must match the address bar exactly (SvelteKit's form CSRF check). +# Local default: http + :8080. Behind a reverse proxy / TLS on 443, e.g.: +# URL_SCHEME=https +# URL_PORT= +# (empty URL_PORT = no port in the URL; leave the line out to get :8080) +DOMAIN=localhost +URL_SCHEME=http +URL_PORT=:8080 + +# Caddy scheme for the reverse proxy on port 8080. +# http (default) — plain HTTP; matches the ORIGIN vars in docker-compose.yml +# https — HTTPS on :8080 (self-signed unless you add a cert issuer; +# also update ORIGIN/PUBLIC_* URLs to https://...) +CADDY_SCHEME=http diff --git a/AGENTS.md b/AGENTS.md index 30f842e..e7d9c5c 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -144,7 +144,7 @@ To add an email block type: add to `EmailBlockType` in `packages/shared/src/type - Create/edit forms open in `$lib/admin/Modal.svelte`; destructive submits use `confirmSubmit()` from `$lib/admin/confirm` (returning `false` from a handler does not cancel). - Server actions wrap PocketBase writes in `attempt()` from `$lib/server/actions` so failures show as an error banner, not a 500. - Editors with unsaved state use `use:enhance` + `warnOnUnsavedChanges()`. -- **Dates, times and prices shown to people** use `DISPLAY_LOCALE` (`en-GB`: day-month-year, 24-hour clock) and the helpers in `$shared/calendar` (`formatInstant`, `formatPrice`, `formatDateTime`) — never `toLocale*(undefined)`, which renders US formats on the server. Booking times on the public site are shown in `SITE_TIMEZONE` too (layout data `timezone`, read by blocks via `page.data.timezone`). Native `` fields follow the visitor's browser language and can't be changed from the page. +- **Dates, times and prices shown to people** use `DISPLAY_LOCALE` (`en-GB`: day-month-year, 24-hour clock) and the helpers in `$shared/calendar` (`formatInstant`, `formatPrice` with the shop `currency`, `formatDateTime`) — never `toLocale*(undefined)`, which renders US formats on the server. Booking times on the public site are shown in `SITE_TIMEZONE` too (layout data `timezone`, read by blocks via `page.data.timezone`). Native `` fields follow the visitor's browser language and can't be changed from the page. - **Dates/times in admin**: entered and displayed in `SITE_TIMEZONE` (layout data `timezone`), via `toZonedDatetimeInput`, `zonedInputToIso`, `formatDateTime` in `$shared/calendar`. Never `new Date(localInput)` on the server — the container runs in UTC. - **Prose**: `.prose-wellness` class for rich text content styling @@ -185,6 +185,7 @@ cd app-public && npm run build - `PUBLIC_SITE_URL` — public site URL used by the admin app for cross-subdomain preview links (`://www.${DOMAIN}`). - `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASS`, `EMAIL_FROM`, `EMAIL_FROM_NAME` — Brevo SMTP relay for booking emails (both apps). Empty `SMTP_HOST` = log emails instead of sending. - `SITE_TIMEZONE` — IANA timezone for booking times in emails (default `Europe/Oslo`). +- `STRIPE_CURRENCY` — shop currency (ISO 4217, default `eur`; e.g. `nok`). One setting for what Stripe charges and how prices are shown: PocketBase, both apps' layout data (`currency`, read by blocks via `page.data.currency`) and booking emails. Format prices with `formatPrice(amount, currency)` from `$shared/calendar` — never hard-code a currency. - Secrets (Stripe, `INTERNAL_API_SECRET`, SMTP) are read from a root-level `.env` next to `docker-compose.yml`; see `.env.example`. - `STRIPE_SECRET_KEY`, `STRIPE_WEBHOOK_SECRET` → PocketBase only. `INTERNAL_API_SECRET` → PocketBase and app-public (booking-email status). No app gets PocketBase superuser credentials. - Shop listings are public data, loaded anonymously by `loadShopData()` in `packages/shared/src/server/shop.ts` (public layout and admin `/preview`; slots via `GET /api/shop/slots`); if a Shop block has nothing to show it says "Nothing is available". diff --git a/app-admin/src/routes/+layout.server.ts b/app-admin/src/routes/+layout.server.ts index 09856fa..697edf0 100644 --- a/app-admin/src/routes/+layout.server.ts +++ b/app-admin/src/routes/+layout.server.ts @@ -1,5 +1,6 @@ import { env } from '$env/dynamic/private'; import { defaultColors } from '$lib/types'; +import { shopCurrency } from '$shared/calendar'; import type { LayoutServerLoad } from './$types'; async function getGlobal(pb: App.Locals['pb'], key: string, fallback: unknown) { @@ -19,6 +20,6 @@ export const load: LayoutServerLoad = async ({ locals }) => { // Admin dates/times are entered and shown in the site's timezone, and // prices in the shop currency — same settings the booking emails use. timezone: env.SITE_TIMEZONE || 'Europe/Oslo', - currency: (env.STRIPE_CURRENCY || 'eur').toUpperCase() + currency: shopCurrency(env.STRIPE_CURRENCY) }; }; diff --git a/app-public/src/lib/components/Header.svelte b/app-public/src/lib/components/Header.svelte index 490ac3e..ba39c91 100644 --- a/app-public/src/lib/components/Header.svelte +++ b/app-public/src/lib/components/Header.svelte @@ -3,7 +3,7 @@ import { page } from '$app/state'; import type { HeaderGlobal } from '$lib/types'; import { getBasketContext } from '$lib/basket.svelte'; - import { formatPrice } from '$shared/calendar'; + import { DEFAULT_CURRENCY, formatPrice } from '$shared/calendar'; let { header } = $props<{ header: HeaderGlobal }>(); @@ -30,7 +30,7 @@ return href === '/' ? path === '/' : path === href || path.startsWith(href + '/'); } - const formatTotal = formatPrice; + const formatTotal = (amount: number) => formatPrice(amount, page.data.currency ?? DEFAULT_CURRENCY); {#snippet basketIcon()} diff --git a/app-public/src/routes/+layout.server.ts b/app-public/src/routes/+layout.server.ts index 8a95977..5f630e4 100644 --- a/app-public/src/routes/+layout.server.ts +++ b/app-public/src/routes/+layout.server.ts @@ -2,7 +2,7 @@ import { defaultColors, defaultFooter, defaultHeader } from '$lib/types'; import type { ItemRecord, SlotRecord } from '$lib/types'; import { BASKET_COOKIE, getBasket } from '$lib/server/shop'; import { loadShopData } from '$shared/server/shop'; -import { DEFAULT_TIMEZONE } from '$shared/calendar'; +import { DEFAULT_TIMEZONE, shopCurrency } from '$shared/calendar'; import { env } from '$env/dynamic/private'; import type { LayoutServerLoad } from './$types'; @@ -44,6 +44,8 @@ export const load: LayoutServerLoad = async ({ locals, cookies, url }) => { // Booking times are shown in the studio's timezone, wherever the visitor is. const timezone = env.SITE_TIMEZONE || DEFAULT_TIMEZONE; + // Prices are shown in the currency Stripe charges in. + const currency = shopCurrency(env.STRIPE_CURRENCY); - return { header, footer, colors, user: locals.user, basket, items, slots, timezone }; + return { header, footer, colors, user: locals.user, basket, items, slots, timezone, currency }; }; diff --git a/app-public/src/routes/checkout/return/+page.svelte b/app-public/src/routes/checkout/return/+page.svelte index 1b090f7..01b0a30 100644 --- a/app-public/src/routes/checkout/return/+page.svelte +++ b/app-public/src/routes/checkout/return/+page.svelte @@ -33,12 +33,12 @@
{formatSlotDate(line.start)}
{/if} - {formatPrice(line.price * line.quantity)} + {formatPrice(line.price * line.quantity, data.currency)} {/each}
- Total{formatPrice(data.order.total)} + Total{formatPrice(data.order.total, data.currency)}
diff --git a/packages/shared/src/blocks/CheckoutBlock.svelte b/packages/shared/src/blocks/CheckoutBlock.svelte index bd25fa3..5b12b23 100644 --- a/packages/shared/src/blocks/CheckoutBlock.svelte +++ b/packages/shared/src/blocks/CheckoutBlock.svelte @@ -5,7 +5,7 @@ import { page } from '$app/state'; import { DEFAULT_NOTES_HINT, DEFAULT_PRIVACY_NOTICE, type BasketLine, type CheckoutBlockData } from '$shared/types'; import { getBasketContext } from '$shared/basket.svelte'; - import { DEFAULT_TIMEZONE, formatInstant, formatPrice } from '$shared/calendar'; + import { DEFAULT_CURRENCY, DEFAULT_TIMEZONE, formatInstant, formatPrice } from '$shared/calendar'; import { loadStripe } from '$shared/stripe-client'; import type { Stripe, StripeElements, StripePaymentElement } from '@stripe/stripe-js'; @@ -43,12 +43,15 @@ lastSig = sig; }); + // Prices in the shop currency (STRIPE_CURRENCY, via layout data). + const currency = $derived(page.data.currency ?? DEFAULT_CURRENCY); + const payLabel = $derived( payState === 'confirming' ? 'Confirming…' : payState === 'creating' ? 'Preparing payment…' - : `Pay ${formatPrice(basket.total)}` + : `Pay ${formatPrice(basket.total, currency)}` ); const timezone = $derived(page.data.timezone ?? DEFAULT_TIMEZONE); @@ -345,7 +348,7 @@ {#if line.lineType === 'slot' && line.slotStart}

{formatSlotTime(line.slotStart)}

{/if} -

{formatPrice(line.itemPrice ?? 0)}

+

{formatPrice(line.itemPrice ?? 0, currency)}

@@ -374,7 +377,7 @@ {/if}

- {formatPrice((line.itemPrice ?? 0) * line.quantity)} + {formatPrice((line.itemPrice ?? 0) * line.quantity, currency)}

{/if} diff --git a/packages/shared/src/blocks/ShopBlock.svelte b/packages/shared/src/blocks/ShopBlock.svelte index e6f6740..b8480cf 100644 --- a/packages/shared/src/blocks/ShopBlock.svelte +++ b/packages/shared/src/blocks/ShopBlock.svelte @@ -2,7 +2,7 @@ import { page } from '$app/state'; import type { BasketLine, ShopBlockData, ItemRecord, SlotRecord } from '$shared/types'; import { getBasketContext } from '$shared/basket.svelte'; - import { DEFAULT_TIMEZONE, addMonths, createCalendarMonth, formatInstant, formatPrice, startOfMonth, zonedDateKey } from '$shared/calendar'; + import { DEFAULT_CURRENCY, DEFAULT_TIMEZONE, addMonths, createCalendarMonth, formatInstant, formatPrice, startOfMonth, zonedDateKey } from '$shared/calendar'; import { formatDateKey } from '$shared/slots'; let { data = {}, items = [], slots = [] } = $props<{ @@ -32,6 +32,7 @@ const MONTHS_SHOWN = 2; const weekdayLetters = ['M', 'T', 'W', 'T', 'F', 'S', 'S']; const timezone = $derived(page.data.timezone ?? DEFAULT_TIMEZONE); + const currency = $derived(page.data.currency ?? DEFAULT_CURRENCY); let visibleItems: ItemRecord[] = $derived.by(() => { let result: ItemRecord[] = items; @@ -234,7 +235,7 @@ {/if}
- {formatPrice(item.price)} + {formatPrice(item.price, currency)} {#if isService && item.duration != null} {item.duration} min {:else if !isService && item.stock != null && item.stock > 0 && item.stock <= 5} diff --git a/packages/shared/src/calendar.ts b/packages/shared/src/calendar.ts index 7c24cf8..1b51896 100644 --- a/packages/shared/src/calendar.ts +++ b/packages/shared/src/calendar.ts @@ -58,8 +58,25 @@ export const DISPLAY_LOCALE = 'en-GB'; /** Fallback when no SITE_TIMEZONE reaches the page. */ export const DEFAULT_TIMEZONE = 'Europe/Oslo'; -export function formatPrice(amount: number) { - return new Intl.NumberFormat(DISPLAY_LOCALE, { style: 'currency', currency: 'EUR' }).format(amount); +/** Fallback when no shop currency reaches the page. */ +export const DEFAULT_CURRENCY = 'EUR'; + +/** + * The shop currency as an ISO 4217 code, from STRIPE_CURRENCY ('nok' → 'NOK'). + * One setting for what Stripe charges and what every price shows. + */ +export function shopCurrency(value: string | undefined) { + return (value || DEFAULT_CURRENCY).toUpperCase(); +} + +/** A price in the shop currency (layout data `currency`). */ +export function formatPrice(amount: number, currency = DEFAULT_CURRENCY) { + try { + return new Intl.NumberFormat(DISPLAY_LOCALE, { style: 'currency', currency }).format(amount); + } catch { + // Not a currency code Intl knows (a typo in STRIPE_CURRENCY). + return `${amount.toFixed(2)} ${currency}`; + } } /** Format an instant in `timeZone` with the display locale. */