@quantaroute/checkout 1.4.0 → 1.4.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.
- package/CHANGELOG.md +103 -0
- package/README.md +76 -49
- package/dist/lib/index.d.ts +19 -3
- package/dist/lib/quantaroute-checkout.es.js +54 -51
- package/dist/lib/quantaroute-checkout.umd.js +64 -61
- package/dist/style.css +1 -1
- package/package.json +2 -1
- package/src/components/AddressForm.native.tsx +55 -1
- package/src/components/AddressForm.tsx +39 -1
- package/src/components/CheckoutWidget.tsx +8 -2
- package/src/components/EmailOtpStep.tsx +4 -1
- package/src/components/EmailStep.tsx +2 -2
- package/src/core/checkout-api.ts +7 -1
- package/src/core/types.ts +13 -2
- package/src/index.ts +3 -0
- package/src/styles/checkout.css +42 -0
- package/src/styles/checkout.native.ts +48 -0
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
} from '../core/api';
|
|
9
9
|
import type {
|
|
10
10
|
AddressFormProps,
|
|
11
|
+
AddressLabel,
|
|
11
12
|
AdministrativeInfo,
|
|
12
13
|
CompleteAddress,
|
|
13
14
|
LocationAlternative,
|
|
@@ -30,6 +31,7 @@ type FormState =
|
|
|
30
31
|
adminInfo: AdministrativeInfo;
|
|
31
32
|
alternatives: LocationAlternative[];
|
|
32
33
|
selectedLocality: string; // Selected locality name (from adminInfo or alternatives)
|
|
34
|
+
selectedLabel: AddressLabel;
|
|
33
35
|
fields: ManualFields;
|
|
34
36
|
submitting: boolean;
|
|
35
37
|
/** At least one India Post office with "Delivery" (not only "Non Delivery") */
|
|
@@ -48,6 +50,7 @@ type FormAction =
|
|
|
48
50
|
| { type: 'LOAD_ERROR'; message: string }
|
|
49
51
|
| { type: 'SET_FIELD'; key: keyof ManualFields; value: string }
|
|
50
52
|
| { type: 'SET_LOCALITY'; locality: string }
|
|
53
|
+
| { type: 'SET_LABEL'; label: AddressLabel }
|
|
51
54
|
| { type: 'SUBMIT_START' }
|
|
52
55
|
| { type: 'SUBMIT_END' };
|
|
53
56
|
|
|
@@ -103,6 +106,7 @@ function reducer(state: FormState, action: FormAction): FormState {
|
|
|
103
106
|
adminInfo: action.adminInfo,
|
|
104
107
|
alternatives: action.alternatives || [],
|
|
105
108
|
selectedLocality: action.adminInfo.locality,
|
|
109
|
+
selectedLabel: 'home',
|
|
106
110
|
fields: { ...INITIAL_FIELDS, ...preFilled },
|
|
107
111
|
submitting: false,
|
|
108
112
|
pincodeHasDelivery: action.pincodeHasDelivery,
|
|
@@ -116,6 +120,9 @@ function reducer(state: FormState, action: FormAction): FormState {
|
|
|
116
120
|
case 'SET_LOCALITY':
|
|
117
121
|
if (state.status !== 'ready') return state;
|
|
118
122
|
return { ...state, selectedLocality: action.locality };
|
|
123
|
+
case 'SET_LABEL':
|
|
124
|
+
if (state.status !== 'ready') return state;
|
|
125
|
+
return { ...state, selectedLabel: action.label };
|
|
119
126
|
case 'SUBMIT_START':
|
|
120
127
|
if (state.status !== 'ready') return state;
|
|
121
128
|
return { ...state, submitting: true };
|
|
@@ -202,7 +209,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
202
209
|
e.preventDefault();
|
|
203
210
|
if (state.status !== 'ready') return;
|
|
204
211
|
|
|
205
|
-
const { adminInfo, selectedLocality, fields } = state;
|
|
212
|
+
const { adminInfo, selectedLocality, selectedLabel, fields } = state;
|
|
206
213
|
|
|
207
214
|
const parts: string[] = [
|
|
208
215
|
fields.flatNumber,
|
|
@@ -228,6 +235,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
228
235
|
country: adminInfo.country ?? 'India',
|
|
229
236
|
...fields,
|
|
230
237
|
formattedAddress: parts.join(', '),
|
|
238
|
+
label: selectedLabel,
|
|
231
239
|
};
|
|
232
240
|
|
|
233
241
|
dispatch({ type: 'SUBMIT_START' });
|
|
@@ -387,6 +395,36 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
387
395
|
</div>
|
|
388
396
|
)}
|
|
389
397
|
|
|
398
|
+
{/* Address label */}
|
|
399
|
+
<fieldset className="qr-fieldset">
|
|
400
|
+
<legend className="qr-fieldset__legend">Save as</legend>
|
|
401
|
+
<div className="qr-label-picker" role="radiogroup" aria-label="Address label">
|
|
402
|
+
{(
|
|
403
|
+
[
|
|
404
|
+
{ value: 'home' as const, title: 'Home', icon: '🏠' },
|
|
405
|
+
{ value: 'work' as const, title: 'Work', icon: '💼' },
|
|
406
|
+
{ value: 'other' as const, title: 'Other', icon: '📍' },
|
|
407
|
+
] as const
|
|
408
|
+
).map(({ value, title, icon }) => (
|
|
409
|
+
<button
|
|
410
|
+
key={value}
|
|
411
|
+
type="button"
|
|
412
|
+
role="radio"
|
|
413
|
+
aria-checked={state.selectedLabel === value}
|
|
414
|
+
className={`qr-label-picker__option${
|
|
415
|
+
state.selectedLabel === value ? ' qr-label-picker__option--active' : ''
|
|
416
|
+
}`}
|
|
417
|
+
onClick={() => dispatch({ type: 'SET_LABEL', label: value })}
|
|
418
|
+
>
|
|
419
|
+
<span className="qr-label-picker__icon" aria-hidden="true">
|
|
420
|
+
{icon}
|
|
421
|
+
</span>
|
|
422
|
+
<span>{title}</span>
|
|
423
|
+
</button>
|
|
424
|
+
))}
|
|
425
|
+
</div>
|
|
426
|
+
</fieldset>
|
|
427
|
+
|
|
390
428
|
{/* Manual entry section */}
|
|
391
429
|
<fieldset className="qr-fieldset">
|
|
392
430
|
<legend className="qr-fieldset__legend">
|
|
@@ -180,6 +180,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
180
180
|
const [completedAddress, setCompletedAddress] = useState<CompleteAddress | null>(null);
|
|
181
181
|
const [auth, setAuth] = useState<AuthState | null>(null);
|
|
182
182
|
const [emailPending, setEmailPending] = useState('');
|
|
183
|
+
const [emailCodeReused, setEmailCodeReused] = useState(false);
|
|
183
184
|
const [codAllowed, setCodAllowed] = useState(false);
|
|
184
185
|
const [sessionOffer, setSessionOffer] = useState<SessionOffer | null>(null);
|
|
185
186
|
const [paymentMethod, setPaymentMethod] = useState<string | null>(null);
|
|
@@ -323,8 +324,9 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
323
324
|
|
|
324
325
|
// ── Email step callbacks ──────────────────────────────────────────────────
|
|
325
326
|
|
|
326
|
-
const handleEmailOtpSent = (sentEmail: string) => {
|
|
327
|
+
const handleEmailOtpSent = (sentEmail: string, info?: { reused?: boolean }) => {
|
|
327
328
|
setEmailPending(sentEmail);
|
|
329
|
+
setEmailCodeReused(Boolean(info?.reused));
|
|
328
330
|
emit({ type: 'email_otp_sent', step: 'email-otp', mobile: sentEmail });
|
|
329
331
|
setStep('email-otp');
|
|
330
332
|
};
|
|
@@ -374,6 +376,9 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
374
376
|
]
|
|
375
377
|
.filter(Boolean)
|
|
376
378
|
.join(', '),
|
|
379
|
+
label: (saved.label === 'work' || saved.label === 'other'
|
|
380
|
+
? saved.label
|
|
381
|
+
: 'home') as CompleteAddress['label'],
|
|
377
382
|
};
|
|
378
383
|
void finishAddress(complete);
|
|
379
384
|
};
|
|
@@ -424,7 +429,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
424
429
|
building_name: address.buildingName,
|
|
425
430
|
street_name: address.streetName,
|
|
426
431
|
full_address_json: address as unknown as Record<string, unknown>,
|
|
427
|
-
label: 'home',
|
|
432
|
+
label: address.label ?? 'home',
|
|
428
433
|
is_default: true,
|
|
429
434
|
});
|
|
430
435
|
} catch {
|
|
@@ -591,6 +596,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
591
596
|
{step === 'email-otp' && enableEmailAuth && emailPending && (
|
|
592
597
|
<EmailOtpStep
|
|
593
598
|
email={emailPending}
|
|
599
|
+
codeReused={emailCodeReused}
|
|
594
600
|
onVerified={handleEmailVerified}
|
|
595
601
|
onBack={() => setStep('email')}
|
|
596
602
|
functionBaseUrl={supabaseFunctionBaseUrl}
|
|
@@ -14,6 +14,7 @@ function maskEmail(email: string): string {
|
|
|
14
14
|
|
|
15
15
|
const EmailOtpStep: React.FC<EmailOtpStepProps> = ({
|
|
16
16
|
email,
|
|
17
|
+
codeReused = false,
|
|
17
18
|
onVerified,
|
|
18
19
|
onBack,
|
|
19
20
|
functionBaseUrl,
|
|
@@ -138,7 +139,9 @@ const EmailOtpStep: React.FC<EmailOtpStepProps> = ({
|
|
|
138
139
|
|
|
139
140
|
<h3 className="qr-auth-step__title">Check your email</h3>
|
|
140
141
|
<p className="qr-auth-step__sub">
|
|
141
|
-
|
|
142
|
+
{codeReused
|
|
143
|
+
? <>Use the code we sent earlier — it's still valid for your next checkout.</>
|
|
144
|
+
: <>6-digit code sent to <strong>{maskEmail(email)}</strong></>}
|
|
142
145
|
</p>
|
|
143
146
|
|
|
144
147
|
<div className="qr-otp-boxes" role="group" aria-label="Enter verification code">
|
|
@@ -28,8 +28,8 @@ const EmailStep: React.FC<EmailStepProps> = ({
|
|
|
28
28
|
setLoading(true);
|
|
29
29
|
setError(null);
|
|
30
30
|
try {
|
|
31
|
-
await sendEmailOtp({ functionBaseUrl, merchantId }, email);
|
|
32
|
-
onOtpSent(email.toLowerCase());
|
|
31
|
+
const { reused } = await sendEmailOtp({ functionBaseUrl, merchantId }, email);
|
|
32
|
+
onOtpSent(email.toLowerCase(), { reused });
|
|
33
33
|
} catch (err: unknown) {
|
|
34
34
|
setError(err instanceof Error ? err.message : 'Failed to send code. Please try again.');
|
|
35
35
|
} finally {
|
package/src/core/checkout-api.ts
CHANGED
|
@@ -206,11 +206,16 @@ export async function notifyAddressCompleted(
|
|
|
206
206
|
|
|
207
207
|
// ─── Email OTP auth ───────────────────────────────────────────────────────────
|
|
208
208
|
|
|
209
|
+
/** Result from send email OTP — includes whether an existing code was reused (no new email). */
|
|
210
|
+
export interface SendEmailOtpResult {
|
|
211
|
+
reused: boolean;
|
|
212
|
+
}
|
|
213
|
+
|
|
209
214
|
/** Send a 6-digit OTP to the given email via Resend (Edge: `checkout-email-otp`). */
|
|
210
215
|
export async function sendEmailOtp(
|
|
211
216
|
config: CheckoutApiConfig,
|
|
212
217
|
email: string
|
|
213
|
-
): Promise<
|
|
218
|
+
): Promise<SendEmailOtpResult> {
|
|
214
219
|
const res = await fetch(`${config.functionBaseUrl}/checkout-email-otp`, {
|
|
215
220
|
method: 'POST',
|
|
216
221
|
headers: { 'Content-Type': 'application/json' },
|
|
@@ -220,6 +225,7 @@ export async function sendEmailOtp(
|
|
|
220
225
|
if (!res.ok || !data.success) {
|
|
221
226
|
throw new Error(data.error ?? 'Failed to send OTP. Please try again.');
|
|
222
227
|
}
|
|
228
|
+
return { reused: Boolean(data.reused) };
|
|
223
229
|
}
|
|
224
230
|
|
|
225
231
|
export interface VerifyEmailOtpResult {
|
package/src/core/types.ts
CHANGED
|
@@ -49,6 +49,13 @@ export interface LocationLookupResponse {
|
|
|
49
49
|
message?: string;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
// ─── Address labels (saved-address book) ─────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/** Label for a saved buyer address — stored in checkout-addresses API. */
|
|
55
|
+
export type AddressLabel = 'home' | 'work' | 'other';
|
|
56
|
+
|
|
57
|
+
export const ADDRESS_LABELS: AddressLabel[] = ['home', 'work', 'other'];
|
|
58
|
+
|
|
52
59
|
// ─── Complete Address (output) ───────────────────────────────────────────────
|
|
53
60
|
|
|
54
61
|
export interface CompleteAddress {
|
|
@@ -82,6 +89,8 @@ export interface CompleteAddress {
|
|
|
82
89
|
streetName: string;
|
|
83
90
|
/** Combined formatted address string */
|
|
84
91
|
formattedAddress: string;
|
|
92
|
+
/** Buyer-chosen label for saved-address book (defaults to home) */
|
|
93
|
+
label?: AddressLabel;
|
|
85
94
|
}
|
|
86
95
|
|
|
87
96
|
// ─── Checkout event (client-side analytics hook) ─────────────────────────────
|
|
@@ -339,8 +348,8 @@ export interface OtpStepProps {
|
|
|
339
348
|
}
|
|
340
349
|
|
|
341
350
|
export interface EmailStepProps {
|
|
342
|
-
/** Called with the normalised (lowercased) email once OTP is
|
|
343
|
-
onOtpSent: (email: string) => void;
|
|
351
|
+
/** Called with the normalised (lowercased) email once OTP is ready (sent or reused). */
|
|
352
|
+
onOtpSent: (email: string, info?: { reused?: boolean }) => void;
|
|
344
353
|
onSkip: () => void;
|
|
345
354
|
functionBaseUrl: string;
|
|
346
355
|
merchantId: string;
|
|
@@ -348,6 +357,8 @@ export interface EmailStepProps {
|
|
|
348
357
|
|
|
349
358
|
export interface EmailOtpStepProps {
|
|
350
359
|
email: string;
|
|
360
|
+
/** When true, no new email was sent — shopper should use their existing code. */
|
|
361
|
+
codeReused?: boolean;
|
|
351
362
|
onVerified: (sessionToken: string, buyerId: string, sessionId?: string) => void;
|
|
352
363
|
onBack: () => void;
|
|
353
364
|
functionBaseUrl: string;
|
package/src/index.ts
CHANGED
|
@@ -80,6 +80,7 @@ export type {
|
|
|
80
80
|
CheckoutApiConfig,
|
|
81
81
|
VerifyOtpResult,
|
|
82
82
|
VerifyEmailOtpResult,
|
|
83
|
+
SendEmailOtpResult,
|
|
83
84
|
SavedAddress,
|
|
84
85
|
SavedAddressPayload,
|
|
85
86
|
CodEligibilityResult,
|
|
@@ -119,6 +120,8 @@ export type {
|
|
|
119
120
|
PaymentStepProps,
|
|
120
121
|
PrepaidNudgeProps,
|
|
121
122
|
CompleteAddress,
|
|
123
|
+
AddressLabel,
|
|
124
|
+
ADDRESS_LABELS,
|
|
122
125
|
AdministrativeInfo,
|
|
123
126
|
LocationAlternative,
|
|
124
127
|
LocationLookupResponse,
|
package/src/styles/checkout.css
CHANGED
|
@@ -814,6 +814,47 @@
|
|
|
814
814
|
font-size: 14px;
|
|
815
815
|
}
|
|
816
816
|
|
|
817
|
+
.qr-label-picker {
|
|
818
|
+
display: grid;
|
|
819
|
+
grid-template-columns: repeat(3, 1fr);
|
|
820
|
+
gap: 8px;
|
|
821
|
+
padding: 0 16px 12px;
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
.qr-label-picker__option {
|
|
825
|
+
display: flex;
|
|
826
|
+
flex-direction: column;
|
|
827
|
+
align-items: center;
|
|
828
|
+
justify-content: center;
|
|
829
|
+
gap: 4px;
|
|
830
|
+
min-height: 72px;
|
|
831
|
+
padding: 10px 8px;
|
|
832
|
+
border: 1.5px solid var(--qr-border);
|
|
833
|
+
border-radius: 10px;
|
|
834
|
+
background: var(--qr-bg, #fff);
|
|
835
|
+
color: var(--qr-text-muted);
|
|
836
|
+
font-family: inherit;
|
|
837
|
+
font-size: 13px;
|
|
838
|
+
font-weight: 500;
|
|
839
|
+
cursor: pointer;
|
|
840
|
+
transition: border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
.qr-label-picker__option:hover:not(.qr-label-picker__option--active) {
|
|
844
|
+
border-color: color-mix(in srgb, var(--qr-accent, var(--qr-navy)) 35%, var(--qr-border));
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
.qr-label-picker__option--active {
|
|
848
|
+
border-color: var(--qr-accent, var(--qr-navy));
|
|
849
|
+
background: color-mix(in srgb, var(--qr-accent, var(--qr-navy)) 10%, var(--qr-bg, #fff));
|
|
850
|
+
color: var(--qr-text);
|
|
851
|
+
}
|
|
852
|
+
|
|
853
|
+
.qr-label-picker__icon {
|
|
854
|
+
font-size: 18px;
|
|
855
|
+
line-height: 1;
|
|
856
|
+
}
|
|
857
|
+
|
|
817
858
|
/* ── Auto-filled grid ────────────────────────────────────── */
|
|
818
859
|
.qr-auto-grid {
|
|
819
860
|
display: grid;
|
|
@@ -1375,6 +1416,7 @@
|
|
|
1375
1416
|
|
|
1376
1417
|
/* ── Focus visible (keyboard nav) ────────────────────────── */
|
|
1377
1418
|
.qr-btn:focus-visible,
|
|
1419
|
+
.qr-label-picker__option:focus-visible,
|
|
1378
1420
|
.qr-back-btn:focus-visible,
|
|
1379
1421
|
.qr-locate-btn:focus-visible {
|
|
1380
1422
|
outline: 2px solid var(--qr-primary);
|
|
@@ -721,6 +721,54 @@ export const styles = StyleSheet.create({
|
|
|
721
721
|
fontWeight: '700',
|
|
722
722
|
},
|
|
723
723
|
|
|
724
|
+
// ── Address label picker ────────────────────────────────────────────────────
|
|
725
|
+
labelPicker: {
|
|
726
|
+
flexDirection: 'row',
|
|
727
|
+
gap: 8,
|
|
728
|
+
paddingHorizontal: 16,
|
|
729
|
+
paddingBottom: 12,
|
|
730
|
+
},
|
|
731
|
+
labelPickerOption: {
|
|
732
|
+
flex: 1,
|
|
733
|
+
alignItems: 'center',
|
|
734
|
+
justifyContent: 'center',
|
|
735
|
+
gap: 4,
|
|
736
|
+
minHeight: 72,
|
|
737
|
+
paddingVertical: 10,
|
|
738
|
+
paddingHorizontal: 8,
|
|
739
|
+
borderWidth: 1.5,
|
|
740
|
+
borderColor: COLORS.border,
|
|
741
|
+
borderRadius: 10,
|
|
742
|
+
backgroundColor: COLORS.bg,
|
|
743
|
+
},
|
|
744
|
+
labelPickerOptionDark: {
|
|
745
|
+
borderColor: COLORS.borderDark,
|
|
746
|
+
backgroundColor: COLORS.bgDark,
|
|
747
|
+
},
|
|
748
|
+
labelPickerOptionActive: {
|
|
749
|
+
borderColor: COLORS.primary,
|
|
750
|
+
backgroundColor: COLORS.primaryLight,
|
|
751
|
+
},
|
|
752
|
+
labelPickerOptionActiveDark: {
|
|
753
|
+
borderColor: COLORS.primary,
|
|
754
|
+
backgroundColor: '#0c4a6e33',
|
|
755
|
+
},
|
|
756
|
+
labelPickerIcon: {
|
|
757
|
+
fontSize: 18,
|
|
758
|
+
},
|
|
759
|
+
labelPickerText: {
|
|
760
|
+
fontSize: 13,
|
|
761
|
+
fontWeight: '500',
|
|
762
|
+
color: COLORS.textMuted,
|
|
763
|
+
},
|
|
764
|
+
labelPickerTextDark: {
|
|
765
|
+
color: COLORS.textMutedDark,
|
|
766
|
+
},
|
|
767
|
+
labelPickerTextActive: {
|
|
768
|
+
color: COLORS.text,
|
|
769
|
+
fontWeight: '600',
|
|
770
|
+
},
|
|
771
|
+
|
|
724
772
|
// ── Manual fields ───────────────────────────────────────────────────────────
|
|
725
773
|
fieldsGrid: {
|
|
726
774
|
paddingHorizontal: 16,
|