@quantaroute/checkout 1.3.2 → 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/README.md +345 -0
- package/dist/lib/index.d.ts +481 -8
- package/dist/lib/quantaroute-checkout.es.js +238 -1285
- package/dist/lib/quantaroute-checkout.umd.js +211 -64
- package/dist/style.css +1 -1
- package/package.json +2 -1
- package/src/components/AddressForm.native.tsx +56 -1
- package/src/components/AddressForm.tsx +55 -1
- package/src/components/CheckoutWidget.native.tsx +88 -21
- package/src/components/CheckoutWidget.tsx +579 -47
- package/src/components/CodAvailabilityBanner.native.tsx +108 -0
- package/src/components/CodAvailabilityBanner.tsx +104 -0
- package/src/components/EmailOtpStep.tsx +198 -0
- package/src/components/EmailStep.tsx +108 -0
- package/src/components/MapPinSelector.native.tsx +35 -24
- package/src/components/MapPinSelector.tsx +7 -4
- package/src/components/OtpStep.tsx +227 -0
- package/src/components/PaymentStep.tsx +162 -0
- package/src/components/PhoneStep.tsx +124 -0
- package/src/components/PrepaidNudge.tsx +31 -0
- package/src/components/SavedAddressStep.tsx +281 -0
- package/src/core/api.ts +86 -0
- package/src/core/checkout-api.ts +458 -0
- package/src/core/device.ts +47 -0
- package/src/core/types.ts +217 -3
- package/src/core/utm.ts +29 -0
- package/src/index.ts +65 -1
- package/src/styles/checkout.css +614 -0
- package/src/styles/checkout.native.ts +144 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
// ─── Configuration ────────────────────────────────────────────────────────────
|
|
2
|
+
|
|
3
|
+
export interface CheckoutApiConfig {
|
|
4
|
+
/** Supabase Edge Function base URL, e.g. https://<project>.supabase.co/functions/v1 */
|
|
5
|
+
functionBaseUrl: string;
|
|
6
|
+
/** UUID of the merchant from checkout.merchants */
|
|
7
|
+
merchantId: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** Device + UTM context sent with OTP verify to enrich checkout sessions. */
|
|
11
|
+
export interface SessionContextPayload {
|
|
12
|
+
utm_source?: string;
|
|
13
|
+
utm_medium?: string;
|
|
14
|
+
utm_campaign?: string;
|
|
15
|
+
device_type?: string;
|
|
16
|
+
platform?: string;
|
|
17
|
+
user_agent?: string;
|
|
18
|
+
experiment_variant?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// ─── OTP ──────────────────────────────────────────────────────────────────────
|
|
22
|
+
|
|
23
|
+
/** Send OTP to the given mobile number. Throws on failure. */
|
|
24
|
+
export async function sendOtp(config: CheckoutApiConfig, mobile: string): Promise<void> {
|
|
25
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-otp`, {
|
|
26
|
+
method: 'POST',
|
|
27
|
+
headers: { 'Content-Type': 'application/json' },
|
|
28
|
+
body: JSON.stringify({ action: 'send', mobile, merchant_id: config.merchantId }),
|
|
29
|
+
});
|
|
30
|
+
const data = await res.json().catch(() => ({}));
|
|
31
|
+
if (!res.ok || !data.success) {
|
|
32
|
+
throw new Error(data.error ?? 'Failed to send OTP. Please try again.');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface VerifyOtpResult {
|
|
37
|
+
session_token: string;
|
|
38
|
+
session_id: string;
|
|
39
|
+
expires_at: string;
|
|
40
|
+
buyer_id: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Verify OTP and return a session token. Throws on invalid OTP. */
|
|
44
|
+
export async function verifyOtp(
|
|
45
|
+
config: CheckoutApiConfig,
|
|
46
|
+
mobile: string,
|
|
47
|
+
otp: string,
|
|
48
|
+
context?: SessionContextPayload
|
|
49
|
+
): Promise<VerifyOtpResult> {
|
|
50
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-otp`, {
|
|
51
|
+
method: 'POST',
|
|
52
|
+
headers: { 'Content-Type': 'application/json' },
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
action: 'verify',
|
|
55
|
+
mobile,
|
|
56
|
+
otp,
|
|
57
|
+
merchant_id: config.merchantId,
|
|
58
|
+
...context,
|
|
59
|
+
}),
|
|
60
|
+
});
|
|
61
|
+
const data = await res.json().catch(() => ({}));
|
|
62
|
+
if (!res.ok || !data.success) {
|
|
63
|
+
throw new Error(data.error ?? 'Invalid or expired OTP.');
|
|
64
|
+
}
|
|
65
|
+
return data as VerifyOtpResult;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ─── Saved Addresses ──────────────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
export interface SavedAddress {
|
|
71
|
+
id: string;
|
|
72
|
+
digipin: string;
|
|
73
|
+
lat: number;
|
|
74
|
+
lng: number;
|
|
75
|
+
state: string;
|
|
76
|
+
district: string;
|
|
77
|
+
locality: string;
|
|
78
|
+
pincode: string;
|
|
79
|
+
delivery_status: string;
|
|
80
|
+
flat_number: string;
|
|
81
|
+
floor_number: string;
|
|
82
|
+
building_name: string;
|
|
83
|
+
street_name: string;
|
|
84
|
+
label: string;
|
|
85
|
+
is_default: boolean;
|
|
86
|
+
created_at: string;
|
|
87
|
+
updated_at: string;
|
|
88
|
+
/** Brand name of the merchant where this address was first saved. Null if unknown. */
|
|
89
|
+
saved_at_brand: string | null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export interface SavedAddressPayload {
|
|
93
|
+
digipin: string;
|
|
94
|
+
lat: number;
|
|
95
|
+
lng: number;
|
|
96
|
+
state?: string;
|
|
97
|
+
district?: string;
|
|
98
|
+
locality?: string;
|
|
99
|
+
pincode?: string;
|
|
100
|
+
delivery_status?: string;
|
|
101
|
+
flat_number?: string;
|
|
102
|
+
floor_number?: string;
|
|
103
|
+
building_name?: string;
|
|
104
|
+
street_name?: string;
|
|
105
|
+
full_address_json?: Record<string, unknown>;
|
|
106
|
+
label?: string;
|
|
107
|
+
is_default?: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function addressHeaders(sessionToken: string) {
|
|
111
|
+
return { 'Content-Type': 'application/json', 'x-session-token': sessionToken };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** Fetch all saved addresses for the authenticated buyer. */
|
|
115
|
+
export async function listAddresses(
|
|
116
|
+
config: CheckoutApiConfig,
|
|
117
|
+
sessionToken: string
|
|
118
|
+
): Promise<SavedAddress[]> {
|
|
119
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
120
|
+
method: 'POST',
|
|
121
|
+
headers: addressHeaders(sessionToken),
|
|
122
|
+
body: JSON.stringify({ action: 'list' }),
|
|
123
|
+
});
|
|
124
|
+
const data = await res.json().catch(() => ({}));
|
|
125
|
+
if (!res.ok || !data.success) {
|
|
126
|
+
throw new Error(data.error ?? 'Failed to load saved addresses.');
|
|
127
|
+
}
|
|
128
|
+
return (data.data ?? []) as SavedAddress[];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Save a new address for the authenticated buyer. */
|
|
132
|
+
export async function createAddress(
|
|
133
|
+
config: CheckoutApiConfig,
|
|
134
|
+
sessionToken: string,
|
|
135
|
+
address: SavedAddressPayload
|
|
136
|
+
): Promise<SavedAddress> {
|
|
137
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
138
|
+
method: 'POST',
|
|
139
|
+
headers: addressHeaders(sessionToken),
|
|
140
|
+
body: JSON.stringify({ action: 'create', address }),
|
|
141
|
+
});
|
|
142
|
+
const data = await res.json().catch(() => ({}));
|
|
143
|
+
if (!res.ok || !data.success) {
|
|
144
|
+
throw new Error(data.error ?? 'Failed to save address.');
|
|
145
|
+
}
|
|
146
|
+
return data.data as SavedAddress;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Soft-delete a saved address. */
|
|
150
|
+
export async function deleteAddress(
|
|
151
|
+
config: CheckoutApiConfig,
|
|
152
|
+
sessionToken: string,
|
|
153
|
+
addressId: string
|
|
154
|
+
): Promise<void> {
|
|
155
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
156
|
+
method: 'POST',
|
|
157
|
+
headers: addressHeaders(sessionToken),
|
|
158
|
+
body: JSON.stringify({ action: 'delete', address_id: addressId }),
|
|
159
|
+
});
|
|
160
|
+
const data = await res.json().catch(() => ({}));
|
|
161
|
+
if (!res.ok || !data.success) {
|
|
162
|
+
throw new Error(data.error ?? 'Failed to delete address.');
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Set an address as the default. */
|
|
167
|
+
export async function setDefaultAddress(
|
|
168
|
+
config: CheckoutApiConfig,
|
|
169
|
+
sessionToken: string,
|
|
170
|
+
addressId: string
|
|
171
|
+
): Promise<void> {
|
|
172
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
173
|
+
method: 'POST',
|
|
174
|
+
headers: addressHeaders(sessionToken),
|
|
175
|
+
body: JSON.stringify({ action: 'set_default', address_id: addressId }),
|
|
176
|
+
});
|
|
177
|
+
const data = await res.json().catch(() => ({}));
|
|
178
|
+
if (!res.ok || !data.success) {
|
|
179
|
+
throw new Error(data.error ?? 'Failed to set default address.');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// ─── Webhook: address completed ──────────────────────────────────────────────
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Notify the server that the buyer has confirmed their delivery address.
|
|
187
|
+
* Triggers `session.address_completed` webhook on the merchant's configured URL.
|
|
188
|
+
* Call this once `onComplete` fires in the widget (or from your own checkout page).
|
|
189
|
+
*/
|
|
190
|
+
export async function notifyAddressCompleted(
|
|
191
|
+
config: CheckoutApiConfig,
|
|
192
|
+
sessionToken: string,
|
|
193
|
+
address: { pincode: string; digipin: string; state: string; district: string; locality: string }
|
|
194
|
+
): Promise<void> {
|
|
195
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
196
|
+
method: 'POST',
|
|
197
|
+
headers: addressHeaders(sessionToken),
|
|
198
|
+
body: JSON.stringify({ action: 'address_completed', data: address }),
|
|
199
|
+
});
|
|
200
|
+
const data = await res.json().catch(() => ({}));
|
|
201
|
+
if (!res.ok || !data.success) {
|
|
202
|
+
// Non-blocking: log but don't throw — this is analytics, not critical path
|
|
203
|
+
console.warn('[checkout-api] notifyAddressCompleted failed:', data.error);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// ─── Email OTP auth ───────────────────────────────────────────────────────────
|
|
208
|
+
|
|
209
|
+
/** Send a 6-digit OTP to the given email via Resend (Edge: `checkout-email-otp`). */
|
|
210
|
+
export async function sendEmailOtp(
|
|
211
|
+
config: CheckoutApiConfig,
|
|
212
|
+
email: string
|
|
213
|
+
): Promise<void> {
|
|
214
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-email-otp`, {
|
|
215
|
+
method: 'POST',
|
|
216
|
+
headers: { 'Content-Type': 'application/json' },
|
|
217
|
+
body: JSON.stringify({ action: 'send', email, merchant_id: config.merchantId }),
|
|
218
|
+
});
|
|
219
|
+
const data = await res.json().catch(() => ({}));
|
|
220
|
+
if (!res.ok || !data.success) {
|
|
221
|
+
throw new Error(data.error ?? 'Failed to send OTP. Please try again.');
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export interface VerifyEmailOtpResult {
|
|
226
|
+
session_token: string;
|
|
227
|
+
session_id: string;
|
|
228
|
+
expires_at: string;
|
|
229
|
+
buyer_id: string;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/** Verify a 6-digit email OTP. Returns a session token on success. */
|
|
233
|
+
export async function verifyEmailOtp(
|
|
234
|
+
config: CheckoutApiConfig,
|
|
235
|
+
email: string,
|
|
236
|
+
code: string,
|
|
237
|
+
context?: SessionContextPayload
|
|
238
|
+
): Promise<VerifyEmailOtpResult> {
|
|
239
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-email-otp`, {
|
|
240
|
+
method: 'POST',
|
|
241
|
+
headers: { 'Content-Type': 'application/json' },
|
|
242
|
+
body: JSON.stringify({
|
|
243
|
+
action: 'verify',
|
|
244
|
+
email,
|
|
245
|
+
code,
|
|
246
|
+
merchant_id: config.merchantId,
|
|
247
|
+
...context,
|
|
248
|
+
}),
|
|
249
|
+
});
|
|
250
|
+
const data = await res.json().catch(() => ({}));
|
|
251
|
+
if (!res.ok || !data.success) {
|
|
252
|
+
throw new Error(data.error ?? 'Invalid or expired code.');
|
|
253
|
+
}
|
|
254
|
+
return data as VerifyEmailOtpResult;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// ─── COD eligibility (merchant pincode blocklist) ─────────────────────────────
|
|
258
|
+
|
|
259
|
+
export interface CodEligibilityResult {
|
|
260
|
+
/** Whether COD is allowed for this pincode for the session merchant. */
|
|
261
|
+
cod_allowed: boolean;
|
|
262
|
+
/** Normalized 6-digit pincode when valid; otherwise the trimmed input. */
|
|
263
|
+
pincode: string;
|
|
264
|
+
/** Present when COD is not allowed or the pincode could not be normalized. */
|
|
265
|
+
reason?: 'cod_disabled' | 'pincode_blocked' | 'invalid_pincode' | 'cart_value_exceeded';
|
|
266
|
+
/** Merchant COD cart cap in paise; null when unlimited. */
|
|
267
|
+
cod_max_cart_value_paise?: number | null;
|
|
268
|
+
/** Echo of `cartTotalPaise` when sent. */
|
|
269
|
+
cart_total_paise?: number;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Check COD availability for a delivery pincode (Edge: `cod_eligibility`).
|
|
274
|
+
* Respects `checkout.merchants.enable_cod` (C4), `cod_blocked_pincodes` / `cod_max_cart_value_paise` (D1/D2).
|
|
275
|
+
* Pass `cartTotalPaise` in paise (e.g. ₹499 → 49900) to enforce the cart cap when set.
|
|
276
|
+
*/
|
|
277
|
+
export async function checkCodEligibility(
|
|
278
|
+
config: CheckoutApiConfig,
|
|
279
|
+
sessionToken: string,
|
|
280
|
+
pincode: string,
|
|
281
|
+
cartTotalPaise?: number
|
|
282
|
+
): Promise<CodEligibilityResult> {
|
|
283
|
+
const body: Record<string, unknown> = { action: 'cod_eligibility', pincode };
|
|
284
|
+
if (cartTotalPaise !== undefined) {
|
|
285
|
+
body.cart_total_paise = cartTotalPaise;
|
|
286
|
+
}
|
|
287
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
288
|
+
method: 'POST',
|
|
289
|
+
headers: addressHeaders(sessionToken),
|
|
290
|
+
body: JSON.stringify(body),
|
|
291
|
+
});
|
|
292
|
+
const data = await res.json().catch(() => ({}));
|
|
293
|
+
if (!res.ok || !data.success) {
|
|
294
|
+
throw new Error(data.error ?? 'Failed to check COD eligibility.');
|
|
295
|
+
}
|
|
296
|
+
return data.data as CodEligibilityResult;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// ─── Funnel events (server-side analytics) ────────────────────────────────────
|
|
300
|
+
|
|
301
|
+
export interface FunnelEventPayload {
|
|
302
|
+
event_type: string;
|
|
303
|
+
step?: string;
|
|
304
|
+
session_token?: string;
|
|
305
|
+
buyer_id?: string;
|
|
306
|
+
auth_channel?: string;
|
|
307
|
+
device_type?: string;
|
|
308
|
+
platform?: string;
|
|
309
|
+
user_agent?: string;
|
|
310
|
+
payment_method?: string;
|
|
311
|
+
experiment_variant?: string;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** Log a checkout funnel event to the server (fire-and-forget). */
|
|
315
|
+
export async function logFunnelEvent(
|
|
316
|
+
config: CheckoutApiConfig,
|
|
317
|
+
payload: FunnelEventPayload,
|
|
318
|
+
sessionToken?: string
|
|
319
|
+
): Promise<void> {
|
|
320
|
+
try {
|
|
321
|
+
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
|
|
322
|
+
if (sessionToken) headers['x-session-token'] = sessionToken;
|
|
323
|
+
await fetch(`${config.functionBaseUrl}/checkout-funnel-events`, {
|
|
324
|
+
method: 'POST',
|
|
325
|
+
headers,
|
|
326
|
+
body: JSON.stringify({ merchant_id: config.merchantId, ...payload }),
|
|
327
|
+
});
|
|
328
|
+
} catch {
|
|
329
|
+
// Non-blocking analytics
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** Signal explicit session abandonment (page exit). */
|
|
334
|
+
export async function notifySessionAbandoned(
|
|
335
|
+
config: CheckoutApiConfig,
|
|
336
|
+
sessionToken: string,
|
|
337
|
+
lastStep: string
|
|
338
|
+
): Promise<void> {
|
|
339
|
+
try {
|
|
340
|
+
await fetch(`${config.functionBaseUrl}/checkout-addresses`, {
|
|
341
|
+
method: 'POST',
|
|
342
|
+
headers: addressHeaders(sessionToken),
|
|
343
|
+
body: JSON.stringify({ action: 'abandon', last_step: lastStep }),
|
|
344
|
+
});
|
|
345
|
+
} catch {
|
|
346
|
+
// Non-blocking
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// ─── UTM offers ───────────────────────────────────────────────────────────────
|
|
351
|
+
|
|
352
|
+
export interface SessionOffer {
|
|
353
|
+
offer_id: string;
|
|
354
|
+
name: string;
|
|
355
|
+
banner_copy: string;
|
|
356
|
+
banner_cta_url: string | null;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** Fetch UTM-matched offer banner for the current session. */
|
|
360
|
+
export async function fetchSessionOffer(
|
|
361
|
+
config: CheckoutApiConfig,
|
|
362
|
+
sessionToken: string
|
|
363
|
+
): Promise<SessionOffer | null> {
|
|
364
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-offers`, {
|
|
365
|
+
method: 'GET',
|
|
366
|
+
headers: addressHeaders(sessionToken),
|
|
367
|
+
});
|
|
368
|
+
const data = await res.json().catch(() => ({}));
|
|
369
|
+
if (!res.ok || !data.success) return null;
|
|
370
|
+
return (data.data as SessionOffer | null) ?? null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// ─── Payment (Razorpay) ───────────────────────────────────────────────────────
|
|
374
|
+
|
|
375
|
+
export interface CreatePaymentOrderResult {
|
|
376
|
+
razorpay_order_id: string;
|
|
377
|
+
razorpay_key_id: string;
|
|
378
|
+
amount_paise: number;
|
|
379
|
+
currency: string;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export async function createPaymentOrder(
|
|
383
|
+
config: CheckoutApiConfig,
|
|
384
|
+
sessionToken: string,
|
|
385
|
+
amountPaise: number
|
|
386
|
+
): Promise<CreatePaymentOrderResult> {
|
|
387
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-payment`, {
|
|
388
|
+
method: 'POST',
|
|
389
|
+
headers: addressHeaders(sessionToken),
|
|
390
|
+
body: JSON.stringify({ action: 'create_order', amount_paise: amountPaise }),
|
|
391
|
+
});
|
|
392
|
+
const data = await res.json().catch(() => ({}));
|
|
393
|
+
if (!res.ok || !data.success) {
|
|
394
|
+
throw new Error(data.error ?? 'Failed to create payment order.');
|
|
395
|
+
}
|
|
396
|
+
return data.data as CreatePaymentOrderResult;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
export async function verifyPayment(
|
|
400
|
+
config: CheckoutApiConfig,
|
|
401
|
+
sessionToken: string,
|
|
402
|
+
params: {
|
|
403
|
+
razorpay_order_id: string;
|
|
404
|
+
razorpay_payment_id: string;
|
|
405
|
+
razorpay_signature: string;
|
|
406
|
+
payment_method?: string;
|
|
407
|
+
}
|
|
408
|
+
): Promise<{ payment_method: string }> {
|
|
409
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-payment`, {
|
|
410
|
+
method: 'POST',
|
|
411
|
+
headers: addressHeaders(sessionToken),
|
|
412
|
+
body: JSON.stringify({ action: 'verify', ...params }),
|
|
413
|
+
});
|
|
414
|
+
const data = await res.json().catch(() => ({}));
|
|
415
|
+
if (!res.ok || !data.success) {
|
|
416
|
+
throw new Error(data.error ?? 'Payment verification failed.');
|
|
417
|
+
}
|
|
418
|
+
return { payment_method: data.payment_method as string };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
export async function recordCodPayment(
|
|
422
|
+
config: CheckoutApiConfig,
|
|
423
|
+
sessionToken: string
|
|
424
|
+
): Promise<{ payment_method: string }> {
|
|
425
|
+
const res = await fetch(`${config.functionBaseUrl}/checkout-payment`, {
|
|
426
|
+
method: 'POST',
|
|
427
|
+
headers: addressHeaders(sessionToken),
|
|
428
|
+
body: JSON.stringify({ action: 'record_cod' }),
|
|
429
|
+
});
|
|
430
|
+
const data = await res.json().catch(() => ({}));
|
|
431
|
+
if (!res.ok || !data.success) {
|
|
432
|
+
throw new Error(data.error ?? 'Failed to record COD selection.');
|
|
433
|
+
}
|
|
434
|
+
return { payment_method: data.payment_method as string };
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// ─── Merchant prepaid nudge config ────────────────────────────────────────────
|
|
438
|
+
|
|
439
|
+
export interface PrepaidNudgeConfig {
|
|
440
|
+
prepaid_nudge_copy: string | null;
|
|
441
|
+
prepaid_nudge_cta_url: string | null;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Fetch merchant prepaid nudge copy via Admin API (optional; use props to avoid). */
|
|
445
|
+
export async function fetchPrepaidNudgeConfig(
|
|
446
|
+
functionBaseUrl: string,
|
|
447
|
+
adminApiKey: string
|
|
448
|
+
): Promise<PrepaidNudgeConfig | null> {
|
|
449
|
+
const res = await fetch(`${functionBaseUrl}/checkout-admin?action=cod_config`, {
|
|
450
|
+
headers: { 'x-api-key': adminApiKey },
|
|
451
|
+
});
|
|
452
|
+
const data = await res.json().catch(() => ({}));
|
|
453
|
+
if (!res.ok || !data.success) return null;
|
|
454
|
+
return {
|
|
455
|
+
prepaid_nudge_copy: data.data.prepaid_nudge_copy,
|
|
456
|
+
prepaid_nudge_cta_url: data.data.prepaid_nudge_cta_url,
|
|
457
|
+
};
|
|
458
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Device / platform metadata attached to checkout analytics events. */
|
|
2
|
+
|
|
3
|
+
export type DeviceType = 'mobile' | 'tablet' | 'desktop';
|
|
4
|
+
export type CheckoutPlatform = 'web' | 'ios' | 'android';
|
|
5
|
+
|
|
6
|
+
/** Detect coarse device class from viewport / user agent (web only). */
|
|
7
|
+
export function detectDeviceType(userAgent?: string): DeviceType {
|
|
8
|
+
if (typeof window !== 'undefined') {
|
|
9
|
+
const w = window.innerWidth;
|
|
10
|
+
if (w < 768) return 'mobile';
|
|
11
|
+
if (w < 1024) return 'tablet';
|
|
12
|
+
return 'desktop';
|
|
13
|
+
}
|
|
14
|
+
const ua = (userAgent ?? '').toLowerCase();
|
|
15
|
+
if (/ipad|tablet/.test(ua)) return 'tablet';
|
|
16
|
+
if (/mobile|iphone|android/.test(ua)) return 'mobile';
|
|
17
|
+
return 'desktop';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Web checkout platform; native widgets pass platform explicitly. */
|
|
21
|
+
export function detectPlatform(): CheckoutPlatform {
|
|
22
|
+
return 'web';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Best-effort user agent string (empty on native when unavailable). */
|
|
26
|
+
export function getUserAgent(): string | undefined {
|
|
27
|
+
if (typeof navigator !== 'undefined' && navigator.userAgent) {
|
|
28
|
+
return navigator.userAgent.slice(0, 512);
|
|
29
|
+
}
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface DeviceContext {
|
|
34
|
+
deviceType: DeviceType;
|
|
35
|
+
platform: CheckoutPlatform;
|
|
36
|
+
userAgent?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Build device context for web embeds. */
|
|
40
|
+
export function getWebDeviceContext(): DeviceContext {
|
|
41
|
+
const userAgent = getUserAgent();
|
|
42
|
+
return {
|
|
43
|
+
deviceType: detectDeviceType(userAgent),
|
|
44
|
+
platform: detectPlatform(),
|
|
45
|
+
userAgent,
|
|
46
|
+
};
|
|
47
|
+
}
|