@quantaroute/checkout 1.3.1 → 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/src/core/types.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import type { ReactNode } from 'react';
2
+ import type { UtmParams } from './utm';
3
+ import type { SessionContextPayload } from './checkout-api';
4
+
1
5
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
6
  type AnyStyle = any; // CSSProperties on web · StyleProp<ViewStyle> on native
3
7
 
@@ -18,9 +22,6 @@ export interface AdministrativeInfo {
18
22
  pincode: string;
19
23
  delivery: string;
20
24
  district: string;
21
- mean_population_density?: number;
22
- min_population_density?: number;
23
- max_population_density?: number;
24
25
  }
25
26
 
26
27
  export interface LocationAlternative {
@@ -83,6 +84,64 @@ export interface CompleteAddress {
83
84
  formattedAddress: string;
84
85
  }
85
86
 
87
+ // ─── Checkout event (client-side analytics hook) ─────────────────────────────
88
+
89
+ /** All step-transition event types emitted by the widget. */
90
+ export type CheckoutEventType =
91
+ // Phone auth
92
+ | 'phone_step_viewed'
93
+ | 'otp_sent'
94
+ | 'phone_verified'
95
+ // Email auth
96
+ | 'email_step_viewed'
97
+ | 'email_otp_sent'
98
+ | 'email_verified'
99
+ // Shared
100
+ | 'auth_skipped'
101
+ | 'saved_addresses_viewed'
102
+ | 'saved_address_selected'
103
+ | 'map_step_viewed'
104
+ | 'location_confirmed'
105
+ | 'form_step_viewed'
106
+ | 'address_completed'
107
+ | 'widget_reset'
108
+ | 'payment_step_viewed'
109
+ | 'payment_method_selected'
110
+ | 'session_abandoned';
111
+
112
+ /**
113
+ * Payload delivered to `onCheckoutEvent` on every step transition.
114
+ * Forward this to MoEngage, Clevertap, GTM, or your own analytics.
115
+ */
116
+ export interface CheckoutEvent {
117
+ /** The event that just occurred. */
118
+ type: CheckoutEventType;
119
+ /** The step the widget moved INTO. */
120
+ step: string;
121
+ /** Buyer's phone number (E.164) or email. Available after OTP sent. */
122
+ mobile?: string;
123
+ /** Internal buyer UUID. Available after auth verification. */
124
+ buyerId?: string;
125
+ /** Checkout session UUID. Available after auth verification. */
126
+ sessionId?: string;
127
+ /** Auth channel used in this session. */
128
+ authChannel?: 'phone' | 'email' | 'none';
129
+ /** Device class: mobile | tablet | desktop */
130
+ deviceType?: 'mobile' | 'tablet' | 'desktop';
131
+ /** Platform: web | ios | android */
132
+ platform?: 'web' | 'ios' | 'android';
133
+ /** Truncated user agent (web). */
134
+ userAgent?: string;
135
+ /** Payment method when selected: cod | prepaid | upi | card | … */
136
+ paymentMethod?: string;
137
+ /** A/B experiment variant label from `variant` prop. */
138
+ experimentVariant?: string;
139
+ /** DigiPin of the confirmed location. Available after `location_confirmed` and `address_completed`. */
140
+ digipin?: string;
141
+ /** ISO timestamp of when the event fired. */
142
+ timestamp: string;
143
+ }
144
+
86
145
  // ─── Component Props ─────────────────────────────────────────────────────────
87
146
 
88
147
  export interface MapPinSelectorProps {
@@ -112,6 +171,10 @@ export interface MapPinSelectorProps {
112
171
  apiKey?: string;
113
172
  /** API base URL for autocomplete. Defaults to https://api.quantaroute.com */
114
173
  apiBaseUrl?: string;
174
+ /**
175
+ * Brand accent colour (hex, e.g. `#7c3aed`). Sets map pin, buttons, focus rings via `--qr-primary` on web.
176
+ */
177
+ brandColor?: string;
115
178
  }
116
179
 
117
180
  export interface AddressFormProps {
@@ -166,4 +229,155 @@ export interface CheckoutWidgetProps {
166
229
  * and the map instantly flies to their ~4m × 4m location — zero API cost.
167
230
  */
168
231
  enableSearch?: boolean;
232
+ /**
233
+ * Enable phone OTP authentication for saved addresses (default: false).
234
+ * When true, the widget starts with a phone entry step so returning buyers
235
+ * can access their previously saved addresses — reducing re-entry friction.
236
+ * Requires merchantId and supabaseFunctionBaseUrl to be set.
237
+ * Takes precedence over enableEmailAuth if both are set.
238
+ */
239
+ enablePhoneAuth?: boolean;
240
+ /**
241
+ * Enable email OTP authentication via Resend (default: false).
242
+ * When true (and enablePhoneAuth is false), the widget starts with an email
243
+ * entry step. Requires merchantId and supabaseFunctionBaseUrl to be set.
244
+ * Needs RESEND_API_KEY + RESEND_FROM_EMAIL on the checkout-email-otp Edge Function.
245
+ */
246
+ enableEmailAuth?: boolean;
247
+ /**
248
+ * UUID of your merchant record in checkout.merchants.
249
+ * Required when enablePhoneAuth or enableEmailAuth is true.
250
+ */
251
+ merchantId?: string;
252
+ /**
253
+ * Base URL of your Supabase Edge Functions deployment.
254
+ * Example: "https://ikfiewesidedxozotmrl.supabase.co/functions/v1"
255
+ * Required when enablePhoneAuth or enableEmailAuth is true.
256
+ */
257
+ supabaseFunctionBaseUrl?: string;
258
+ /**
259
+ * When true, shows a Cash on Delivery availability strip on the success screen
260
+ * (calls `cod_eligibility`). Requires `merchantId`, `supabaseFunctionBaseUrl`, and a
261
+ * session token from phone auth or `codSessionToken`. On the server, set
262
+ * `checkout.merchants.enable_cod` and optional D1/D2 columns.
263
+ */
264
+ enableCod?: boolean;
265
+ /**
266
+ * Cart total in paise for the COD cart cap (D2). Omit if the cart total is unknown.
267
+ */
268
+ codCartTotalPaise?: number;
269
+ /**
270
+ * Session token for the COD check when your app manages auth outside the widget’s phone flow.
271
+ */
272
+ codSessionToken?: string;
273
+ /**
274
+ * Brand accent colour (hex, e.g. `#7c3aed`). On web, sets `--qr-primary` on the root; on native, passed to the map pin and header accents.
275
+ */
276
+ brandColor?: string;
277
+ /**
278
+ * Merchant logo URL shown in the widget header (replaces default map icon when set).
279
+ */
280
+ logoUrl?: string;
281
+ /**
282
+ * Optional slot below the header row for trust badges, free-shipping strip, or any custom JSX.
283
+ */
284
+ headerBanner?: ReactNode;
285
+ /**
286
+ * Client-side analytics hook. Called on every step transition so you can
287
+ * forward events to MoEngage, Clevertap, GTM, or your own backend.
288
+ *
289
+ * @example
290
+ * onCheckoutEvent={(event) => {
291
+ * Moengage.track_event(event.type, { step: event.step, buyerId: event.buyerId });
292
+ * }}
293
+ */
294
+ onCheckoutEvent?: (event: CheckoutEvent) => void;
295
+ /**
296
+ * A/B experiment variant label — included on every `onCheckoutEvent` payload
297
+ * and stored on the checkout session for funnel breakdowns.
298
+ */
299
+ variant?: string;
300
+ /**
301
+ * UTM params captured from the page URL at widget mount (web).
302
+ * Stored on the session when the buyer verifies OTP.
303
+ */
304
+ utmParams?: UtmParams;
305
+ /**
306
+ * Enable Razorpay payment step after address completion (requires merchant Razorpay keys).
307
+ */
308
+ enablePayment?: boolean;
309
+ /** Cart total in paise for Razorpay order creation. Required when enablePayment is true. */
310
+ paymentAmountPaise?: number;
311
+ /** Called when payment completes (prepaid or COD). */
312
+ onPaymentComplete?: (result: { paymentMethod: string; sessionId?: string }) => void;
313
+ /**
314
+ * Show prepaid conversion nudge when COD is eligible (merchant `prepaid_nudge_copy` or prop override).
315
+ */
316
+ enablePrepaidNudge?: boolean;
317
+ /** Override merchant prepaid nudge copy. */
318
+ prepaidNudgeCopy?: string;
319
+ /** Override merchant prepaid nudge CTA URL. */
320
+ prepaidNudgeCtaUrl?: string;
321
+ }
322
+
323
+ // ─── Phone / OTP step props ───────────────────────────────────────────────────
324
+
325
+ export interface PhoneStepProps {
326
+ onOtpSent: (mobile: string) => void;
327
+ onSkip: () => void;
328
+ functionBaseUrl: string;
329
+ merchantId: string;
330
+ }
331
+
332
+ export interface OtpStepProps {
333
+ mobile: string;
334
+ onVerified: (sessionToken: string, buyerId: string, sessionId?: string) => void;
335
+ onBack: () => void;
336
+ functionBaseUrl: string;
337
+ merchantId: string;
338
+ sessionContext?: SessionContextPayload;
339
+ }
340
+
341
+ export interface EmailStepProps {
342
+ /** Called with the normalised (lowercased) email once OTP is dispatched. */
343
+ onOtpSent: (email: string) => void;
344
+ onSkip: () => void;
345
+ functionBaseUrl: string;
346
+ merchantId: string;
347
+ }
348
+
349
+ export interface EmailOtpStepProps {
350
+ email: string;
351
+ onVerified: (sessionToken: string, buyerId: string, sessionId?: string) => void;
352
+ onBack: () => void;
353
+ functionBaseUrl: string;
354
+ merchantId: string;
355
+ sessionContext?: SessionContextPayload;
356
+ }
357
+
358
+ export interface SavedAddressStepProps {
359
+ sessionToken: string;
360
+ functionBaseUrl: string;
361
+ merchantId: string;
362
+ onSelectAddress: (address: import('./checkout-api').SavedAddress) => void;
363
+ onAddNew: () => void;
364
+ }
365
+
366
+ export interface PaymentStepProps {
367
+ checkoutConfig: import('./checkout-api').CheckoutApiConfig;
368
+ sessionToken: string;
369
+ sessionId?: string;
370
+ amountPaise: number;
371
+ enableCod?: boolean;
372
+ codAllowed?: boolean;
373
+ theme?: 'light' | 'dark';
374
+ onComplete: (paymentMethod: string) => void;
375
+ onBack?: () => void;
376
+ onEvent?: (type: CheckoutEventType, paymentMethod?: string) => void;
377
+ }
378
+
379
+ export interface PrepaidNudgeProps {
380
+ copy: string;
381
+ ctaUrl?: string;
382
+ theme?: 'light' | 'dark';
169
383
  }
@@ -0,0 +1,29 @@
1
+ /** UTM parameters captured from the storefront URL at checkout start. */
2
+
3
+ export interface UtmParams {
4
+ utm_source?: string;
5
+ utm_medium?: string;
6
+ utm_campaign?: string;
7
+ }
8
+
9
+ const UTM_KEYS = ['utm_source', 'utm_medium', 'utm_campaign'] as const;
10
+
11
+ /** Read UTM params from the current page URL (web). Returns {} on native or SSR. */
12
+ export function captureUtmFromUrl(href?: string): UtmParams {
13
+ try {
14
+ const url = new URL(href ?? (typeof window !== 'undefined' ? window.location.href : ''));
15
+ const out: UtmParams = {};
16
+ for (const key of UTM_KEYS) {
17
+ const v = url.searchParams.get(key);
18
+ if (v) out[key] = v.slice(0, 256);
19
+ }
20
+ return out;
21
+ } catch {
22
+ return {};
23
+ }
24
+ }
25
+
26
+ /** True when at least one UTM field is set. */
27
+ export function hasUtmParams(utm: UtmParams): boolean {
28
+ return Boolean(utm.utm_source || utm.utm_medium || utm.utm_campaign);
29
+ }
package/src/index.ts CHANGED
@@ -28,6 +28,14 @@ export { default } from './components/CheckoutWidget';
28
28
  // ── Sub-components (for advanced composition) ─────────────────────────────────
29
29
  export { default as MapPinSelector } from './components/MapPinSelector';
30
30
  export { default as AddressForm } from './components/AddressForm';
31
+ export { default as PhoneStep } from './components/PhoneStep';
32
+ export { default as OtpStep } from './components/OtpStep';
33
+ export { default as EmailStep } from './components/EmailStep';
34
+ export { default as EmailOtpStep } from './components/EmailOtpStep';
35
+ export { default as CodAvailabilityBanner } from './components/CodAvailabilityBanner';
36
+ export { default as PrepaidNudge } from './components/PrepaidNudge';
37
+ export { default as PaymentStep } from './components/PaymentStep';
38
+ export { default as SavedAddressStep } from './components/SavedAddressStep';
31
39
 
32
40
  // ── Core utilities (offline, zero-dependency) ─────────────────────────────────
33
41
  export {
@@ -39,12 +47,56 @@ export {
39
47
  } from './core/digipin';
40
48
 
41
49
  // ── API client ────────────────────────────────────────────────────────────────
42
- export { lookupLocation, reverseGeocode, autocompleteAddress, looksLikeDigiPin } from './core/api';
50
+ export {
51
+ lookupLocation,
52
+ reverseGeocode,
53
+ autocompleteAddress,
54
+ looksLikeDigiPin,
55
+ validatePincode,
56
+ pincodeHasDeliveryOffice,
57
+ } from './core/api';
58
+
59
+ // ── Checkout backend API ──────────────────────────────────────────────────────
60
+ export {
61
+ sendOtp,
62
+ verifyOtp,
63
+ sendEmailOtp,
64
+ verifyEmailOtp,
65
+ listAddresses,
66
+ createAddress,
67
+ deleteAddress,
68
+ setDefaultAddress,
69
+ checkCodEligibility,
70
+ notifyAddressCompleted,
71
+ logFunnelEvent,
72
+ notifySessionAbandoned,
73
+ fetchSessionOffer,
74
+ createPaymentOrder,
75
+ verifyPayment,
76
+ recordCodPayment,
77
+ fetchPrepaidNudgeConfig,
78
+ } from './core/checkout-api';
79
+ export type {
80
+ CheckoutApiConfig,
81
+ VerifyOtpResult,
82
+ VerifyEmailOtpResult,
83
+ SavedAddress,
84
+ SavedAddressPayload,
85
+ CodEligibilityResult,
86
+ SessionContextPayload,
87
+ FunnelEventPayload,
88
+ SessionOffer,
89
+ CreatePaymentOrderResult,
90
+ PrepaidNudgeConfig,
91
+ } from './core/checkout-api';
92
+
43
93
  export type {
44
94
  AddressComponents,
45
95
  ReverseGeocodeResponse,
46
96
  AutocompleteSuggestion,
47
97
  AutocompleteResponse,
98
+ PincodeOffice,
99
+ PincodeValidateData,
48
100
  } from './core/api';
49
101
 
50
102
  // ── Hooks ─────────────────────────────────────────────────────────────────────
@@ -52,13 +104,25 @@ export { useGeolocation } from './hooks/useGeolocation';
52
104
  export { useDigiPin } from './hooks/useDigiPin';
53
105
 
54
106
  // ── TypeScript types ──────────────────────────────────────────────────────────
107
+ export { captureUtmFromUrl, hasUtmParams } from './core/utm';
108
+ export type { UtmParams } from './core/utm';
109
+ export { getWebDeviceContext, detectDeviceType, detectPlatform } from './core/device';
55
110
  export type {
56
111
  CheckoutWidgetProps,
57
112
  MapPinSelectorProps,
58
113
  AddressFormProps,
114
+ PhoneStepProps,
115
+ OtpStepProps,
116
+ EmailStepProps,
117
+ EmailOtpStepProps,
118
+ SavedAddressStepProps,
119
+ PaymentStepProps,
120
+ PrepaidNudgeProps,
59
121
  CompleteAddress,
60
122
  AdministrativeInfo,
61
123
  LocationAlternative,
62
124
  LocationLookupResponse,
63
125
  Coordinates,
126
+ CheckoutEvent,
127
+ CheckoutEventType,
64
128
  } from './core/types';