@quantaroute/checkout 1.7.3 → 1.8.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/CHANGELOG.md +24 -0
- package/README.md +60 -1
- package/dist/embed/quantaroute-checkout.css +1 -0
- package/dist/embed/quantaroute-checkout.js +28 -0
- package/dist/lib/index.d.ts +20 -1
- package/dist/lib/quantaroute-checkout.es.js +146 -131
- package/dist/lib/quantaroute-checkout.umd.js +148 -133
- package/package.json +12 -6
- package/src/components/AddressForm.native.tsx +3 -3
- package/src/components/AddressForm.tsx +3 -3
- package/src/components/CheckoutWidget.tsx +48 -14
- package/src/components/MapPinSelector.native.tsx +14 -3
- package/src/components/MapPinSelector.tsx +23 -2
- package/src/components/PaymentStep.tsx +36 -8
- package/src/core/map-runtime.ts +22 -0
- package/src/core/types.ts +13 -1
- package/src/embed/handoff.test.ts +86 -0
- package/src/embed/handoff.ts +132 -0
- package/src/embed/index.tsx +380 -0
- package/src/index.ts +2 -0
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quantaroute/checkout",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Embeddable DigiPin-powered smart address checkout widget — React (web), React Native & Expo (iOS/Android)",
|
|
3
|
+
"version": "1.8.0",
|
|
4
|
+
"description": "Embeddable DigiPin-powered smart address checkout widget — React (web), a script-tag embed for any website, React Native & Expo (iOS/Android)",
|
|
5
5
|
"main": "./dist/lib/quantaroute-checkout.umd.js",
|
|
6
6
|
"module": "./dist/lib/quantaroute-checkout.es.js",
|
|
7
7
|
"types": "./dist/lib/index.d.ts",
|
|
@@ -17,11 +17,13 @@
|
|
|
17
17
|
"./plugin": {
|
|
18
18
|
"default": "./expo-plugin.js"
|
|
19
19
|
},
|
|
20
|
-
"./style.css": "./dist/style.css"
|
|
20
|
+
"./style.css": "./dist/style.css",
|
|
21
|
+
"./embed": "./dist/embed/quantaroute-checkout.js"
|
|
21
22
|
},
|
|
22
23
|
"files": [
|
|
23
24
|
"src",
|
|
24
25
|
"dist/lib",
|
|
26
|
+
"dist/embed",
|
|
25
27
|
"dist/style.css",
|
|
26
28
|
"expo-plugin.js",
|
|
27
29
|
"babel.config.js",
|
|
@@ -31,7 +33,7 @@
|
|
|
31
33
|
],
|
|
32
34
|
"scripts": {
|
|
33
35
|
"dev": "vite",
|
|
34
|
-
"build": "npm run build:lib && npm run build:demo",
|
|
36
|
+
"build": "npm run build:lib && npm run build:embed && npm run build:demo",
|
|
35
37
|
"build:lib": "vite build --config vite.lib.config.ts",
|
|
36
38
|
"build:demo": "vite build",
|
|
37
39
|
"preview": "vite preview --outDir dist/demo",
|
|
@@ -39,7 +41,9 @@
|
|
|
39
41
|
"type-check:native": "tsc --project tsconfig.native.json --noEmit",
|
|
40
42
|
"verify:digipin": "npx tsx scripts/verify-digipin-formats.mjs",
|
|
41
43
|
"verify:locality": "npx tsx scripts/verify-locality-names.mjs",
|
|
42
|
-
"prepublishOnly": "npm run type-check && npm run type-check:native && npm run build:lib"
|
|
44
|
+
"prepublishOnly": "npm run type-check && npm run type-check:native && npm run test && npm run build:lib && npm run build:embed",
|
|
45
|
+
"build:embed": "vite build --config vite.embed.config.ts",
|
|
46
|
+
"test": "node --test src/embed/*.test.ts"
|
|
43
47
|
},
|
|
44
48
|
"keywords": [
|
|
45
49
|
"digipin",
|
|
@@ -136,5 +140,7 @@
|
|
|
136
140
|
"typescript": "^5.6.3",
|
|
137
141
|
"vite": "^6.4.3",
|
|
138
142
|
"vite-plugin-dts": "^4.3.0"
|
|
139
|
-
}
|
|
143
|
+
},
|
|
144
|
+
"jsdelivr": "./dist/embed/quantaroute-checkout.js",
|
|
145
|
+
"unpkg": "./dist/embed/quantaroute-checkout.js"
|
|
140
146
|
}
|
|
@@ -268,8 +268,8 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
268
268
|
}, [fetchAdminInfo]);
|
|
269
269
|
|
|
270
270
|
// ── Submit ────────────────────────────────────────────────────────────────
|
|
271
|
-
const handleSubmit = useCallback(() => {
|
|
272
|
-
if (state.status !== 'ready') return;
|
|
271
|
+
const handleSubmit = useCallback(async () => {
|
|
272
|
+
if (state.status !== 'ready' || state.submitting) return;
|
|
273
273
|
if (!state.fields.flatNumber.trim()) return;
|
|
274
274
|
if (state.selectedLabel === 'other' && !state.labelText.trim()) return;
|
|
275
275
|
const serviceabilityBlocked =
|
|
@@ -310,7 +310,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
310
310
|
|
|
311
311
|
dispatch({ type: 'SUBMIT_START' });
|
|
312
312
|
try {
|
|
313
|
-
onAddressComplete(complete);
|
|
313
|
+
await onAddressComplete(complete);
|
|
314
314
|
} finally {
|
|
315
315
|
dispatch({ type: 'SUBMIT_END' });
|
|
316
316
|
}
|
|
@@ -286,9 +286,9 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
286
286
|
|
|
287
287
|
// ── Submit ────────────────────────────────────────────────────────────────
|
|
288
288
|
const handleSubmit = useCallback(
|
|
289
|
-
(e: React.FormEvent<HTMLFormElement>) => {
|
|
289
|
+
async (e: React.FormEvent<HTMLFormElement>) => {
|
|
290
290
|
e.preventDefault();
|
|
291
|
-
if (state.status !== 'ready') return;
|
|
291
|
+
if (state.status !== 'ready' || state.submitting) return;
|
|
292
292
|
|
|
293
293
|
const { adminInfo, selectedLocality, selectedLabel, labelText, fields } = state;
|
|
294
294
|
|
|
@@ -329,7 +329,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
|
|
|
329
329
|
|
|
330
330
|
dispatch({ type: 'SUBMIT_START' });
|
|
331
331
|
try {
|
|
332
|
-
onAddressComplete(complete);
|
|
332
|
+
await onAddressComplete(complete);
|
|
333
333
|
} finally {
|
|
334
334
|
dispatch({ type: 'SUBMIT_END' });
|
|
335
335
|
}
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
notifyAddressCompleted,
|
|
11
11
|
logFunnelEvent,
|
|
12
12
|
notifySessionAbandoned,
|
|
13
|
+
checkCodEligibility,
|
|
13
14
|
fetchSessionOffer,
|
|
14
15
|
scoreAddressRisk,
|
|
15
16
|
selectAddress,
|
|
@@ -214,6 +215,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
214
215
|
mapStyle,
|
|
215
216
|
mapTheme,
|
|
216
217
|
embedMode,
|
|
218
|
+
mapsKey,
|
|
217
219
|
enablePhoneAuth = false,
|
|
218
220
|
merchantId = '',
|
|
219
221
|
supabaseFunctionBaseUrl = '',
|
|
@@ -278,10 +280,10 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
278
280
|
[utmParams, deviceCtx, variant]
|
|
279
281
|
);
|
|
280
282
|
|
|
281
|
-
const checkoutConfig =
|
|
282
|
-
functionBaseUrl: supabaseFunctionBaseUrl,
|
|
283
|
-
merchantId
|
|
284
|
-
|
|
283
|
+
const checkoutConfig = useMemo(
|
|
284
|
+
() => ({ functionBaseUrl: supabaseFunctionBaseUrl, merchantId }),
|
|
285
|
+
[supabaseFunctionBaseUrl, merchantId]
|
|
286
|
+
);
|
|
285
287
|
|
|
286
288
|
const codSession = auth?.sessionToken || codSessionToken;
|
|
287
289
|
|
|
@@ -370,6 +372,32 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
370
372
|
.catch(() => setSessionOffer(null));
|
|
371
373
|
}, [auth?.sessionToken, merchantId, supabaseFunctionBaseUrl, checkoutConfig]);
|
|
372
374
|
|
|
375
|
+
// Resolve COD eligibility as soon as an address is chosen, so the payment
|
|
376
|
+
// step (which comes before `done`) already knows whether to offer COD.
|
|
377
|
+
const completedPincode = completedAddress?.pincode;
|
|
378
|
+
const codCheckTotalPaise = codCartTotalPaise ?? paymentAmountPaise;
|
|
379
|
+
useEffect(() => {
|
|
380
|
+
setCodAllowed(false);
|
|
381
|
+
if (!withPayment || !enableCod || !merchantId || !codSession || !completedPincode) return;
|
|
382
|
+
let cancelled = false;
|
|
383
|
+
checkCodEligibility(checkoutConfig, codSession, completedPincode, codCheckTotalPaise)
|
|
384
|
+
.then((r) => {
|
|
385
|
+
if (!cancelled) setCodAllowed(r.cod_allowed);
|
|
386
|
+
})
|
|
387
|
+
.catch(() => {/* stay false: fail closed */});
|
|
388
|
+
return () => {
|
|
389
|
+
cancelled = true;
|
|
390
|
+
};
|
|
391
|
+
}, [
|
|
392
|
+
withPayment,
|
|
393
|
+
enableCod,
|
|
394
|
+
merchantId,
|
|
395
|
+
codSession,
|
|
396
|
+
completedPincode,
|
|
397
|
+
codCheckTotalPaise,
|
|
398
|
+
checkoutConfig,
|
|
399
|
+
]);
|
|
400
|
+
|
|
373
401
|
const rootStyle = useMemo(
|
|
374
402
|
() => mergeBrandRootStyle(style as CSSProperties | undefined, brandColor),
|
|
375
403
|
[style, brandColor]
|
|
@@ -439,14 +467,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
439
467
|
digipin: saved.digipin,
|
|
440
468
|
});
|
|
441
469
|
|
|
442
|
-
|
|
443
|
-
pincode: saved.pincode,
|
|
444
|
-
digipin: saved.digipin,
|
|
445
|
-
state: saved.state,
|
|
446
|
-
district: saved.district,
|
|
447
|
-
locality: saved.locality,
|
|
448
|
-
}).catch(() => {/* silent */});
|
|
449
|
-
|
|
470
|
+
// finishAddress() sends the address_completed notification.
|
|
450
471
|
await finishAddress(savedAddressToComplete(saved), { skipCreate: true, paymentBackStep: 'saved' });
|
|
451
472
|
};
|
|
452
473
|
|
|
@@ -674,12 +695,23 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
674
695
|
}, [registerController, auth?.sessionToken, verifyAddress, paymentBackStep]);
|
|
675
696
|
|
|
676
697
|
const handleEditAddress = () => {
|
|
677
|
-
|
|
678
|
-
|
|
698
|
+
// A recorded payment completes the server session, so its token is dead:
|
|
699
|
+
// drop it and re-authenticate. Otherwise the session is still live, so a
|
|
700
|
+
// verified buyer goes back to their saved addresses instead of the OTP gate.
|
|
701
|
+
const sessionCompleted = paymentMethod !== null;
|
|
702
|
+
const nextStep: Step =
|
|
703
|
+
!sessionCompleted && auth?.sessionToken ? 'saved' : initialStep;
|
|
704
|
+
emit({ type: 'widget_reset', step: nextStep });
|
|
705
|
+
if (sessionCompleted) {
|
|
706
|
+
setAuth(null);
|
|
707
|
+
setSessionOffer(null);
|
|
708
|
+
}
|
|
709
|
+
setStep(nextStep);
|
|
679
710
|
setConfirmedLocation(null);
|
|
680
711
|
setCompletedAddress(null);
|
|
681
712
|
setAddressRisk(null);
|
|
682
713
|
setVerifyAddress(null);
|
|
714
|
+
setPaymentMethod(null);
|
|
683
715
|
};
|
|
684
716
|
|
|
685
717
|
// ── Emit "step viewed" event on every step change ────────────────────────
|
|
@@ -948,6 +980,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
948
980
|
mapStyle={mapStyle}
|
|
949
981
|
mapTheme={mapTheme}
|
|
950
982
|
embedMode={embedMode}
|
|
983
|
+
mapsKey={mapsKey}
|
|
951
984
|
/>
|
|
952
985
|
</>
|
|
953
986
|
)}
|
|
@@ -968,6 +1001,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
968
1001
|
mapStyle={mapStyle}
|
|
969
1002
|
mapTheme={mapTheme}
|
|
970
1003
|
embedMode={embedMode}
|
|
1004
|
+
mapsKey={mapsKey}
|
|
971
1005
|
/>
|
|
972
1006
|
)}
|
|
973
1007
|
|
|
@@ -16,7 +16,7 @@ import { getDigiPin, getLatLngFromDigiPin, formatDigiPin, isWithinIndia } from '
|
|
|
16
16
|
import { autocompleteAddress, looksLikeDigiPin, couldBeDigiPinInput } from '../core/api';
|
|
17
17
|
import type { AutocompleteSuggestion } from '../core/api';
|
|
18
18
|
import type { MapPinSelectorProps } from '../core/types';
|
|
19
|
-
import { QUANTAROUTE_MAP_STYLE_URL } from '../core/map-runtime';
|
|
19
|
+
import { QUANTAROUTE_MAP_STYLE_URL, withMapsKey } from '../core/map-runtime';
|
|
20
20
|
import { styles, COLORS } from '../styles/checkout.native';
|
|
21
21
|
|
|
22
22
|
// ─── Constants ────────────────────────────────────────────────────────────────
|
|
@@ -72,6 +72,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
72
72
|
apiKey,
|
|
73
73
|
apiBaseUrl,
|
|
74
74
|
brandColor = DEFAULT_BRAND,
|
|
75
|
+
mapsKey,
|
|
75
76
|
}) => {
|
|
76
77
|
const mapRef = useRef<OSMViewRef>(null);
|
|
77
78
|
|
|
@@ -92,6 +93,8 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
92
93
|
const [searchLoading, setSearchLoading] = useState(false);
|
|
93
94
|
const [searchError, setSearchError] = useState<string | null>(null);
|
|
94
95
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
96
|
+
/** Bumped on every input change so a slow, superseded autocomplete response is ignored. */
|
|
97
|
+
const searchSeqRef = useRef(0);
|
|
95
98
|
|
|
96
99
|
const isDark = theme === 'dark';
|
|
97
100
|
const accentColor = brandColor ?? DEFAULT_BRAND;
|
|
@@ -120,6 +123,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
120
123
|
setSearchQuery(value);
|
|
121
124
|
setSearchError(null);
|
|
122
125
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
126
|
+
const seq = ++searchSeqRef.current;
|
|
123
127
|
|
|
124
128
|
const trimmed = value.trim();
|
|
125
129
|
|
|
@@ -132,6 +136,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
132
136
|
// DigiPin — offline, instant
|
|
133
137
|
// Keep the text visible in the input; just close the keyboard and dropdown.
|
|
134
138
|
if (looksLikeDigiPin(trimmed)) {
|
|
139
|
+
setSearchLoading(false);
|
|
135
140
|
try {
|
|
136
141
|
const coords = getLatLngFromDigiPin(trimmed);
|
|
137
142
|
flyToCoord(parseFloat(coords.latitude), parseFloat(coords.longitude));
|
|
@@ -153,6 +158,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
153
158
|
|
|
154
159
|
if (trimmed.length < 3) {
|
|
155
160
|
setSuggestions([]);
|
|
161
|
+
setSearchLoading(false);
|
|
156
162
|
return;
|
|
157
163
|
}
|
|
158
164
|
|
|
@@ -164,18 +170,23 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
164
170
|
trimmed, apiKey ?? '', apiBaseUrl, 5,
|
|
165
171
|
coord.latitude, coord.longitude,
|
|
166
172
|
);
|
|
173
|
+
if (seq !== searchSeqRef.current) return;
|
|
167
174
|
setSuggestions(results);
|
|
168
175
|
} catch (err) {
|
|
176
|
+
if (seq !== searchSeqRef.current) return;
|
|
169
177
|
setSearchError(err instanceof Error ? err.message : 'Search failed');
|
|
170
178
|
setSuggestions([]);
|
|
171
179
|
} finally {
|
|
172
|
-
setSearchLoading(false);
|
|
180
|
+
if (seq === searchSeqRef.current) setSearchLoading(false);
|
|
173
181
|
}
|
|
174
182
|
}, 400);
|
|
175
183
|
}, [apiKey, apiBaseUrl, flyToCoord]);
|
|
176
184
|
|
|
177
185
|
const handleSuggestionSelect = useCallback((s: AutocompleteSuggestion) => {
|
|
178
186
|
flyToCoord(s.coordinates.latitude, s.coordinates.longitude);
|
|
187
|
+
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
188
|
+
searchSeqRef.current += 1;
|
|
189
|
+
setSearchLoading(false);
|
|
179
190
|
setSearchQuery('');
|
|
180
191
|
setSuggestions([]);
|
|
181
192
|
Keyboard.dismiss();
|
|
@@ -312,7 +323,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
312
323
|
style={{ flex: 1 }}
|
|
313
324
|
initialCenter={{ latitude: initLat, longitude: initLng }}
|
|
314
325
|
initialZoom={hasDefault ? STREET_ZOOM : OVERVIEW_ZOOM}
|
|
315
|
-
styleUrl={QUANTAROUTE_MAP_STYLE_URL}
|
|
326
|
+
styleUrl={withMapsKey(QUANTAROUTE_MAP_STYLE_URL, mapsKey)}
|
|
316
327
|
markers={pinMarkers}
|
|
317
328
|
showUserLocation={false}
|
|
318
329
|
scrollEnabled
|
|
@@ -9,8 +9,10 @@ import { useGeolocation } from '../hooks/useGeolocation';
|
|
|
9
9
|
import type { MapPinSelectorProps } from '../core/types';
|
|
10
10
|
import {
|
|
11
11
|
addQuantaRouteWatermark,
|
|
12
|
+
mapsTransformRequest,
|
|
12
13
|
resolveQuantaRouteMapStyle,
|
|
13
14
|
waitForExternalMapRuntime,
|
|
15
|
+
withMapsKey,
|
|
14
16
|
type MapRuntime,
|
|
15
17
|
} from '../core/map-runtime';
|
|
16
18
|
|
|
@@ -120,6 +122,7 @@ function initMapWithRuntime(
|
|
|
120
122
|
initZoom: number;
|
|
121
123
|
brandColor: string;
|
|
122
124
|
indiaBoundaryUrl?: string;
|
|
125
|
+
mapsKey?: string;
|
|
123
126
|
refreshDigipin: (la: number, lo: number) => void;
|
|
124
127
|
onReady: () => void;
|
|
125
128
|
onError: (message: string) => void;
|
|
@@ -132,6 +135,7 @@ function initMapWithRuntime(
|
|
|
132
135
|
initZoom,
|
|
133
136
|
brandColor,
|
|
134
137
|
indiaBoundaryUrl,
|
|
138
|
+
mapsKey,
|
|
135
139
|
refreshDigipin,
|
|
136
140
|
onReady,
|
|
137
141
|
onError,
|
|
@@ -145,7 +149,8 @@ function initMapWithRuntime(
|
|
|
145
149
|
|
|
146
150
|
const map = new runtime.Map({
|
|
147
151
|
container,
|
|
148
|
-
style: styleUrl,
|
|
152
|
+
style: withMapsKey(styleUrl, mapsKey),
|
|
153
|
+
transformRequest: mapsTransformRequest(mapsKey),
|
|
149
154
|
center: [initLng, initLat],
|
|
150
155
|
zoom: initZoom,
|
|
151
156
|
minZoom: MIN_ZOOM,
|
|
@@ -263,6 +268,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
263
268
|
mapStyle,
|
|
264
269
|
mapTheme,
|
|
265
270
|
embedMode = 'bundled',
|
|
271
|
+
mapsKey,
|
|
266
272
|
}) => {
|
|
267
273
|
const resolvedMapStyle = resolveQuantaRouteMapStyle(mapStyle, mapTheme);
|
|
268
274
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
@@ -289,6 +295,8 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
289
295
|
const [searchError, setSearchError] = useState<string | null>(null);
|
|
290
296
|
const [dropdownOpen, setDropdownOpen] = useState(false);
|
|
291
297
|
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
298
|
+
/** Bumped on every input change so a slow, superseded autocomplete response is ignored. */
|
|
299
|
+
const searchSeqRef = useRef(0);
|
|
292
300
|
const searchInputRef = useRef<HTMLInputElement>(null);
|
|
293
301
|
const listboxId = useId();
|
|
294
302
|
|
|
@@ -314,6 +322,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
314
322
|
setSearchError(null);
|
|
315
323
|
|
|
316
324
|
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
325
|
+
const seq = ++searchSeqRef.current;
|
|
317
326
|
|
|
318
327
|
const trimmed = value.trim();
|
|
319
328
|
|
|
@@ -325,6 +334,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
325
334
|
}
|
|
326
335
|
|
|
327
336
|
if (looksLikeDigiPin(trimmed)) {
|
|
337
|
+
setSearchLoading(false);
|
|
328
338
|
try {
|
|
329
339
|
const coords = getLatLngFromDigiPin(trimmed);
|
|
330
340
|
const la = parseFloat(coords.latitude);
|
|
@@ -349,6 +359,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
349
359
|
if (trimmed.length < 3) {
|
|
350
360
|
setSuggestions([]);
|
|
351
361
|
setDropdownOpen(false);
|
|
362
|
+
setSearchLoading(false);
|
|
352
363
|
return;
|
|
353
364
|
}
|
|
354
365
|
|
|
@@ -357,20 +368,25 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
357
368
|
try {
|
|
358
369
|
const key = apiKey ?? '';
|
|
359
370
|
const results = await autocompleteAddress(trimmed, key, apiBaseUrl, 5, lat, lng);
|
|
371
|
+
if (seq !== searchSeqRef.current) return;
|
|
360
372
|
setSuggestions(results);
|
|
361
373
|
setDropdownOpen(results.length > 0);
|
|
362
374
|
} catch (err) {
|
|
375
|
+
if (seq !== searchSeqRef.current) return;
|
|
363
376
|
setSearchError(err instanceof Error ? err.message : 'Search failed');
|
|
364
377
|
setSuggestions([]);
|
|
365
378
|
setDropdownOpen(false);
|
|
366
379
|
} finally {
|
|
367
|
-
setSearchLoading(false);
|
|
380
|
+
if (seq === searchSeqRef.current) setSearchLoading(false);
|
|
368
381
|
}
|
|
369
382
|
}, 400);
|
|
370
383
|
}, [apiKey, apiBaseUrl, flyToCoord, lat, lng]);
|
|
371
384
|
|
|
372
385
|
const handleSuggestionSelect = useCallback((s: AutocompleteSuggestion) => {
|
|
373
386
|
flyToCoord(s.coordinates.latitude, s.coordinates.longitude);
|
|
387
|
+
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
388
|
+
searchSeqRef.current += 1;
|
|
389
|
+
setSearchLoading(false);
|
|
374
390
|
setSearchQuery('');
|
|
375
391
|
setSuggestions([]);
|
|
376
392
|
setDropdownOpen(false);
|
|
@@ -379,6 +395,9 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
379
395
|
|
|
380
396
|
const handleSearchKeyDown = useCallback((e: React.KeyboardEvent<HTMLInputElement>) => {
|
|
381
397
|
if (e.key === 'Escape') {
|
|
398
|
+
if (debounceRef.current) clearTimeout(debounceRef.current);
|
|
399
|
+
searchSeqRef.current += 1;
|
|
400
|
+
setSearchLoading(false);
|
|
382
401
|
setSuggestions([]);
|
|
383
402
|
setDropdownOpen(false);
|
|
384
403
|
setSearchQuery('');
|
|
@@ -407,6 +426,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
407
426
|
initZoom,
|
|
408
427
|
brandColor,
|
|
409
428
|
indiaBoundaryUrl,
|
|
429
|
+
mapsKey,
|
|
410
430
|
refreshDigipin,
|
|
411
431
|
onReady: () => {
|
|
412
432
|
if (cancelled) return;
|
|
@@ -456,6 +476,7 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
456
476
|
indiaBoundaryUrl,
|
|
457
477
|
initLat,
|
|
458
478
|
initLng,
|
|
479
|
+
mapsKey,
|
|
459
480
|
resolvedMapStyle,
|
|
460
481
|
refreshDigipin,
|
|
461
482
|
]);
|
|
@@ -14,21 +14,49 @@ declare global {
|
|
|
14
14
|
|
|
15
15
|
const RAZORPAY_SCRIPT = 'https://checkout.razorpay.com/v1/checkout.js';
|
|
16
16
|
|
|
17
|
+
const RAZORPAY_LOAD_TIMEOUT_MS = 15_000;
|
|
18
|
+
let razorpayLoad: Promise<void> | null = null;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Load Razorpay Checkout once. Always settles: a failed or stalled tag is
|
|
22
|
+
* removed and the cached promise cleared, so the next attempt starts clean.
|
|
23
|
+
*/
|
|
17
24
|
function loadRazorpayScript(): Promise<void> {
|
|
18
25
|
if (window.Razorpay) return Promise.resolve();
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
26
|
+
if (razorpayLoad) return razorpayLoad;
|
|
27
|
+
|
|
28
|
+
razorpayLoad = new Promise<void>((resolve, reject) => {
|
|
29
|
+
// A tag we are not already tracking may have finished or failed before we
|
|
30
|
+
// could listen to it; without window.Razorpay it is useless, so replace it.
|
|
31
|
+
document.querySelector(`script[src="${RAZORPAY_SCRIPT}"]`)?.remove();
|
|
32
|
+
|
|
25
33
|
const script = document.createElement('script');
|
|
26
34
|
script.src = RAZORPAY_SCRIPT;
|
|
27
35
|
script.async = true;
|
|
28
|
-
|
|
29
|
-
|
|
36
|
+
|
|
37
|
+
const fail = (message: string) => {
|
|
38
|
+
window.clearTimeout(timer);
|
|
39
|
+
script.remove();
|
|
40
|
+
razorpayLoad = null;
|
|
41
|
+
reject(new Error(message));
|
|
42
|
+
};
|
|
43
|
+
const timer = window.setTimeout(
|
|
44
|
+
() => fail('Razorpay took too long to load'),
|
|
45
|
+
RAZORPAY_LOAD_TIMEOUT_MS
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
script.onload = () => {
|
|
49
|
+
window.clearTimeout(timer);
|
|
50
|
+
if (window.Razorpay) {
|
|
51
|
+
resolve();
|
|
52
|
+
} else {
|
|
53
|
+
fail('Razorpay unavailable');
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
script.onerror = () => fail('Failed to load Razorpay');
|
|
30
57
|
document.body.appendChild(script);
|
|
31
58
|
});
|
|
59
|
+
return razorpayLoad;
|
|
32
60
|
}
|
|
33
61
|
|
|
34
62
|
const PaymentStep: React.FC<PaymentStepProps> = ({
|
package/src/core/map-runtime.ts
CHANGED
|
@@ -63,6 +63,28 @@ export function resolveQuantaRouteMapStyle(
|
|
|
63
63
|
return QUANTAROUTE_MAP_STYLE_URL;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
const MAPS_ORIGIN = 'https://maps.quantaroute.com';
|
|
67
|
+
|
|
68
|
+
/** Attach `?key=` for MapLibre requests to maps.quantaroute.com. No-op for other hosts or a missing key. */
|
|
69
|
+
export function withMapsKey(url: string, key?: string): string {
|
|
70
|
+
if (!key || !url.startsWith(MAPS_ORIGIN)) return url;
|
|
71
|
+
try {
|
|
72
|
+
const next = new URL(url);
|
|
73
|
+
next.searchParams.set('key', key);
|
|
74
|
+
return next.toString();
|
|
75
|
+
} catch {
|
|
76
|
+
return url;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function mapsTransformRequest(
|
|
81
|
+
key?: string
|
|
82
|
+
): ((url: string) => { url: string }) | undefined {
|
|
83
|
+
if (!key) return undefined;
|
|
84
|
+
return (url: string) =>
|
|
85
|
+
url.startsWith(MAPS_ORIGIN) ? { url: withMapsKey(url, key) } : { url };
|
|
86
|
+
}
|
|
87
|
+
|
|
66
88
|
/** Official India outline GeoJSON (Survey of India / Datameet). Same file as the style overlay. */
|
|
67
89
|
export const QUANTAROUTE_INDIA_BOUNDARY_URL =
|
|
68
90
|
'https://maps.quantaroute.com/geojson/india.geojson';
|
package/src/core/types.ts
CHANGED
|
@@ -234,6 +234,12 @@ export interface MapPinSelectorProps {
|
|
|
234
234
|
* `external` waits for `window.maplibregl` (CDN script loaded by the host).
|
|
235
235
|
*/
|
|
236
236
|
embedMode?: MapEmbedMode;
|
|
237
|
+
/**
|
|
238
|
+
* Public `dp_` key with the maps scope. Required when the style is on
|
|
239
|
+
* maps.quantaroute.com and the page is not a first-party origin. MapLibre
|
|
240
|
+
* `transformRequest` attaches it to the style, sprite, font and tile URLs.
|
|
241
|
+
*/
|
|
242
|
+
mapsKey?: string;
|
|
237
243
|
}
|
|
238
244
|
|
|
239
245
|
/** Pre-fill manual fields when verifying an imported saved address. */
|
|
@@ -253,7 +259,8 @@ export interface AddressFormProps {
|
|
|
253
259
|
lng: number;
|
|
254
260
|
apiKey: string;
|
|
255
261
|
apiBaseUrl?: string;
|
|
256
|
-
|
|
262
|
+
/** May return a promise; the form stays in its submitting state until it settles. */
|
|
263
|
+
onAddressComplete: (address: CompleteAddress) => void | Promise<void>;
|
|
257
264
|
onBack: () => void;
|
|
258
265
|
onError?: (error: Error) => void;
|
|
259
266
|
theme?: 'light' | 'dark';
|
|
@@ -331,6 +338,11 @@ export interface CheckoutWidgetProps {
|
|
|
331
338
|
* when the host loads MapLibre from a CDN (e.g. Shopify theme extensions).
|
|
332
339
|
*/
|
|
333
340
|
embedMode?: MapEmbedMode;
|
|
341
|
+
/**
|
|
342
|
+
* Public `dp_` key with the maps scope. Passed through to the map step.
|
|
343
|
+
* Shopify and other third-party hosts must set this; first-party sites can omit it.
|
|
344
|
+
*/
|
|
345
|
+
mapsKey?: string;
|
|
334
346
|
/**
|
|
335
347
|
* Show the smart search bar on the map step (default: true).
|
|
336
348
|
* Supports address search (API) and DigiPin lookup (100% offline).
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { describe, it } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { buildRedirectUrl, parseSelectors, readScriptConfig } from './handoff.ts';
|
|
4
|
+
|
|
5
|
+
describe('buildRedirectUrl', () => {
|
|
6
|
+
it('fills single and double brace placeholders and encodes values', () => {
|
|
7
|
+
const url = buildRedirectUrl(
|
|
8
|
+
'https://shop.example/pay?sid={sessionId}&pin={{pincode}}&addr={formattedAddress}',
|
|
9
|
+
{ sessionId: 'abc-123', pincode: '560099', formattedAddress: '4B, MG Road & Co' }
|
|
10
|
+
);
|
|
11
|
+
assert.equal(
|
|
12
|
+
url,
|
|
13
|
+
'https://shop.example/pay?sid=abc-123&pin=560099&addr=4B%2C%20MG%20Road%20%26%20Co'
|
|
14
|
+
);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('handles numbers and leaves unknown or missing placeholders empty', () => {
|
|
18
|
+
const url = buildRedirectUrl('https://x.test/?lat={lat}&lng={lng}&u={unknown}&s={sessionId}', {
|
|
19
|
+
lat: 12.9716,
|
|
20
|
+
lng: 77.5946,
|
|
21
|
+
});
|
|
22
|
+
assert.equal(url, 'https://x.test/?lat=12.9716&lng=77.5946&u=&s=');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('leaves a template without placeholders alone', () => {
|
|
26
|
+
assert.equal(buildRedirectUrl('https://x.test/pay', { sessionId: 'a' }), 'https://x.test/pay');
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('parseSelectors', () => {
|
|
31
|
+
it('splits on top-level commas and trims', () => {
|
|
32
|
+
assert.deepEqual(parseSelectors('#a, .b ,button[name="checkout"]'), [
|
|
33
|
+
'#a',
|
|
34
|
+
'.b',
|
|
35
|
+
'button[name="checkout"]',
|
|
36
|
+
]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('keeps commas inside brackets, parentheses and quotes', () => {
|
|
40
|
+
assert.deepEqual(parseSelectors('a[href*="x,y"], :is(.a, .b)'), [
|
|
41
|
+
'a[href*="x,y"]',
|
|
42
|
+
':is(.a, .b)',
|
|
43
|
+
]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('accepts arrays and empty input', () => {
|
|
47
|
+
assert.deepEqual(parseSelectors([' #a ', '', '.b']), ['#a', '.b']);
|
|
48
|
+
assert.deepEqual(parseSelectors(undefined), []);
|
|
49
|
+
assert.deepEqual(parseSelectors(''), []);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('readScriptConfig', () => {
|
|
54
|
+
it('reads data attributes and auto-inits when merchant id and key are present', () => {
|
|
55
|
+
const config = readScriptConfig({
|
|
56
|
+
merchantId: 'm1',
|
|
57
|
+
apiKey: 'dp_key',
|
|
58
|
+
mapsKey: 'dp_key',
|
|
59
|
+
functionsUrl: 'https://f.test/functions/v1',
|
|
60
|
+
trigger: '#checkout, .go',
|
|
61
|
+
redirectUrl: 'https://shop/pay?sid={sessionId}',
|
|
62
|
+
theme: 'dark',
|
|
63
|
+
auth: 'phone',
|
|
64
|
+
});
|
|
65
|
+
assert.equal(config.autoInit, true);
|
|
66
|
+
assert.deepEqual(config.triggers, ['#checkout', '.go']);
|
|
67
|
+
assert.equal(config.theme, 'dark');
|
|
68
|
+
assert.equal(config.authMode, 'phone');
|
|
69
|
+
assert.equal(config.functionsUrl, 'https://f.test/functions/v1');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('does not auto-init without credentials or when switched off', () => {
|
|
73
|
+
assert.equal(readScriptConfig({}).autoInit, false);
|
|
74
|
+
assert.equal(readScriptConfig({ merchantId: 'm1' }).autoInit, false);
|
|
75
|
+
assert.equal(
|
|
76
|
+
readScriptConfig({ merchantId: 'm1', apiKey: 'k', autoInit: 'false' }).autoInit,
|
|
77
|
+
false
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('ignores unknown theme and auth values', () => {
|
|
82
|
+
const config = readScriptConfig({ merchantId: 'm', apiKey: 'k', theme: 'neon', auth: 'sms' });
|
|
83
|
+
assert.equal(config.theme, undefined);
|
|
84
|
+
assert.equal(config.authMode, undefined);
|
|
85
|
+
});
|
|
86
|
+
});
|