create-brainerce-store 1.67.0 → 1.71.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/dist/index.js +22 -2
  2. package/messages/en.json +52 -2
  3. package/messages/he.json +52 -2
  4. package/package.json +1 -1
  5. package/templates/nextjs/base/.env.local.ejs +7 -0
  6. package/templates/nextjs/base/AGENTS.md.ejs +7 -0
  7. package/templates/nextjs/base/CLAUDE.md.ejs +7 -0
  8. package/templates/nextjs/base/src/app/blog/[slug]/page.tsx.ejs +8 -2
  9. package/templates/nextjs/base/src/app/category/[slug]/page.tsx +16 -7
  10. package/templates/nextjs/base/src/app/checkout/page.tsx +1018 -1017
  11. package/templates/nextjs/base/src/app/error.tsx.ejs +53 -0
  12. package/templates/nextjs/base/src/app/pages/[slug]/page.tsx.ejs +8 -2
  13. package/templates/nextjs/base/src/app/products/[slug]/page.tsx +17 -7
  14. package/templates/nextjs/base/src/app/register/page.tsx +67 -64
  15. package/templates/nextjs/base/src/components/account/profile-section.tsx +303 -226
  16. package/templates/nextjs/base/src/components/auth/register-form.tsx +326 -245
  17. package/templates/nextjs/base/src/components/checkout/custom-fields-step.tsx +306 -294
  18. package/templates/nextjs/base/src/components/checkout/date-picker.tsx +13 -1
  19. package/templates/nextjs/base/src/components/checkout/datetime-picker.tsx +61 -21
  20. package/templates/nextjs/base/src/components/shared/birthday-picker.tsx +258 -0
  21. package/templates/nextjs/base/src/core/lib/auth.ts +162 -154
  22. package/templates/nextjs/base/src/core/lib/birthday.ts +74 -0
  23. package/templates/nextjs/base/src/core/lib/site-url.ts +42 -9
  24. package/templates/nextjs/base/src/core/lib/store-info.ts +10 -0
  25. package/templates/nextjs/base/src/ui/layout/newsletter-signup.tsx +143 -0
  26. package/templates/nextjs/base/src/ui/layout/site-footer.tsx.ejs +18 -2
  27. package/templates/nextjs/base/src/ui/product/back-in-stock-form.tsx +173 -0
  28. package/templates/nextjs/base/src/ui/product/product-client-section.tsx +484 -455
  29. package/templates/nextjs/base/src/ui/product/review-form.tsx +136 -12
  30. package/templates/nextjs/base/src/ui/product/reviews-section.tsx.ejs +139 -108
  31. package/templates/nextjs/designs/atelier/ui/layout/site-footer.tsx.ejs +155 -142
  32. package/templates/nextjs/designs/atelier/ui/product/product-client-section.tsx +500 -477
  33. package/templates/nextjs/designs/atelier/ui/product/review-form.tsx +135 -11
  34. package/templates/nextjs/designs/atelier/ui/product/reviews-section.tsx.ejs +179 -148
  35. package/templates/nextjs/ui-canvas/layout/newsletter-signup.tsx +122 -0
  36. package/templates/nextjs/ui-canvas/layout/site-footer.tsx.ejs +87 -83
  37. package/templates/nextjs/ui-canvas/product/back-in-stock-form.tsx +151 -0
  38. package/templates/nextjs/ui-canvas/product/product-client-section.tsx +373 -352
  39. package/templates/nextjs/ui-canvas/product/review-form.tsx +129 -11
  40. package/templates/nextjs/ui-canvas/product/reviews-section.tsx.ejs +127 -96
@@ -1,1017 +1,1018 @@
1
- 'use client';
2
-
3
- import { Suspense, useEffect, useState, useCallback, useRef } from 'react';
4
- import { useSearchParams } from 'next/navigation';
5
- import { CdnImage as Image } from '@/ui/shared/cdn-image';
6
- import { Link } from '@/core/lib/navigation';
7
- import type {
8
- Checkout,
9
- ShippingRate,
10
- SetShippingAddressDto,
11
- ShippingDestinations,
12
- PickupLocation,
13
- CheckoutBumpsResponse,
14
- CheckoutCustomFieldDefinition,
15
- } from 'brainerce';
16
- import { formatPrice } from 'brainerce';
17
- import { getClient } from '@/core/lib/brainerce';
18
- import { useStoreInfo, useCart, useAuth } from '@/core/providers/store-provider';
19
- import { useCurrency } from '@/core/lib/use-currency';
20
- import { CheckoutForm } from '@/components/checkout/checkout-form';
21
- import { ShippingStep } from '@/components/checkout/shipping-step';
22
- import { PaymentStep } from '@/components/checkout/payment-step';
23
- import { DeliveryMethodStep } from '@/components/checkout/delivery-method-step';
24
- import { PickupStep } from '@/components/checkout/pickup-step';
25
- import { CustomFieldsStep } from '@/components/checkout/custom-fields-step';
26
- import { TaxDisplay } from '@/components/checkout/tax-display';
27
- import { OrderBumpCard } from '@/components/checkout/order-bump-card';
28
- import { CouponInput } from '@/ui/cart/coupon-input';
29
- import { ReservationCountdown } from '@/ui/cart/reservation-countdown';
30
- import { LoadingSpinner } from '@/ui/shared/loading-spinner';
31
- import { useTranslations } from '@/core/lib/translations';
32
- import { cn } from '@/core/lib/utils';
33
- import { isValidCheckoutId } from '@/core/lib/safe-redirect';
34
- import { trackBeginCheckout } from '@/core/lib/tracking';
35
-
36
- type CheckoutStep = 'method' | 'address' | 'shipping' | 'pickup' | 'custom-fields' | 'payment';
37
-
38
- function CheckoutContent() {
39
- const searchParams = useSearchParams();
40
- const { storeInfo } = useStoreInfo();
41
- const { cart, refreshCart } = useCart();
42
- const { isLoggedIn } = useAuth();
43
- const currency = useCurrency();
44
- const t = useTranslations('checkout');
45
- const tc = useTranslations('common');
46
-
47
- const [step, setStep] = useState<CheckoutStep>('address');
48
- const [checkout, setCheckout] = useState<Checkout | null>(null);
49
- const [shippingRates, setShippingRates] = useState<ShippingRate[]>([]);
50
- const [selectedRateId, setSelectedRateId] = useState<string | null>(null);
51
- const [loading, setLoading] = useState(false);
52
- const [initializing, setInitializing] = useState(true);
53
- const [error, setError] = useState<string | null>(null);
54
- const [destinations, setDestinations] = useState<ShippingDestinations | null>(null);
55
- const [pickupLocations, setPickupLocations] = useState<PickupLocation[]>([]);
56
- const [deliveryType, setDeliveryType] = useState<'shipping' | 'pickup'>('shipping');
57
- const [isAllDigital, setIsAllDigital] = useState(false);
58
- const [prefillAddress, setPrefillAddress] = useState<SetShippingAddressDto | null>(null);
59
- const [prefillCustomer, setPrefillCustomer] = useState<{
60
- email: string;
61
- firstName?: string;
62
- lastName?: string;
63
- phone?: string;
64
- } | null>(null);
65
- const [hasSavedAddress, setHasSavedAddress] = useState(false);
66
- const [orderBumps, setOrderBumps] = useState<CheckoutBumpsResponse | null>(null);
67
- const [addedBumpIds, setAddedBumpIds] = useState<Set<string>>(new Set());
68
- const [bumpLoading, setBumpLoading] = useState<string | null>(null);
69
- const [customFields, setCustomFields] = useState<CheckoutCustomFieldDefinition[]>([]);
70
- const [customFieldValues, setCustomFieldValues] = useState<Record<string, unknown>>({});
71
- const [customFieldsLoading, setCustomFieldsLoading] = useState(false);
72
-
73
- // `begin_checkout` — fired once the cart is loaded, guarded by a ref so a
74
- // re-render (or the shopper returning from a canceled payment) doesn't
75
- // report a second checkout start for the same cart.
76
- const beganCheckoutForCartRef = useRef<string | null>(null);
77
- useEffect(() => {
78
- if (!cart?.id || !cart.items?.length) return;
79
- if (beganCheckoutForCartRef.current === cart.id) return;
80
- beganCheckoutForCartRef.current = cart.id;
81
- trackBeginCheckout(cart, currency);
82
- }, [cart, currency]);
83
-
84
- // Check for returning from canceled payment
85
- const canceled = searchParams.get('canceled') === 'true';
86
- const checkoutIdParam = searchParams.get('checkout_id');
87
- const existingCheckoutId = isValidCheckoutId(checkoutIdParam) ? checkoutIdParam : null;
88
-
89
- // Pre-fill address and customer data from profile when logged in
90
- useEffect(() => {
91
- if (!isLoggedIn) return;
92
- getClient()
93
- .getCheckoutPrefillData()
94
- .then((data) => {
95
- if (data.customer) setPrefillCustomer(data.customer);
96
- if (data.shippingAddress) {
97
- setPrefillAddress(data.shippingAddress);
98
- setHasSavedAddress(true);
99
- }
100
- })
101
- .catch(() => {});
102
- }, [isLoggedIn]);
103
-
104
- // Initialize or resume checkout (only once)
105
- const checkoutInitRef = useRef(false);
106
- const cartIdRef = useRef<string | null>(null);
107
-
108
- useEffect(() => {
109
- // Only init once, or if cart ID actually changed (e.g. cart was replaced)
110
- if (!cart?.id) return;
111
- if (checkoutInitRef.current && cartIdRef.current === cart.id) return;
112
- checkoutInitRef.current = true;
113
- cartIdRef.current = cart.id;
114
-
115
- const initCheckout = async () => {
116
- try {
117
- setInitializing(true);
118
- setError(null);
119
- const client = getClient();
120
-
121
- // Fetch shipping destinations and pickup locations in parallel
122
- client
123
- .getShippingDestinations()
124
- .then(setDestinations)
125
- .catch(() => {});
126
-
127
- const locations = await client.getPickupLocations().catch(() => [] as PickupLocation[]);
128
- setPickupLocations(locations);
129
-
130
- // If returning with existing checkout ID, resume it
131
- if (existingCheckoutId) {
132
- const existing = await client.getCheckout(existingCheckoutId);
133
- setCheckout(existing);
134
-
135
- // Preload custom field definitions and any existing values so the
136
- // step indicator and "change options" affordance work on resume.
137
- client
138
- .getCheckoutCustomFields(existing.id)
139
- .then((fields) => {
140
- setCustomFields(fields);
141
- const existingValues = (
142
- existing as unknown as {
143
- customFieldValues?: Record<string, unknown> | null;
144
- }
145
- ).customFieldValues;
146
- if (existingValues) setCustomFieldValues(existingValues);
147
- })
148
- .catch(() => {
149
- setCustomFields([]);
150
- });
151
-
152
- // Determine step based on checkout state
153
- const allDigital = existing.lineItems.every(
154
- (i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
155
- );
156
- setIsAllDigital(allDigital);
157
- if (allDigital) {
158
- // Digital products: show contact info step if email not set, else payment
159
- setStep(existing.email ? 'payment' : 'address');
160
- } else if (existing.deliveryType === 'pickup' && existing.pickupLocation) {
161
- setDeliveryType('pickup');
162
- setStep('payment');
163
- } else if (existing.shippingAddress && existing.shippingRateId) {
164
- setStep('payment');
165
- } else if (existing.shippingAddress) {
166
- // Fetch shipping rates
167
- const rates = await client.getShippingRates(existing.id);
168
- setShippingRates(rates);
169
- setStep('shipping');
170
- } else if (locations.length > 0) {
171
- setStep('method');
172
- }
173
- return;
174
- }
175
-
176
- // Create new checkout — cart is always server-side now
177
- const newCheckout = await client.createCheckout({ cartId: cart.id });
178
- setCheckout(newCheckout);
179
-
180
- // If all items are downloadable, skip shipping — show contact info step
181
- const allDigital = newCheckout.lineItems.every(
182
- (i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
183
- );
184
- setIsAllDigital(allDigital);
185
- if (allDigital) {
186
- setStep('address');
187
- return;
188
- }
189
-
190
- // If pickup locations exist, start with delivery method selection
191
- if (locations.length > 0) {
192
- setStep('method');
193
- }
194
- } catch (err) {
195
- const message = err instanceof Error ? err.message : t('failedToInitCheckout');
196
- setError(message);
197
- } finally {
198
- setInitializing(false);
199
- }
200
- };
201
-
202
- initCheckout();
203
- }, [cart?.id, existingCheckoutId]);
204
-
205
- // Load order bumps when checkout is available
206
- useEffect(() => {
207
- if (!checkout?.id || storeInfo?.upsell?.checkoutOrderBumpEnabled === false) {
208
- setOrderBumps(null);
209
- return;
210
- }
211
- const client = getClient();
212
- client
213
- .getCheckoutBumps(checkout.id)
214
- .then((data) => {
215
- setOrderBumps(data);
216
- // Detect already-added bumps from cart
217
- if (cart?.items) {
218
- const existingBumpIds = new Set<string>();
219
- for (const item of cart.items) {
220
- const meta = item.metadata as Record<string, unknown> | undefined;
221
- if (meta?.isOrderBump && meta?.orderBumpId) {
222
- existingBumpIds.add(meta.orderBumpId as string);
223
- }
224
- }
225
- setAddedBumpIds(existingBumpIds);
226
- }
227
- })
228
- .catch(() => {});
229
- }, [checkout?.id, storeInfo?.upsell?.checkoutOrderBumpEnabled]);
230
-
231
- // Handle bump toggle
232
- async function handleBumpToggle(bumpId: string, add: boolean, variantId?: string) {
233
- if (!cart?.id || bumpLoading) return;
234
- try {
235
- setBumpLoading(bumpId);
236
- const client = getClient();
237
- if (add) {
238
- await client.addOrderBump(cart.id, bumpId, variantId);
239
- setAddedBumpIds((prev) => new Set([...prev, bumpId]));
240
- } else {
241
- await client.removeOrderBump(cart.id, bumpId);
242
- setAddedBumpIds((prev) => {
243
- const next = new Set(prev);
244
- next.delete(bumpId);
245
- return next;
246
- });
247
- }
248
- await refreshCart();
249
- } catch (err) {
250
- console.error('Failed to toggle order bump:', err);
251
- } finally {
252
- setBumpLoading(null);
253
- }
254
- }
255
-
256
- // Handle shipping address submission
257
- async function handleAddressSubmit(
258
- address: SetShippingAddressDto,
259
- consent: { acceptsMarketing: boolean; saveDetails: boolean }
260
- ) {
261
- if (!checkout) return;
262
-
263
- try {
264
- setLoading(true);
265
- setError(null);
266
- const client = getClient();
267
-
268
- if (isAllDigital) {
269
- // Digital products: set customer info only, skip shipping
270
- const updated = await client.setCheckoutCustomer(checkout.id, {
271
- email: address.email,
272
- firstName: address.firstName,
273
- lastName: address.lastName,
274
- phone: address.phone,
275
- acceptsMarketing: consent.acceptsMarketing,
276
- notes: address.notes,
277
- });
278
- setCheckout(updated);
279
- setStep('payment');
280
- } else {
281
- const response = await client.setShippingAddress(checkout.id, address);
282
- setCheckout(response.checkout);
283
- setShippingRates(response.rates);
284
- setStep('shipping');
285
- }
286
-
287
- // Update marketing preference for logged-in users
288
- if (isLoggedIn) {
289
- try {
290
- await client.updateMyProfile({ acceptsMarketing: consent.acceptsMarketing });
291
- } catch {
292
- // non-critical
293
- }
294
- }
295
-
296
- // Save address to profile if checkbox was checked and no existing saved address
297
- if (isLoggedIn && consent.saveDetails && !hasSavedAddress && !isAllDigital) {
298
- try {
299
- await client.addMyAddress({
300
- firstName: address.firstName,
301
- lastName: address.lastName,
302
- line1: address.line1,
303
- line2: address.line2,
304
- city: address.city,
305
- region: address.region,
306
- postalCode: address.postalCode,
307
- country: address.country,
308
- phone: address.phone,
309
- isDefault: true,
310
- });
311
- } catch {
312
- // non-critical
313
- }
314
- }
315
- } catch (err) {
316
- const message = err instanceof Error ? err.message : t('failedToSaveAddress');
317
- setError(message);
318
- } finally {
319
- setLoading(false);
320
- }
321
- }
322
-
323
- // After shipping/pickup is set, decide whether to show the custom-fields step
324
- // or jump straight to payment. Returns the next step.
325
- async function loadCustomFieldsOrSkip(checkoutId: string): Promise<CheckoutStep> {
326
- try {
327
- const fields = await getClient().getCheckoutCustomFields(checkoutId);
328
- setCustomFields(fields);
329
- return fields.length > 0 ? 'custom-fields' : 'payment';
330
- } catch {
331
- // If the endpoint isn't available or fails, fall through to payment
332
- // rather than blocking the customer.
333
- setCustomFields([]);
334
- return 'payment';
335
- }
336
- }
337
-
338
- // Handle shipping method selection
339
- async function handleShippingSelect(rateId: string) {
340
- if (!checkout) return;
341
-
342
- try {
343
- setLoading(true);
344
- setError(null);
345
- setSelectedRateId(rateId);
346
- const client = getClient();
347
-
348
- const updated = await client.selectShippingMethod(checkout.id, rateId);
349
- setCheckout(updated);
350
- setStep(await loadCustomFieldsOrSkip(updated.id));
351
- } catch (err) {
352
- const message = err instanceof Error ? err.message : t('failedToSelectShipping');
353
- setError(message);
354
- } finally {
355
- setLoading(false);
356
- }
357
- }
358
-
359
- // Submit custom fields
360
- async function handleCustomFieldsApply() {
361
- if (!checkout) return;
362
- try {
363
- setCustomFieldsLoading(true);
364
- setError(null);
365
- const updated = await getClient().setCheckoutCustomFields(checkout.id, customFieldValues);
366
- setCheckout(updated);
367
- setStep('payment');
368
- } catch (err) {
369
- const message = err instanceof Error ? err.message : t('customFieldsFailed');
370
- setError(message);
371
- } finally {
372
- setCustomFieldsLoading(false);
373
- }
374
- }
375
-
376
- // Handle delivery method selection
377
- async function handleDeliveryTypeSelect(method: 'shipping' | 'pickup') {
378
- if (!checkout) return;
379
-
380
- try {
381
- setLoading(true);
382
- setError(null);
383
- setDeliveryType(method);
384
- const client = getClient();
385
-
386
- await client.setDeliveryType(checkout.id, method);
387
-
388
- if (method === 'shipping') {
389
- setStep('address');
390
- } else {
391
- setStep('pickup');
392
- }
393
- } catch (err) {
394
- const message = err instanceof Error ? err.message : t('failedToSetDeliveryMethod');
395
- setError(message);
396
- } finally {
397
- setLoading(false);
398
- }
399
- }
400
-
401
- // Handle pickup location selection
402
- async function handlePickupSelect(
403
- locationId: string,
404
- customerInfo: { email: string; firstName?: string; lastName?: string; phone?: string }
405
- ) {
406
- if (!checkout) return;
407
-
408
- try {
409
- setLoading(true);
410
- setError(null);
411
- const client = getClient();
412
-
413
- const updated = await client.selectPickupLocation(checkout.id, {
414
- pickupRateId: locationId,
415
- email: customerInfo.email,
416
- firstName: customerInfo.firstName,
417
- lastName: customerInfo.lastName,
418
- phone: customerInfo.phone,
419
- });
420
- setCheckout(updated);
421
- setStep(await loadCustomFieldsOrSkip(updated.id));
422
- } catch (err) {
423
- const message = err instanceof Error ? err.message : t('failedToSelectPickup');
424
- setError(message);
425
- } finally {
426
- setLoading(false);
427
- }
428
- }
429
-
430
- // Refresh cart after coupon apply/remove.
431
- // The checkout totals are updated server-side by applyCheckoutCoupon/removeCheckoutCoupon,
432
- // so we re-fetch the checkout to get the updated discountAmount and total.
433
- const handleCouponUpdate = useCallback(async () => {
434
- await refreshCart();
435
- if (checkout) {
436
- try {
437
- const client = getClient();
438
- const updated = await client.getCheckout(checkout.id);
439
- setCheckout(updated);
440
- } catch (err) {
441
- console.error('Failed to refresh checkout after coupon update:', err);
442
- }
443
- }
444
- }, [checkout, refreshCart]);
445
-
446
- if (initializing) {
447
- return (
448
- <div className="flex min-h-[60vh] items-center justify-center">
449
- <LoadingSpinner size="lg" />
450
- </div>
451
- );
452
- }
453
-
454
- // Empty cart
455
- if (!cart || cart.items.length === 0) {
456
- return (
457
- <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
458
- <h1 className="text-foreground text-2xl font-bold">{t('emptyCart')}</h1>
459
- <p className="text-muted-foreground mt-2">{t('emptyCartSubtitle')}</p>
460
- <Link
461
- href="/products"
462
- className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
463
- >
464
- {tc('shopNow')}
465
- </Link>
466
- </div>
467
- );
468
- }
469
-
470
- if (error && !checkout) {
471
- return (
472
- <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
473
- <h1 className="text-foreground text-2xl font-bold">{t('errorTitle')}</h1>
474
- <p className="text-destructive mt-2">{error}</p>
475
- <Link
476
- href="/cart"
477
- className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
478
- >
479
- {t('returnToCart')}
480
- </Link>
481
- </div>
482
- );
483
- }
484
-
485
- const customFieldsStep =
486
- customFields.length > 0
487
- ? [{ key: 'custom-fields' as CheckoutStep, label: t('stepCustomFields') }]
488
- : [];
489
-
490
- const steps: { key: CheckoutStep; label: string }[] = isAllDigital
491
- ? [
492
- { key: 'address', label: t('stepContactInfo') },
493
- ...customFieldsStep,
494
- { key: 'payment', label: t('stepPayment') },
495
- ]
496
- : pickupLocations.length > 0
497
- ? deliveryType === 'pickup'
498
- ? [
499
- { key: 'method', label: t('stepMethod') },
500
- { key: 'pickup', label: t('stepPickup') },
501
- ...customFieldsStep,
502
- { key: 'payment', label: t('stepPayment') },
503
- ]
504
- : [
505
- { key: 'method', label: t('stepMethod') },
506
- { key: 'address', label: t('stepAddress') },
507
- { key: 'shipping', label: t('stepShipping') },
508
- ...customFieldsStep,
509
- { key: 'payment', label: t('stepPayment') },
510
- ]
511
- : [
512
- { key: 'address', label: t('stepAddress') },
513
- { key: 'shipping', label: t('stepShipping') },
514
- ...customFieldsStep,
515
- { key: 'payment', label: t('stepPayment') },
516
- ];
517
-
518
- const currentStepIndex = steps.findIndex((s) => s.key === step);
519
-
520
- return (
521
- <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
522
- <h1 className="text-foreground mb-6 text-2xl font-bold">{t('title')}</h1>
523
-
524
- {/* Canceled payment banner */}
525
- {canceled && (
526
- <div className="mb-6 rounded-lg border border-orange-200 bg-orange-50 px-4 py-3 text-sm text-orange-800 dark:border-orange-800 dark:bg-orange-950/30 dark:text-orange-300">
527
- {t('paymentCanceledBanner')}
528
- </div>
529
- )}
530
-
531
- {/* Reservation countdown */}
532
- {checkout?.reservation?.hasReservation && (
533
- <ReservationCountdown reservation={checkout.reservation} className="mb-6" />
534
- )}
535
-
536
- {/* Step indicator */}
537
- <div className="mb-8 flex items-center gap-2">
538
- {steps.map((s, index) => (
539
- <div key={s.key} className="flex items-center">
540
- {index > 0 && (
541
- <div
542
- className={cn(
543
- 'mx-2 h-px w-8 sm:w-12',
544
- index <= currentStepIndex ? 'bg-primary' : 'bg-border'
545
- )}
546
- />
547
- )}
548
- <div className="flex items-center gap-2">
549
- <div
550
- className={cn(
551
- 'flex h-7 w-7 items-center justify-center rounded-full text-xs font-medium',
552
- index < currentStepIndex
553
- ? 'bg-primary text-primary-foreground'
554
- : index === currentStepIndex
555
- ? 'bg-primary text-primary-foreground'
556
- : 'bg-muted text-muted-foreground'
557
- )}
558
- >
559
- {index < currentStepIndex ? (
560
- <svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
561
- <path
562
- strokeLinecap="round"
563
- strokeLinejoin="round"
564
- strokeWidth={2}
565
- d="M5 13l4 4L19 7"
566
- />
567
- </svg>
568
- ) : (
569
- index + 1
570
- )}
571
- </div>
572
- <span
573
- className={cn(
574
- 'hidden text-sm sm:block',
575
- index <= currentStepIndex
576
- ? 'text-foreground font-medium'
577
- : 'text-muted-foreground'
578
- )}
579
- >
580
- {s.label}
581
- </span>
582
- </div>
583
- </div>
584
- ))}
585
- </div>
586
-
587
- {/* Error banner */}
588
- {error && checkout && (
589
- <div className="bg-destructive/10 border-destructive/20 text-destructive mb-6 rounded-lg border px-4 py-3 text-sm">
590
- {error}
591
- </div>
592
- )}
593
-
594
- <div className="grid grid-cols-1 gap-8 lg:grid-cols-3">
595
- {/* Main content */}
596
- <div className="lg:col-span-2">
597
- {/* Delivery Method */}
598
- {step === 'method' && (
599
- <div>
600
- <h2 className="text-foreground mb-4 text-lg font-semibold">{t('deliveryMethod')}</h2>
601
- <DeliveryMethodStep onSelect={handleDeliveryTypeSelect} />
602
- </div>
603
- )}
604
-
605
- {/* Address */}
606
- {step === 'address' && (
607
- <div>
608
- <div className="mb-4 flex items-center justify-between">
609
- <h2 className="text-foreground text-lg font-semibold">
610
- {isAllDigital ? t('contactInfo') : t('shippingAddress')}
611
- </h2>
612
- {!isAllDigital && pickupLocations.length > 0 && (
613
- <button
614
- type="button"
615
- onClick={() => setStep('method')}
616
- className="text-primary text-sm hover:underline"
617
- >
618
- {t('changeMethod')}
619
- </button>
620
- )}
621
- </div>
622
- <CheckoutForm
623
- onSubmit={handleAddressSubmit}
624
- loading={loading}
625
- destinations={isAllDigital ? null : destinations}
626
- showSaveDetails={isLoggedIn && !hasSavedAddress && !isAllDigital}
627
- emailOnly={isAllDigital}
628
- initialValues={
629
- checkout?.shippingAddress
630
- ? {
631
- email: checkout.email || '',
632
- firstName: checkout.shippingAddress.firstName,
633
- lastName: checkout.shippingAddress.lastName,
634
- line1: checkout.shippingAddress.line1,
635
- line2: checkout.shippingAddress.line2 || '',
636
- city: checkout.shippingAddress.city,
637
- region: checkout.shippingAddress.region || '',
638
- postalCode: checkout.shippingAddress.postalCode,
639
- country: checkout.shippingAddress.country,
640
- phone: checkout.shippingAddress.phone || '',
641
- }
642
- : prefillAddress
643
- ? {
644
- email: prefillAddress.email,
645
- firstName: prefillAddress.firstName,
646
- lastName: prefillAddress.lastName,
647
- line1: prefillAddress.line1,
648
- line2: prefillAddress.line2 || '',
649
- city: prefillAddress.city,
650
- region: prefillAddress.region || '',
651
- postalCode: prefillAddress.postalCode,
652
- country: prefillAddress.country,
653
- phone: prefillAddress.phone || '',
654
- }
655
- : prefillCustomer
656
- ? {
657
- email: prefillCustomer.email,
658
- firstName: prefillCustomer.firstName || '',
659
- lastName: prefillCustomer.lastName || '',
660
- phone: prefillCustomer.phone || '',
661
- }
662
- : undefined
663
- }
664
- />
665
- </div>
666
- )}
667
-
668
- {/* Step 2: Shipping */}
669
- {step === 'shipping' && (
670
- <div>
671
- <div className="mb-4 flex items-center justify-between">
672
- <h2 className="text-foreground text-lg font-semibold">{t('shippingMethod')}</h2>
673
- <button
674
- type="button"
675
- onClick={() => setStep('address')}
676
- className="text-primary text-sm hover:underline"
677
- >
678
- {t('editAddress')}
679
- </button>
680
- </div>
681
-
682
- <ShippingStep
683
- rates={shippingRates}
684
- selectedRateId={selectedRateId}
685
- onSelect={handleShippingSelect}
686
- loading={loading}
687
- />
688
- </div>
689
- )}
690
-
691
- {/* Pickup */}
692
- {step === 'pickup' && (
693
- <div>
694
- <div className="mb-4 flex items-center justify-between">
695
- <h2 className="text-foreground text-lg font-semibold">{t('pickupLocation')}</h2>
696
- <button
697
- type="button"
698
- onClick={() => setStep('method')}
699
- className="text-primary text-sm hover:underline"
700
- >
701
- {t('changeMethod')}
702
- </button>
703
- </div>
704
- <PickupStep
705
- locations={pickupLocations}
706
- onSelect={handlePickupSelect}
707
- loading={loading}
708
- initialEmail={checkout?.email || ''}
709
- />
710
- </div>
711
- )}
712
-
713
- {/* Custom Fields (optional, between shipping/pickup and payment) */}
714
- {step === 'custom-fields' && checkout && (
715
- <div>
716
- <div className="mb-4 flex items-center justify-between">
717
- <h2 className="text-foreground text-lg font-semibold">{t('customFieldsTitle')}</h2>
718
- <button
719
- type="button"
720
- onClick={() => setStep(deliveryType === 'pickup' ? 'pickup' : 'shipping')}
721
- className="text-primary text-sm hover:underline"
722
- >
723
- {deliveryType === 'pickup' ? t('changePickup') : t('changeShipping')}
724
- </button>
725
- </div>
726
- <CustomFieldsStep
727
- fields={customFields}
728
- values={customFieldValues}
729
- onChange={(key, value) =>
730
- setCustomFieldValues((prev) => ({ ...prev, [key]: value }))
731
- }
732
- onApply={handleCustomFieldsApply}
733
- onUploadFile={(file) => getClient().uploadCustomizationFile(file)}
734
- loading={customFieldsLoading}
735
- />
736
- </div>
737
- )}
738
-
739
- {/* Payment */}
740
- {step === 'payment' && checkout && (
741
- <div>
742
- <div className="mb-4 flex items-center justify-between">
743
- <h2 className="text-foreground text-lg font-semibold">{t('payment')}</h2>
744
- {customFields.length > 0 ? (
745
- <button
746
- type="button"
747
- onClick={() => setStep('custom-fields')}
748
- className="text-primary text-sm hover:underline"
749
- >
750
- {t('changeOptions')}
751
- </button>
752
- ) : (
753
- !isAllDigital && (
754
- <button
755
- type="button"
756
- onClick={() => setStep(deliveryType === 'pickup' ? 'pickup' : 'shipping')}
757
- className="text-primary text-sm hover:underline"
758
- >
759
- {deliveryType === 'pickup' ? t('changePickup') : t('changeShipping')}
760
- </button>
761
- )
762
- )}
763
- </div>
764
-
765
- <PaymentStep checkoutId={checkout.id} />
766
- </div>
767
- )}
768
- </div>
769
-
770
- {/* Order summary sidebar */}
771
- <div className="lg:col-span-1">
772
- <div className="bg-muted/50 border-border sticky top-24 rounded-lg border p-6">
773
- <h3 className="text-foreground mb-4 text-lg font-semibold">{t('orderSummary')}</h3>
774
-
775
- {/* Line items */}
776
- {checkout?.lineItems && checkout.lineItems.length > 0 ? (
777
- <div className="mb-4 space-y-3">
778
- {checkout.lineItems.map((item) => {
779
- const imageUrl = item.product.images?.[0]?.url || null;
780
- const name = item.variant?.name || item.product.name;
781
- const lineTotal = parseFloat(item.unitPrice) * item.quantity;
782
-
783
- return (
784
- <div key={item.id} className="flex gap-3">
785
- <div className="bg-muted relative h-12 w-12 flex-shrink-0 overflow-hidden rounded">
786
- {imageUrl ? (
787
- <Image
788
- src={imageUrl}
789
- alt={name}
790
- fill
791
- sizes="48px"
792
- className="object-cover"
793
- />
794
- ) : (
795
- <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
796
- <svg
797
- className="h-5 w-5"
798
- fill="none"
799
- viewBox="0 0 24 24"
800
- stroke="currentColor"
801
- >
802
- <path
803
- strokeLinecap="round"
804
- strokeLinejoin="round"
805
- strokeWidth={1.5}
806
- d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
807
- />
808
- </svg>
809
- </div>
810
- )}
811
- </div>
812
-
813
- <div className="min-w-0 flex-1">
814
- <p className="text-foreground truncate text-sm">{name}</p>
815
- <p className="text-muted-foreground text-xs">
816
- {tc('qty')} {item.quantity}
817
- </p>
818
- </div>
819
-
820
- <span className="text-foreground flex-shrink-0 text-sm font-medium">
821
- {formatPrice(lineTotal, { currency }) as string}
822
- </span>
823
- </div>
824
- );
825
- })}
826
- </div>
827
- ) : (
828
- // Fallback to cart items if checkout line items aren't loaded yet
829
- cart && (
830
- <div className="mb-4 space-y-2">
831
- <p className="text-muted-foreground text-sm">
832
- {cart.items.length} {cart.items.length === 1 ? tc('item') : tc('items')}
833
- </p>
834
- </div>
835
- )
836
- )}
837
-
838
- {/* Order bumps */}
839
- {orderBumps?.bumps && orderBumps.bumps.length > 0 && (
840
- <div className="border-border space-y-2 border-t pt-4">
841
- <p className="text-foreground text-xs font-semibold uppercase tracking-wide">
842
- {t('addToYourOrder')}
843
- </p>
844
- {orderBumps.bumps.map((bump) => (
845
- <OrderBumpCard
846
- key={bump.id}
847
- bump={bump}
848
- isAdded={addedBumpIds.has(bump.id)}
849
- onToggle={handleBumpToggle}
850
- loading={bumpLoading === bump.id}
851
- />
852
- ))}
853
- </div>
854
- )}
855
-
856
- {/* Coupon input — show from shipping/pickup step onwards (or immediately if digital) */}
857
- {cart &&
858
- (isAllDigital || step === 'shipping' || step === 'pickup' || step === 'payment') && (
859
- <div className="border-border border-t pt-4">
860
- <CouponInput
861
- cart={cart}
862
- checkoutId={checkout?.id}
863
- onUpdate={handleCouponUpdate}
864
- />
865
- </div>
866
- )}
867
-
868
- {/* Totals */}
869
- {checkout &&
870
- (() => {
871
- // When the store prices include tax (VAT-style), the on-row
872
- // `checkout.subtotal` is GROSS it already contains the tax.
873
- // Show the net (tax-excluded) value here, then a separate VAT
874
- // line below, so the customer sees the breakdown the merchant
875
- // asked for. Falls back to the raw subtotal when no breakdown
876
- // is available yet (e.g. shipping address not entered).
877
- const isInclusive = checkout.taxBreakdown?.pricesIncludeTax === true;
878
- const displayedSubtotal =
879
- isInclusive && typeof checkout.taxBreakdown?.subtotal === 'number'
880
- ? checkout.taxBreakdown.subtotal
881
- : parseFloat(checkout.subtotal);
882
- const subtotalLabel = isInclusive ? tc('subtotalExclTax') : tc('subtotal');
883
- return (
884
- <div className="border-border space-y-2 border-t pt-4 text-sm">
885
- <div className="flex items-center justify-between">
886
- <span className="text-muted-foreground">{subtotalLabel}</span>
887
- <span className="text-foreground">
888
- {formatPrice(displayedSubtotal, { currency }) as string}
889
- </span>
890
- </div>
891
-
892
- {(() => {
893
- const totalDiscount = parseFloat(checkout.discountAmount);
894
- const ruleAmt = parseFloat(checkout.ruleDiscountAmount || '0');
895
- const couponAmt = totalDiscount - ruleAmt;
896
- const rules = cart?.appliedDiscounts;
897
- if (totalDiscount <= 0) return null;
898
- return (
899
- <>
900
- {rules && rules.length > 0
901
- ? rules.map((rule) => (
902
- <div
903
- key={rule.ruleId}
904
- className="flex items-center justify-between"
905
- >
906
- <span className="text-muted-foreground">{rule.ruleName}</span>
907
- <span className="text-destructive">
908
- -
909
- {
910
- formatPrice(parseFloat(rule.discountAmount), {
911
- currency,
912
- }) as string
913
- }
914
- </span>
915
- </div>
916
- ))
917
- : ruleAmt > 0 && (
918
- <div className="flex items-center justify-between">
919
- <span className="text-muted-foreground">
920
- {tc('generalDiscount')}
921
- </span>
922
- <span className="text-destructive">
923
- -{formatPrice(ruleAmt, { currency }) as string}
924
- </span>
925
- </div>
926
- )}
927
- {checkout.couponCode && couponAmt > 0 && (
928
- <div className="flex items-center justify-between">
929
- <span className="text-muted-foreground">
930
- {tc('couponDiscount')} ({checkout.couponCode})
931
- </span>
932
- <span className="text-destructive">
933
- -{formatPrice(couponAmt, { currency }) as string}
934
- </span>
935
- </div>
936
- )}
937
- {!checkout.couponCode &&
938
- ruleAmt <= 0 &&
939
- (!rules || rules.length === 0) && (
940
- <div className="flex items-center justify-between">
941
- <span className="text-muted-foreground">{tc('discount')}</span>
942
- <span className="text-destructive">
943
- -{formatPrice(totalDiscount, { currency }) as string}
944
- </span>
945
- </div>
946
- )}
947
- </>
948
- );
949
- })()}
950
-
951
- {(parseFloat(checkout.shippingAmount) > 0 ||
952
- checkout.deliveryType === 'pickup') && (
953
- <div className="flex items-center justify-between">
954
- <span className="text-muted-foreground">
955
- {checkout.deliveryType === 'pickup' ? tc('pickup') : tc('shipping')}
956
- </span>
957
- <span className="text-foreground">
958
- {parseFloat(checkout.shippingAmount) === 0
959
- ? tc('free')
960
- : (formatPrice(parseFloat(checkout.shippingAmount), {
961
- currency,
962
- }) as string)}
963
- </span>
964
- </div>
965
- )}
966
-
967
- <TaxDisplay
968
- addressSet={!!checkout.shippingAddress}
969
- taxAmount={checkout.taxAmount}
970
- taxBreakdown={checkout.taxBreakdown}
971
- />
972
-
973
- {/* Custom field surcharges (one line per applied surcharge) */}
974
- {checkout.appliedSurcharges && checkout.appliedSurcharges.length > 0 && (
975
- <>
976
- {checkout.appliedSurcharges.map((s) => (
977
- <div key={s.key} className="flex items-center justify-between">
978
- <span className="text-muted-foreground">{s.name}</span>
979
- <span className="text-foreground">
980
- {formatPrice(Number(s.amount), { currency }) as string}
981
- </span>
982
- </div>
983
- ))}
984
- </>
985
- )}
986
-
987
- <div className="border-border mt-2 border-t pt-2">
988
- <div className="flex items-center justify-between">
989
- <span className="text-foreground font-semibold">{tc('total')}</span>
990
- <span className="text-foreground text-base font-semibold">
991
- {formatPrice(parseFloat(checkout.total), { currency }) as string}
992
- </span>
993
- </div>
994
- </div>
995
- </div>
996
- );
997
- })()}
998
- </div>
999
- </div>
1000
- </div>
1001
- </div>
1002
- );
1003
- }
1004
-
1005
- export default function CheckoutPage() {
1006
- return (
1007
- <Suspense
1008
- fallback={
1009
- <div className="flex min-h-[60vh] items-center justify-center">
1010
- <LoadingSpinner size="lg" />
1011
- </div>
1012
- }
1013
- >
1014
- <CheckoutContent />
1015
- </Suspense>
1016
- );
1017
- }
1
+ 'use client';
2
+
3
+ import { Suspense, useEffect, useState, useCallback, useRef } from 'react';
4
+ import { useSearchParams } from 'next/navigation';
5
+ import { CdnImage as Image } from '@/ui/shared/cdn-image';
6
+ import { Link } from '@/core/lib/navigation';
7
+ import type {
8
+ Checkout,
9
+ ShippingRate,
10
+ SetShippingAddressDto,
11
+ ShippingDestinations,
12
+ PickupLocation,
13
+ CheckoutBumpsResponse,
14
+ CheckoutCustomFieldDefinition,
15
+ } from 'brainerce';
16
+ import { formatPrice } from 'brainerce';
17
+ import { getClient } from '@/core/lib/brainerce';
18
+ import { useStoreInfo, useCart, useAuth } from '@/core/providers/store-provider';
19
+ import { useCurrency } from '@/core/lib/use-currency';
20
+ import { CheckoutForm } from '@/components/checkout/checkout-form';
21
+ import { ShippingStep } from '@/components/checkout/shipping-step';
22
+ import { PaymentStep } from '@/components/checkout/payment-step';
23
+ import { DeliveryMethodStep } from '@/components/checkout/delivery-method-step';
24
+ import { PickupStep } from '@/components/checkout/pickup-step';
25
+ import { CustomFieldsStep } from '@/components/checkout/custom-fields-step';
26
+ import { TaxDisplay } from '@/components/checkout/tax-display';
27
+ import { OrderBumpCard } from '@/components/checkout/order-bump-card';
28
+ import { CouponInput } from '@/ui/cart/coupon-input';
29
+ import { ReservationCountdown } from '@/ui/cart/reservation-countdown';
30
+ import { LoadingSpinner } from '@/ui/shared/loading-spinner';
31
+ import { useTranslations } from '@/core/lib/translations';
32
+ import { cn } from '@/core/lib/utils';
33
+ import { isValidCheckoutId } from '@/core/lib/safe-redirect';
34
+ import { trackBeginCheckout } from '@/core/lib/tracking';
35
+
36
+ type CheckoutStep = 'method' | 'address' | 'shipping' | 'pickup' | 'custom-fields' | 'payment';
37
+
38
+ function CheckoutContent() {
39
+ const searchParams = useSearchParams();
40
+ const { storeInfo } = useStoreInfo();
41
+ const { cart, refreshCart } = useCart();
42
+ const { isLoggedIn } = useAuth();
43
+ const currency = useCurrency();
44
+ const t = useTranslations('checkout');
45
+ const tc = useTranslations('common');
46
+
47
+ const [step, setStep] = useState<CheckoutStep>('address');
48
+ const [checkout, setCheckout] = useState<Checkout | null>(null);
49
+ const [shippingRates, setShippingRates] = useState<ShippingRate[]>([]);
50
+ const [selectedRateId, setSelectedRateId] = useState<string | null>(null);
51
+ const [loading, setLoading] = useState(false);
52
+ const [initializing, setInitializing] = useState(true);
53
+ const [error, setError] = useState<string | null>(null);
54
+ const [destinations, setDestinations] = useState<ShippingDestinations | null>(null);
55
+ const [pickupLocations, setPickupLocations] = useState<PickupLocation[]>([]);
56
+ const [deliveryType, setDeliveryType] = useState<'shipping' | 'pickup'>('shipping');
57
+ const [isAllDigital, setIsAllDigital] = useState(false);
58
+ const [prefillAddress, setPrefillAddress] = useState<SetShippingAddressDto | null>(null);
59
+ const [prefillCustomer, setPrefillCustomer] = useState<{
60
+ email: string;
61
+ firstName?: string;
62
+ lastName?: string;
63
+ phone?: string;
64
+ } | null>(null);
65
+ const [hasSavedAddress, setHasSavedAddress] = useState(false);
66
+ const [orderBumps, setOrderBumps] = useState<CheckoutBumpsResponse | null>(null);
67
+ const [addedBumpIds, setAddedBumpIds] = useState<Set<string>>(new Set());
68
+ const [bumpLoading, setBumpLoading] = useState<string | null>(null);
69
+ const [customFields, setCustomFields] = useState<CheckoutCustomFieldDefinition[]>([]);
70
+ const [customFieldValues, setCustomFieldValues] = useState<Record<string, unknown>>({});
71
+ const [customFieldsLoading, setCustomFieldsLoading] = useState(false);
72
+
73
+ // `begin_checkout` — fired once the cart is loaded, guarded by a ref so a
74
+ // re-render (or the shopper returning from a canceled payment) doesn't
75
+ // report a second checkout start for the same cart.
76
+ const beganCheckoutForCartRef = useRef<string | null>(null);
77
+ useEffect(() => {
78
+ if (!cart?.id || !cart.items?.length) return;
79
+ if (beganCheckoutForCartRef.current === cart.id) return;
80
+ beganCheckoutForCartRef.current = cart.id;
81
+ trackBeginCheckout(cart, currency);
82
+ }, [cart, currency]);
83
+
84
+ // Check for returning from canceled payment
85
+ const canceled = searchParams.get('canceled') === 'true';
86
+ const checkoutIdParam = searchParams.get('checkout_id');
87
+ const existingCheckoutId = isValidCheckoutId(checkoutIdParam) ? checkoutIdParam : null;
88
+
89
+ // Pre-fill address and customer data from profile when logged in
90
+ useEffect(() => {
91
+ if (!isLoggedIn) return;
92
+ getClient()
93
+ .getCheckoutPrefillData()
94
+ .then((data) => {
95
+ if (data.customer) setPrefillCustomer(data.customer);
96
+ if (data.shippingAddress) {
97
+ setPrefillAddress(data.shippingAddress);
98
+ setHasSavedAddress(true);
99
+ }
100
+ })
101
+ .catch(() => {});
102
+ }, [isLoggedIn]);
103
+
104
+ // Initialize or resume checkout (only once)
105
+ const checkoutInitRef = useRef(false);
106
+ const cartIdRef = useRef<string | null>(null);
107
+
108
+ useEffect(() => {
109
+ // Only init once, or if cart ID actually changed (e.g. cart was replaced)
110
+ if (!cart?.id) return;
111
+ if (checkoutInitRef.current && cartIdRef.current === cart.id) return;
112
+ checkoutInitRef.current = true;
113
+ cartIdRef.current = cart.id;
114
+
115
+ const initCheckout = async () => {
116
+ try {
117
+ setInitializing(true);
118
+ setError(null);
119
+ const client = getClient();
120
+
121
+ // Fetch shipping destinations and pickup locations in parallel
122
+ client
123
+ .getShippingDestinations()
124
+ .then(setDestinations)
125
+ .catch(() => {});
126
+
127
+ const locations = await client.getPickupLocations().catch(() => [] as PickupLocation[]);
128
+ setPickupLocations(locations);
129
+
130
+ // If returning with existing checkout ID, resume it
131
+ if (existingCheckoutId) {
132
+ const existing = await client.getCheckout(existingCheckoutId);
133
+ setCheckout(existing);
134
+
135
+ // Preload custom field definitions and any existing values so the
136
+ // step indicator and "change options" affordance work on resume.
137
+ client
138
+ .getCheckoutCustomFields(existing.id)
139
+ .then((fields) => {
140
+ setCustomFields(fields);
141
+ const existingValues = (
142
+ existing as unknown as {
143
+ customFieldValues?: Record<string, unknown> | null;
144
+ }
145
+ ).customFieldValues;
146
+ if (existingValues) setCustomFieldValues(existingValues);
147
+ })
148
+ .catch(() => {
149
+ setCustomFields([]);
150
+ });
151
+
152
+ // Determine step based on checkout state
153
+ const allDigital = existing.lineItems.every(
154
+ (i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
155
+ );
156
+ setIsAllDigital(allDigital);
157
+ if (allDigital) {
158
+ // Digital products: show contact info step if email not set, else payment
159
+ setStep(existing.email ? 'payment' : 'address');
160
+ } else if (existing.deliveryType === 'pickup' && existing.pickupLocation) {
161
+ setDeliveryType('pickup');
162
+ setStep('payment');
163
+ } else if (existing.shippingAddress && existing.shippingRateId) {
164
+ setStep('payment');
165
+ } else if (existing.shippingAddress) {
166
+ // Fetch shipping rates
167
+ const rates = await client.getShippingRates(existing.id);
168
+ setShippingRates(rates);
169
+ setStep('shipping');
170
+ } else if (locations.length > 0) {
171
+ setStep('method');
172
+ }
173
+ return;
174
+ }
175
+
176
+ // Create new checkout — cart is always server-side now
177
+ const newCheckout = await client.createCheckout({ cartId: cart.id });
178
+ setCheckout(newCheckout);
179
+
180
+ // If all items are downloadable, skip shipping — show contact info step
181
+ const allDigital = newCheckout.lineItems.every(
182
+ (i) => (i.product as unknown as { isDownloadable?: boolean }).isDownloadable
183
+ );
184
+ setIsAllDigital(allDigital);
185
+ if (allDigital) {
186
+ setStep('address');
187
+ return;
188
+ }
189
+
190
+ // If pickup locations exist, start with delivery method selection
191
+ if (locations.length > 0) {
192
+ setStep('method');
193
+ }
194
+ } catch (err) {
195
+ const message = err instanceof Error ? err.message : t('failedToInitCheckout');
196
+ setError(message);
197
+ } finally {
198
+ setInitializing(false);
199
+ }
200
+ };
201
+
202
+ initCheckout();
203
+ }, [cart?.id, existingCheckoutId]);
204
+
205
+ // Load order bumps when checkout is available
206
+ useEffect(() => {
207
+ if (!checkout?.id || storeInfo?.upsell?.checkoutOrderBumpEnabled === false) {
208
+ setOrderBumps(null);
209
+ return;
210
+ }
211
+ const client = getClient();
212
+ client
213
+ .getCheckoutBumps(checkout.id)
214
+ .then((data) => {
215
+ setOrderBumps(data);
216
+ // Detect already-added bumps from cart
217
+ if (cart?.items) {
218
+ const existingBumpIds = new Set<string>();
219
+ for (const item of cart.items) {
220
+ const meta = item.metadata as Record<string, unknown> | undefined;
221
+ if (meta?.isOrderBump && meta?.orderBumpId) {
222
+ existingBumpIds.add(meta.orderBumpId as string);
223
+ }
224
+ }
225
+ setAddedBumpIds(existingBumpIds);
226
+ }
227
+ })
228
+ .catch(() => {});
229
+ }, [checkout?.id, storeInfo?.upsell?.checkoutOrderBumpEnabled]);
230
+
231
+ // Handle bump toggle
232
+ async function handleBumpToggle(bumpId: string, add: boolean, variantId?: string) {
233
+ if (!cart?.id || bumpLoading) return;
234
+ try {
235
+ setBumpLoading(bumpId);
236
+ const client = getClient();
237
+ if (add) {
238
+ await client.addOrderBump(cart.id, bumpId, variantId);
239
+ setAddedBumpIds((prev) => new Set([...prev, bumpId]));
240
+ } else {
241
+ await client.removeOrderBump(cart.id, bumpId);
242
+ setAddedBumpIds((prev) => {
243
+ const next = new Set(prev);
244
+ next.delete(bumpId);
245
+ return next;
246
+ });
247
+ }
248
+ await refreshCart();
249
+ } catch (err) {
250
+ console.error('Failed to toggle order bump:', err);
251
+ } finally {
252
+ setBumpLoading(null);
253
+ }
254
+ }
255
+
256
+ // Handle shipping address submission
257
+ async function handleAddressSubmit(
258
+ address: SetShippingAddressDto,
259
+ consent: { acceptsMarketing: boolean; saveDetails: boolean }
260
+ ) {
261
+ if (!checkout) return;
262
+
263
+ try {
264
+ setLoading(true);
265
+ setError(null);
266
+ const client = getClient();
267
+
268
+ if (isAllDigital) {
269
+ // Digital products: set customer info only, skip shipping
270
+ const updated = await client.setCheckoutCustomer(checkout.id, {
271
+ email: address.email,
272
+ firstName: address.firstName,
273
+ lastName: address.lastName,
274
+ phone: address.phone,
275
+ acceptsMarketing: consent.acceptsMarketing,
276
+ notes: address.notes,
277
+ });
278
+ setCheckout(updated);
279
+ setStep('payment');
280
+ } else {
281
+ const response = await client.setShippingAddress(checkout.id, address);
282
+ setCheckout(response.checkout);
283
+ setShippingRates(response.rates);
284
+ setStep('shipping');
285
+ }
286
+
287
+ // Update marketing preference for logged-in users
288
+ if (isLoggedIn) {
289
+ try {
290
+ await client.updateMyProfile({ acceptsMarketing: consent.acceptsMarketing });
291
+ } catch {
292
+ // non-critical
293
+ }
294
+ }
295
+
296
+ // Save address to profile if checkbox was checked and no existing saved address
297
+ if (isLoggedIn && consent.saveDetails && !hasSavedAddress && !isAllDigital) {
298
+ try {
299
+ await client.addMyAddress({
300
+ firstName: address.firstName,
301
+ lastName: address.lastName,
302
+ line1: address.line1,
303
+ line2: address.line2,
304
+ city: address.city,
305
+ region: address.region,
306
+ postalCode: address.postalCode,
307
+ country: address.country,
308
+ phone: address.phone,
309
+ isDefault: true,
310
+ });
311
+ } catch {
312
+ // non-critical
313
+ }
314
+ }
315
+ } catch (err) {
316
+ const message = err instanceof Error ? err.message : t('failedToSaveAddress');
317
+ setError(message);
318
+ } finally {
319
+ setLoading(false);
320
+ }
321
+ }
322
+
323
+ // After shipping/pickup is set, decide whether to show the custom-fields step
324
+ // or jump straight to payment. Returns the next step.
325
+ async function loadCustomFieldsOrSkip(checkoutId: string): Promise<CheckoutStep> {
326
+ try {
327
+ const fields = await getClient().getCheckoutCustomFields(checkoutId);
328
+ setCustomFields(fields);
329
+ return fields.length > 0 ? 'custom-fields' : 'payment';
330
+ } catch {
331
+ // If the endpoint isn't available or fails, fall through to payment
332
+ // rather than blocking the customer.
333
+ setCustomFields([]);
334
+ return 'payment';
335
+ }
336
+ }
337
+
338
+ // Handle shipping method selection
339
+ async function handleShippingSelect(rateId: string) {
340
+ if (!checkout) return;
341
+
342
+ try {
343
+ setLoading(true);
344
+ setError(null);
345
+ setSelectedRateId(rateId);
346
+ const client = getClient();
347
+
348
+ const updated = await client.selectShippingMethod(checkout.id, rateId);
349
+ setCheckout(updated);
350
+ setStep(await loadCustomFieldsOrSkip(updated.id));
351
+ } catch (err) {
352
+ const message = err instanceof Error ? err.message : t('failedToSelectShipping');
353
+ setError(message);
354
+ } finally {
355
+ setLoading(false);
356
+ }
357
+ }
358
+
359
+ // Submit custom fields
360
+ async function handleCustomFieldsApply() {
361
+ if (!checkout) return;
362
+ try {
363
+ setCustomFieldsLoading(true);
364
+ setError(null);
365
+ const updated = await getClient().setCheckoutCustomFields(checkout.id, customFieldValues);
366
+ setCheckout(updated);
367
+ setStep('payment');
368
+ } catch (err) {
369
+ const message = err instanceof Error ? err.message : t('customFieldsFailed');
370
+ setError(message);
371
+ } finally {
372
+ setCustomFieldsLoading(false);
373
+ }
374
+ }
375
+
376
+ // Handle delivery method selection
377
+ async function handleDeliveryTypeSelect(method: 'shipping' | 'pickup') {
378
+ if (!checkout) return;
379
+
380
+ try {
381
+ setLoading(true);
382
+ setError(null);
383
+ setDeliveryType(method);
384
+ const client = getClient();
385
+
386
+ await client.setDeliveryType(checkout.id, method);
387
+
388
+ if (method === 'shipping') {
389
+ setStep('address');
390
+ } else {
391
+ setStep('pickup');
392
+ }
393
+ } catch (err) {
394
+ const message = err instanceof Error ? err.message : t('failedToSetDeliveryMethod');
395
+ setError(message);
396
+ } finally {
397
+ setLoading(false);
398
+ }
399
+ }
400
+
401
+ // Handle pickup location selection
402
+ async function handlePickupSelect(
403
+ locationId: string,
404
+ customerInfo: { email: string; firstName?: string; lastName?: string; phone?: string }
405
+ ) {
406
+ if (!checkout) return;
407
+
408
+ try {
409
+ setLoading(true);
410
+ setError(null);
411
+ const client = getClient();
412
+
413
+ const updated = await client.selectPickupLocation(checkout.id, {
414
+ pickupRateId: locationId,
415
+ email: customerInfo.email,
416
+ firstName: customerInfo.firstName,
417
+ lastName: customerInfo.lastName,
418
+ phone: customerInfo.phone,
419
+ });
420
+ setCheckout(updated);
421
+ setStep(await loadCustomFieldsOrSkip(updated.id));
422
+ } catch (err) {
423
+ const message = err instanceof Error ? err.message : t('failedToSelectPickup');
424
+ setError(message);
425
+ } finally {
426
+ setLoading(false);
427
+ }
428
+ }
429
+
430
+ // Refresh cart after coupon apply/remove.
431
+ // The checkout totals are updated server-side by applyCheckoutCoupon/removeCheckoutCoupon,
432
+ // so we re-fetch the checkout to get the updated discountAmount and total.
433
+ const handleCouponUpdate = useCallback(async () => {
434
+ await refreshCart();
435
+ if (checkout) {
436
+ try {
437
+ const client = getClient();
438
+ const updated = await client.getCheckout(checkout.id);
439
+ setCheckout(updated);
440
+ } catch (err) {
441
+ console.error('Failed to refresh checkout after coupon update:', err);
442
+ }
443
+ }
444
+ }, [checkout, refreshCart]);
445
+
446
+ if (initializing) {
447
+ return (
448
+ <div className="flex min-h-[60vh] items-center justify-center">
449
+ <LoadingSpinner size="lg" />
450
+ </div>
451
+ );
452
+ }
453
+
454
+ // Empty cart
455
+ if (!cart || cart.items.length === 0) {
456
+ return (
457
+ <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
458
+ <h1 className="text-foreground text-2xl font-bold">{t('emptyCart')}</h1>
459
+ <p className="text-muted-foreground mt-2">{t('emptyCartSubtitle')}</p>
460
+ <Link
461
+ href="/products"
462
+ className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
463
+ >
464
+ {tc('shopNow')}
465
+ </Link>
466
+ </div>
467
+ );
468
+ }
469
+
470
+ if (error && !checkout) {
471
+ return (
472
+ <div className="mx-auto max-w-7xl px-4 py-16 text-center sm:px-6 lg:px-8">
473
+ <h1 className="text-foreground text-2xl font-bold">{t('errorTitle')}</h1>
474
+ <p className="text-destructive mt-2">{error}</p>
475
+ <Link
476
+ href="/cart"
477
+ className="bg-primary text-primary-foreground mt-6 inline-flex items-center rounded px-6 py-3 font-medium transition-opacity hover:opacity-90"
478
+ >
479
+ {t('returnToCart')}
480
+ </Link>
481
+ </div>
482
+ );
483
+ }
484
+
485
+ const customFieldsStep =
486
+ customFields.length > 0
487
+ ? [{ key: 'custom-fields' as CheckoutStep, label: t('stepCustomFields') }]
488
+ : [];
489
+
490
+ const steps: { key: CheckoutStep; label: string }[] = isAllDigital
491
+ ? [
492
+ { key: 'address', label: t('stepContactInfo') },
493
+ ...customFieldsStep,
494
+ { key: 'payment', label: t('stepPayment') },
495
+ ]
496
+ : pickupLocations.length > 0
497
+ ? deliveryType === 'pickup'
498
+ ? [
499
+ { key: 'method', label: t('stepMethod') },
500
+ { key: 'pickup', label: t('stepPickup') },
501
+ ...customFieldsStep,
502
+ { key: 'payment', label: t('stepPayment') },
503
+ ]
504
+ : [
505
+ { key: 'method', label: t('stepMethod') },
506
+ { key: 'address', label: t('stepAddress') },
507
+ { key: 'shipping', label: t('stepShipping') },
508
+ ...customFieldsStep,
509
+ { key: 'payment', label: t('stepPayment') },
510
+ ]
511
+ : [
512
+ { key: 'address', label: t('stepAddress') },
513
+ { key: 'shipping', label: t('stepShipping') },
514
+ ...customFieldsStep,
515
+ { key: 'payment', label: t('stepPayment') },
516
+ ];
517
+
518
+ const currentStepIndex = steps.findIndex((s) => s.key === step);
519
+
520
+ return (
521
+ <div className="mx-auto max-w-7xl px-4 py-8 sm:px-6 lg:px-8">
522
+ <h1 className="text-foreground mb-6 text-2xl font-bold">{t('title')}</h1>
523
+
524
+ {/* Canceled payment banner */}
525
+ {canceled && (
526
+ <div className="mb-6 rounded-lg border border-orange-200 bg-orange-50 px-4 py-3 text-sm text-orange-800 dark:border-orange-800 dark:bg-orange-950/30 dark:text-orange-300">
527
+ {t('paymentCanceledBanner')}
528
+ </div>
529
+ )}
530
+
531
+ {/* Reservation countdown */}
532
+ {checkout?.reservation?.hasReservation && (
533
+ <ReservationCountdown reservation={checkout.reservation} className="mb-6" />
534
+ )}
535
+
536
+ {/* Step indicator */}
537
+ <div className="mb-8 flex items-center gap-2">
538
+ {steps.map((s, index) => (
539
+ <div key={s.key} className="flex items-center">
540
+ {index > 0 && (
541
+ <div
542
+ className={cn(
543
+ 'mx-2 h-px w-8 sm:w-12',
544
+ index <= currentStepIndex ? 'bg-primary' : 'bg-border'
545
+ )}
546
+ />
547
+ )}
548
+ <div className="flex items-center gap-2">
549
+ <div
550
+ className={cn(
551
+ 'flex h-7 w-7 items-center justify-center rounded-full text-xs font-medium',
552
+ index < currentStepIndex
553
+ ? 'bg-primary text-primary-foreground'
554
+ : index === currentStepIndex
555
+ ? 'bg-primary text-primary-foreground'
556
+ : 'bg-muted text-muted-foreground'
557
+ )}
558
+ >
559
+ {index < currentStepIndex ? (
560
+ <svg className="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor">
561
+ <path
562
+ strokeLinecap="round"
563
+ strokeLinejoin="round"
564
+ strokeWidth={2}
565
+ d="M5 13l4 4L19 7"
566
+ />
567
+ </svg>
568
+ ) : (
569
+ index + 1
570
+ )}
571
+ </div>
572
+ <span
573
+ className={cn(
574
+ 'hidden text-sm sm:block',
575
+ index <= currentStepIndex
576
+ ? 'text-foreground font-medium'
577
+ : 'text-muted-foreground'
578
+ )}
579
+ >
580
+ {s.label}
581
+ </span>
582
+ </div>
583
+ </div>
584
+ ))}
585
+ </div>
586
+
587
+ {/* Error banner */}
588
+ {error && checkout && (
589
+ <div className="bg-destructive/10 border-destructive/20 text-destructive mb-6 rounded-lg border px-4 py-3 text-sm">
590
+ {error}
591
+ </div>
592
+ )}
593
+
594
+ <div className="grid grid-cols-1 gap-8 lg:grid-cols-3">
595
+ {/* Main content */}
596
+ <div className="lg:col-span-2">
597
+ {/* Delivery Method */}
598
+ {step === 'method' && (
599
+ <div>
600
+ <h2 className="text-foreground mb-4 text-lg font-semibold">{t('deliveryMethod')}</h2>
601
+ <DeliveryMethodStep onSelect={handleDeliveryTypeSelect} />
602
+ </div>
603
+ )}
604
+
605
+ {/* Address */}
606
+ {step === 'address' && (
607
+ <div>
608
+ <div className="mb-4 flex items-center justify-between">
609
+ <h2 className="text-foreground text-lg font-semibold">
610
+ {isAllDigital ? t('contactInfo') : t('shippingAddress')}
611
+ </h2>
612
+ {!isAllDigital && pickupLocations.length > 0 && (
613
+ <button
614
+ type="button"
615
+ onClick={() => setStep('method')}
616
+ className="text-primary text-sm hover:underline"
617
+ >
618
+ {t('changeMethod')}
619
+ </button>
620
+ )}
621
+ </div>
622
+ <CheckoutForm
623
+ onSubmit={handleAddressSubmit}
624
+ loading={loading}
625
+ destinations={isAllDigital ? null : destinations}
626
+ showSaveDetails={isLoggedIn && !hasSavedAddress && !isAllDigital}
627
+ emailOnly={isAllDigital}
628
+ initialValues={
629
+ checkout?.shippingAddress
630
+ ? {
631
+ email: checkout.email || '',
632
+ firstName: checkout.shippingAddress.firstName,
633
+ lastName: checkout.shippingAddress.lastName,
634
+ line1: checkout.shippingAddress.line1,
635
+ line2: checkout.shippingAddress.line2 || '',
636
+ city: checkout.shippingAddress.city,
637
+ region: checkout.shippingAddress.region || '',
638
+ postalCode: checkout.shippingAddress.postalCode,
639
+ country: checkout.shippingAddress.country,
640
+ phone: checkout.shippingAddress.phone || '',
641
+ }
642
+ : prefillAddress
643
+ ? {
644
+ email: prefillAddress.email,
645
+ firstName: prefillAddress.firstName,
646
+ lastName: prefillAddress.lastName,
647
+ line1: prefillAddress.line1,
648
+ line2: prefillAddress.line2 || '',
649
+ city: prefillAddress.city,
650
+ region: prefillAddress.region || '',
651
+ postalCode: prefillAddress.postalCode,
652
+ country: prefillAddress.country,
653
+ phone: prefillAddress.phone || '',
654
+ }
655
+ : prefillCustomer
656
+ ? {
657
+ email: prefillCustomer.email,
658
+ firstName: prefillCustomer.firstName || '',
659
+ lastName: prefillCustomer.lastName || '',
660
+ phone: prefillCustomer.phone || '',
661
+ }
662
+ : undefined
663
+ }
664
+ />
665
+ </div>
666
+ )}
667
+
668
+ {/* Step 2: Shipping */}
669
+ {step === 'shipping' && (
670
+ <div>
671
+ <div className="mb-4 flex items-center justify-between">
672
+ <h2 className="text-foreground text-lg font-semibold">{t('shippingMethod')}</h2>
673
+ <button
674
+ type="button"
675
+ onClick={() => setStep('address')}
676
+ className="text-primary text-sm hover:underline"
677
+ >
678
+ {t('editAddress')}
679
+ </button>
680
+ </div>
681
+
682
+ <ShippingStep
683
+ rates={shippingRates}
684
+ selectedRateId={selectedRateId}
685
+ onSelect={handleShippingSelect}
686
+ loading={loading}
687
+ />
688
+ </div>
689
+ )}
690
+
691
+ {/* Pickup */}
692
+ {step === 'pickup' && (
693
+ <div>
694
+ <div className="mb-4 flex items-center justify-between">
695
+ <h2 className="text-foreground text-lg font-semibold">{t('pickupLocation')}</h2>
696
+ <button
697
+ type="button"
698
+ onClick={() => setStep('method')}
699
+ className="text-primary text-sm hover:underline"
700
+ >
701
+ {t('changeMethod')}
702
+ </button>
703
+ </div>
704
+ <PickupStep
705
+ locations={pickupLocations}
706
+ onSelect={handlePickupSelect}
707
+ loading={loading}
708
+ initialEmail={checkout?.email || ''}
709
+ />
710
+ </div>
711
+ )}
712
+
713
+ {/* Custom Fields (optional, between shipping/pickup and payment) */}
714
+ {step === 'custom-fields' && checkout && (
715
+ <div>
716
+ <div className="mb-4 flex items-center justify-between">
717
+ <h2 className="text-foreground text-lg font-semibold">{t('customFieldsTitle')}</h2>
718
+ <button
719
+ type="button"
720
+ onClick={() => setStep(deliveryType === 'pickup' ? 'pickup' : 'shipping')}
721
+ className="text-primary text-sm hover:underline"
722
+ >
723
+ {deliveryType === 'pickup' ? t('changePickup') : t('changeShipping')}
724
+ </button>
725
+ </div>
726
+ <CustomFieldsStep
727
+ fields={customFields}
728
+ values={customFieldValues}
729
+ onChange={(key, value) =>
730
+ setCustomFieldValues((prev) => ({ ...prev, [key]: value }))
731
+ }
732
+ onApply={handleCustomFieldsApply}
733
+ onUploadFile={(file) => getClient().uploadCustomizationFile(file)}
734
+ timezone={storeInfo?.timezone}
735
+ loading={customFieldsLoading}
736
+ />
737
+ </div>
738
+ )}
739
+
740
+ {/* Payment */}
741
+ {step === 'payment' && checkout && (
742
+ <div>
743
+ <div className="mb-4 flex items-center justify-between">
744
+ <h2 className="text-foreground text-lg font-semibold">{t('payment')}</h2>
745
+ {customFields.length > 0 ? (
746
+ <button
747
+ type="button"
748
+ onClick={() => setStep('custom-fields')}
749
+ className="text-primary text-sm hover:underline"
750
+ >
751
+ {t('changeOptions')}
752
+ </button>
753
+ ) : (
754
+ !isAllDigital && (
755
+ <button
756
+ type="button"
757
+ onClick={() => setStep(deliveryType === 'pickup' ? 'pickup' : 'shipping')}
758
+ className="text-primary text-sm hover:underline"
759
+ >
760
+ {deliveryType === 'pickup' ? t('changePickup') : t('changeShipping')}
761
+ </button>
762
+ )
763
+ )}
764
+ </div>
765
+
766
+ <PaymentStep checkoutId={checkout.id} />
767
+ </div>
768
+ )}
769
+ </div>
770
+
771
+ {/* Order summary sidebar */}
772
+ <div className="lg:col-span-1">
773
+ <div className="bg-muted/50 border-border sticky top-24 rounded-lg border p-6">
774
+ <h3 className="text-foreground mb-4 text-lg font-semibold">{t('orderSummary')}</h3>
775
+
776
+ {/* Line items */}
777
+ {checkout?.lineItems && checkout.lineItems.length > 0 ? (
778
+ <div className="mb-4 space-y-3">
779
+ {checkout.lineItems.map((item) => {
780
+ const imageUrl = item.product.images?.[0]?.url || null;
781
+ const name = item.variant?.name || item.product.name;
782
+ const lineTotal = parseFloat(item.unitPrice) * item.quantity;
783
+
784
+ return (
785
+ <div key={item.id} className="flex gap-3">
786
+ <div className="bg-muted relative h-12 w-12 flex-shrink-0 overflow-hidden rounded">
787
+ {imageUrl ? (
788
+ <Image
789
+ src={imageUrl}
790
+ alt={name}
791
+ fill
792
+ sizes="48px"
793
+ className="object-cover"
794
+ />
795
+ ) : (
796
+ <div className="text-muted-foreground absolute inset-0 flex items-center justify-center">
797
+ <svg
798
+ className="h-5 w-5"
799
+ fill="none"
800
+ viewBox="0 0 24 24"
801
+ stroke="currentColor"
802
+ >
803
+ <path
804
+ strokeLinecap="round"
805
+ strokeLinejoin="round"
806
+ strokeWidth={1.5}
807
+ d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"
808
+ />
809
+ </svg>
810
+ </div>
811
+ )}
812
+ </div>
813
+
814
+ <div className="min-w-0 flex-1">
815
+ <p className="text-foreground truncate text-sm">{name}</p>
816
+ <p className="text-muted-foreground text-xs">
817
+ {tc('qty')} {item.quantity}
818
+ </p>
819
+ </div>
820
+
821
+ <span className="text-foreground flex-shrink-0 text-sm font-medium">
822
+ {formatPrice(lineTotal, { currency }) as string}
823
+ </span>
824
+ </div>
825
+ );
826
+ })}
827
+ </div>
828
+ ) : (
829
+ // Fallback to cart items if checkout line items aren't loaded yet
830
+ cart && (
831
+ <div className="mb-4 space-y-2">
832
+ <p className="text-muted-foreground text-sm">
833
+ {cart.items.length} {cart.items.length === 1 ? tc('item') : tc('items')}
834
+ </p>
835
+ </div>
836
+ )
837
+ )}
838
+
839
+ {/* Order bumps */}
840
+ {orderBumps?.bumps && orderBumps.bumps.length > 0 && (
841
+ <div className="border-border space-y-2 border-t pt-4">
842
+ <p className="text-foreground text-xs font-semibold uppercase tracking-wide">
843
+ {t('addToYourOrder')}
844
+ </p>
845
+ {orderBumps.bumps.map((bump) => (
846
+ <OrderBumpCard
847
+ key={bump.id}
848
+ bump={bump}
849
+ isAdded={addedBumpIds.has(bump.id)}
850
+ onToggle={handleBumpToggle}
851
+ loading={bumpLoading === bump.id}
852
+ />
853
+ ))}
854
+ </div>
855
+ )}
856
+
857
+ {/* Coupon input — show from shipping/pickup step onwards (or immediately if digital) */}
858
+ {cart &&
859
+ (isAllDigital || step === 'shipping' || step === 'pickup' || step === 'payment') && (
860
+ <div className="border-border border-t pt-4">
861
+ <CouponInput
862
+ cart={cart}
863
+ checkoutId={checkout?.id}
864
+ onUpdate={handleCouponUpdate}
865
+ />
866
+ </div>
867
+ )}
868
+
869
+ {/* Totals */}
870
+ {checkout &&
871
+ (() => {
872
+ // When the store prices include tax (VAT-style), the on-row
873
+ // `checkout.subtotal` is GROSS it already contains the tax.
874
+ // Show the net (tax-excluded) value here, then a separate VAT
875
+ // line below, so the customer sees the breakdown the merchant
876
+ // asked for. Falls back to the raw subtotal when no breakdown
877
+ // is available yet (e.g. shipping address not entered).
878
+ const isInclusive = checkout.taxBreakdown?.pricesIncludeTax === true;
879
+ const displayedSubtotal =
880
+ isInclusive && typeof checkout.taxBreakdown?.subtotal === 'number'
881
+ ? checkout.taxBreakdown.subtotal
882
+ : parseFloat(checkout.subtotal);
883
+ const subtotalLabel = isInclusive ? tc('subtotalExclTax') : tc('subtotal');
884
+ return (
885
+ <div className="border-border space-y-2 border-t pt-4 text-sm">
886
+ <div className="flex items-center justify-between">
887
+ <span className="text-muted-foreground">{subtotalLabel}</span>
888
+ <span className="text-foreground">
889
+ {formatPrice(displayedSubtotal, { currency }) as string}
890
+ </span>
891
+ </div>
892
+
893
+ {(() => {
894
+ const totalDiscount = parseFloat(checkout.discountAmount);
895
+ const ruleAmt = parseFloat(checkout.ruleDiscountAmount || '0');
896
+ const couponAmt = totalDiscount - ruleAmt;
897
+ const rules = cart?.appliedDiscounts;
898
+ if (totalDiscount <= 0) return null;
899
+ return (
900
+ <>
901
+ {rules && rules.length > 0
902
+ ? rules.map((rule) => (
903
+ <div
904
+ key={rule.ruleId}
905
+ className="flex items-center justify-between"
906
+ >
907
+ <span className="text-muted-foreground">{rule.ruleName}</span>
908
+ <span className="text-destructive">
909
+ -
910
+ {
911
+ formatPrice(parseFloat(rule.discountAmount), {
912
+ currency,
913
+ }) as string
914
+ }
915
+ </span>
916
+ </div>
917
+ ))
918
+ : ruleAmt > 0 && (
919
+ <div className="flex items-center justify-between">
920
+ <span className="text-muted-foreground">
921
+ {tc('generalDiscount')}
922
+ </span>
923
+ <span className="text-destructive">
924
+ -{formatPrice(ruleAmt, { currency }) as string}
925
+ </span>
926
+ </div>
927
+ )}
928
+ {checkout.couponCode && couponAmt > 0 && (
929
+ <div className="flex items-center justify-between">
930
+ <span className="text-muted-foreground">
931
+ {tc('couponDiscount')} ({checkout.couponCode})
932
+ </span>
933
+ <span className="text-destructive">
934
+ -{formatPrice(couponAmt, { currency }) as string}
935
+ </span>
936
+ </div>
937
+ )}
938
+ {!checkout.couponCode &&
939
+ ruleAmt <= 0 &&
940
+ (!rules || rules.length === 0) && (
941
+ <div className="flex items-center justify-between">
942
+ <span className="text-muted-foreground">{tc('discount')}</span>
943
+ <span className="text-destructive">
944
+ -{formatPrice(totalDiscount, { currency }) as string}
945
+ </span>
946
+ </div>
947
+ )}
948
+ </>
949
+ );
950
+ })()}
951
+
952
+ {(parseFloat(checkout.shippingAmount) > 0 ||
953
+ checkout.deliveryType === 'pickup') && (
954
+ <div className="flex items-center justify-between">
955
+ <span className="text-muted-foreground">
956
+ {checkout.deliveryType === 'pickup' ? tc('pickup') : tc('shipping')}
957
+ </span>
958
+ <span className="text-foreground">
959
+ {parseFloat(checkout.shippingAmount) === 0
960
+ ? tc('free')
961
+ : (formatPrice(parseFloat(checkout.shippingAmount), {
962
+ currency,
963
+ }) as string)}
964
+ </span>
965
+ </div>
966
+ )}
967
+
968
+ <TaxDisplay
969
+ addressSet={!!checkout.shippingAddress}
970
+ taxAmount={checkout.taxAmount}
971
+ taxBreakdown={checkout.taxBreakdown}
972
+ />
973
+
974
+ {/* Custom field surcharges (one line per applied surcharge) */}
975
+ {checkout.appliedSurcharges && checkout.appliedSurcharges.length > 0 && (
976
+ <>
977
+ {checkout.appliedSurcharges.map((s) => (
978
+ <div key={s.key} className="flex items-center justify-between">
979
+ <span className="text-muted-foreground">{s.name}</span>
980
+ <span className="text-foreground">
981
+ {formatPrice(Number(s.amount), { currency }) as string}
982
+ </span>
983
+ </div>
984
+ ))}
985
+ </>
986
+ )}
987
+
988
+ <div className="border-border mt-2 border-t pt-2">
989
+ <div className="flex items-center justify-between">
990
+ <span className="text-foreground font-semibold">{tc('total')}</span>
991
+ <span className="text-foreground text-base font-semibold">
992
+ {formatPrice(parseFloat(checkout.total), { currency }) as string}
993
+ </span>
994
+ </div>
995
+ </div>
996
+ </div>
997
+ );
998
+ })()}
999
+ </div>
1000
+ </div>
1001
+ </div>
1002
+ </div>
1003
+ );
1004
+ }
1005
+
1006
+ export default function CheckoutPage() {
1007
+ return (
1008
+ <Suspense
1009
+ fallback={
1010
+ <div className="flex min-h-[60vh] items-center justify-center">
1011
+ <LoadingSpinner size="lg" />
1012
+ </div>
1013
+ }
1014
+ >
1015
+ <CheckoutContent />
1016
+ </Suspense>
1017
+ );
1018
+ }