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