@quantaroute/checkout 1.7.3 → 1.8.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/CHANGELOG.md +24 -0
- package/README.md +60 -1
- package/dist/embed/quantaroute-checkout.css +1 -0
- package/dist/embed/quantaroute-checkout.js +28 -0
- package/dist/lib/index.d.ts +20 -1
- package/dist/lib/quantaroute-checkout.es.js +146 -131
- package/dist/lib/quantaroute-checkout.umd.js +148 -133
- package/package.json +12 -6
- package/src/components/AddressForm.native.tsx +3 -3
- package/src/components/AddressForm.tsx +3 -3
- package/src/components/CheckoutWidget.tsx +48 -14
- package/src/components/MapPinSelector.native.tsx +14 -3
- package/src/components/MapPinSelector.tsx +23 -2
- package/src/components/PaymentStep.tsx +36 -8
- package/src/core/map-runtime.ts +22 -0
- package/src/core/types.ts +13 -1
- package/src/embed/handoff.test.ts +86 -0
- package/src/embed/handoff.ts +132 -0
- package/src/embed/index.tsx +380 -0
- package/src/index.ts +2 -0
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure helpers for the script-tag embed. No DOM and no React, so they can be
|
|
3
|
+
* unit tested on their own.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** What the embed hands to the merchant when the shopper finishes. */
|
|
7
|
+
export type HandoffPayload = {
|
|
8
|
+
/** Checkout session id. Verify it on your server with the admin API. */
|
|
9
|
+
sessionId: string;
|
|
10
|
+
digipin: string;
|
|
11
|
+
lat: number;
|
|
12
|
+
lng: number;
|
|
13
|
+
pincode: string;
|
|
14
|
+
state: string;
|
|
15
|
+
district: string;
|
|
16
|
+
locality: string;
|
|
17
|
+
flatNumber: string;
|
|
18
|
+
floorNumber: string;
|
|
19
|
+
buildingName: string;
|
|
20
|
+
streetName: string;
|
|
21
|
+
formattedAddress: string;
|
|
22
|
+
addressId?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const PLACEHOLDER = /\{\{\s*([a-zA-Z]+)\s*\}\}|\{\s*([a-zA-Z]+)\s*\}/g;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Fill `{name}` or `{{name}}` placeholders in a redirect URL with the
|
|
29
|
+
* (URL-encoded) handoff values. Unknown placeholders are left empty.
|
|
30
|
+
*
|
|
31
|
+
* Example: https://shop.example/pay?sid={sessionId}&pin={pincode}
|
|
32
|
+
*/
|
|
33
|
+
export function buildRedirectUrl(
|
|
34
|
+
template: string,
|
|
35
|
+
payload: Partial<HandoffPayload> & Record<string, unknown>
|
|
36
|
+
): string {
|
|
37
|
+
return template.replace(PLACEHOLDER, (_match, doubleKey: string, singleKey: string) => {
|
|
38
|
+
const value = payload[doubleKey ?? singleKey];
|
|
39
|
+
return value === undefined || value === null ? '' : encodeURIComponent(String(value));
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `"#a, .b"` -> `["#a", ".b"]`. Selectors may themselves contain commas inside brackets. */
|
|
44
|
+
export function parseSelectors(value: string | string[] | undefined | null): string[] {
|
|
45
|
+
if (!value) return [];
|
|
46
|
+
const list = Array.isArray(value) ? value : splitTopLevel(value);
|
|
47
|
+
return list.map((item) => item.trim()).filter(Boolean);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function splitTopLevel(value: string): string[] {
|
|
51
|
+
const out: string[] = [];
|
|
52
|
+
let depth = 0;
|
|
53
|
+
let quote: string | null = null;
|
|
54
|
+
let current = '';
|
|
55
|
+
for (const char of value) {
|
|
56
|
+
if (quote) {
|
|
57
|
+
current += char;
|
|
58
|
+
if (char === quote) quote = null;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if (char === '"' || char === "'") {
|
|
62
|
+
quote = char;
|
|
63
|
+
current += char;
|
|
64
|
+
} else if (char === '[' || char === '(') {
|
|
65
|
+
depth += 1;
|
|
66
|
+
current += char;
|
|
67
|
+
} else if (char === ']' || char === ')') {
|
|
68
|
+
depth = Math.max(0, depth - 1);
|
|
69
|
+
current += char;
|
|
70
|
+
} else if (char === ',' && depth === 0) {
|
|
71
|
+
out.push(current);
|
|
72
|
+
current = '';
|
|
73
|
+
} else {
|
|
74
|
+
current += char;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
out.push(current);
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export type ScriptConfig = {
|
|
82
|
+
merchantId?: string;
|
|
83
|
+
apiKey?: string;
|
|
84
|
+
mapsKey?: string;
|
|
85
|
+
functionsUrl?: string;
|
|
86
|
+
triggers?: string[];
|
|
87
|
+
redirectUrl?: string;
|
|
88
|
+
brandColor?: string;
|
|
89
|
+
theme?: 'light' | 'dark';
|
|
90
|
+
title?: string;
|
|
91
|
+
logoUrl?: string;
|
|
92
|
+
authMode?: 'email' | 'phone' | 'none';
|
|
93
|
+
autoInit: boolean;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const truthy = (value: string | undefined) =>
|
|
97
|
+
value !== undefined && !['false', '0', 'no', 'off'].includes(value.toLowerCase());
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Read embed options from the data-* attributes of the script tag:
|
|
101
|
+
*
|
|
102
|
+
* <script src=".../quantaroute-checkout.js"
|
|
103
|
+
* data-merchant-id="…" data-api-key="dp_…" data-maps-key="dp_…"
|
|
104
|
+
* data-functions-url="https://….supabase.co/functions/v1"
|
|
105
|
+
* data-trigger="#checkout-button" data-redirect-url="https://shop/pay?sid={sessionId}">
|
|
106
|
+
* </script>
|
|
107
|
+
*
|
|
108
|
+
* `autoInit` is true when the tag carries enough to start (a merchant id and
|
|
109
|
+
* an API key); otherwise call QuantaRouteCheckout.init() yourself.
|
|
110
|
+
*/
|
|
111
|
+
export function readScriptConfig(data: Record<string, string | undefined>): ScriptConfig {
|
|
112
|
+
const authMode = (data.auth ?? data.authMode ?? '').toLowerCase();
|
|
113
|
+
const theme = (data.theme ?? '').toLowerCase();
|
|
114
|
+
const config: ScriptConfig = {
|
|
115
|
+
merchantId: data.merchantId,
|
|
116
|
+
apiKey: data.apiKey,
|
|
117
|
+
mapsKey: data.mapsKey,
|
|
118
|
+
functionsUrl: data.functionsUrl,
|
|
119
|
+
triggers: parseSelectors(data.trigger ?? data.triggers),
|
|
120
|
+
redirectUrl: data.redirectUrl,
|
|
121
|
+
brandColor: data.brandColor,
|
|
122
|
+
theme: theme === 'dark' ? 'dark' : theme === 'light' ? 'light' : undefined,
|
|
123
|
+
title: data.title,
|
|
124
|
+
logoUrl: data.logoUrl,
|
|
125
|
+
authMode:
|
|
126
|
+
authMode === 'phone' || authMode === 'none' || authMode === 'email' ? authMode : undefined,
|
|
127
|
+
autoInit: false,
|
|
128
|
+
};
|
|
129
|
+
config.autoInit =
|
|
130
|
+
Boolean(config.merchantId && config.apiKey) && truthy(data.autoInit ?? 'true');
|
|
131
|
+
return config;
|
|
132
|
+
}
|
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* QuantaRoute Checkout — script-tag embed.
|
|
3
|
+
*
|
|
4
|
+
* For merchants who are not on React and not on Shopify (WooCommerce, Wix,
|
|
5
|
+
* custom PHP/Rails/Laravel stores, static sites). One <script> tag opens the
|
|
6
|
+
* checkout as a modal when the shopper clicks the merchant's own Checkout
|
|
7
|
+
* button. The shopper signs in, picks or pins a verified address, and the
|
|
8
|
+
* result is handed back to the merchant, who takes payment with their OWN
|
|
9
|
+
* payment gateway. QuantaRoute never touches payment in this mode.
|
|
10
|
+
*
|
|
11
|
+
* <script src="https://cdn.jsdelivr.net/npm/@quantaroute/checkout@1/dist/embed/quantaroute-checkout.js"
|
|
12
|
+
* data-merchant-id="…" data-api-key="dp_…" data-maps-key="dp_…"
|
|
13
|
+
* data-functions-url="https://….supabase.co/functions/v1"
|
|
14
|
+
* data-trigger="#checkout-button"
|
|
15
|
+
* data-redirect-url="https://yourshop.com/pay?session={sessionId}">
|
|
16
|
+
* </script>
|
|
17
|
+
*
|
|
18
|
+
* Or by hand: QuantaRouteCheckout.init({ …, onComplete(address) { … } }).
|
|
19
|
+
*/
|
|
20
|
+
import { createRoot, type Root } from 'react-dom/client';
|
|
21
|
+
import CheckoutWidget from '../components/CheckoutWidget';
|
|
22
|
+
import type { CheckoutEvent, CompleteAddress } from '../core/types';
|
|
23
|
+
import {
|
|
24
|
+
buildRedirectUrl,
|
|
25
|
+
parseSelectors,
|
|
26
|
+
readScriptConfig,
|
|
27
|
+
type HandoffPayload,
|
|
28
|
+
} from './handoff';
|
|
29
|
+
|
|
30
|
+
export type { HandoffPayload } from './handoff';
|
|
31
|
+
|
|
32
|
+
export interface EmbedOptions {
|
|
33
|
+
/** Merchant id from the developer portal. */
|
|
34
|
+
merchantId: string;
|
|
35
|
+
/** Public dp_ API key. Safe in the browser. */
|
|
36
|
+
apiKey: string;
|
|
37
|
+
/** Public dp_ key with the Maps scope (usually the same key). */
|
|
38
|
+
mapsKey?: string;
|
|
39
|
+
/** Base URL of the checkout functions, e.g. https://xyz.supabase.co/functions/v1 */
|
|
40
|
+
functionsUrl: string;
|
|
41
|
+
/** CSS selector(s) of the merchant's Checkout button(s). Clicks open the checkout. */
|
|
42
|
+
triggers?: string | string[];
|
|
43
|
+
/** Where to send the shopper afterwards. Supports {sessionId}, {pincode}, {digipin}, … */
|
|
44
|
+
redirectUrl?: string;
|
|
45
|
+
/** Called with the verified address (and session id) when the shopper finishes. */
|
|
46
|
+
onComplete?: (payload: HandoffPayload) => void;
|
|
47
|
+
onError?: (error: Error) => void;
|
|
48
|
+
onEvent?: (event: CheckoutEvent) => void;
|
|
49
|
+
/** Called when the shopper closes the checkout without finishing. */
|
|
50
|
+
onClose?: () => void;
|
|
51
|
+
brandColor?: string;
|
|
52
|
+
logoUrl?: string;
|
|
53
|
+
theme?: 'light' | 'dark';
|
|
54
|
+
title?: string;
|
|
55
|
+
authMode?: 'email' | 'phone' | 'none';
|
|
56
|
+
/** Keep the modal open after completion. Default: closes unless redirecting. */
|
|
57
|
+
keepOpen?: boolean;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface EmbedController {
|
|
61
|
+
open: () => void;
|
|
62
|
+
close: () => void;
|
|
63
|
+
destroy: () => void;
|
|
64
|
+
/** Session id of the current checkout, once the shopper has signed in. */
|
|
65
|
+
getSessionId: () => string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const VERSION = '1.8.0';
|
|
69
|
+
const MAPLIBRE_JS = 'https://cdn.jsdelivr.net/npm/maplibre-gl@4.7.1/dist/maplibre-gl.js';
|
|
70
|
+
const MAPLIBRE_CSS = 'https://cdn.jsdelivr.net/npm/maplibre-gl@4.7.1/dist/maplibre-gl.css';
|
|
71
|
+
const STYLE_ID = 'qrco-style';
|
|
72
|
+
|
|
73
|
+
const MODAL_CSS = `
|
|
74
|
+
.qrco-overlay{position:fixed;inset:0;z-index:2147483000;display:flex;align-items:flex-start;justify-content:center;
|
|
75
|
+
padding:16px;background:rgba(15,23,42,.55);overflow-y:auto;-webkit-overflow-scrolling:touch}
|
|
76
|
+
.qrco-overlay[hidden]{display:none}
|
|
77
|
+
.qrco-dialog{position:relative;width:100%;max-width:480px;margin:auto;background:#fff;border-radius:16px;
|
|
78
|
+
box-shadow:0 24px 60px rgba(15,23,42,.35);overflow:hidden}
|
|
79
|
+
.qrco-close{position:absolute;top:10px;right:10px;z-index:2;width:34px;height:34px;border:0;border-radius:50%;
|
|
80
|
+
background:rgba(15,23,42,.08);color:#0f172a;font-size:20px;line-height:1;cursor:pointer}
|
|
81
|
+
.qrco-close:hover{background:rgba(15,23,42,.16)}
|
|
82
|
+
@media(max-width:520px){.qrco-overlay{padding:0}.qrco-dialog{max-width:none;min-height:100%;border-radius:0}}
|
|
83
|
+
`;
|
|
84
|
+
|
|
85
|
+
function ensureStylesheet(href: string, id: string) {
|
|
86
|
+
if (document.getElementById(id)) return;
|
|
87
|
+
const link = document.createElement('link');
|
|
88
|
+
link.id = id;
|
|
89
|
+
link.rel = 'stylesheet';
|
|
90
|
+
link.href = href;
|
|
91
|
+
document.head.appendChild(link);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function loadScript(src: string, id: string): Promise<void> {
|
|
95
|
+
return new Promise((resolve, reject) => {
|
|
96
|
+
const existing = document.getElementById(id) as HTMLScriptElement | null;
|
|
97
|
+
if (existing) {
|
|
98
|
+
if ((window as unknown as { maplibregl?: unknown }).maplibregl) resolve();
|
|
99
|
+
else {
|
|
100
|
+
existing.addEventListener('load', () => resolve());
|
|
101
|
+
existing.addEventListener('error', () => reject(new Error('maplibre failed to load')));
|
|
102
|
+
}
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const script = document.createElement('script');
|
|
106
|
+
script.id = id;
|
|
107
|
+
script.src = src;
|
|
108
|
+
script.async = true;
|
|
109
|
+
script.onload = () => resolve();
|
|
110
|
+
script.onerror = () => reject(new Error('maplibre failed to load'));
|
|
111
|
+
document.head.appendChild(script);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* document.currentScript only exists while the script is first executing, and
|
|
117
|
+
* init() usually runs later (from the merchant's own inline script), so read
|
|
118
|
+
* the script's URL once, here.
|
|
119
|
+
*/
|
|
120
|
+
const OWN_SCRIPT_SRC: string =
|
|
121
|
+
(document.currentScript as HTMLScriptElement | null)?.src ??
|
|
122
|
+
(document.querySelector('script[src*="quantaroute-checkout"]') as HTMLScriptElement | null)?.src ??
|
|
123
|
+
'';
|
|
124
|
+
|
|
125
|
+
/** The CSS file sits next to this script; derive it from the script's own URL. */
|
|
126
|
+
function widgetCssUrl(): string | null {
|
|
127
|
+
if (!OWN_SCRIPT_SRC) return null;
|
|
128
|
+
return OWN_SCRIPT_SRC.replace(
|
|
129
|
+
/quantaroute-checkout(\.min)?\.js(\?.*)?$/,
|
|
130
|
+
'quantaroute-checkout.css'
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
let mapReady: Promise<void> | null = null;
|
|
135
|
+
function loadMapRuntime(): Promise<void> {
|
|
136
|
+
if (!mapReady) {
|
|
137
|
+
ensureStylesheet(MAPLIBRE_CSS, 'qrco-maplibre-css');
|
|
138
|
+
mapReady = loadScript(MAPLIBRE_JS, 'qrco-maplibre-js').catch((error) => {
|
|
139
|
+
mapReady = null;
|
|
140
|
+
throw error;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
return mapReady;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function toPayload(address: CompleteAddress, sessionId: string): HandoffPayload {
|
|
147
|
+
return {
|
|
148
|
+
sessionId,
|
|
149
|
+
digipin: address.digipin,
|
|
150
|
+
lat: address.lat,
|
|
151
|
+
lng: address.lng,
|
|
152
|
+
pincode: address.pincode,
|
|
153
|
+
state: address.state,
|
|
154
|
+
district: address.district,
|
|
155
|
+
locality: address.locality,
|
|
156
|
+
flatNumber: address.flatNumber,
|
|
157
|
+
floorNumber: address.floorNumber,
|
|
158
|
+
buildingName: address.buildingName,
|
|
159
|
+
streetName: address.streetName,
|
|
160
|
+
formattedAddress: address.formattedAddress,
|
|
161
|
+
addressId: address.addressId,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function init(options: EmbedOptions): EmbedController {
|
|
166
|
+
if (!options?.merchantId || !options?.apiKey || !options?.functionsUrl) {
|
|
167
|
+
console.warn(
|
|
168
|
+
'[QuantaRouteCheckout] merchantId, apiKey and functionsUrl are required (data-merchant-id, data-api-key, data-functions-url).'
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (!document.getElementById(STYLE_ID)) {
|
|
173
|
+
const style = document.createElement('style');
|
|
174
|
+
style.id = STYLE_ID;
|
|
175
|
+
style.textContent = MODAL_CSS;
|
|
176
|
+
document.head.appendChild(style);
|
|
177
|
+
}
|
|
178
|
+
const cssUrl = widgetCssUrl();
|
|
179
|
+
if (cssUrl) ensureStylesheet(cssUrl, 'qrco-widget-css');
|
|
180
|
+
|
|
181
|
+
const triggers = parseSelectors(options.triggers);
|
|
182
|
+
let overlay: HTMLDivElement | null = null;
|
|
183
|
+
let mount: HTMLDivElement | null = null;
|
|
184
|
+
let root: Root | null = null;
|
|
185
|
+
let sessionId = '';
|
|
186
|
+
let opened = false;
|
|
187
|
+
let destroyed = false;
|
|
188
|
+
let runKey = 0;
|
|
189
|
+
|
|
190
|
+
const handleComplete = (address: CompleteAddress) => {
|
|
191
|
+
const payload = toPayload(address, sessionId);
|
|
192
|
+
try {
|
|
193
|
+
options.onComplete?.(payload);
|
|
194
|
+
} catch (error) {
|
|
195
|
+
console.error('[QuantaRouteCheckout] onComplete threw:', error);
|
|
196
|
+
}
|
|
197
|
+
window.dispatchEvent(new CustomEvent('quantaroute:complete', { detail: payload }));
|
|
198
|
+
|
|
199
|
+
if (options.redirectUrl) {
|
|
200
|
+
window.location.assign(buildRedirectUrl(options.redirectUrl, payload));
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
if (!options.keepOpen) close(false);
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const render = () => {
|
|
207
|
+
if (!root) return;
|
|
208
|
+
root.render(
|
|
209
|
+
<CheckoutWidget
|
|
210
|
+
key={runKey}
|
|
211
|
+
apiKey={options.apiKey}
|
|
212
|
+
merchantId={options.merchantId}
|
|
213
|
+
supabaseFunctionBaseUrl={options.functionsUrl}
|
|
214
|
+
mapsKey={options.mapsKey ?? options.apiKey}
|
|
215
|
+
embedMode="external"
|
|
216
|
+
enableEmailAuth={options.authMode !== 'phone' && options.authMode !== 'none'}
|
|
217
|
+
enablePhoneAuth={options.authMode === 'phone'}
|
|
218
|
+
brandColor={options.brandColor}
|
|
219
|
+
logoUrl={options.logoUrl}
|
|
220
|
+
theme={options.theme}
|
|
221
|
+
title={options.title}
|
|
222
|
+
onComplete={handleComplete}
|
|
223
|
+
onError={(error) => options.onError?.(error)}
|
|
224
|
+
onCheckoutEvent={(event) => {
|
|
225
|
+
if (event.sessionId) sessionId = event.sessionId;
|
|
226
|
+
options.onEvent?.(event);
|
|
227
|
+
window.dispatchEvent(new CustomEvent('quantaroute:event', { detail: event }));
|
|
228
|
+
}}
|
|
229
|
+
/>
|
|
230
|
+
);
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const build = () => {
|
|
234
|
+
if (overlay) return;
|
|
235
|
+
overlay = document.createElement('div');
|
|
236
|
+
overlay.className = 'qrco-overlay';
|
|
237
|
+
overlay.hidden = true;
|
|
238
|
+
overlay.setAttribute('role', 'dialog');
|
|
239
|
+
overlay.setAttribute('aria-modal', 'true');
|
|
240
|
+
overlay.addEventListener('mousedown', (event) => {
|
|
241
|
+
if (event.target === overlay) close(true);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
const dialog = document.createElement('div');
|
|
245
|
+
dialog.className = 'qrco-dialog';
|
|
246
|
+
|
|
247
|
+
const closeButton = document.createElement('button');
|
|
248
|
+
closeButton.type = 'button';
|
|
249
|
+
closeButton.className = 'qrco-close';
|
|
250
|
+
closeButton.setAttribute('aria-label', 'Close checkout');
|
|
251
|
+
closeButton.textContent = '×';
|
|
252
|
+
closeButton.addEventListener('click', () => close(true));
|
|
253
|
+
|
|
254
|
+
mount = document.createElement('div');
|
|
255
|
+
dialog.append(closeButton, mount);
|
|
256
|
+
overlay.appendChild(dialog);
|
|
257
|
+
document.body.appendChild(overlay);
|
|
258
|
+
root = createRoot(mount);
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
const onKeydown = (event: KeyboardEvent) => {
|
|
262
|
+
if (event.key === 'Escape' && opened) close(true);
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
function open() {
|
|
266
|
+
if (destroyed || opened) return;
|
|
267
|
+
build();
|
|
268
|
+
loadMapRuntime().catch((error) => options.onError?.(error));
|
|
269
|
+
// A fresh widget each time: the shopper may be back for a different cart.
|
|
270
|
+
runKey += 1;
|
|
271
|
+
render();
|
|
272
|
+
overlay!.hidden = false;
|
|
273
|
+
opened = true;
|
|
274
|
+
document.documentElement.style.overflow = 'hidden';
|
|
275
|
+
document.addEventListener('keydown', onKeydown);
|
|
276
|
+
window.dispatchEvent(new CustomEvent('quantaroute:open'));
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function close(userInitiated: boolean) {
|
|
280
|
+
if (!opened || !overlay) return;
|
|
281
|
+
overlay.hidden = true;
|
|
282
|
+
opened = false;
|
|
283
|
+
document.documentElement.style.overflow = '';
|
|
284
|
+
document.removeEventListener('keydown', onKeydown);
|
|
285
|
+
root?.render(null);
|
|
286
|
+
if (userInitiated) options.onClose?.();
|
|
287
|
+
window.dispatchEvent(new CustomEvent('quantaroute:close'));
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const onDocumentClick = (event: MouseEvent) => {
|
|
291
|
+
if (destroyed || triggers.length === 0) return;
|
|
292
|
+
const target = event.target as Element | null;
|
|
293
|
+
if (!target?.closest) return;
|
|
294
|
+
const hit = triggers.some((selector) => {
|
|
295
|
+
try {
|
|
296
|
+
return Boolean(target.closest(selector));
|
|
297
|
+
} catch {
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
if (!hit) return;
|
|
302
|
+
event.preventDefault();
|
|
303
|
+
event.stopPropagation();
|
|
304
|
+
event.stopImmediatePropagation();
|
|
305
|
+
open();
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
// Capture phase: runs before the merchant's own handlers so their navigation
|
|
309
|
+
// to the native checkout never fires.
|
|
310
|
+
document.addEventListener('click', onDocumentClick, true);
|
|
311
|
+
|
|
312
|
+
if (triggers.length > 0) {
|
|
313
|
+
const idle = (window as unknown as { requestIdleCallback?: (cb: () => void) => void })
|
|
314
|
+
.requestIdleCallback;
|
|
315
|
+
(idle ?? ((cb: () => void) => setTimeout(cb, 1500)))(() => {
|
|
316
|
+
loadMapRuntime().catch(() => undefined);
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
open,
|
|
322
|
+
close: () => close(true),
|
|
323
|
+
getSessionId: () => sessionId,
|
|
324
|
+
destroy() {
|
|
325
|
+
destroyed = true;
|
|
326
|
+
close(false);
|
|
327
|
+
document.removeEventListener('click', onDocumentClick, true);
|
|
328
|
+
root?.unmount();
|
|
329
|
+
overlay?.remove();
|
|
330
|
+
overlay = null;
|
|
331
|
+
root = null;
|
|
332
|
+
},
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
type EmbedApi = {
|
|
337
|
+
init: typeof init;
|
|
338
|
+
version: string;
|
|
339
|
+
controller?: EmbedController;
|
|
340
|
+
};
|
|
341
|
+
|
|
342
|
+
const api: EmbedApi = {
|
|
343
|
+
version: VERSION,
|
|
344
|
+
init(options) {
|
|
345
|
+
api.controller?.destroy();
|
|
346
|
+
api.controller = init(options);
|
|
347
|
+
return api.controller;
|
|
348
|
+
},
|
|
349
|
+
};
|
|
350
|
+
|
|
351
|
+
(window as unknown as { QuantaRouteCheckout?: EmbedApi }).QuantaRouteCheckout = api;
|
|
352
|
+
|
|
353
|
+
// Auto-start from the script tag's data-* attributes.
|
|
354
|
+
(function autoInit() {
|
|
355
|
+
const script =
|
|
356
|
+
(document.currentScript as HTMLScriptElement | null) ??
|
|
357
|
+
(document.querySelector('script[data-merchant-id][src*="quantaroute-checkout"]') as HTMLScriptElement | null);
|
|
358
|
+
if (!script) return;
|
|
359
|
+
|
|
360
|
+
const config = readScriptConfig({ ...script.dataset });
|
|
361
|
+
if (!config.autoInit) return;
|
|
362
|
+
|
|
363
|
+
const start = () =>
|
|
364
|
+
api.init({
|
|
365
|
+
merchantId: config.merchantId!,
|
|
366
|
+
apiKey: config.apiKey!,
|
|
367
|
+
mapsKey: config.mapsKey,
|
|
368
|
+
functionsUrl: config.functionsUrl ?? '',
|
|
369
|
+
triggers: config.triggers,
|
|
370
|
+
redirectUrl: config.redirectUrl,
|
|
371
|
+
brandColor: config.brandColor,
|
|
372
|
+
logoUrl: config.logoUrl,
|
|
373
|
+
theme: config.theme,
|
|
374
|
+
title: config.title,
|
|
375
|
+
authMode: config.authMode,
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', start);
|
|
379
|
+
else start();
|
|
380
|
+
})();
|
package/src/index.ts
CHANGED