@quantaroute/checkout 1.3.2 → 1.4.1

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.
@@ -1,5 +1,11 @@
1
1
  import React, { useEffect, useReducer, useCallback } from 'react';
2
- import { lookupLocation, reverseGeocode, type AddressComponents } from '../core/api';
2
+ import {
3
+ lookupLocation,
4
+ reverseGeocode,
5
+ validatePincode,
6
+ pincodeHasDeliveryOffice,
7
+ type AddressComponents,
8
+ } from '../core/api';
3
9
  import type {
4
10
  AddressFormProps,
5
11
  AdministrativeInfo,
@@ -26,6 +32,8 @@ type FormState =
26
32
  selectedLocality: string; // Selected locality name (from adminInfo or alternatives)
27
33
  fields: ManualFields;
28
34
  submitting: boolean;
35
+ /** At least one India Post office with "Delivery" (not only "Non Delivery") */
36
+ pincodeHasDelivery: boolean;
29
37
  };
30
38
 
31
39
  type FormAction =
@@ -35,6 +43,7 @@ type FormAction =
35
43
  adminInfo: AdministrativeInfo;
36
44
  alternatives: LocationAlternative[];
37
45
  addressComponents: AddressComponents;
46
+ pincodeHasDelivery: boolean;
38
47
  }
39
48
  | { type: 'LOAD_ERROR'; message: string }
40
49
  | { type: 'SET_FIELD'; key: keyof ManualFields; value: string }
@@ -96,6 +105,7 @@ function reducer(state: FormState, action: FormAction): FormState {
96
105
  selectedLocality: action.adminInfo.locality,
97
106
  fields: { ...INITIAL_FIELDS, ...preFilled },
98
107
  submitting: false,
108
+ pincodeHasDelivery: action.pincodeHasDelivery,
99
109
  };
100
110
  }
101
111
  case 'LOAD_ERROR':
@@ -143,11 +153,37 @@ const AddressForm: React.FC<AddressFormProps> = ({
143
153
  reverseGeocode(digipin, apiKey, apiBaseUrl),
144
154
  ]);
145
155
 
156
+ const pincode = adminRes.data.administrative_info.pincode?.replace(/\D/g, '').slice(0, 6) ?? '';
157
+ if (pincode.length !== 6) {
158
+ dispatch({
159
+ type: 'LOAD_ERROR',
160
+ message:
161
+ 'Could not determine a valid 6-digit pincode for this location. Please adjust the map pin.',
162
+ });
163
+ onError?.(new Error('Missing or invalid pincode'));
164
+ return;
165
+ }
166
+
167
+ const pinValidation = await validatePincode(pincode, apiKey, apiBaseUrl);
168
+
169
+ if (!pinValidation.isValid) {
170
+ dispatch({
171
+ type: 'LOAD_ERROR',
172
+ message:
173
+ 'This location resolves to a pincode that is not in the India Post database. Please move the map pin to a valid address.',
174
+ });
175
+ onError?.(new Error('Invalid pincode for location'));
176
+ return;
177
+ }
178
+
179
+ const hasDelivery = pincodeHasDeliveryOffice(pinValidation.offices);
180
+
146
181
  dispatch({
147
182
  type: 'LOAD_SUCCESS',
148
183
  adminInfo: adminRes.data.administrative_info,
149
184
  alternatives: adminRes.data.alternatives || [],
150
185
  addressComponents: reverseRes.data.addressComponents,
186
+ pincodeHasDelivery: hasDelivery,
151
187
  });
152
188
  } catch (err) {
153
189
  const msg = err instanceof Error ? err.message : 'Failed to fetch address data.';
@@ -333,6 +369,24 @@ const AddressForm: React.FC<AddressFormProps> = ({
333
369
  </div>
334
370
  </fieldset>
335
371
 
372
+ {/* Serviceability: India Post "Non Delivery" only — warn (still allow save) */}
373
+ {!state.pincodeHasDelivery && (
374
+ <div
375
+ className="qr-pincode-banner qr-pincode-banner--warning"
376
+ role="status"
377
+ aria-live="polite"
378
+ >
379
+ <span className="qr-pincode-banner__icon" aria-hidden="true">⚠</span>
380
+ <div className="qr-pincode-banner__text">
381
+ <strong>Non-delivery area</strong>
382
+ <span>
383
+ This pincode has no India Post home-delivery office. Courier delivery may be limited —
384
+ confirm with your seller before placing the order.
385
+ </span>
386
+ </div>
387
+ </div>
388
+ )}
389
+
336
390
  {/* Manual entry section */}
337
391
  <fieldset className="qr-fieldset">
338
392
  <legend className="qr-fieldset__legend">
@@ -1,16 +1,18 @@
1
- import React, { useState } from 'react';
1
+ import React, { useState, useEffect } from 'react';
2
2
  import {
3
3
  View,
4
4
  Text,
5
5
  TouchableOpacity,
6
6
  SafeAreaView,
7
7
  Linking,
8
+ Image,
8
9
  } from 'react-native';
9
10
  // Explicit .native imports so TypeScript (tsconfig.native.json) resolves the
10
11
  // correct platform files — Metro handles implicit resolution at bundle time.
11
12
  import MapPinSelector from './MapPinSelector.native';
12
13
  import AddressForm from './AddressForm.native';
13
- import type { CheckoutWidgetProps, CompleteAddress } from '../core/types';
14
+ import type { CheckoutEvent, CheckoutWidgetProps, CompleteAddress } from '../core/types';
15
+ import CodAvailabilityBanner from './CodAvailabilityBanner';
14
16
  import { styles, COLORS } from '../styles/checkout.native';
15
17
 
16
18
  // ─── Types ────────────────────────────────────────────────────────────────────
@@ -88,22 +90,60 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
88
90
  style,
89
91
  mapHeight = 380,
90
92
  title = 'Add Delivery Address',
93
+ brandColor,
94
+ logoUrl,
95
+ headerBanner,
96
+ onCheckoutEvent,
97
+ merchantId = '',
98
+ supabaseFunctionBaseUrl = '',
99
+ enableCod = false,
100
+ codCartTotalPaise,
101
+ codSessionToken = '',
91
102
  }) => {
92
103
  const [step, setStep] = useState<Step>('map');
93
104
  const [confirmedLocation, setConfirmedLocation] = useState<ConfirmedLocation | null>(null);
94
105
  const [completedAddress, setCompletedAddress] = useState<CompleteAddress | null>(null);
95
106
 
107
+ const checkoutConfig = {
108
+ functionBaseUrl: supabaseFunctionBaseUrl,
109
+ merchantId,
110
+ };
111
+ const codSession = codSessionToken;
112
+
96
113
  const isDark = theme === 'dark';
114
+ const accent = brandColor ?? COLORS.primary;
115
+
116
+ /** Fire an analytics event without blocking the UI. */
117
+ const emit = (partial: Omit<CheckoutEvent, 'timestamp'>) => {
118
+ if (!onCheckoutEvent) return;
119
+ try {
120
+ onCheckoutEvent({ ...partial, timestamp: new Date().toISOString() });
121
+ } catch {
122
+ // Never let analytics crash the checkout
123
+ }
124
+ };
125
+
126
+ useEffect(() => {
127
+ const viewedMap: Record<Step, CheckoutEvent['type']> = {
128
+ map: 'map_step_viewed',
129
+ form: 'form_step_viewed',
130
+ done: 'address_completed',
131
+ };
132
+ emit({ type: viewedMap[step], step });
133
+ // eslint-disable-next-line react-hooks/exhaustive-deps
134
+ }, [step]);
97
135
 
98
136
  // ── Handlers ──────────────────────────────────────────────────────────────
99
137
 
100
138
  const handleLocationConfirm = (lat: number, lng: number, digipin: string) => {
101
139
  setConfirmedLocation({ lat, lng, digipin });
140
+ emit({ type: 'location_confirmed', step: 'form', digipin });
102
141
  setStep('form');
103
142
  };
104
143
 
105
144
  const handleAddressComplete = (address: CompleteAddress) => {
106
145
  setCompletedAddress(address);
146
+ emit({ type: 'address_completed', step: 'done', digipin: address.digipin });
107
147
  setStep('done');
108
148
  onComplete(address);
109
149
  };
@@ -111,6 +151,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
111
151
  const handleBack = () => setStep('map');
112
152
 
113
153
  const handleEditAddress = () => {
154
+ emit({ type: 'widget_reset', step: 'map' });
114
155
  setStep('map');
115
156
  setConfirmedLocation(null);
116
157
  setCompletedAddress(null);
@@ -120,20 +161,17 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
120
161
 
121
162
  const renderStepDots = () => (
122
163
  <View style={styles.progress}>
123
- {/* Dot 1 – always active */}
124
- <View style={[styles.progressDot, styles.progressDotActive]} />
125
- {/* Line */}
164
+ <View style={[styles.progressDot, { backgroundColor: accent }]} />
126
165
  <View
127
166
  style={[
128
167
  styles.progressLine,
129
- (step === 'form' || step === 'done') && styles.progressLineActive,
168
+ (step === 'form' || step === 'done') && { backgroundColor: accent },
130
169
  ]}
131
170
  />
132
- {/* Dot 2 */}
133
171
  <View
134
172
  style={[
135
173
  styles.progressDot,
136
- (step === 'form' || step === 'done') && styles.progressDotActive,
174
+ (step === 'form' || step === 'done') && { backgroundColor: accent },
137
175
  ]}
138
176
  />
139
177
  </View>
@@ -153,15 +191,32 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
153
191
  >
154
192
  {/* Widget header (hidden on success screen) */}
155
193
  {step !== 'done' ? (
156
- <View style={[styles.header, isDark && styles.headerDark]}>
157
- <View style={styles.headerBrand}>
158
- <Text style={[styles.headerIcon, { color: COLORS.primary }]}>📍</Text>
159
- <Text style={[styles.headerTitle, isDark && styles.headerTitleDark]}>
160
- {title}
161
- </Text>
194
+ <>
195
+ <View style={[styles.header, isDark && styles.headerDark]}>
196
+ <View style={styles.headerBrand}>
197
+ {logoUrl ? (
198
+ <Image
199
+ source={{ uri: logoUrl }}
200
+ style={styles.headerLogoImg}
201
+ resizeMode="contain"
202
+ accessibilityRole="image"
203
+ accessibilityLabel=""
204
+ />
205
+ ) : (
206
+ <Text style={[styles.headerIcon, { color: accent }]}>📍</Text>
207
+ )}
208
+ <Text style={[styles.headerTitle, isDark && styles.headerTitleDark]}>
209
+ {title}
210
+ </Text>
211
+ </View>
212
+ {renderStepDots()}
162
213
  </View>
163
- {renderStepDots()}
164
- </View>
214
+ {headerBanner ? (
215
+ <View style={[styles.headerBannerSlot, isDark && styles.headerBannerSlotDark]}>
216
+ {headerBanner}
217
+ </View>
218
+ ) : null}
219
+ </>
165
220
  ) : null}
166
221
 
167
222
  {/* ── Step 1: Map ── */}
@@ -172,6 +227,7 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
172
227
  defaultLng={defaultLng}
173
228
  mapHeight={mapHeight}
174
229
  theme={theme}
230
+ brandColor={brandColor}
175
231
  />
176
232
  ) : null}
177
233
 
@@ -192,11 +248,22 @@ const CheckoutWidget: React.FC<CheckoutWidgetProps> = ({
192
248
 
193
249
  {/* ── Step 3: Done ── */}
194
250
  {step === 'done' && completedAddress ? (
195
- <SuccessScreen
196
- address={completedAddress}
197
- onEditAddress={handleEditAddress}
198
- isDark={isDark}
199
- />
251
+ <>
252
+ <SuccessScreen
253
+ address={completedAddress}
254
+ onEditAddress={handleEditAddress}
255
+ isDark={isDark}
256
+ />
257
+ {enableCod && merchantId && supabaseFunctionBaseUrl && codSession ? (
258
+ <CodAvailabilityBanner
259
+ checkoutConfig={checkoutConfig}
260
+ sessionToken={codSession}
261
+ pincode={completedAddress.pincode}
262
+ cartTotalPaise={codCartTotalPaise}
263
+ theme={theme}
264
+ />
265
+ ) : null}
266
+ </>
200
267
  ) : null}
201
268
 
202
269
  {/* Powered-by footer */}