From e824f16e310c26676472b4df39beaedbbbb5ebcc Mon Sep 17 00:00:00 2001 From: agent Date: Sun, 25 Oct 2026 21:39:51 +0000 Subject: [PATCH] Shop and checkout: bundles, gift details, codes, confirm without paying Bundle cards list what they contain and how long the code is valid. In the basket a bundle can be marked as a gift (for / message). Checkout takes a code: covered bookings show "Paid with your code" and cost 0, with what the code has left; when nothing is left to pay the button is Confirm booking and no Stripe step happens. The result page and the confirmation email show bought codes and code-paid bookings. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW --- app-public/src/lib/server/shop.ts | 19 ++- app-public/src/routes/api/basket/+server.ts | 3 + .../api/checkout/create-intent/+server.ts | 18 ++- .../routes/checkout/return/+page.server.ts | 22 ++- .../src/routes/checkout/return/+page.svelte | 26 ++++ packages/shared/src/basket.svelte.ts | 24 ++- .../shared/src/blocks/CheckoutBlock.svelte | 146 +++++++++++++++++- packages/shared/src/blocks/ShopBlock.svelte | 18 ++- packages/shared/src/email/render.ts | 65 +++++++- packages/shared/src/items.ts | 5 + packages/shared/src/server/email/index.ts | 31 +++- packages/shared/src/types.ts | 46 +++++- 12 files changed, 388 insertions(+), 35 deletions(-) diff --git a/app-public/src/lib/server/shop.ts b/app-public/src/lib/server/shop.ts index 2417c44..6b2d771 100644 --- a/app-public/src/lib/server/shop.ts +++ b/app-public/src/lib/server/shop.ts @@ -1,7 +1,7 @@ import { error, isHttpError, json, type RequestHandler } from '@sveltejs/kit'; import { env } from '$env/dynamic/private'; import { getFileUrl } from '$lib/pb'; -import type { Basket, BasketLine, OrderRecord } from '$lib/types'; +import type { Basket, BasketLine, OrderRecord, PassSummary } from '$lib/types'; import { sendOrderEmailsFromData, type OrderEmailData } from '$lib/server/email'; // Client for the shop API in PocketBase (pocketbase/pb_hooks/shop). All @@ -51,7 +51,7 @@ export function postJson(path: string, data: unknown): Promise { }); } -type BasketPayload = { record: Record; lines: Record[] } | null; +type BasketPayload = { record: Record; lines: Record[]; pass?: PassSummary | null } | null; /** PocketBase's "2026-01-01 10:00:00.000Z" → ISO. */ const pbInstant = (value: string) => new Date(String(value).replace(' ', 'T')).toISOString(); @@ -59,7 +59,7 @@ const pbInstant = (value: string) => new Date(String(value).replace(' ', 'T')).t /** Map the API's raw basket records (plus `unitPrice`/`end` per line) to the `Basket` type used by the UI. */ export function mapBasket(payload: BasketPayload): Basket | null { if (!payload) return null; - const { record, lines } = payload; + const { record, lines, pass } = payload; const mappedLines: BasketLine[] = lines.map((line) => { const item = line.expand?.item; const slot = line.expand?.slot; @@ -79,6 +79,10 @@ export function mapBasket(payload: BasketPayload): Basket | null { itemImageUrl: media ? getFileUrl('media', media.id, media.file) : undefined, slotKind: slot?.kind, slotCapacity: slot?.capacity, + passCovered: !!line.pass, + giftTo: line.giftTo || '', + giftMessage: line.giftMessage || '', + bundle: line.bundle || undefined, heldUntil: line.heldUntil ? pbInstant(line.heldUntil) : undefined }; }); @@ -90,6 +94,7 @@ export function mapBasket(payload: BasketPayload): Basket | null { customerDetails: record.customerDetails || null, stripePaymentIntentId: record.stripePaymentIntentId || null, lines: mappedLines, + pass: pass ?? null, serverNow: Date.now() }; } @@ -102,7 +107,7 @@ export async function getBasket(basketId: string | undefined): Promise, lines: Record[]): OrderRecord { +export function mapOrder(order: Record, lines: Record[], passes: PassSummary[] = []): OrderRecord { return { id: order.id, basket: order.basket, @@ -122,8 +127,10 @@ export function mapOrder(order: Record, lines: Record[ name: l.name, price: l.price, start: l.start || null, - minutes: l.minutes || null - })) + minutes: l.minutes || null, + passCode: l.passCode || '' + })), + passes }; } diff --git a/app-public/src/routes/api/basket/+server.ts b/app-public/src/routes/api/basket/+server.ts index 9b43946..48a9642 100644 --- a/app-public/src/routes/api/basket/+server.ts +++ b/app-public/src/routes/api/basket/+server.ts @@ -24,6 +24,9 @@ export const POST: RequestHandler = jsonErrors(async ({ request, cookies }) => { minutes: body.minutes, start: body.start, seats: body.seats, + code: body.code, + giftTo: body.giftTo, + giftMessage: body.giftMessage, lineId: body.lineId, quantity: body.quantity, basketId: cookies.get(BASKET_COOKIE) || '' diff --git a/app-public/src/routes/api/checkout/create-intent/+server.ts b/app-public/src/routes/api/checkout/create-intent/+server.ts index 9172923..08468ed 100644 --- a/app-public/src/routes/api/checkout/create-intent/+server.ts +++ b/app-public/src/routes/api/checkout/create-intent/+server.ts @@ -1,9 +1,15 @@ import { json, error } from '@sveltejs/kit'; import type { RequestHandler } from './$types'; -import { BASKET_COOKIE, jsonErrors, postJson } from '$lib/server/shop'; +import { BASKET_COOKIE, jsonErrors, postJson, sendAndRecordOrderEmails } from '$lib/server/shop'; +import type { OrderEmailData } from '$lib/server/email'; + +type CheckoutResult = { clientSecret: string } | { free: true; orderId: string; orderEmail: OrderEmailData }; // Starts payment: PocketBase re-validates the basket, extends slot holds and -// creates the Stripe PaymentIntent (pocketbase/pb_hooks/shop). +// creates the Stripe PaymentIntent (pocketbase/pb_hooks/shop). When codes +// cover the whole basket there is nothing to pay: PocketBase creates the +// order straight away, and the booking emails are sent from here (as the +// Stripe webhook route does after a payment). export const POST: RequestHandler = jsonErrors(async ({ request, cookies }) => { let body: Record; try { @@ -12,12 +18,16 @@ export const POST: RequestHandler = jsonErrors(async ({ request, cookies }) => { throw error(400, 'Invalid JSON body'); } - const { clientSecret } = await postJson<{ clientSecret: string }>('/checkout', { + const result = await postJson('/checkout', { basketId: cookies.get(BASKET_COOKIE) || '', name: body.name, email: body.email, phone: body.phone, notes: body.notes }); - return json({ clientSecret }); + if ('free' in result) { + void sendAndRecordOrderEmails(result.orderEmail); + return json({ free: true, orderId: result.orderId }); + } + return json({ clientSecret: result.clientSecret }); }); diff --git a/app-public/src/routes/checkout/return/+page.server.ts b/app-public/src/routes/checkout/return/+page.server.ts index 61e5eda..5082f23 100644 --- a/app-public/src/routes/checkout/return/+page.server.ts +++ b/app-public/src/routes/checkout/return/+page.server.ts @@ -1,19 +1,27 @@ import type { PageServerLoad } from './$types'; import { BASKET_COOKIE, mapOrder, shopFetch } from '$lib/server/shop'; +import type { PassSummary } from '$lib/types'; type OrderStatus = | { state: 'missing' | 'invalid' | 'processing' | 'canceled' | 'failed' } - | { state: 'paid'; order: Record; lines: Record[] }; + | { state: 'paid'; order: Record; lines: Record[]; passes: PassSummary[] }; export const load: PageServerLoad = async ({ url, cookies }) => { + // After a Stripe payment: ?payment_intent=… (checked with Stripe by + // PocketBase; the webhook may not have created the order yet). An order + // paid entirely with codes: ?order=…, which PocketBase only shows to the + // basket it came from (this visitor's cookie). const intentId = url.searchParams.get('payment_intent'); - if (!intentId) return { state: 'missing' as const }; + const orderId = url.searchParams.get('order'); + let query: string; + if (intentId) query = `payment_intent=${encodeURIComponent(intentId)}`; + else if (orderId) query = `order=${encodeURIComponent(orderId)}&basket=${encodeURIComponent(cookies.get(BASKET_COOKIE) || '')}`; + else return { state: 'missing' as const }; - // PocketBase checks the intent with Stripe and looks up the order (the - // webhook is the source of truth and may not have landed yet). - const status = await shopFetch(`/order?payment_intent=${encodeURIComponent(intentId)}`); + const status = await shopFetch(`/order?${query}`); if (status.state !== 'paid') return { state: status.state }; - cookies.delete(BASKET_COOKIE, { path: '/' }); - return { state: 'paid' as const, order: mapOrder(status.order, status.lines) }; + // Keep the cookie for a code-paid order: it is what lets this page show it on reload. + if (intentId) cookies.delete(BASKET_COOKIE, { path: '/' }); + return { state: 'paid' as const, order: mapOrder(status.order, status.lines, status.passes) }; }; diff --git a/app-public/src/routes/checkout/return/+page.svelte b/app-public/src/routes/checkout/return/+page.svelte index 01b0a30..4dbc374 100644 --- a/app-public/src/routes/checkout/return/+page.svelte +++ b/app-public/src/routes/checkout/return/+page.svelte @@ -1,6 +1,7 @@