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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hy2vMnHJYP7QGsiH21zHxW
This commit is contained in:
agent 2026-10-25 21:39:51 +00:00
parent 32984ac01d
commit e824f16e31
12 changed files with 388 additions and 35 deletions

View file

@ -47,14 +47,64 @@
// Prices in the shop currency (STRIPE_CURRENCY, via layout data).
const currency = $derived<string>(page.data.currency ?? DEFAULT_CURRENCY);
/** Codes cover everything: the order is confirmed without a payment step. */
const nothingToPay = $derived(basket.lines.length > 0 && basket.total === 0);
const payLabel = $derived(
payState === 'confirming'
? 'Confirming…'
: payState === 'creating'
? 'Preparing payment…'
: `Pay ${formatPrice(basket.total, currency)}`
? nothingToPay
? 'Confirming…'
: 'Preparing payment…'
: nothingToPay
? 'Confirm booking'
: `Pay ${formatPrice(basket.total, currency)}`
);
// ── Bundle/voucher code ──
let codeInput = $state('');
let codeError = $state('');
let codeBusy = $state(false);
async function applyCode() {
if (!codeInput.trim() || codeBusy) return;
codeBusy = true;
codeError = '';
try {
await basket.applyCode(codeInput.trim());
codeInput = '';
} catch (e) {
codeError = e instanceof Error ? e.message : 'Could not apply the code.';
} finally {
codeBusy = false;
}
}
async function removeCode() {
codeBusy = true;
codeError = '';
try {
await basket.removeCode();
} catch (e) {
codeError = e instanceof Error ? e.message : 'Could not remove the code.';
} finally {
codeBusy = false;
}
}
// ── Gifts (bundle lines) ──
let giftOpen = $state<Record<string, boolean>>({});
async function saveGift(line: BasketLine, giftTo: string, giftMessage: string) {
if (giftTo === (line.giftTo ?? '') && giftMessage === (line.giftMessage ?? '')) return;
try {
await basket.setGift(line.id, giftTo, giftMessage);
} catch (e) {
errorMsg = e instanceof Error ? e.message : 'Could not save the gift details.';
}
}
const timezone = $derived<string>(page.data.timezone ?? DEFAULT_TIMEZONE);
/** "Fri 15 Jan, 10:00" in the studio's timezone. */
@ -179,7 +229,13 @@
`Could not start payment (${res.status}). If you booked a time slot, its hold may have expired — please pick it again from the shop section.`
);
}
const { clientSecret } = await res.json();
const result = await res.json();
if (result.free) {
// Paid in full with a code: the order exists, no payment step.
goto('/checkout/return?order=' + encodeURIComponent(result.orderId));
return;
}
const { clientSecret } = result;
// Starting payment holds the slots longer; show the new countdown.
await basket.refresh();
@ -349,11 +405,45 @@
{#if line.lineType === 'slot' && line.start}
<p class="text-sm text-fg-body/70">{formatSlotTime(line.start)}{line.minutes ? ` · ${formatDuration(line.minutes)}` : ''}</p>
{/if}
{#if line.itemPrice == null}
{#if line.passCovered}
<p class="text-sm font-medium text-sage-dark">Paid with your code</p>
{:else if line.itemPrice == null}
<p class="text-sm text-terracotta">No longer offered at this length — please remove it and pick again.</p>
{:else}
<p class="text-sm text-fg-body/70">{formatPrice(line.itemPrice, currency)}</p>
{/if}
{#if line.bundle}
<p class="text-sm text-fg-body/70">
{line.bundle.parts.map((p) => `${p.count}× ${p.name}${p.minutes ? ` (${formatDuration(p.minutes)})` : ''}`).join(', ')}{line.bundle.validMonths
? ` · valid ${line.bundle.validMonths} months`
: ''}
</p>
{#if giftOpen[line.id] || line.giftTo}
<div class="mt-2 grid gap-2 sm:grid-cols-2">
<input
type="text"
maxlength="180"
placeholder="For (their name)"
aria-label="Who the gift is for"
class="rounded-lg border border-charcoal/15 bg-white px-3 py-1.5 text-sm"
value={line.giftTo ?? ''}
onchange={(e) => saveGift(line, e.currentTarget.value.trim(), line.giftMessage ?? '')}
/>
<input
type="text"
maxlength="1000"
placeholder="Message (optional)"
aria-label="Gift message"
class="rounded-lg border border-charcoal/15 bg-white px-3 py-1.5 text-sm"
value={line.giftMessage ?? ''}
onchange={(e) => saveGift(line, line.giftTo ?? '', e.currentTarget.value.trim())}
/>
</div>
<p class="mt-1 text-xs text-fg-body/60">Shown with the code in your confirmation email — forward it to them, or print it.</p>
{:else}
<button type="button" class="mt-1 text-sm text-sage-dark underline underline-offset-2" onclick={() => (giftOpen[line.id] = true)}>This is a gift</button>
{/if}
{/if}
</div>
<!-- Products, and places in a group session (capacity > 1); the server checks what's left. -->
@ -406,6 +496,50 @@
</div>
</div>
<!-- Bundle / voucher code: covers bookings of the same service and length. -->
<div class="mt-4 rounded-2xl border border-charcoal/10 bg-white p-5">
{#if basket.pass}
{@const left = basket.pass.parts.filter((p) => p.redeemable)}
<div class="flex flex-wrap items-start justify-between gap-3">
<div>
<p class="font-medium">Code <span class="font-mono tracking-wider">{basket.pass.code}</span> · {basket.pass.name}</p>
<p class="text-sm text-fg-body/70">
{left.map((p) => `${p.left} × ${p.name} (${formatDuration(p.minutes ?? 0)})`).join(', ')} left after this basket{basket.pass.expiresAt
? ` · valid until ${formatInstant(basket.pass.expiresAt, timezone, { day: 'numeric', month: 'long', year: 'numeric' })}`
: ''}.
</p>
{#if !basket.lines.some((l) => l.passCovered)}
<p class="mt-1 text-sm text-terracotta">Nothing in your basket matches this code — book one of the sessions above, at the same length.</p>
{/if}
</div>
<button type="button" class="text-sm text-fg-body/70 underline underline-offset-2 disabled:opacity-50" disabled={codeBusy} onclick={removeCode}>Remove code</button>
</div>
{:else}
<form
class="flex flex-wrap items-center gap-2"
onsubmit={(e) => {
e.preventDefault();
applyCode();
}}
>
<label class="text-sm font-medium" for="checkout-code">Have a bundle or voucher code?</label>
<input
id="checkout-code"
type="text"
autocomplete="off"
spellcheck="false"
placeholder="ABCD-2345"
class="w-36 rounded-xl border border-charcoal/15 bg-white px-3 py-2 font-mono tracking-wider uppercase focus:ring-2 focus:ring-sage/50 focus:outline-none"
bind:value={codeInput}
/>
<button type="submit" class="rounded-full border border-charcoal/20 px-4 py-2 text-sm font-medium transition hover:bg-charcoal/5 disabled:opacity-50" disabled={codeBusy || !codeInput.trim()}>
{codeBusy ? 'Checking…' : 'Apply'}
</button>
</form>
{/if}
{#if codeError}<p class="mt-2 text-sm text-terracotta" role="alert">{codeError}</p>{/if}
</div>
<div class="mt-10 grid gap-10 md:grid-cols-2">
<!-- Customer details -->
<form
@ -519,7 +653,9 @@
{#if payState === 'idle' && !errorMsg}
<p class="mt-4 text-sm text-fg-body/70">
Fill in your details and press Pay to continue to secure payment.
{nothingToPay
? 'Nothing to pay — your code covers everything. Fill in your details and press Confirm booking.'
: 'Fill in your details and press Pay to continue to secure payment.'}
</p>
{/if}
</div>