@quantaroute/checkout 1.4.2 → 1.4.3
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 +14 -0
- package/dist/lib/index.d.ts +26 -0
- package/dist/lib/quantaroute-checkout.es.js +113 -111
- package/dist/lib/quantaroute-checkout.umd.js +128 -126
- package/package.json +1 -1
- package/src/components/CheckoutWidget.tsx +75 -26
- package/src/components/MapPinSelector.native.tsx +3 -3
- package/src/components/MapPinSelector.tsx +3 -4
- package/src/core/api.ts +2 -5
- package/src/core/digipin.ts +29 -4
- package/src/core/types.ts +14 -0
- package/src/index.ts +3 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quantaroute/checkout",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Embeddable DigiPin-powered smart address checkout widget — React (web), React Native & Expo (iOS/Android)",
|
|
5
5
|
"main": "./dist/lib/quantaroute-checkout.umd.js",
|
|
6
6
|
"module": "./dist/lib/quantaroute-checkout.es.js",
|
|
@@ -170,6 +170,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
170
170
|
enablePrepaidNudge = false,
|
|
171
171
|
prepaidNudgeCopy,
|
|
172
172
|
prepaidNudgeCtaUrl,
|
|
173
|
+
registerController,
|
|
173
174
|
}) => {
|
|
174
175
|
const authMode: AuthMode = enablePhoneAuth ? 'phone' : enableEmailAuth ? 'email' : 'none';
|
|
175
176
|
const initialStep: Step = authMode === 'phone' ? 'phone' : authMode === 'email' ? 'email' : 'map';
|
|
@@ -379,6 +380,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
379
380
|
label: (saved.label === 'work' || saved.label === 'other'
|
|
380
381
|
? saved.label
|
|
381
382
|
: 'home') as CompleteAddress['label'],
|
|
383
|
+
addressId: saved.id,
|
|
382
384
|
};
|
|
383
385
|
void finishAddress(complete);
|
|
384
386
|
};
|
|
@@ -414,26 +416,28 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
414
416
|
locality: address.locality,
|
|
415
417
|
}).catch(() => {/* silent */});
|
|
416
418
|
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
419
|
+
if (!address.addressId) {
|
|
420
|
+
try {
|
|
421
|
+
await createAddress(checkoutConfig, auth.sessionToken, {
|
|
422
|
+
digipin: address.digipin,
|
|
423
|
+
lat: address.lat,
|
|
424
|
+
lng: address.lng,
|
|
425
|
+
state: address.state,
|
|
426
|
+
district: address.district,
|
|
427
|
+
locality: address.locality,
|
|
428
|
+
pincode: address.pincode,
|
|
429
|
+
delivery_status: address.delivery,
|
|
430
|
+
flat_number: address.flatNumber,
|
|
431
|
+
floor_number: address.floorNumber,
|
|
432
|
+
building_name: address.buildingName,
|
|
433
|
+
street_name: address.streetName,
|
|
434
|
+
full_address_json: address as unknown as Record<string, unknown>,
|
|
435
|
+
label: address.label ?? 'home',
|
|
436
|
+
is_default: false,
|
|
437
|
+
});
|
|
438
|
+
} catch {
|
|
439
|
+
// Silent
|
|
440
|
+
}
|
|
437
441
|
}
|
|
438
442
|
}
|
|
439
443
|
|
|
@@ -449,16 +453,61 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
449
453
|
await finishAddress(address);
|
|
450
454
|
};
|
|
451
455
|
|
|
452
|
-
const handleBack = () => {
|
|
456
|
+
const handleBack = (): boolean => {
|
|
453
457
|
if (step === 'form') {
|
|
454
458
|
setStep('map');
|
|
455
|
-
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
if (step === 'map' && auth?.sessionToken) {
|
|
456
462
|
setStep('saved');
|
|
457
|
-
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
if (step === 'email-otp') {
|
|
458
466
|
setStep('email');
|
|
467
|
+
return true;
|
|
459
468
|
}
|
|
469
|
+
if (step === 'otp') {
|
|
470
|
+
setStep('phone');
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
if (step === 'payment') {
|
|
474
|
+
setStep('form');
|
|
475
|
+
return true;
|
|
476
|
+
}
|
|
477
|
+
return false;
|
|
460
478
|
};
|
|
461
479
|
|
|
480
|
+
useEffect(() => {
|
|
481
|
+
if (!registerController) return;
|
|
482
|
+
registerController({
|
|
483
|
+
goBack: () => {
|
|
484
|
+
const current = stepRef.current;
|
|
485
|
+
if (current === 'form') {
|
|
486
|
+
setStep('map');
|
|
487
|
+
return true;
|
|
488
|
+
}
|
|
489
|
+
if (current === 'map' && auth?.sessionToken) {
|
|
490
|
+
setStep('saved');
|
|
491
|
+
return true;
|
|
492
|
+
}
|
|
493
|
+
if (current === 'email-otp') {
|
|
494
|
+
setStep('email');
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
if (current === 'otp') {
|
|
498
|
+
setStep('phone');
|
|
499
|
+
return true;
|
|
500
|
+
}
|
|
501
|
+
if (current === 'payment') {
|
|
502
|
+
setStep('form');
|
|
503
|
+
return true;
|
|
504
|
+
}
|
|
505
|
+
return false;
|
|
506
|
+
},
|
|
507
|
+
getStep: () => stepRef.current,
|
|
508
|
+
});
|
|
509
|
+
}, [registerController, auth?.sessionToken]);
|
|
510
|
+
|
|
462
511
|
const handleEditAddress = () => {
|
|
463
512
|
emit({ type: 'widget_reset', step: initialStep });
|
|
464
513
|
setStep(initialStep);
|
|
@@ -598,7 +647,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
598
647
|
email={emailPending}
|
|
599
648
|
codeReused={emailCodeReused}
|
|
600
649
|
onVerified={handleEmailVerified}
|
|
601
|
-
onBack={
|
|
650
|
+
onBack={handleBack}
|
|
602
651
|
functionBaseUrl={supabaseFunctionBaseUrl}
|
|
603
652
|
merchantId={merchantId}
|
|
604
653
|
sessionContext={sessionContext}
|
|
@@ -620,7 +669,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
620
669
|
<OtpStep
|
|
621
670
|
mobile={auth.mobile}
|
|
622
671
|
onVerified={handleVerified}
|
|
623
|
-
onBack={
|
|
672
|
+
onBack={handleBack}
|
|
624
673
|
functionBaseUrl={supabaseFunctionBaseUrl}
|
|
625
674
|
merchantId={merchantId}
|
|
626
675
|
sessionContext={sessionContext}
|
|
@@ -680,7 +729,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
|
|
|
680
729
|
codAllowed={codAllowed}
|
|
681
730
|
theme={theme}
|
|
682
731
|
onComplete={handlePaymentComplete}
|
|
683
|
-
onBack={
|
|
732
|
+
onBack={handleBack}
|
|
684
733
|
onEvent={(type, paymentMethod) =>
|
|
685
734
|
emit({ type, step: 'payment', paymentMethod, buyerId: auth?.buyerId })
|
|
686
735
|
}
|
|
@@ -12,7 +12,7 @@ import {
|
|
|
12
12
|
import { OSMView, LocationButton } from 'expo-osm-sdk';
|
|
13
13
|
import type { OSMViewRef, MarkerConfig } from 'expo-osm-sdk';
|
|
14
14
|
import * as Location from 'expo-location';
|
|
15
|
-
import { getDigiPin, getLatLngFromDigiPin, isWithinIndia } from '../core/digipin';
|
|
15
|
+
import { getDigiPin, getLatLngFromDigiPin, formatDigiPin, isWithinIndia } from '../core/digipin';
|
|
16
16
|
import { autocompleteAddress, looksLikeDigiPin } from '../core/api';
|
|
17
17
|
import type { AutocompleteSuggestion } from '../core/api';
|
|
18
18
|
import type { MapPinSelectorProps } from '../core/types';
|
|
@@ -139,10 +139,10 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
139
139
|
// Keep the text visible in the input; just close the keyboard and dropdown.
|
|
140
140
|
if (looksLikeDigiPin(trimmed)) {
|
|
141
141
|
try {
|
|
142
|
-
const coords = getLatLngFromDigiPin(trimmed
|
|
142
|
+
const coords = getLatLngFromDigiPin(trimmed);
|
|
143
143
|
flyToCoord(parseFloat(coords.latitude), parseFloat(coords.longitude));
|
|
144
144
|
setSuggestions([]);
|
|
145
|
-
setSearchQuery(trimmed
|
|
145
|
+
setSearchQuery(formatDigiPin(trimmed));
|
|
146
146
|
Keyboard.dismiss();
|
|
147
147
|
} catch {
|
|
148
148
|
setSearchError('Invalid DigiPin code');
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import React, { useEffect, useRef, useState, useCallback, useId } from 'react';
|
|
2
2
|
import maplibregl from 'maplibre-gl';
|
|
3
3
|
import 'maplibre-gl/dist/maplibre-gl.css';
|
|
4
|
-
import { getDigiPin, getLatLngFromDigiPin, isWithinIndia } from '../core/digipin';
|
|
4
|
+
import { getDigiPin, getLatLngFromDigiPin, formatDigiPin, isWithinIndia } from '../core/digipin';
|
|
5
5
|
import { autocompleteAddress, looksLikeDigiPin } from '../core/api';
|
|
6
6
|
import type { AutocompleteSuggestion } from '../core/api';
|
|
7
7
|
import { useGeolocation } from '../hooks/useGeolocation';
|
|
@@ -177,14 +177,13 @@ const MapPinSelector: React.FC<MapPinSelectorProps> = ({
|
|
|
177
177
|
// Keep the text in the input so the user can see what resolved.
|
|
178
178
|
if (looksLikeDigiPin(trimmed)) {
|
|
179
179
|
try {
|
|
180
|
-
const coords = getLatLngFromDigiPin(trimmed
|
|
180
|
+
const coords = getLatLngFromDigiPin(trimmed);
|
|
181
181
|
const la = parseFloat(coords.latitude);
|
|
182
182
|
const lo = parseFloat(coords.longitude);
|
|
183
183
|
flyToCoord(la, lo);
|
|
184
184
|
setSuggestions([]);
|
|
185
185
|
setDropdownOpen(false);
|
|
186
|
-
|
|
187
|
-
setSearchQuery(trimmed.toUpperCase());
|
|
186
|
+
setSearchQuery(formatDigiPin(trimmed));
|
|
188
187
|
} catch {
|
|
189
188
|
setSearchError('Invalid DigiPin code');
|
|
190
189
|
}
|
package/src/core/api.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
import type { LocationLookupResponse } from './types';
|
|
2
|
+
import { isCompleteDigiPinInput } from './digipin';
|
|
2
3
|
|
|
3
4
|
const DEFAULT_BASE_URL = 'https://api.quantaroute.com';
|
|
4
5
|
|
|
5
|
-
// ─── DigiPin regex (matches "XXX-XXX-XXXX" with valid characters) ─────────────
|
|
6
|
-
// Valid chars: F C 9 8 J 3 2 7 K 4 5 6 L M P T (from the 4×4 DIGIPIN_GRID)
|
|
7
|
-
const DIGIPIN_PATTERN = /^[FC98J327K456LMPT]{3}-[FC98J327K456LMPT]{3}-[FC98J327K456LMPT]{4}$/i;
|
|
8
|
-
|
|
9
6
|
/**
|
|
10
7
|
* Cross-platform timeout signal.
|
|
11
8
|
* AbortSignal.timeout() is not available in React Native / Hermes — fall back
|
|
@@ -235,7 +232,7 @@ export interface AutocompleteResponse {
|
|
|
235
232
|
* Used to skip the API call and resolve offline instead.
|
|
236
233
|
*/
|
|
237
234
|
export function looksLikeDigiPin(input: string): boolean {
|
|
238
|
-
return
|
|
235
|
+
return isCompleteDigiPinInput(input);
|
|
239
236
|
}
|
|
240
237
|
|
|
241
238
|
/**
|
package/src/core/digipin.ts
CHANGED
|
@@ -32,6 +32,31 @@ const BOUNDS = {
|
|
|
32
32
|
maxLon: 99.5,
|
|
33
33
|
};
|
|
34
34
|
|
|
35
|
+
/** Characters that appear in the official 4×4 DigiPin grid */
|
|
36
|
+
const DIGIPIN_ALPHABET = 'FC98J327K456LMPT';
|
|
37
|
+
const VALID_DIGIPIN_BODY = new RegExp(`^[${DIGIPIN_ALPHABET}]{10}$`, 'i');
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Normalise user input to a 10-character DigiPin body (no separators).
|
|
41
|
+
* Accepts dashed, spaced, or compact forms — e.g. `4P3-J68-637F`, `4P3 J68 637F`, `4P3J68637F`.
|
|
42
|
+
*/
|
|
43
|
+
export function normalizeDigiPinInput(input: string): string {
|
|
44
|
+
return input.trim().toUpperCase().replace(/[\s\-_.]/g, '');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** Canonical display format: XXX-XXX-XXXX */
|
|
48
|
+
export function formatDigiPin(input: string): string {
|
|
49
|
+
const pin = normalizeDigiPinInput(input);
|
|
50
|
+
if (pin.length !== 10) return input.trim().toUpperCase();
|
|
51
|
+
return `${pin.slice(0, 3)}-${pin.slice(3, 6)}-${pin.slice(6)}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** True when input is a complete 10-character DigiPin (any common separator style). */
|
|
55
|
+
export function isCompleteDigiPinInput(input: string): boolean {
|
|
56
|
+
const pin = normalizeDigiPinInput(input);
|
|
57
|
+
return pin.length === 10 && VALID_DIGIPIN_BODY.test(pin);
|
|
58
|
+
}
|
|
59
|
+
|
|
35
60
|
export interface DigiPinCoordinates {
|
|
36
61
|
latitude: string;
|
|
37
62
|
longitude: string;
|
|
@@ -88,9 +113,9 @@ export function getDigiPin(lat: number, lon: number): string {
|
|
|
88
113
|
* @throws if the DigiPin format is invalid
|
|
89
114
|
*/
|
|
90
115
|
export function getLatLngFromDigiPin(digiPin: string): DigiPinCoordinates {
|
|
91
|
-
const pin = digiPin
|
|
116
|
+
const pin = normalizeDigiPinInput(digiPin);
|
|
92
117
|
if (pin.length !== 10) {
|
|
93
|
-
throw new Error('Invalid DIGIPIN: must be 10 characters (excluding
|
|
118
|
+
throw new Error('Invalid DIGIPIN: must be 10 characters (excluding separators)');
|
|
94
119
|
}
|
|
95
120
|
|
|
96
121
|
let minLat = BOUNDS.minLat;
|
|
@@ -145,9 +170,9 @@ export function getLatLngFromDigiPin(digiPin: string): DigiPinCoordinates {
|
|
|
145
170
|
*/
|
|
146
171
|
export function isValidDigiPin(digiPin: string): boolean {
|
|
147
172
|
if (!digiPin || typeof digiPin !== 'string') return false;
|
|
148
|
-
if (
|
|
173
|
+
if (!isCompleteDigiPinInput(digiPin)) return false;
|
|
149
174
|
try {
|
|
150
|
-
getLatLngFromDigiPin(digiPin
|
|
175
|
+
getLatLngFromDigiPin(digiPin);
|
|
151
176
|
return true;
|
|
152
177
|
} catch {
|
|
153
178
|
return false;
|
package/src/core/types.ts
CHANGED
|
@@ -91,6 +91,15 @@ export interface CompleteAddress {
|
|
|
91
91
|
formattedAddress: string;
|
|
92
92
|
/** Buyer-chosen label for saved-address book (defaults to home) */
|
|
93
93
|
label?: AddressLabel;
|
|
94
|
+
/** Set when the buyer re-selected an existing saved row (skip createAddress). */
|
|
95
|
+
addressId?: string;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Imperative hooks for host pages (e.g. Shopify pre-checkout shell). */
|
|
99
|
+
export interface CheckoutWidgetController {
|
|
100
|
+
/** Navigate one step back within the widget. Returns true if handled. */
|
|
101
|
+
goBack: () => boolean;
|
|
102
|
+
getStep: () => string;
|
|
94
103
|
}
|
|
95
104
|
|
|
96
105
|
// ─── Checkout event (client-side analytics hook) ─────────────────────────────
|
|
@@ -327,6 +336,11 @@ export interface CheckoutWidgetProps {
|
|
|
327
336
|
prepaidNudgeCopy?: string;
|
|
328
337
|
/** Override merchant prepaid nudge CTA URL. */
|
|
329
338
|
prepaidNudgeCtaUrl?: string;
|
|
339
|
+
/**
|
|
340
|
+
* Called once on mount with imperative navigation helpers for host shells
|
|
341
|
+
* (step-aware back button, etc.).
|
|
342
|
+
*/
|
|
343
|
+
registerController?: (controller: CheckoutWidgetController) => void;
|
|
330
344
|
}
|
|
331
345
|
|
|
332
346
|
// ─── Phone / OTP step props ───────────────────────────────────────────────────
|