@quantaroute/checkout 1.4.5 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantaroute/checkout",
3
- "version": "1.4.5",
3
+ "version": "1.5.0",
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",
@@ -38,6 +38,7 @@
38
38
  "type-check": "tsc --noEmit",
39
39
  "type-check:native": "tsc --project tsconfig.native.json --noEmit",
40
40
  "verify:digipin": "npx tsx scripts/verify-digipin-formats.mjs",
41
+ "verify:locality": "npx tsx scripts/verify-locality-names.mjs",
41
42
  "prepublishOnly": "npm run type-check && npm run type-check:native && npm run build:lib"
42
43
  },
43
44
  "keywords": [
@@ -19,6 +19,7 @@ import {
19
19
  type AddressComponents,
20
20
  } from '../core/api';
21
21
  import { checkServiceability, type ServiceabilityResult } from '../core/checkout-api';
22
+ import { processLocalityLookup } from '../core/locality';
22
23
  import type {
23
24
  AddressFormProps,
24
25
  AddressLabel,
@@ -45,6 +46,7 @@ type FormState =
45
46
  status: 'ready';
46
47
  adminInfo: AdministrativeInfo;
47
48
  alternatives: LocationAlternative[];
49
+ localityOptions: string[];
48
50
  selectedLocality: string;
49
51
  selectedLabel: AddressLabel;
50
52
  labelText: string;
@@ -106,11 +108,13 @@ function reducer(state: FormState, action: FormAction): FormState {
106
108
  return { status: 'loading' };
107
109
  case 'LOAD_SUCCESS': {
108
110
  const preFilled = parseAddressComponents(action.addressComponents);
111
+ const processed = processLocalityLookup(action.adminInfo, action.alternatives || []);
109
112
  return {
110
113
  status: 'ready',
111
- adminInfo: action.adminInfo,
112
- alternatives: action.alternatives || [],
113
- selectedLocality: action.adminInfo.locality,
114
+ adminInfo: processed.adminInfo,
115
+ alternatives: processed.alternatives,
116
+ localityOptions: processed.localityOptions,
117
+ selectedLocality: processed.selectedLocality,
114
118
  selectedLabel: 'home',
115
119
  labelText: '',
116
120
  fields: { ...INITIAL_FIELDS, ...preFilled },
@@ -476,7 +480,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
476
480
  <Text style={[styles.autoRowLabel, isDark && styles.autoRowLabelDark]}>
477
481
  Locality
478
482
  </Text>
479
- {state.alternatives.length > 0 ? (
483
+ {state.localityOptions.length > 1 ? (
480
484
  <TouchableOpacity
481
485
  style={[
482
486
  styles.localitySelector,
@@ -739,7 +743,7 @@ const AddressForm: React.FC<AddressFormProps> = ({
739
743
  ) : null}
740
744
 
741
745
  {/* ── Locality picker modal ── */}
742
- {state.status === 'ready' && state.alternatives.length > 0 ? (
746
+ {state.status === 'ready' && state.localityOptions.length > 1 ? (
743
747
  <Modal
744
748
  visible={showLocalityPicker}
745
749
  transparent
@@ -761,11 +765,8 @@ const AddressForm: React.FC<AddressFormProps> = ({
761
765
  Select Locality
762
766
  </Text>
763
767
  <FlatList
764
- data={[
765
- { name: state.adminInfo.locality } as LocationAlternative,
766
- ...state.alternatives,
767
- ]}
768
- keyExtractor={(item, idx) => `${item.name}-${idx}`}
768
+ data={state.localityOptions.map((name) => ({ name }))}
769
+ keyExtractor={(item) => item.name}
769
770
  renderItem={({ item }) => {
770
771
  const isSelected = item.name === state.selectedLocality;
771
772
  return (
@@ -7,6 +7,7 @@ import {
7
7
  type AddressComponents,
8
8
  } from '../core/api';
9
9
  import { checkServiceability, type ServiceabilityResult } from '../core/checkout-api';
10
+ import { processLocalityLookup } from '../core/locality';
10
11
  import type {
11
12
  AddressFormProps,
12
13
  AddressLabel,
@@ -32,6 +33,7 @@ type FormState =
32
33
  status: 'ready';
33
34
  adminInfo: AdministrativeInfo;
34
35
  alternatives: LocationAlternative[];
36
+ localityOptions: string[];
35
37
  selectedLocality: string; // Selected locality name (from adminInfo or alternatives)
36
38
  selectedLabel: AddressLabel;
37
39
  labelText: string;
@@ -106,12 +108,13 @@ function reducer(state: FormState, action: FormAction): FormState {
106
108
  case 'LOAD_SUCCESS': {
107
109
  // Pre-fill fields from addressComponents
108
110
  const preFilled = parseAddressComponents(action.addressComponents);
109
- // Default to primary locality from adminInfo
111
+ const processed = processLocalityLookup(action.adminInfo, action.alternatives || []);
110
112
  return {
111
113
  status: 'ready',
112
- adminInfo: action.adminInfo,
113
- alternatives: action.alternatives || [],
114
- selectedLocality: action.adminInfo.locality,
114
+ adminInfo: processed.adminInfo,
115
+ alternatives: processed.alternatives,
116
+ localityOptions: processed.localityOptions,
117
+ selectedLocality: processed.selectedLocality,
115
118
  selectedLabel: 'home',
116
119
  labelText: '',
117
120
  fields: { ...INITIAL_FIELDS, ...preFilled },
@@ -421,24 +424,19 @@ const AddressForm: React.FC<AddressFormProps> = ({
421
424
  <span className="qr-auto-row__value">{state.adminInfo.district}</span>
422
425
  </div>
423
426
 
424
- {/* Locality - show dropdown if alternatives exist */}
427
+ {/* Locality - show dropdown if multiple unique localities exist */}
425
428
  <div className="qr-auto-row qr-auto-row--full">
426
429
  <span className="qr-auto-row__label">Locality</span>
427
- {state.alternatives.length > 0 ? (
430
+ {state.localityOptions.length > 1 ? (
428
431
  <select
429
432
  className="qr-auto-row__select"
430
433
  value={state.selectedLocality}
431
434
  onChange={(e) => dispatch({ type: 'SET_LOCALITY', locality: e.target.value })}
432
435
  aria-label="Select locality"
433
436
  >
434
- {/* Primary option (from adminInfo) */}
435
- <option value={state.adminInfo.locality}>
436
- {state.adminInfo.locality}
437
- </option>
438
- {/* Alternatives */}
439
- {state.alternatives.map((alt, idx) => (
440
- <option key={idx} value={alt.name}>
441
- {alt.name}
437
+ {state.localityOptions.map((name) => (
438
+ <option key={name} value={name}>
439
+ {name}
442
440
  </option>
443
441
  ))}
444
442
  </select>
@@ -174,6 +174,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
174
174
  prepaidNudgeCopy,
175
175
  prepaidNudgeCtaUrl,
176
176
  registerController,
177
+ orderConfirmationUrl,
177
178
  }) => {
178
179
  const authMode: AuthMode = enablePhoneAuth ? 'phone' : enableEmailAuth ? 'email' : 'none';
179
180
  const initialStep: Step = authMode === 'phone' ? 'phone' : authMode === 'email' ? 'email' : 'map';
@@ -389,12 +390,21 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
389
390
  void finishAddress(complete);
390
391
  };
391
392
 
392
- const handlePaymentComplete = (method: string) => {
393
- setPaymentMethod(method);
394
- emit({ type: 'payment_method_selected', step: 'done', paymentMethod: method });
393
+ const handlePaymentComplete = ({ paymentMethod, razorpayOrderId, razorpayPaymentId }: {
394
+ paymentMethod: string;
395
+ razorpayOrderId?: string;
396
+ razorpayPaymentId?: string;
397
+ }) => {
398
+ setPaymentMethod(paymentMethod);
399
+ emit({ type: 'payment_method_selected', step: 'done', paymentMethod });
395
400
  setStep('done');
396
401
  if (completedAddress) onComplete(completedAddress);
397
- onPaymentComplete?.({ paymentMethod: method, sessionId: auth?.sessionId });
402
+ onPaymentComplete?.({
403
+ paymentMethod,
404
+ sessionId: auth?.sessionId,
405
+ razorpayOrderId,
406
+ razorpayPaymentId,
407
+ });
398
408
  };
399
409
 
400
410
  // ── Map step callbacks ────────────────────────────────────────────────────
@@ -449,6 +459,11 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
449
459
 
450
460
  if (withPayment && codSession) {
451
461
  setStep('payment');
462
+ } else if (withPayment && !codSession) {
463
+ onError?.(new Error('enablePayment requires phone/email auth or a codSessionToken prop.'));
464
+ setStep('done');
465
+ onComplete(address);
466
+ return;
452
467
  } else {
453
468
  setStep('done');
454
469
  onComplete(address);
@@ -744,6 +759,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
744
759
  onEvent={(type, paymentMethod) =>
745
760
  emit({ type, step: 'payment', paymentMethod, buyerId: auth?.buyerId })
746
761
  }
762
+ orderConfirmationUrl={orderConfirmationUrl}
747
763
  />
748
764
  )}
749
765
 
@@ -42,6 +42,7 @@ const PaymentStep: React.FC<PaymentStepProps> = ({
42
42
  onComplete,
43
43
  onBack,
44
44
  onEvent,
45
+ orderConfirmationUrl,
45
46
  }) => {
46
47
  const [loading, setLoading] = useState(false);
47
48
  const [error, setError] = useState<string | null>(null);
@@ -82,7 +83,15 @@ const PaymentStep: React.FC<PaymentStepProps> = ({
82
83
  payment_method: 'prepaid',
83
84
  });
84
85
  onEvent?.('payment_method_selected', result.payment_method);
85
- onComplete(result.payment_method);
86
+ onComplete({
87
+ paymentMethod: result.payment_method,
88
+ razorpayOrderId: result.razorpay_order_id,
89
+ razorpayPaymentId: result.razorpay_payment_id,
90
+ });
91
+ if (orderConfirmationUrl) {
92
+ const url = orderConfirmationUrl.replace('{{sessionId}}', sessionId ?? '');
93
+ window.location.href = url;
94
+ }
86
95
  resolve();
87
96
  } catch (e) {
88
97
  reject(e);
@@ -107,7 +116,7 @@ const PaymentStep: React.FC<PaymentStepProps> = ({
107
116
  try {
108
117
  const result = await recordCodPayment(checkoutConfig, sessionToken);
109
118
  onEvent?.('payment_method_selected', result.payment_method);
110
- onComplete(result.payment_method);
119
+ onComplete({ paymentMethod: result.payment_method });
111
120
  } catch (e) {
112
121
  setError(e instanceof Error ? e.message : 'Failed to select COD');
113
122
  } finally {
@@ -467,7 +467,7 @@ export async function verifyPayment(
467
467
  razorpay_signature: string;
468
468
  payment_method?: string;
469
469
  }
470
- ): Promise<{ payment_method: string }> {
470
+ ): Promise<{ payment_method: string; razorpay_order_id: string; razorpay_payment_id: string }> {
471
471
  const res = await fetch(`${config.functionBaseUrl}/checkout-payment`, {
472
472
  method: 'POST',
473
473
  headers: addressHeaders(sessionToken),
@@ -477,7 +477,11 @@ export async function verifyPayment(
477
477
  if (!res.ok || !data.success) {
478
478
  throw new Error(data.error ?? 'Payment verification failed.');
479
479
  }
480
- return { payment_method: data.payment_method as string };
480
+ return {
481
+ payment_method: data.payment_method as string,
482
+ razorpay_order_id: data.razorpay_order_id as string,
483
+ razorpay_payment_id: data.razorpay_payment_id as string,
484
+ };
481
485
  }
482
486
 
483
487
  export async function recordCodPayment(
@@ -0,0 +1,74 @@
1
+ import type { AdministrativeInfo, LocationAlternative } from './types';
2
+
3
+ /**
4
+ * India Post office-type suffixes on locality names (e.g. "Hessarghatta Lake S.O").
5
+ * These are post-office designations, not buyer-facing locality labels.
6
+ */
7
+ const INDIA_POST_OFFICE_SUFFIX =
8
+ /\s+(?:B\.?\s*O\.?|S\.?\s*O\.?|H\.?\s*O\.?|P\.?\s*O\.?|D\.?\s*S\.?\s*O\.?|S\.?\s*D\.?\s*O\.?|S\.?\s*B\.?\s*C\.?|C\.?\s*P\.?\s*C\.?|M\.?\s*D\.?\s*G\.?)\.?\s*$/i;
9
+
10
+ /** Remove trailing India Post office suffix from a locality name. */
11
+ export function stripIndiaPostOfficeSuffix(name: string): string {
12
+ return name.replace(INDIA_POST_OFFICE_SUFFIX, '').replace(/\s+/g, ' ').trim();
13
+ }
14
+
15
+ /** Case-insensitive key for deduplicating locality names. */
16
+ export function localityDedupeKey(name: string): string {
17
+ return stripIndiaPostOfficeSuffix(name).toLowerCase();
18
+ }
19
+
20
+ export interface ProcessedLocalityLookup {
21
+ adminInfo: AdministrativeInfo;
22
+ alternatives: LocationAlternative[];
23
+ localityOptions: string[];
24
+ selectedLocality: string;
25
+ }
26
+
27
+ /**
28
+ * Normalize primary locality + alternatives from location lookup:
29
+ * - strip S.O / H.O / B.O style suffixes for display
30
+ * - merge duplicates that differ only by office suffix
31
+ */
32
+ export function processLocalityLookup(
33
+ adminInfo: AdministrativeInfo,
34
+ alternatives: LocationAlternative[] = [],
35
+ ): ProcessedLocalityLookup {
36
+ const seen = new Set<string>();
37
+ const localityOptions: string[] = [];
38
+
39
+ const addUnique = (rawName: string) => {
40
+ const key = localityDedupeKey(rawName);
41
+ if (!key || seen.has(key)) return;
42
+ seen.add(key);
43
+ localityOptions.push(stripIndiaPostOfficeSuffix(rawName));
44
+ };
45
+
46
+ addUnique(adminInfo.locality);
47
+ for (const alt of alternatives) {
48
+ addUnique(alt.name);
49
+ }
50
+
51
+ const primaryClean = stripIndiaPostOfficeSuffix(adminInfo.locality);
52
+ const primaryKey = localityDedupeKey(adminInfo.locality);
53
+
54
+ const cleanedAlternatives = alternatives
55
+ .map((alt) => ({
56
+ ...alt,
57
+ name: stripIndiaPostOfficeSuffix(alt.name),
58
+ }))
59
+ .filter((alt) => {
60
+ const key = localityDedupeKey(alt.name);
61
+ return key && key !== primaryKey;
62
+ })
63
+ .filter(
64
+ (alt, idx, arr) =>
65
+ arr.findIndex((a) => localityDedupeKey(a.name) === localityDedupeKey(alt.name)) === idx,
66
+ );
67
+
68
+ return {
69
+ adminInfo: { ...adminInfo, locality: primaryClean },
70
+ alternatives: cleanedAlternatives,
71
+ localityOptions,
72
+ selectedLocality: primaryClean,
73
+ };
74
+ }
package/src/core/types.ts CHANGED
@@ -356,7 +356,19 @@ export interface CheckoutWidgetProps {
356
356
  /** Cart total in paise for Razorpay order creation. Required when enablePayment is true. */
357
357
  paymentAmountPaise?: number;
358
358
  /** Called when payment completes (prepaid or COD). */
359
- onPaymentComplete?: (result: { paymentMethod: string; sessionId?: string }) => void;
359
+ onPaymentComplete?: (result: {
360
+ paymentMethod: string;
361
+ sessionId?: string;
362
+ razorpayOrderId?: string;
363
+ razorpayPaymentId?: string;
364
+ }) => void;
365
+ /**
366
+ * URL to redirect the buyer to after successful payment.
367
+ * Use `{{sessionId}}` as a placeholder — the widget replaces it before redirecting.
368
+ * Example: "https://yourstore.com/order/confirm?sid={{sessionId}}"
369
+ * When omitted, no redirect occurs and the host app handles navigation via onPaymentComplete.
370
+ */
371
+ orderConfirmationUrl?: string;
360
372
  /**
361
373
  * Show prepaid conversion nudge when COD is eligible (merchant `prepaid_nudge_copy` or prop override).
362
374
  */
@@ -425,9 +437,14 @@ export interface PaymentStepProps {
425
437
  enableCod?: boolean;
426
438
  codAllowed?: boolean;
427
439
  theme?: 'light' | 'dark';
428
- onComplete: (paymentMethod: string) => void;
440
+ onComplete: (result: {
441
+ paymentMethod: string;
442
+ razorpayOrderId?: string;
443
+ razorpayPaymentId?: string;
444
+ }) => void;
429
445
  onBack?: () => void;
430
446
  onEvent?: (type: CheckoutEventType, paymentMethod?: string) => void;
447
+ orderConfirmationUrl?: string;
431
448
  }
432
449
 
433
450
  export interface PrepaidNudgeProps {