@quantaroute/checkout 1.3.2 → 1.4.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.
- package/README.md +345 -0
- package/dist/lib/index.d.ts +481 -8
- package/dist/lib/quantaroute-checkout.es.js +238 -1285
- package/dist/lib/quantaroute-checkout.umd.js +211 -64
- package/dist/style.css +1 -1
- package/package.json +2 -1
- package/src/components/AddressForm.native.tsx +56 -1
- package/src/components/AddressForm.tsx +55 -1
- package/src/components/CheckoutWidget.native.tsx +88 -21
- package/src/components/CheckoutWidget.tsx +579 -47
- package/src/components/CodAvailabilityBanner.native.tsx +108 -0
- package/src/components/CodAvailabilityBanner.tsx +104 -0
- package/src/components/EmailOtpStep.tsx +198 -0
- package/src/components/EmailStep.tsx +108 -0
- package/src/components/MapPinSelector.native.tsx +35 -24
- package/src/components/MapPinSelector.tsx +7 -4
- package/src/components/OtpStep.tsx +227 -0
- package/src/components/PaymentStep.tsx +162 -0
- package/src/components/PhoneStep.tsx +124 -0
- package/src/components/PrepaidNudge.tsx +31 -0
- package/src/components/SavedAddressStep.tsx +281 -0
- package/src/core/api.ts +86 -0
- package/src/core/checkout-api.ts +458 -0
- package/src/core/device.ts +47 -0
- package/src/core/types.ts +217 -3
- package/src/core/utm.ts +29 -0
- package/src/index.ts +65 -1
- package/src/styles/checkout.css +614 -0
- package/src/styles/checkout.native.ts +144 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import { View, Text, ActivityIndicator } from 'react-native';
|
|
3
|
+
import { checkCodEligibility, type CheckoutApiConfig } from '../core/checkout-api';
|
|
4
|
+
import { styles, COLORS } from '../styles/checkout.native';
|
|
5
|
+
|
|
6
|
+
function messageForReason(
|
|
7
|
+
reason: 'cod_disabled' | 'pincode_blocked' | 'invalid_pincode' | 'cart_value_exceeded'
|
|
8
|
+
): string {
|
|
9
|
+
switch (reason) {
|
|
10
|
+
case 'cod_disabled':
|
|
11
|
+
return 'Cash on Delivery is not enabled for this store.';
|
|
12
|
+
case 'pincode_blocked':
|
|
13
|
+
return 'COD is not available for this pincode.';
|
|
14
|
+
case 'cart_value_exceeded':
|
|
15
|
+
return 'COD is not available for this order value.';
|
|
16
|
+
case 'invalid_pincode':
|
|
17
|
+
return 'Enter a valid pincode to check COD.';
|
|
18
|
+
default:
|
|
19
|
+
return 'COD is not available.';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface CodAvailabilityBannerProps {
|
|
24
|
+
checkoutConfig: CheckoutApiConfig;
|
|
25
|
+
sessionToken: string;
|
|
26
|
+
pincode: string;
|
|
27
|
+
cartTotalPaise?: number;
|
|
28
|
+
theme?: 'light' | 'dark';
|
|
29
|
+
onCodAllowedChange?: (allowed: boolean) => void;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const CodAvailabilityBanner: React.FC<CodAvailabilityBannerProps> = ({
|
|
33
|
+
checkoutConfig,
|
|
34
|
+
sessionToken,
|
|
35
|
+
pincode,
|
|
36
|
+
cartTotalPaise,
|
|
37
|
+
theme = 'light',
|
|
38
|
+
onCodAllowedChange,
|
|
39
|
+
}) => {
|
|
40
|
+
const [phase, setPhase] = useState<'loading' | 'ready' | 'error'>('loading');
|
|
41
|
+
const [allowed, setAllowed] = useState(false);
|
|
42
|
+
const [detail, setDetail] = useState<string | undefined>();
|
|
43
|
+
const isDark = theme === 'dark';
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
let cancelled = false;
|
|
47
|
+
setPhase('loading');
|
|
48
|
+
(async () => {
|
|
49
|
+
try {
|
|
50
|
+
const r = await checkCodEligibility(
|
|
51
|
+
checkoutConfig,
|
|
52
|
+
sessionToken,
|
|
53
|
+
pincode,
|
|
54
|
+
cartTotalPaise
|
|
55
|
+
);
|
|
56
|
+
if (cancelled) return;
|
|
57
|
+
setAllowed(r.cod_allowed);
|
|
58
|
+
onCodAllowedChange?.(r.cod_allowed);
|
|
59
|
+
setDetail(
|
|
60
|
+
r.cod_allowed
|
|
61
|
+
? undefined
|
|
62
|
+
: r.reason
|
|
63
|
+
? messageForReason(r.reason)
|
|
64
|
+
: undefined
|
|
65
|
+
);
|
|
66
|
+
setPhase('ready');
|
|
67
|
+
} catch {
|
|
68
|
+
if (!cancelled) {
|
|
69
|
+
setPhase('error');
|
|
70
|
+
setAllowed(false);
|
|
71
|
+
setDetail('Could not verify COD. Try again.');
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
})();
|
|
75
|
+
return () => {
|
|
76
|
+
cancelled = true;
|
|
77
|
+
};
|
|
78
|
+
}, [checkoutConfig, sessionToken, pincode, cartTotalPaise]);
|
|
79
|
+
|
|
80
|
+
return (
|
|
81
|
+
<View style={[styles.codBanner, isDark && styles.codBannerDark]}>
|
|
82
|
+
<Text style={[styles.codBannerTitle, isDark && styles.codBannerTitleDark]}>
|
|
83
|
+
Cash on Delivery
|
|
84
|
+
</Text>
|
|
85
|
+
{phase === 'loading' && (
|
|
86
|
+
<View style={styles.codBannerRow}>
|
|
87
|
+
<ActivityIndicator size="small" color={COLORS.primary} />
|
|
88
|
+
<Text style={[styles.codBannerMuted, isDark && styles.codBannerMutedDark]}>
|
|
89
|
+
Checking availability…
|
|
90
|
+
</Text>
|
|
91
|
+
</View>
|
|
92
|
+
)}
|
|
93
|
+
{phase === 'error' && (
|
|
94
|
+
<Text style={[styles.codBannerWarn, isDark && styles.codBannerWarnDark]}>{detail}</Text>
|
|
95
|
+
)}
|
|
96
|
+
{phase === 'ready' && allowed && (
|
|
97
|
+
<Text style={[styles.codBannerOk, isDark && styles.codBannerOkDark]}>
|
|
98
|
+
Available for this address.
|
|
99
|
+
</Text>
|
|
100
|
+
)}
|
|
101
|
+
{phase === 'ready' && !allowed && detail ? (
|
|
102
|
+
<Text style={[styles.codBannerMuted, isDark && styles.codBannerMutedDark]}>{detail}</Text>
|
|
103
|
+
) : null}
|
|
104
|
+
</View>
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export default CodAvailabilityBanner;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import React, { useEffect, useState } from 'react';
|
|
2
|
+
import { checkCodEligibility, type CheckoutApiConfig } from '../core/checkout-api';
|
|
3
|
+
|
|
4
|
+
function messageForReason(
|
|
5
|
+
reason: 'cod_disabled' | 'pincode_blocked' | 'invalid_pincode' | 'cart_value_exceeded'
|
|
6
|
+
): string {
|
|
7
|
+
switch (reason) {
|
|
8
|
+
case 'cod_disabled':
|
|
9
|
+
return 'Cash on Delivery is not enabled for this store.';
|
|
10
|
+
case 'pincode_blocked':
|
|
11
|
+
return 'COD is not available for this pincode.';
|
|
12
|
+
case 'cart_value_exceeded':
|
|
13
|
+
return 'COD is not available for this order value.';
|
|
14
|
+
case 'invalid_pincode':
|
|
15
|
+
return 'Enter a valid pincode to check COD.';
|
|
16
|
+
default:
|
|
17
|
+
return 'COD is not available.';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface CodAvailabilityBannerProps {
|
|
22
|
+
checkoutConfig: CheckoutApiConfig;
|
|
23
|
+
sessionToken: string;
|
|
24
|
+
pincode: string;
|
|
25
|
+
cartTotalPaise?: number;
|
|
26
|
+
theme?: 'light' | 'dark';
|
|
27
|
+
/** Called when COD eligibility is resolved (for prepaid nudge gating). */
|
|
28
|
+
onCodAllowedChange?: (allowed: boolean) => void;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Shows COD availability after checkout using `cod_eligibility` (merchant `enable_cod` + D1/D2).
|
|
33
|
+
*/
|
|
34
|
+
const CodAvailabilityBanner: React.FC<CodAvailabilityBannerProps> = ({
|
|
35
|
+
checkoutConfig,
|
|
36
|
+
sessionToken,
|
|
37
|
+
pincode,
|
|
38
|
+
cartTotalPaise,
|
|
39
|
+
theme = 'light',
|
|
40
|
+
onCodAllowedChange,
|
|
41
|
+
}) => {
|
|
42
|
+
const [phase, setPhase] = useState<'loading' | 'ready' | 'error'>('loading');
|
|
43
|
+
const [allowed, setAllowed] = useState(false);
|
|
44
|
+
const [detail, setDetail] = useState<string | undefined>();
|
|
45
|
+
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
let cancelled = false;
|
|
48
|
+
setPhase('loading');
|
|
49
|
+
(async () => {
|
|
50
|
+
try {
|
|
51
|
+
const r = await checkCodEligibility(
|
|
52
|
+
checkoutConfig,
|
|
53
|
+
sessionToken,
|
|
54
|
+
pincode,
|
|
55
|
+
cartTotalPaise
|
|
56
|
+
);
|
|
57
|
+
if (cancelled) return;
|
|
58
|
+
setAllowed(r.cod_allowed);
|
|
59
|
+
onCodAllowedChange?.(r.cod_allowed);
|
|
60
|
+
setDetail(
|
|
61
|
+
r.cod_allowed
|
|
62
|
+
? undefined
|
|
63
|
+
: r.reason
|
|
64
|
+
? messageForReason(r.reason)
|
|
65
|
+
: undefined
|
|
66
|
+
);
|
|
67
|
+
setPhase('ready');
|
|
68
|
+
} catch {
|
|
69
|
+
if (!cancelled) {
|
|
70
|
+
setPhase('error');
|
|
71
|
+
setAllowed(false);
|
|
72
|
+
setDetail('Could not verify COD. Try again.');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
})();
|
|
76
|
+
return () => {
|
|
77
|
+
cancelled = true;
|
|
78
|
+
};
|
|
79
|
+
}, [checkoutConfig, sessionToken, pincode, cartTotalPaise]);
|
|
80
|
+
|
|
81
|
+
const isDark = theme === 'dark';
|
|
82
|
+
|
|
83
|
+
return (
|
|
84
|
+
<div
|
|
85
|
+
className={`qr-cod-banner${isDark ? ' qr-cod-banner--dark' : ''}`}
|
|
86
|
+
role="status"
|
|
87
|
+
aria-live="polite"
|
|
88
|
+
>
|
|
89
|
+
<div className="qr-cod-banner__title">Cash on Delivery</div>
|
|
90
|
+
{phase === 'loading' && (
|
|
91
|
+
<p className="qr-cod-banner__text qr-cod-banner__text--muted">Checking availability…</p>
|
|
92
|
+
)}
|
|
93
|
+
{phase === 'error' && <p className="qr-cod-banner__text qr-cod-banner__text--warn">{detail}</p>}
|
|
94
|
+
{phase === 'ready' && allowed && (
|
|
95
|
+
<p className="qr-cod-banner__text qr-cod-banner__text--ok">Available for this address.</p>
|
|
96
|
+
)}
|
|
97
|
+
{phase === 'ready' && !allowed && detail && (
|
|
98
|
+
<p className="qr-cod-banner__text qr-cod-banner__text--muted">{detail}</p>
|
|
99
|
+
)}
|
|
100
|
+
</div>
|
|
101
|
+
);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export default CodAvailabilityBanner;
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
import React, { useState, useRef, useCallback, useEffect } from 'react';
|
|
2
|
+
import { verifyEmailOtp, sendEmailOtp } from '../core/checkout-api';
|
|
3
|
+
import type { EmailOtpStepProps } from '../core/types';
|
|
4
|
+
|
|
5
|
+
const OTP_LENGTH = 6;
|
|
6
|
+
const RESEND_COOLDOWN = 60; // seconds — email resend is slower than SMS
|
|
7
|
+
|
|
8
|
+
/** Mask email for display: y***@example.com */
|
|
9
|
+
function maskEmail(email: string): string {
|
|
10
|
+
const [local, domain] = email.split('@');
|
|
11
|
+
if (!domain || local.length <= 1) return email;
|
|
12
|
+
return `${local[0]}***@${domain}`;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const EmailOtpStep: React.FC<EmailOtpStepProps> = ({
|
|
16
|
+
email,
|
|
17
|
+
onVerified,
|
|
18
|
+
onBack,
|
|
19
|
+
functionBaseUrl,
|
|
20
|
+
merchantId,
|
|
21
|
+
sessionContext,
|
|
22
|
+
}) => {
|
|
23
|
+
const [digits, setDigits] = useState<string[]>(Array(OTP_LENGTH).fill(''));
|
|
24
|
+
const [loading, setLoading] = useState(false);
|
|
25
|
+
const [error, setError] = useState<string | null>(null);
|
|
26
|
+
const [resendCountdown, setResendCountdown] = useState(RESEND_COOLDOWN);
|
|
27
|
+
const [resendLoading, setResendLoading] = useState(false);
|
|
28
|
+
const inputRefs = useRef<(HTMLInputElement | null)[]>([]);
|
|
29
|
+
const timerRef = useRef<ReturnType<typeof setInterval> | null>(null);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
timerRef.current = setInterval(() => {
|
|
33
|
+
setResendCountdown((c) => {
|
|
34
|
+
if (c <= 1) { clearInterval(timerRef.current!); return 0; }
|
|
35
|
+
return c - 1;
|
|
36
|
+
});
|
|
37
|
+
}, 1000);
|
|
38
|
+
return () => { if (timerRef.current) clearInterval(timerRef.current); };
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
const focusBox = (index: number) => inputRefs.current[index]?.focus();
|
|
42
|
+
|
|
43
|
+
const handleDigitChange = useCallback((index: number, value: string) => {
|
|
44
|
+
const char = value.replace(/\D/g, '').slice(-1);
|
|
45
|
+
setError(null);
|
|
46
|
+
setDigits((prev) => { const next = [...prev]; next[index] = char; return next; });
|
|
47
|
+
if (char && index < OTP_LENGTH - 1) focusBox(index + 1);
|
|
48
|
+
}, []);
|
|
49
|
+
|
|
50
|
+
const handleKeyDown = useCallback((index: number, e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
51
|
+
if (e.key === 'Backspace') {
|
|
52
|
+
if (digits[index]) {
|
|
53
|
+
setDigits((prev) => { const n = [...prev]; n[index] = ''; return n; });
|
|
54
|
+
} else if (index > 0) {
|
|
55
|
+
setDigits((prev) => { const n = [...prev]; n[index - 1] = ''; return n; });
|
|
56
|
+
focusBox(index - 1);
|
|
57
|
+
}
|
|
58
|
+
} else if (e.key === 'ArrowLeft' && index > 0) {
|
|
59
|
+
focusBox(index - 1);
|
|
60
|
+
} else if (e.key === 'ArrowRight' && index < OTP_LENGTH - 1) {
|
|
61
|
+
focusBox(index + 1);
|
|
62
|
+
}
|
|
63
|
+
}, [digits]);
|
|
64
|
+
|
|
65
|
+
const handlePaste = useCallback((e: React.ClipboardEvent<HTMLInputElement>) => {
|
|
66
|
+
e.preventDefault();
|
|
67
|
+
const pasted = e.clipboardData.getData('text').replace(/\D/g, '').slice(0, OTP_LENGTH);
|
|
68
|
+
if (!pasted) return;
|
|
69
|
+
const next = Array(OTP_LENGTH).fill('');
|
|
70
|
+
pasted.split('').forEach((ch, i) => { next[i] = ch; });
|
|
71
|
+
setDigits(next);
|
|
72
|
+
setError(null);
|
|
73
|
+
focusBox(Math.min(pasted.length, OTP_LENGTH - 1));
|
|
74
|
+
}, []);
|
|
75
|
+
|
|
76
|
+
const code = digits.join('');
|
|
77
|
+
const isComplete = code.length === OTP_LENGTH && !digits.includes('');
|
|
78
|
+
|
|
79
|
+
const handleVerify = useCallback(async () => {
|
|
80
|
+
if (!isComplete) return;
|
|
81
|
+
setLoading(true);
|
|
82
|
+
setError(null);
|
|
83
|
+
try {
|
|
84
|
+
const result = await verifyEmailOtp({ functionBaseUrl, merchantId }, email, code, sessionContext);
|
|
85
|
+
onVerified(result.session_token, result.buyer_id, result.session_id);
|
|
86
|
+
} catch (err: unknown) {
|
|
87
|
+
setError(err instanceof Error ? err.message : 'Verification failed. Please try again.');
|
|
88
|
+
setDigits(Array(OTP_LENGTH).fill(''));
|
|
89
|
+
focusBox(0);
|
|
90
|
+
} finally {
|
|
91
|
+
setLoading(false);
|
|
92
|
+
}
|
|
93
|
+
}, [isComplete, code, email, functionBaseUrl, merchantId, onVerified, sessionContext]);
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (isComplete && !loading) handleVerify();
|
|
97
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
98
|
+
}, [isComplete]);
|
|
99
|
+
|
|
100
|
+
const handleResend = useCallback(async () => {
|
|
101
|
+
if (resendCountdown > 0 || resendLoading) return;
|
|
102
|
+
setResendLoading(true);
|
|
103
|
+
setError(null);
|
|
104
|
+
setDigits(Array(OTP_LENGTH).fill(''));
|
|
105
|
+
try {
|
|
106
|
+
await sendEmailOtp({ functionBaseUrl, merchantId }, email);
|
|
107
|
+
setResendCountdown(RESEND_COOLDOWN);
|
|
108
|
+
timerRef.current = setInterval(() => {
|
|
109
|
+
setResendCountdown((c) => {
|
|
110
|
+
if (c <= 1) { clearInterval(timerRef.current!); return 0; }
|
|
111
|
+
return c - 1;
|
|
112
|
+
});
|
|
113
|
+
}, 1000);
|
|
114
|
+
focusBox(0);
|
|
115
|
+
} catch (err: unknown) {
|
|
116
|
+
setError(err instanceof Error ? err.message : 'Failed to resend code.');
|
|
117
|
+
} finally {
|
|
118
|
+
setResendLoading(false);
|
|
119
|
+
}
|
|
120
|
+
}, [resendCountdown, resendLoading, functionBaseUrl, merchantId, email]);
|
|
121
|
+
|
|
122
|
+
return (
|
|
123
|
+
<div className="qr-auth-step">
|
|
124
|
+
<button type="button" className="qr-back-btn qr-back-btn--auth" onClick={onBack} disabled={loading}>
|
|
125
|
+
<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
|
|
126
|
+
<path d="M19 12H5M12 5l-7 7 7 7" />
|
|
127
|
+
</svg>
|
|
128
|
+
</button>
|
|
129
|
+
|
|
130
|
+
{/* Icon */}
|
|
131
|
+
<div className="qr-auth-step__icon" aria-hidden="true">
|
|
132
|
+
<svg viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
133
|
+
<circle cx="24" cy="24" r="24" fill="var(--qr-primary-light)" />
|
|
134
|
+
<rect x="11" y="15" width="26" height="18" rx="3" stroke="var(--qr-primary)" strokeWidth="1.8" fill="none" />
|
|
135
|
+
<path d="M11 18l13 9 13-9" stroke="var(--qr-primary)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
|
|
136
|
+
</svg>
|
|
137
|
+
</div>
|
|
138
|
+
|
|
139
|
+
<h3 className="qr-auth-step__title">Check your email</h3>
|
|
140
|
+
<p className="qr-auth-step__sub">
|
|
141
|
+
6-digit code sent to <strong>{maskEmail(email)}</strong>
|
|
142
|
+
</p>
|
|
143
|
+
|
|
144
|
+
<div className="qr-otp-boxes" role="group" aria-label="Enter verification code">
|
|
145
|
+
{digits.map((d, i) => (
|
|
146
|
+
<input
|
|
147
|
+
key={i}
|
|
148
|
+
ref={(el) => { inputRefs.current[i] = el; }}
|
|
149
|
+
type="text"
|
|
150
|
+
inputMode="numeric"
|
|
151
|
+
maxLength={1}
|
|
152
|
+
className={`qr-otp-box${d ? ' qr-otp-box--filled' : ''}${error ? ' qr-otp-box--error' : ''}`}
|
|
153
|
+
value={d}
|
|
154
|
+
onChange={(e) => handleDigitChange(i, e.target.value)}
|
|
155
|
+
onKeyDown={(e) => handleKeyDown(i, e)}
|
|
156
|
+
onPaste={handlePaste}
|
|
157
|
+
onFocus={(e) => e.target.select()}
|
|
158
|
+
aria-label={`Code digit ${i + 1}`}
|
|
159
|
+
disabled={loading}
|
|
160
|
+
autoComplete={i === 0 ? 'one-time-code' : 'off'}
|
|
161
|
+
/>
|
|
162
|
+
))}
|
|
163
|
+
</div>
|
|
164
|
+
|
|
165
|
+
{error && (
|
|
166
|
+
<p className="qr-auth-step__error" role="alert">{error}</p>
|
|
167
|
+
)}
|
|
168
|
+
|
|
169
|
+
<button
|
|
170
|
+
type="button"
|
|
171
|
+
className="qr-btn qr-btn--primary qr-btn--full"
|
|
172
|
+
onClick={handleVerify}
|
|
173
|
+
disabled={!isComplete || loading}
|
|
174
|
+
>
|
|
175
|
+
{loading ? (
|
|
176
|
+
<span className="qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
177
|
+
) : 'Verify Code'}
|
|
178
|
+
</button>
|
|
179
|
+
|
|
180
|
+
<p className="qr-auth-step__resend">
|
|
181
|
+
{resendCountdown > 0 ? (
|
|
182
|
+
<span>Resend code in {resendCountdown}s</span>
|
|
183
|
+
) : (
|
|
184
|
+
<button
|
|
185
|
+
type="button"
|
|
186
|
+
className="qr-link-btn"
|
|
187
|
+
onClick={handleResend}
|
|
188
|
+
disabled={resendLoading}
|
|
189
|
+
>
|
|
190
|
+
{resendLoading ? 'Sending…' : 'Resend Code'}
|
|
191
|
+
</button>
|
|
192
|
+
)}
|
|
193
|
+
</p>
|
|
194
|
+
</div>
|
|
195
|
+
);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export default EmailOtpStep;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import React, { useState, useRef, useCallback } from 'react';
|
|
2
|
+
import { sendEmailOtp } from '../core/checkout-api';
|
|
3
|
+
import type { EmailStepProps } from '../core/types';
|
|
4
|
+
|
|
5
|
+
const EmailStep: React.FC<EmailStepProps> = ({
|
|
6
|
+
onOtpSent,
|
|
7
|
+
onSkip,
|
|
8
|
+
functionBaseUrl,
|
|
9
|
+
merchantId,
|
|
10
|
+
}) => {
|
|
11
|
+
const [email, setEmail] = useState('');
|
|
12
|
+
const [loading, setLoading] = useState(false);
|
|
13
|
+
const [error, setError] = useState<string | null>(null);
|
|
14
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
15
|
+
|
|
16
|
+
const handleChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
|
|
17
|
+
setEmail(e.target.value.trim());
|
|
18
|
+
setError(null);
|
|
19
|
+
}, []);
|
|
20
|
+
|
|
21
|
+
const handleSubmit = useCallback(async (e: React.FormEvent) => {
|
|
22
|
+
e.preventDefault();
|
|
23
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
|
|
24
|
+
setError('Enter a valid email address.');
|
|
25
|
+
inputRef.current?.focus();
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
setLoading(true);
|
|
29
|
+
setError(null);
|
|
30
|
+
try {
|
|
31
|
+
await sendEmailOtp({ functionBaseUrl, merchantId }, email);
|
|
32
|
+
onOtpSent(email.toLowerCase());
|
|
33
|
+
} catch (err: unknown) {
|
|
34
|
+
setError(err instanceof Error ? err.message : 'Failed to send code. Please try again.');
|
|
35
|
+
} finally {
|
|
36
|
+
setLoading(false);
|
|
37
|
+
}
|
|
38
|
+
}, [email, functionBaseUrl, merchantId, onOtpSent]);
|
|
39
|
+
|
|
40
|
+
return (
|
|
41
|
+
<div className="qr-auth-step">
|
|
42
|
+
{/* Icon */}
|
|
43
|
+
<div className="qr-auth-step__icon" aria-hidden="true">
|
|
44
|
+
<svg viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
45
|
+
<circle cx="24" cy="24" r="24" fill="var(--qr-primary-light)" />
|
|
46
|
+
<rect x="11" y="15" width="26" height="18" rx="3" stroke="var(--qr-primary)" strokeWidth="1.8" fill="none" />
|
|
47
|
+
<path d="M11 18l13 9 13-9" stroke="var(--qr-primary)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round" />
|
|
48
|
+
</svg>
|
|
49
|
+
</div>
|
|
50
|
+
|
|
51
|
+
<h3 className="qr-auth-step__title">Faster checkout</h3>
|
|
52
|
+
<p className="qr-auth-step__sub">Enter your email to load saved addresses</p>
|
|
53
|
+
|
|
54
|
+
<form onSubmit={handleSubmit} noValidate>
|
|
55
|
+
<div className={`qr-email-field${error ? ' qr-email-field--error' : ''}`}>
|
|
56
|
+
<input
|
|
57
|
+
ref={inputRef}
|
|
58
|
+
type="email"
|
|
59
|
+
inputMode="email"
|
|
60
|
+
className="qr-email-field__input"
|
|
61
|
+
placeholder="you@example.com"
|
|
62
|
+
value={email}
|
|
63
|
+
onChange={handleChange}
|
|
64
|
+
autoComplete="email"
|
|
65
|
+
aria-label="Email address"
|
|
66
|
+
aria-describedby={error ? 'qr-email-error' : undefined}
|
|
67
|
+
aria-invalid={!!error}
|
|
68
|
+
disabled={loading}
|
|
69
|
+
/>
|
|
70
|
+
</div>
|
|
71
|
+
|
|
72
|
+
{error && (
|
|
73
|
+
<p id="qr-email-error" className="qr-auth-step__error" role="alert">
|
|
74
|
+
{error}
|
|
75
|
+
</p>
|
|
76
|
+
)}
|
|
77
|
+
|
|
78
|
+
<button
|
|
79
|
+
type="submit"
|
|
80
|
+
className="qr-btn qr-btn--primary qr-btn--full"
|
|
81
|
+
disabled={loading || email.length < 5}
|
|
82
|
+
>
|
|
83
|
+
{loading ? (
|
|
84
|
+
<span className="qr-spinner qr-spinner--sm" aria-hidden="true" />
|
|
85
|
+
) : (
|
|
86
|
+
<>
|
|
87
|
+
Send Code
|
|
88
|
+
<svg aria-hidden="true" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
|
|
89
|
+
<path d="M5 12h14M12 5l7 7-7 7" />
|
|
90
|
+
</svg>
|
|
91
|
+
</>
|
|
92
|
+
)}
|
|
93
|
+
</button>
|
|
94
|
+
</form>
|
|
95
|
+
|
|
96
|
+
<button
|
|
97
|
+
type="button"
|
|
98
|
+
className="qr-auth-step__skip"
|
|
99
|
+
onClick={onSkip}
|
|
100
|
+
disabled={loading}
|
|
101
|
+
>
|
|
102
|
+
Skip, enter address manually
|
|
103
|
+
</button>
|
|
104
|
+
</div>
|
|
105
|
+
);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
export default EmailStep;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, { useRef, useState, useCallback } from 'react';
|
|
1
|
+
import React, { useRef, useState, useCallback, useMemo } from 'react';
|
|
2
2
|
import {
|
|
3
3
|
View,
|
|
4
4
|
Text,
|
|
@@ -31,19 +31,24 @@ const STREET_ZOOM = 16;
|
|
|
31
31
|
const CARTO_STYLE_URL =
|
|
32
32
|
'https://basemaps.cartocdn.com/gl/positron-gl-style/style.json';
|
|
33
33
|
|
|
34
|
+
const DEFAULT_BRAND = '#0ea5e9';
|
|
35
|
+
|
|
34
36
|
/**
|
|
35
|
-
* Custom pin SVG as a data URI —
|
|
36
|
-
* Teardrop shape with white inner circle and blue dot.
|
|
37
|
+
* Custom pin SVG as a data URI — teardrop with white inner circle and brand-colour dot.
|
|
37
38
|
*/
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
'
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
function makePinIconDataUri(hex: string): string {
|
|
40
|
+
const c = hex.startsWith('#') ? hex : `#${hex}`;
|
|
41
|
+
return (
|
|
42
|
+
'data:image/svg+xml;utf8,' +
|
|
43
|
+
encodeURIComponent(
|
|
44
|
+
'<svg width="40" height="52" viewBox="0 0 40 52" fill="none" xmlns="http://www.w3.org/2000/svg">' +
|
|
45
|
+
`<path d="M20 0C9.402 0 0 9.402 0 20C0 34 20 52 20 52S40 34 40 20C40 9.402 30.598 0 20 0Z" fill="${c}"/>` +
|
|
46
|
+
'<circle cx="20" cy="20" r="9" fill="white"/>' +
|
|
47
|
+
`<circle cx="20" cy="20" r="5" fill="${c}"/>` +
|
|
48
|
+
'</svg>'
|
|
49
|
+
)
|
|
46
50
|
);
|
|
51
|
+
}
|
|
47
52
|
|
|
48
53
|
// ─── Helper ───────────────────────────────────────────────────────────────────
|
|
49
54
|
|
|
@@ -72,6 +77,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
72
77
|
enableSearch = true,
|
|
73
78
|
apiKey,
|
|
74
79
|
apiBaseUrl,
|
|
80
|
+
brandColor = DEFAULT_BRAND,
|
|
75
81
|
}) => {
|
|
76
82
|
const mapRef = useRef<OSMViewRef>(null);
|
|
77
83
|
|
|
@@ -94,6 +100,8 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
94
100
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
95
101
|
|
|
96
102
|
const isDark = theme === 'dark';
|
|
103
|
+
const accentColor = brandColor ?? DEFAULT_BRAND;
|
|
104
|
+
const pinIconUri = useMemo(() => makePinIconDataUri(accentColor), [accentColor]);
|
|
97
105
|
|
|
98
106
|
// Resolve numeric height: web passes '380px', native should pass 380 or a number
|
|
99
107
|
const mapHeightNum =
|
|
@@ -173,18 +181,21 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
173
181
|
}, [flyToCoord]);
|
|
174
182
|
|
|
175
183
|
// ── Marker config (draggable checkout pin) ──────────────────────────────────
|
|
176
|
-
const pinMarkers: MarkerConfig[] =
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
184
|
+
const pinMarkers: MarkerConfig[] = useMemo(
|
|
185
|
+
() => [
|
|
186
|
+
{
|
|
187
|
+
id: 'checkout-pin',
|
|
188
|
+
coordinate: coord,
|
|
189
|
+
draggable: true,
|
|
190
|
+
icon: {
|
|
191
|
+
uri: pinIconUri,
|
|
192
|
+
size: 40,
|
|
193
|
+
anchor: { x: 0.5, y: 1.0 }, // tip of the teardrop
|
|
194
|
+
},
|
|
185
195
|
},
|
|
186
|
-
|
|
187
|
-
|
|
196
|
+
],
|
|
197
|
+
[coord, pinIconUri]
|
|
198
|
+
);
|
|
188
199
|
|
|
189
200
|
// ── Locate-me: delegate to expo-osm-sdk's LocationButton via getCurrentLocation ──
|
|
190
201
|
const getCurrentLocation = useCallback(async () => {
|
|
@@ -245,7 +256,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
245
256
|
{searchLoading && (
|
|
246
257
|
<ActivityIndicator
|
|
247
258
|
size="small"
|
|
248
|
-
color={
|
|
259
|
+
color={accentColor}
|
|
249
260
|
style={styles.searchSpinner}
|
|
250
261
|
/>
|
|
251
262
|
)}
|
|
@@ -338,7 +349,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
338
349
|
{/* Locate-me button — expo-osm-sdk's built-in LocationButton */}
|
|
339
350
|
<LocationButton
|
|
340
351
|
style={styles.locateBtn}
|
|
341
|
-
color={
|
|
352
|
+
color={accentColor}
|
|
342
353
|
size={44}
|
|
343
354
|
getCurrentLocation={getCurrentLocation}
|
|
344
355
|
onLocationFound={({ latitude, longitude }) => {
|
|
@@ -41,7 +41,9 @@ function computeDigiPinSafe(la: number, lo: number): string | null {
|
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
const DEFAULT_BRAND = '#0ea5e9';
|
|
45
|
+
|
|
46
|
+
function createPinElement(brandColor: string = DEFAULT_BRAND): HTMLDivElement {
|
|
45
47
|
const el = document.createElement('div');
|
|
46
48
|
el.className = 'qr-pin';
|
|
47
49
|
el.setAttribute('aria-label', 'Drag to adjust your location');
|
|
@@ -79,12 +81,12 @@ function createPinElement(): HTMLDivElement {
|
|
|
79
81
|
<!-- Teardrop body — tip is at (20, 52) = bottom-center of the SVG -->
|
|
80
82
|
<path
|
|
81
83
|
d="M20 0C9.402 0 0 9.402 0 20C0 34 20 52 20 52S40 34 40 20C40 9.402 30.598 0 20 0Z"
|
|
82
|
-
fill="
|
|
84
|
+
fill="${brandColor}"
|
|
83
85
|
filter="url(#qr-pin-shadow)"
|
|
84
86
|
/>
|
|
85
87
|
<!-- Dot inside the pin -->
|
|
86
88
|
<circle cx="20" cy="20" r="9" fill="white"/>
|
|
87
|
-
<circle cx="20" cy="20" r="5" fill="
|
|
89
|
+
<circle cx="20" cy="20" r="5" fill="${brandColor}"/>
|
|
88
90
|
</svg>
|
|
89
91
|
|
|
90
92
|
<!--
|
|
@@ -109,6 +111,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
109
111
|
enableSearch = true,
|
|
110
112
|
apiKey,
|
|
111
113
|
apiBaseUrl,
|
|
114
|
+
brandColor = DEFAULT_BRAND,
|
|
112
115
|
}) => {
|
|
113
116
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
114
117
|
const mapRef = useRef<maplibregl.Map | null>(null);
|
|
@@ -306,7 +309,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
306
309
|
// anchor='bottom' → MapLibre aligns element's bottom-center with the coord.
|
|
307
310
|
// Our SVG tip is at (20, 52) = exactly the bottom-center of the 40×52 element.
|
|
308
311
|
// No offset needed — any offset shifts the tip away from the coordinate.
|
|
309
|
-
const pinEl = createPinElement();
|
|
312
|
+
const pinEl = createPinElement(brandColor);
|
|
310
313
|
const marker = new maplibregl.Marker({
|
|
311
314
|
element: pinEl,
|
|
312
315
|
draggable: true,
|