@mainmoneyapp/js-checkout 0.0.0-stage → 0.1.5

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/dist/index.js ADDED
@@ -0,0 +1,1572 @@
1
+ // src/checkout.ts
2
+ import {
3
+ ConfigurationException as ConfigurationException2,
4
+ CurrencyMismatchException,
5
+ Emitter,
6
+ MerchantBackendException,
7
+ createTranslator,
8
+ extractCustomerName,
9
+ isColorScheme,
10
+ isPhoneIdentifier,
11
+ isTerminalStatus as isTerminalStatus2
12
+ } from "@mainmoneyapp/js-core";
13
+ import { createHttp } from "@mainmoneyapp/js-http";
14
+
15
+ // src/poll.ts
16
+ import { ConfigurationException, isTerminalStatus } from "@mainmoneyapp/js-core";
17
+ import { decodeJson } from "@mainmoneyapp/js-http";
18
+ async function pollStatus(options) {
19
+ if (options.pollUrl.trim() === "") {
20
+ throw new ConfigurationException("pollUrl is required when polling is enabled");
21
+ }
22
+ let latest = { status: "PENDING" };
23
+ let failures = 0;
24
+ while (!options.signal?.aborted) {
25
+ const url = withQuery(options.pollUrl, { reference: options.reference, operation: options.operation });
26
+ let delay = options.intervalMs;
27
+ try {
28
+ const response = await options.fetchImpl(url, {
29
+ method: "GET",
30
+ headers: { ...options.pollHeaders },
31
+ signal: options.signal
32
+ });
33
+ latest = decodeJson(response.status, await response.text());
34
+ if (failures > 0) {
35
+ failures = 0;
36
+ options.onConnectionChange?.(false);
37
+ }
38
+ options.onResponse?.(latest);
39
+ } catch (error) {
40
+ if (options.signal?.aborted) {
41
+ throw error;
42
+ }
43
+ failures += 1;
44
+ if (failures === 1) {
45
+ options.onConnectionChange?.(true);
46
+ }
47
+ delay = Math.min(options.intervalMs * 2 ** Math.min(failures, 6), options.maxBackoffMs ?? 1e4);
48
+ }
49
+ if (failures === 0 && typeof latest.status === "string" && isTerminalStatus(latest.status)) {
50
+ return latest;
51
+ }
52
+ await sleep(delay, options.signal);
53
+ }
54
+ return latest;
55
+ }
56
+ function withQuery(uri, query) {
57
+ const parsed = new URL(uri, uri.startsWith("http") ? void 0 : "https://placeholder.invalid");
58
+ for (const [name, value] of Object.entries(query)) {
59
+ parsed.searchParams.set(name, value);
60
+ }
61
+ if (uri.startsWith("http://") || uri.startsWith("https://")) {
62
+ return parsed.toString();
63
+ }
64
+ return `${parsed.pathname}${parsed.search}`;
65
+ }
66
+ function sleep(ms, signal) {
67
+ return new Promise((resolve, reject) => {
68
+ if (signal?.aborted) {
69
+ reject(signal.reason ?? new Error("aborted"));
70
+ return;
71
+ }
72
+ const timer = setTimeout(resolve, ms);
73
+ signal?.addEventListener(
74
+ "abort",
75
+ () => {
76
+ clearTimeout(timer);
77
+ reject(signal.reason ?? new Error("aborted"));
78
+ },
79
+ { once: true }
80
+ );
81
+ });
82
+ }
83
+
84
+ // src/checkout.ts
85
+ var MIN_NATIONAL_AFTER_DIAL_CODE = 8;
86
+ var MIN_LOCAL_NUMBER_DIGITS = 7;
87
+ var MAX_LOCAL_NUMBER_DIGITS = 12;
88
+ function toE164Digits(input, dialCode) {
89
+ const code = dialCode.replace(/\D/g, "");
90
+ const digits = input.replace(/\D/g, "").replace(/^0+/, "");
91
+ const national = digits.startsWith(code) && digits.length - code.length >= MIN_NATIONAL_AFTER_DIAL_CODE ? digits.slice(code.length).replace(/^0+/, "") : digits;
92
+ return `${code}${national}`;
93
+ }
94
+ function providerAcceptedCurrencies(provider) {
95
+ if (provider.accepted_currencies !== void 0 && provider.accepted_currencies.length > 0) {
96
+ return [...provider.accepted_currencies];
97
+ }
98
+ return [provider.currency_code];
99
+ }
100
+ function unionProviderCurrencies(providers) {
101
+ const codes = /* @__PURE__ */ new Set();
102
+ for (const provider of providers) {
103
+ for (const code of providerAcceptedCurrencies(provider)) {
104
+ codes.add(code);
105
+ }
106
+ }
107
+ return [...codes].sort();
108
+ }
109
+ function createCheckout(session, options) {
110
+ const pollEnabled = options.pollStatus !== false;
111
+ if (pollEnabled) {
112
+ if (options.pollUrl === void 0 || options.pollUrl.trim() === "") {
113
+ throw new ConfigurationException2("pollUrl is required when pollStatus is true");
114
+ }
115
+ if (options.pollHeaders === void 0) {
116
+ throw new ConfigurationException2("pollHeaders is required when pollStatus is true");
117
+ }
118
+ }
119
+ const http = options.http ?? createHttp(session);
120
+ const catalogOverrides = { ...session.messages, ...options.messages };
121
+ let translate = createTranslator(options.locale ?? session.locale, catalogOverrides);
122
+ const t = (key, vars) => translate(key, vars);
123
+ const theme = { ...session.theme, ...options.theme };
124
+ const emitter = new Emitter();
125
+ const pollAbort = new AbortController();
126
+ const explicitCurrency = (options.currency ?? session.currency)?.trim().toUpperCase() ?? "";
127
+ const fixedCurrency = explicitCurrency !== "";
128
+ const lockCurrency = (options.lockCurrency ?? session.lockCurrency) === true;
129
+ const state = {
130
+ step: "country",
131
+ operation: options.operation,
132
+ countries: [],
133
+ providers: [],
134
+ identifier: "",
135
+ customerName: null,
136
+ amount: options.amount ?? "",
137
+ lockAmount: options.lockAmount === true,
138
+ currency: explicitCurrency,
139
+ lockCurrency,
140
+ currencyFixed: fixedCurrency,
141
+ payCurrency: explicitCurrency,
142
+ availableCurrencies: fixedCurrency ? [explicitCurrency] : [],
143
+ quoteUnavailable: false,
144
+ partnerFee: null,
145
+ locale: options.locale ?? session.locale,
146
+ paymentMethods: ["mobile_money"],
147
+ activeMethod: "mobile_money",
148
+ feeBearer: "MERCHANT",
149
+ customerEmail: options.customerEmail?.trim() ?? "",
150
+ canPay: false,
151
+ slow: false,
152
+ connectionLost: false,
153
+ pollConfirmed: false,
154
+ theme,
155
+ colorScheme: "auto",
156
+ resolvedColorScheme: "light"
157
+ };
158
+ let lastMatchedIdentifier = "";
159
+ let submissions = 0;
160
+ let baseReference;
161
+ const providerCache = /* @__PURE__ */ new Map();
162
+ const cachedProviders = (country) => {
163
+ const key = `${country}:${routingOperationType()}`;
164
+ let pending = providerCache.get(key);
165
+ if (pending === void 0) {
166
+ pending = http.get(session.paths.providers, { country, operation_type: routingOperationType() });
167
+ providerCache.set(key, pending);
168
+ pending.catch(() => providerCache.delete(key));
169
+ }
170
+ return pending;
171
+ };
172
+ let returnStep = "overview";
173
+ const notify = () => {
174
+ emitter.emit("change", getState());
175
+ };
176
+ const getState = () => ({ ...structuredClone(state), canPay: computeCanPay() });
177
+ const hasValidLocalNumber = () => {
178
+ const digits = e164OrIdentifier().replace(/\D/g, "");
179
+ const dial = dialPrefix() ? state.selectedCountry?.phone_code?.replace(/\D/g, "") ?? "" : "";
180
+ const national = digits.length - dial.length;
181
+ return national >= MIN_LOCAL_NUMBER_DIGITS && national <= MAX_LOCAL_NUMBER_DIGITS;
182
+ };
183
+ const computeCanPay = () => {
184
+ const amount = Number(state.amount);
185
+ return state.step === "details" && state.activeMethod === "mobile_money" && state.selectedCountry !== void 0 && state.providers.length > 0 && state.currency !== "" && !state.quoteUnavailable && Number.isFinite(amount) && amount > 0 && hasValidLocalNumber();
186
+ };
187
+ const messageFromError = (error) => {
188
+ if (error instanceof Error && error.message !== "") {
189
+ return error.message;
190
+ }
191
+ return "Merchant backend request failed";
192
+ };
193
+ const fail = (error) => {
194
+ state.error = messageFromError(error);
195
+ notify();
196
+ };
197
+ const routingOperationType = () => {
198
+ return state.operation === "deposit" ? "DEPOSIT" : "PAYOUT";
199
+ };
200
+ const paginated = (payload) => {
201
+ if (Array.isArray(payload)) {
202
+ return payload;
203
+ }
204
+ if (payload !== null && typeof payload === "object" && Array.isArray(payload.results)) {
205
+ return payload.results;
206
+ }
207
+ return [];
208
+ };
209
+ const defaultPayCurrency = (accepted) => {
210
+ if (accepted.length === 0 || accepted.includes(state.currency)) {
211
+ return state.currency;
212
+ }
213
+ return accepted[0];
214
+ };
215
+ const applyProviderCurrency = (provider) => {
216
+ if (state.lockCurrency) {
217
+ return;
218
+ }
219
+ const accepted = providerAcceptedCurrencies(provider);
220
+ state.availableCurrencies = accepted;
221
+ state.quoteUnavailable = false;
222
+ if (fixedCurrency) {
223
+ state.payCurrency = defaultPayCurrency(accepted);
224
+ return;
225
+ }
226
+ if (accepted.includes(provider.currency_code)) {
227
+ state.currency = provider.currency_code;
228
+ } else if (accepted.length > 0) {
229
+ state.currency = accepted[0];
230
+ }
231
+ state.payCurrency = state.currency;
232
+ };
233
+ const isNoQuote = (error) => {
234
+ if (!(error instanceof MerchantBackendException)) {
235
+ return false;
236
+ }
237
+ if (error.errors.payment_currency !== void 0) {
238
+ return true;
239
+ }
240
+ return JSON.stringify(error.responseBody ?? "").includes("no_quote");
241
+ };
242
+ const PAYMENT_METHODS = ["mobile_money", "card", "mainmoney"];
243
+ const applyPaymentPreferences = (prefs) => {
244
+ if (Array.isArray(prefs.payment_methods)) {
245
+ const methods = PAYMENT_METHODS.filter((method) => prefs.payment_methods.includes(method));
246
+ state.paymentMethods = methods;
247
+ if (!methods.includes(state.activeMethod)) {
248
+ state.activeMethod = methods[0] ?? "mobile_money";
249
+ }
250
+ }
251
+ if (prefs.fee_bearer === "CUSTOMER" || prefs.fee_bearer === "MERCHANT") {
252
+ state.feeBearer = prefs.fee_bearer;
253
+ }
254
+ };
255
+ const allowedCountries = (countries) => {
256
+ const seen = /* @__PURE__ */ new Set();
257
+ return countries.filter((country) => {
258
+ if (typeof country.code !== "string" || country.code === "" || seen.has(country.code)) {
259
+ return false;
260
+ }
261
+ seen.add(country.code);
262
+ return true;
263
+ });
264
+ };
265
+ const loadCountries = async () => {
266
+ const countriesRequest = http.get(session.paths.countries);
267
+ const preferences = http.get(session.paths.checkoutPreferences).catch(() => void 0);
268
+ try {
269
+ const payload = await countriesRequest;
270
+ state.countries = allowedCountries(paginated(payload));
271
+ state.error = void 0;
272
+ } catch (error) {
273
+ void preferences;
274
+ fail(error);
275
+ return;
276
+ }
277
+ const prefs = await preferences;
278
+ if (prefs !== void 0) {
279
+ applyPaymentPreferences(prefs);
280
+ if (prefs.primary_color !== void 0) {
281
+ state.theme = {
282
+ primary: String(prefs.primary_color ?? state.theme.primary),
283
+ secondary: String(prefs.secondary_color ?? state.theme.secondary),
284
+ accent: String(prefs.accent_color ?? state.theme.accent),
285
+ background: String(prefs.background_color ?? state.theme.background)
286
+ };
287
+ }
288
+ }
289
+ notify();
290
+ };
291
+ const selectCountry = async (code) => {
292
+ const country = state.countries.find((item) => item.code === code);
293
+ if (country === void 0) {
294
+ fail(new ConfigurationException2("Unknown country"));
295
+ return;
296
+ }
297
+ try {
298
+ const payload = await cachedProviders(code);
299
+ state.selectedCountry = country;
300
+ state.selectedProvider = void 0;
301
+ state.highlightedProviderCode = void 0;
302
+ state.customerName = null;
303
+ lastMatchedIdentifier = "";
304
+ state.providers = paginated(payload);
305
+ state.quoteUnavailable = false;
306
+ if (!state.lockCurrency) {
307
+ state.availableCurrencies = unionProviderCurrencies(state.providers);
308
+ if (state.currency === "" && state.availableCurrencies.length > 0) {
309
+ state.currency = state.availableCurrencies[0];
310
+ }
311
+ state.payCurrency = fixedCurrency ? defaultPayCurrency(state.availableCurrencies) : state.currency;
312
+ }
313
+ state.error = void 0;
314
+ state.step = "details";
315
+ if (state.providers.length === 1 && state.operation === "deposit") {
316
+ state.selectedProvider = state.providers[0];
317
+ applyProviderCurrency(state.providers[0]);
318
+ await refreshLimitsAndFees();
319
+ }
320
+ notify();
321
+ if (state.identifier.trim() !== "") {
322
+ await matchProvider();
323
+ }
324
+ } catch (error) {
325
+ fail(error);
326
+ }
327
+ };
328
+ const selectProvider = async (code) => {
329
+ const provider = state.providers.find((item) => item.code === code);
330
+ if (provider === void 0) {
331
+ fail(new ConfigurationException2("Unknown provider"));
332
+ return;
333
+ }
334
+ try {
335
+ state.selectedProvider = provider;
336
+ applyProviderCurrency(provider);
337
+ await refreshLimitsAndFees();
338
+ notify();
339
+ } catch (error) {
340
+ fail(error);
341
+ }
342
+ };
343
+ const setIdentifier = (value) => {
344
+ state.identifier = value;
345
+ };
346
+ const setCustomerEmail = (value) => {
347
+ state.customerEmail = value.trim();
348
+ };
349
+ const setMethod = (method) => {
350
+ if (!state.paymentMethods.includes(method) || state.activeMethod === method) {
351
+ return;
352
+ }
353
+ state.activeMethod = method;
354
+ state.error = void 0;
355
+ notify();
356
+ };
357
+ const setLocale = (locale) => {
358
+ if (state.locale === locale) {
359
+ return;
360
+ }
361
+ state.locale = locale;
362
+ translate = createTranslator(locale, catalogOverrides);
363
+ notify();
364
+ };
365
+ const matchProvider = async () => {
366
+ const trimmed = state.identifier.trim();
367
+ if (trimmed === "") {
368
+ return;
369
+ }
370
+ if (trimmed === lastMatchedIdentifier) {
371
+ return;
372
+ }
373
+ try {
374
+ const payload = await http.get(session.paths.matchProvider, {
375
+ account_number: e164OrIdentifier(),
376
+ get_lookup: true,
377
+ operation_type: routingOperationType()
378
+ });
379
+ const entity = typeof payload.entity === "string" ? payload.entity : void 0;
380
+ if (entity !== void 0) {
381
+ state.highlightedProviderCode = entity;
382
+ const matched = state.providers.find((item) => item.code === entity);
383
+ if (matched !== void 0) {
384
+ state.selectedProvider = matched;
385
+ applyProviderCurrency(matched);
386
+ }
387
+ }
388
+ state.customerName = extractCustomerName(payload);
389
+ lastMatchedIdentifier = trimmed;
390
+ await refreshLimitsAndFees();
391
+ notify();
392
+ } catch (error) {
393
+ fail(error);
394
+ }
395
+ };
396
+ const setCurrency = async (code) => {
397
+ if (state.lockCurrency) {
398
+ return;
399
+ }
400
+ const normalized = code.trim().toUpperCase();
401
+ if (normalized === "" || !state.availableCurrencies.includes(normalized)) {
402
+ fail(new ConfigurationException2("Unknown currency"));
403
+ return;
404
+ }
405
+ try {
406
+ if (fixedCurrency) {
407
+ state.payCurrency = normalized;
408
+ } else {
409
+ state.currency = normalized;
410
+ state.payCurrency = normalized;
411
+ }
412
+ await refreshLimitsAndFees();
413
+ notify();
414
+ } catch (error) {
415
+ fail(error);
416
+ }
417
+ };
418
+ const setAmount = async (amount) => {
419
+ if (state.lockAmount) {
420
+ return;
421
+ }
422
+ try {
423
+ state.amount = amount;
424
+ await refreshLimitsAndFees();
425
+ notify();
426
+ } catch (error) {
427
+ fail(error);
428
+ }
429
+ };
430
+ const feeSimulation = async (payCurrency) => {
431
+ const fees = merchantFees();
432
+ return http.post(session.paths.feesSimulate, {
433
+ provider_code: state.selectedProvider?.code ?? "",
434
+ operation_type: routingOperationType(),
435
+ amount: state.amount,
436
+ currency: state.currency,
437
+ ...payCurrency !== state.currency ? { payment_currency: payCurrency } : {},
438
+ ...fees !== "" ? { fees } : {}
439
+ });
440
+ };
441
+ const loadLimits = async (provider) => {
442
+ const payload = await http.get(session.paths.amountLimits, {
443
+ financial_entity_code: provider.code,
444
+ currency: state.payCurrency !== "" ? state.payCurrency : state.currency,
445
+ operation_type: routingOperationType()
446
+ });
447
+ state.limits = paginated(payload)[0];
448
+ };
449
+ const limitsError = (simulation) => {
450
+ if (state.limits === void 0) {
451
+ return void 0;
452
+ }
453
+ const amount = Number(simulation?.quote?.converted_amount ?? state.amount);
454
+ const min = state.limits.amount_min === null ? void 0 : Number(state.limits.amount_min);
455
+ const max = state.limits.amount_max === null ? void 0 : Number(state.limits.amount_max);
456
+ if (min !== void 0 && amount < min || max !== void 0 && amount > max) {
457
+ return t("limits", { min: state.limits.amount_min ?? "", max: state.limits.amount_max ?? "" });
458
+ }
459
+ return void 0;
460
+ };
461
+ const refreshLimitsAndFees = async () => {
462
+ const provider = state.selectedProvider;
463
+ if (provider === void 0 || state.amount.trim() === "" || state.currency === "") {
464
+ return;
465
+ }
466
+ const payCurrency = state.payCurrency !== "" ? state.payCurrency : state.currency;
467
+ state.quoteUnavailable = false;
468
+ await loadLimits(provider);
469
+ let notice;
470
+ let simulation;
471
+ try {
472
+ simulation = await feeSimulation(payCurrency);
473
+ } catch (error) {
474
+ if (!isNoQuote(error) || payCurrency === state.currency) {
475
+ throw error;
476
+ }
477
+ notice = t("noQuote", { from: payCurrency, to: state.currency });
478
+ state.fees = void 0;
479
+ if (!providerAcceptedCurrencies(provider).includes(state.currency)) {
480
+ state.quoteUnavailable = true;
481
+ state.error = notice;
482
+ return;
483
+ }
484
+ state.payCurrency = state.currency;
485
+ await loadLimits(provider);
486
+ simulation = await feeSimulation(state.currency);
487
+ }
488
+ state.fees = simulation;
489
+ state.error = limitsError(simulation) ?? notice;
490
+ };
491
+ const prepare = async () => {
492
+ if (state.selectedCountry === void 0 || state.selectedProvider === void 0) {
493
+ state.error = t("required");
494
+ notify();
495
+ return false;
496
+ }
497
+ if (state.identifier.trim() === "" || state.amount.trim() === "" || state.currency === "") {
498
+ state.error = t("required");
499
+ notify();
500
+ return false;
501
+ }
502
+ try {
503
+ if (options.onPartnerFee !== void 0) {
504
+ const fee = await options.onPartnerFee(getState());
505
+ if (fee !== null && fee.currency !== state.currency) {
506
+ fail(new CurrencyMismatchException());
507
+ return false;
508
+ }
509
+ state.partnerFee = fee;
510
+ }
511
+ if (state.operation === "payout" && options.onValidateBalance !== void 0) {
512
+ const result = await options.onValidateBalance(getState());
513
+ if (!result.ok) {
514
+ state.error = result.message ?? t("balanceRejected");
515
+ notify();
516
+ return false;
517
+ }
518
+ }
519
+ state.error = void 0;
520
+ return true;
521
+ } catch (error) {
522
+ fail(error);
523
+ return false;
524
+ }
525
+ };
526
+ const goOverview = async () => {
527
+ if (await prepare()) {
528
+ state.step = "overview";
529
+ notify();
530
+ }
531
+ };
532
+ const pay = async () => {
533
+ if (state.activeMethod !== "mobile_money") {
534
+ return;
535
+ }
536
+ if (state.selectedProvider === void 0 || lastMatchedIdentifier !== state.identifier.trim()) {
537
+ await matchProvider();
538
+ }
539
+ if (state.selectedProvider === void 0) {
540
+ state.error = t("selectProvider");
541
+ notify();
542
+ return;
543
+ }
544
+ if (await prepare()) {
545
+ returnStep = "details";
546
+ await confirm();
547
+ }
548
+ };
549
+ const goBack = () => {
550
+ if (state.step === "overview") {
551
+ state.step = "details";
552
+ } else if (state.step === "details") {
553
+ state.step = "country";
554
+ }
555
+ notify();
556
+ };
557
+ const confirm = async () => {
558
+ if (state.selectedProvider === void 0) {
559
+ fail(new ConfigurationException2("Provider is required"));
560
+ return;
561
+ }
562
+ state.step = "confirming";
563
+ state.error = void 0;
564
+ notify();
565
+ try {
566
+ baseReference ??= options.reference ?? crypto.randomUUID();
567
+ const reference = submissions === 0 ? baseReference : `${baseReference}-r${submissions}`;
568
+ submissions += 1;
569
+ let created;
570
+ if (state.operation === "deposit") {
571
+ const payload = {
572
+ provider_code: state.selectedProvider.code,
573
+ reference,
574
+ amount: state.amount,
575
+ currency: state.currency,
576
+ customer_phone: e164OrIdentifier()
577
+ };
578
+ if (state.payCurrency !== "" && state.payCurrency !== state.currency) {
579
+ payload.payment_currency = state.payCurrency;
580
+ }
581
+ if (state.customerName !== null) {
582
+ payload.customer_name = state.customerName;
583
+ }
584
+ if (state.customerEmail !== "") {
585
+ payload.customer_email = state.customerEmail;
586
+ }
587
+ const fees = merchantFees();
588
+ if (fees !== "") {
589
+ payload.fees = fees;
590
+ }
591
+ created = await http.post(session.paths.deposits, payload, {
592
+ "Idempotency-Key": reference
593
+ });
594
+ } else {
595
+ created = await http.post(session.paths.payouts, {
596
+ provider_code: state.selectedProvider.code,
597
+ reference,
598
+ amount: state.amount,
599
+ currency: state.currency,
600
+ destination_account: e164OrIdentifier()
601
+ }, { "Idempotency-Key": reference });
602
+ }
603
+ state.status = created;
604
+ if (typeof created.status === "string" && isTerminalStatus2(created.status)) {
605
+ state.step = "terminal";
606
+ notify();
607
+ emitter.emit("complete", getState());
608
+ return;
609
+ }
610
+ if (!pollEnabled) {
611
+ state.step = "ongoing";
612
+ notify();
613
+ emitter.emit("ongoing", getState());
614
+ return;
615
+ }
616
+ state.step = "polling";
617
+ state.slow = false;
618
+ state.connectionLost = false;
619
+ state.pollConfirmed = false;
620
+ notify();
621
+ const slowTimer = setTimeout(() => {
622
+ state.slow = true;
623
+ notify();
624
+ }, options.slowNoticeAfterMs ?? 3e4);
625
+ pollAbort.signal.addEventListener("abort", () => clearTimeout(slowTimer), { once: true });
626
+ let status;
627
+ try {
628
+ status = await pollStatus({
629
+ pollUrl: options.pollUrl ?? "",
630
+ pollHeaders: options.pollHeaders ?? {},
631
+ fetchImpl: session.fetch,
632
+ reference,
633
+ operation: state.operation,
634
+ intervalMs: options.pollIntervalMs ?? 1e3,
635
+ signal: pollAbort.signal,
636
+ onConnectionChange: (lost) => {
637
+ state.connectionLost = lost;
638
+ notify();
639
+ },
640
+ onResponse: () => {
641
+ if (!state.pollConfirmed) {
642
+ state.pollConfirmed = true;
643
+ notify();
644
+ }
645
+ }
646
+ });
647
+ } finally {
648
+ clearTimeout(slowTimer);
649
+ }
650
+ state.slow = false;
651
+ state.connectionLost = false;
652
+ state.status = status;
653
+ state.step = "terminal";
654
+ notify();
655
+ emitter.emit("complete", getState());
656
+ if (typeof status.status === "string") {
657
+ emitter.emit("status", status);
658
+ }
659
+ } catch (error) {
660
+ state.step = options.onRetry !== void 0 ? "error" : returnStep;
661
+ fail(error);
662
+ }
663
+ };
664
+ const retry = async () => {
665
+ if (options.onRetry !== void 0) {
666
+ await options.onRetry();
667
+ return;
668
+ }
669
+ state.error = void 0;
670
+ state.status = void 0;
671
+ state.step = returnStep;
672
+ notify();
673
+ };
674
+ const merchantFees = () => {
675
+ if (state.operation !== "deposit") {
676
+ return "";
677
+ }
678
+ return state.partnerFee?.amount ?? options.fees ?? "";
679
+ };
680
+ const dialPrefix = () => {
681
+ if (state.activeMethod !== "mobile_money" || state.selectedCountry?.phone_code === void 0) {
682
+ return false;
683
+ }
684
+ return state.selectedProvider === void 0 || isPhoneIdentifier(state.selectedProvider);
685
+ };
686
+ const e164OrIdentifier = () => {
687
+ const raw = state.identifier.trim();
688
+ if (!dialPrefix() || state.selectedCountry?.phone_code === void 0) {
689
+ return raw;
690
+ }
691
+ return toE164Digits(raw, state.selectedCountry.phone_code);
692
+ };
693
+ const darkQuery = typeof globalThis.matchMedia === "function" ? globalThis.matchMedia("(prefers-color-scheme: dark)") : void 0;
694
+ const resolveColorScheme = () => state.colorScheme === "auto" ? darkQuery?.matches === true ? "dark" : "light" : state.colorScheme;
695
+ const setColorScheme = (scheme) => {
696
+ if (!isColorScheme(scheme) || state.colorScheme === scheme) {
697
+ return;
698
+ }
699
+ state.colorScheme = scheme;
700
+ state.resolvedColorScheme = resolveColorScheme();
701
+ notify();
702
+ };
703
+ const onSystemSchemeChange = () => {
704
+ const resolved = resolveColorScheme();
705
+ if (state.colorScheme === "auto" && state.resolvedColorScheme !== resolved) {
706
+ state.resolvedColorScheme = resolved;
707
+ notify();
708
+ }
709
+ };
710
+ const releases = [];
711
+ const colorSource = options.colorScheme;
712
+ if (colorSource !== void 0 && typeof colorSource === "object") {
713
+ state.colorScheme = isColorScheme(colorSource.get()) ? colorSource.get() : "auto";
714
+ releases.push(colorSource.subscribe(setColorScheme));
715
+ } else if (isColorScheme(colorSource)) {
716
+ state.colorScheme = colorSource;
717
+ }
718
+ state.resolvedColorScheme = resolveColorScheme();
719
+ if (darkQuery !== void 0) {
720
+ darkQuery.addEventListener("change", onSystemSchemeChange);
721
+ releases.push(() => darkQuery.removeEventListener("change", onSystemSchemeChange));
722
+ }
723
+ const close = () => {
724
+ pollAbort.abort();
725
+ for (const release of releases.splice(0)) {
726
+ release();
727
+ }
728
+ };
729
+ return {
730
+ t,
731
+ getState,
732
+ subscribe: (listener) => emitter.on("change", listener),
733
+ loadCountries,
734
+ selectCountry,
735
+ selectProvider,
736
+ setIdentifier,
737
+ setCustomerEmail,
738
+ setMethod,
739
+ setLocale,
740
+ setColorScheme,
741
+ matchProvider,
742
+ setCurrency,
743
+ retry,
744
+ setAmount,
745
+ goOverview,
746
+ goBack,
747
+ pay,
748
+ confirm,
749
+ close
750
+ };
751
+ }
752
+
753
+ // src/mount.ts
754
+ import { applyColorScheme, applyTheme, isPhoneIdentifier as isPhoneIdentifier2 } from "@mainmoneyapp/js-core";
755
+
756
+ // src/assets/logo.ts
757
+ var DEFAULT_LOGO_URL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAewAAAHsCAYAAAAQKp5gAAAguklEQVR4nO3dT2yUZ57g8V/vdEvzz1WjbbVmZaia0UozYeKYuTQJjsyFaHCvsBSkMURygrSkycE+pBeQBqNGC0hEmJECSR/sAyQcSCy14wORsBQYNXvAwpnxnOI4TbSH2a7CaFarltb2zLZGPTO9B1w0SWgCrreq3qfq85FarRDq9dt0d771PO/zPs83uos9vwoAINf+Q6tvAAD4eoINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAd9s9Q2QX/uG98TmcvcXfm1tZS1WVtYiI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JNrlW76tDeTpk4s3/8z839Lnp/3snt1PiQPMINrm2urJW13PSiPyM4ub/+ecxvXLniT5T/eUv4sQ/ftagO2o972LD4xNscu/TxZ/W9fk8ve974h8/i2tr//uxfm/1l7+Ivf9rPlb//ZcNvquNq3dKvMvCM3hs32z1DcDXqdY5wi6VN8V81H+gSBZW/+2X8Wr172PfH2yOI9/509j8kNe1Vv/9X+PCz/8hLv78H3Id64h7R6IeGj3e6tuAjvCN7mLPr1p9E9Cp+n7v2/HMbxei6z/c23p06V9WY/6ffp77UAPNJ9gAkADPsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAYINAAkQbABIgGADQAIEGwASINgAkADBBoAECDYAJECwASABgg0ACRBsAEiAYANAAgQbABIg2ACQAMEGgAQINgAkQLABIAGCDQAJEGwASIBgA0ACBBsAEiDYAJAAwQaABAg2ACRAsAEgAf8f3StgqrS6+EwAAAAASUVORK5CYII=";
758
+
759
+ // src/form.ts
760
+ var LOCALES = ["en", "fr"];
761
+ var TAB_LABEL_KEYS = {
762
+ mobile_money: "tabMobileMoney",
763
+ card: "tabCard",
764
+ mainmoney: "tabMainMoney"
765
+ };
766
+ var TAB_ORDER = ["card", "mobile_money", "mainmoney"];
767
+ var FOOTER_LINK_KEYS = ["privacy", "terms", "security", "help"];
768
+ function formChrome(checkout, state, logoUrl, links, body) {
769
+ const root = document.createElement("div");
770
+ root.className = "mm-form";
771
+ root.append(header(checkout, state, logoUrl), body, footer(checkout, links));
772
+ return root;
773
+ }
774
+ function formBody(checkout, state) {
775
+ const body = document.createElement("div");
776
+ body.className = "mm-form-body";
777
+ const title = document.createElement("h2");
778
+ title.className = "mm-title";
779
+ title.textContent = checkout.t("formTitle");
780
+ const subtitle = document.createElement("p");
781
+ subtitle.className = "mm-subtitle";
782
+ subtitle.textContent = checkout.t("formSubtitle");
783
+ body.append(title, subtitle);
784
+ if (state.paymentMethods.length > 1) {
785
+ body.append(tabs(checkout, state));
786
+ }
787
+ if (state.activeMethod === "mobile_money") {
788
+ body.append(mobileMoneyPane(checkout, state));
789
+ } else if (state.activeMethod === "card") {
790
+ body.append(placeholderPane(checkout.t("cardSoon")));
791
+ } else {
792
+ body.append(qrPane(checkout.t("mainmoneySoon")));
793
+ }
794
+ body.append(payButton(checkout, state));
795
+ return body;
796
+ }
797
+ function header(checkout, state, logoUrl) {
798
+ const row = document.createElement("div");
799
+ row.className = "mm-header";
800
+ const language = document.createElement("div");
801
+ language.className = "mm-language";
802
+ const caption = document.createElement("span");
803
+ caption.textContent = `${checkout.t("language")}:`;
804
+ language.append(caption);
805
+ for (const locale of LOCALES) {
806
+ const button = document.createElement("button");
807
+ button.type = "button";
808
+ button.className = "mm-language-option";
809
+ button.textContent = locale.toUpperCase();
810
+ button.setAttribute("aria-pressed", String(state.locale === locale));
811
+ if (state.locale === locale) {
812
+ button.classList.add("is-active");
813
+ }
814
+ button.addEventListener("click", () => checkout.setLocale(locale));
815
+ language.append(button);
816
+ }
817
+ const powered = document.createElement("div");
818
+ powered.className = "mm-powered";
819
+ const text = document.createElement("span");
820
+ text.textContent = checkout.t("poweredBy");
821
+ const logo = document.createElement("img");
822
+ logo.src = logoUrl;
823
+ logo.alt = "";
824
+ logo.className = "mm-powered-logo";
825
+ const brand = document.createElement("strong");
826
+ brand.className = "mm-powered-brand";
827
+ brand.textContent = "MainMoney";
828
+ powered.append(text, logo, brand);
829
+ row.append(language, powered);
830
+ return row;
831
+ }
832
+ function footer(checkout, links) {
833
+ const wrap = document.createElement("div");
834
+ wrap.className = "mm-footer";
835
+ const copyright = document.createElement("div");
836
+ copyright.className = "mm-copyright";
837
+ copyright.textContent = `\xA9 ${(/* @__PURE__ */ new Date()).getFullYear()} MainMoney. ${checkout.t("rights")}`;
838
+ wrap.append(copyright);
839
+ const nav = document.createElement("nav");
840
+ nav.className = "mm-footer-links";
841
+ for (const key of FOOTER_LINK_KEYS) {
842
+ const href = links[key];
843
+ if (href === void 0 || href === "") {
844
+ continue;
845
+ }
846
+ const anchor = document.createElement("a");
847
+ anchor.href = href;
848
+ anchor.target = "_blank";
849
+ anchor.rel = "noopener noreferrer";
850
+ anchor.textContent = checkout.t(key);
851
+ nav.append(anchor);
852
+ }
853
+ if (nav.childElementCount > 0) {
854
+ wrap.append(nav);
855
+ }
856
+ return wrap;
857
+ }
858
+ function tabs(checkout, state) {
859
+ const row = document.createElement("div");
860
+ row.className = "mm-tabs";
861
+ row.setAttribute("role", "tablist");
862
+ for (const method of TAB_ORDER.filter((item) => state.paymentMethods.includes(item))) {
863
+ const tab = document.createElement("button");
864
+ tab.type = "button";
865
+ tab.className = "mm-tab";
866
+ tab.setAttribute("role", "tab");
867
+ tab.setAttribute("aria-selected", String(state.activeMethod === method));
868
+ tab.textContent = checkout.t(TAB_LABEL_KEYS[method]);
869
+ if (state.activeMethod === method) {
870
+ tab.classList.add("is-active");
871
+ }
872
+ tab.addEventListener("click", () => checkout.setMethod(method));
873
+ row.append(tab);
874
+ }
875
+ return row;
876
+ }
877
+ function mobileMoneyPane(checkout, state) {
878
+ const pane = document.createElement("div");
879
+ pane.className = "mm-pane";
880
+ pane.append(emailField(checkout, state));
881
+ pane.append(phoneField(checkout, state));
882
+ if (!state.lockAmount || state.amount === "") {
883
+ pane.append(amountField(checkout, state));
884
+ }
885
+ if (payCurrencyOptions(state).length > 1) {
886
+ pane.append(payCurrencyField(checkout, state));
887
+ }
888
+ pane.append(feeBox(checkout, state));
889
+ pane.append(providerLogos(checkout, state));
890
+ appendError(pane, state);
891
+ return pane;
892
+ }
893
+ function placeholderPane(message) {
894
+ const pane = document.createElement("div");
895
+ pane.className = "mm-pane mm-placeholder";
896
+ pane.textContent = message;
897
+ return pane;
898
+ }
899
+ function qrPane(message) {
900
+ const pane = document.createElement("div");
901
+ pane.className = "mm-pane mm-placeholder mm-qr";
902
+ const frame = document.createElement("div");
903
+ frame.className = "mm-qr-frame";
904
+ frame.setAttribute("aria-hidden", "true");
905
+ const text = document.createElement("p");
906
+ text.textContent = message;
907
+ pane.append(frame, text);
908
+ return pane;
909
+ }
910
+ function floatingField(labelText, control, hint) {
911
+ const wrap = document.createElement("div");
912
+ wrap.className = "mm-floating";
913
+ const box = document.createElement("div");
914
+ box.className = "mm-floating-box";
915
+ const label2 = document.createElement("span");
916
+ label2.className = "mm-floating-label";
917
+ label2.textContent = labelText;
918
+ box.append(label2, control);
919
+ wrap.append(box);
920
+ if (hint !== void 0) {
921
+ const note = document.createElement("p");
922
+ note.className = "mm-hint";
923
+ note.textContent = hint;
924
+ wrap.append(note);
925
+ }
926
+ return wrap;
927
+ }
928
+ function emailField(checkout, state) {
929
+ const input = document.createElement("input");
930
+ input.className = "mm-input mm-input-bare";
931
+ input.type = "email";
932
+ input.autocomplete = "email";
933
+ input.placeholder = checkout.t("emailPlaceholder");
934
+ input.value = state.customerEmail;
935
+ input.addEventListener("input", () => checkout.setCustomerEmail(input.value));
936
+ return floatingField(checkout.t("emailLabel"), input, checkout.t("emailHint"));
937
+ }
938
+ var LOOKUP_DEBOUNCE_MS = 600;
939
+ var LOOKUP_MIN_DIGITS = 7;
940
+ function phoneField(checkout, state) {
941
+ const row = document.createElement("div");
942
+ row.className = "mm-phone-row";
943
+ const flag = document.createElement("span");
944
+ flag.className = "mm-flag";
945
+ flag.append(flagNode(state.selectedCountry));
946
+ const dial = document.createElement("select");
947
+ dial.className = "mm-dial";
948
+ dial.setAttribute("aria-label", checkout.t("country"));
949
+ for (const country of state.countries) {
950
+ if (country.phone_code === void 0 || country.phone_code === "") {
951
+ continue;
952
+ }
953
+ const option = document.createElement("option");
954
+ option.value = country.code;
955
+ option.textContent = `+${country.phone_code}`;
956
+ option.title = country.name;
957
+ option.selected = state.selectedCountry?.code === country.code;
958
+ dial.append(option);
959
+ }
960
+ dial.addEventListener("change", () => {
961
+ void checkout.selectCountry(dial.value).catch(() => void 0);
962
+ });
963
+ const divider = document.createElement("span");
964
+ divider.className = "mm-phone-divider";
965
+ const input = document.createElement("input");
966
+ input.className = "mm-input mm-input-bare";
967
+ input.type = "tel";
968
+ input.inputMode = "tel";
969
+ input.autocomplete = "tel-national";
970
+ input.placeholder = "812345678";
971
+ input.value = state.identifier;
972
+ const lookup = document.createElement("span");
973
+ lookup.className = "mm-spinner-sm";
974
+ lookup.setAttribute("role", "status");
975
+ lookup.setAttribute("aria-label", checkout.t("lookingUp"));
976
+ lookup.hidden = true;
977
+ const runLookup = () => {
978
+ lookup.hidden = false;
979
+ void checkout.matchProvider().catch(() => void 0).finally(() => {
980
+ lookup.hidden = true;
981
+ });
982
+ };
983
+ let timer;
984
+ input.addEventListener("input", () => {
985
+ checkout.setIdentifier(input.value);
986
+ refreshPayButton(checkout, input);
987
+ clearTimeout(timer);
988
+ if (input.value.replace(/\D/g, "").length >= LOOKUP_MIN_DIGITS) {
989
+ timer = setTimeout(runLookup, LOOKUP_DEBOUNCE_MS);
990
+ }
991
+ });
992
+ input.addEventListener("blur", () => {
993
+ clearTimeout(timer);
994
+ if (input.value.trim() !== "") {
995
+ runLookup();
996
+ }
997
+ });
998
+ row.append(flag, dial, divider, input, lookup);
999
+ return floatingField(checkout.t("phone"), row, checkout.t("phoneHint"));
1000
+ }
1001
+ function amountField(checkout, state) {
1002
+ const row = document.createElement("div");
1003
+ row.className = "mm-amount-row";
1004
+ const input = document.createElement("input");
1005
+ input.className = "mm-input mm-input-bare";
1006
+ input.inputMode = "decimal";
1007
+ input.value = state.amount;
1008
+ input.disabled = state.lockAmount;
1009
+ input.addEventListener("input", () => refreshPayButton(checkout, input));
1010
+ input.addEventListener("change", () => {
1011
+ void checkout.setAmount(input.value).catch(() => void 0);
1012
+ });
1013
+ row.append(input);
1014
+ if (state.lockCurrency || state.currencyFixed || state.availableCurrencies.length <= 1) {
1015
+ const badge = document.createElement("span");
1016
+ badge.className = "mm-currency-suffix";
1017
+ badge.textContent = state.currency;
1018
+ row.append(badge);
1019
+ } else {
1020
+ const select = document.createElement("select");
1021
+ select.className = "mm-currency-suffix mm-select";
1022
+ for (const code of state.availableCurrencies) {
1023
+ const option = document.createElement("option");
1024
+ option.value = code;
1025
+ option.textContent = code;
1026
+ option.selected = state.currency === code;
1027
+ select.append(option);
1028
+ }
1029
+ select.addEventListener("change", () => {
1030
+ void checkout.setCurrency(select.value).catch(() => void 0);
1031
+ });
1032
+ row.append(select);
1033
+ }
1034
+ return floatingField(checkout.t("amount"), row);
1035
+ }
1036
+ function payCurrencyOptions(state) {
1037
+ if (!state.currencyFixed || state.lockCurrency || state.selectedProvider === void 0) {
1038
+ return [];
1039
+ }
1040
+ return [...new Set(state.availableCurrencies)].sort();
1041
+ }
1042
+ function payCurrencyField(checkout, state) {
1043
+ const select = document.createElement("select");
1044
+ select.className = "mm-input mm-input-bare mm-select";
1045
+ for (const code of payCurrencyOptions(state)) {
1046
+ const option = document.createElement("option");
1047
+ option.value = code;
1048
+ option.textContent = code;
1049
+ option.selected = state.payCurrency === code;
1050
+ select.append(option);
1051
+ }
1052
+ select.addEventListener("change", () => {
1053
+ void checkout.setCurrency(select.value).catch(() => void 0);
1054
+ });
1055
+ return floatingField(checkout.t("payIn"), select);
1056
+ }
1057
+ function feeBox(checkout, state) {
1058
+ const box = document.createElement("div");
1059
+ box.className = "mm-fee-box";
1060
+ const q = quote(state);
1061
+ const currency = state.fees?.quote?.to_currency ?? state.currency;
1062
+ box.append(feeRow(checkout.t("processingFee"), formatMoney(q.fee, state, currency)));
1063
+ const total = feeRow(checkout.t("youPay"), formatMoney(q.pay, state, currency));
1064
+ total.classList.add("mm-fee-total");
1065
+ box.append(total);
1066
+ const fx = state.fees?.quote;
1067
+ if (fx !== void 0) {
1068
+ const note = document.createElement("p");
1069
+ note.className = "mm-hint";
1070
+ note.textContent = checkout.t("exchangeRate", { from: fx.from_currency, to: fx.to_currency, rate: fx.rate });
1071
+ box.append(note);
1072
+ }
1073
+ return box;
1074
+ }
1075
+ function feeRow(labelText, value) {
1076
+ const row = document.createElement("div");
1077
+ row.className = "mm-fee-row";
1078
+ const label2 = document.createElement("span");
1079
+ label2.textContent = labelText;
1080
+ const amount = document.createElement("span");
1081
+ amount.textContent = value;
1082
+ row.append(label2, amount);
1083
+ return row;
1084
+ }
1085
+ function providerLogos(checkout, state) {
1086
+ const wrap = document.createElement("div");
1087
+ wrap.className = "mm-providers";
1088
+ if (state.providers.length === 0) {
1089
+ const empty = document.createElement("p");
1090
+ empty.className = "mm-hint";
1091
+ empty.textContent = state.selectedCountry === void 0 ? "" : checkout.t("noProviders");
1092
+ wrap.append(empty);
1093
+ return wrap;
1094
+ }
1095
+ for (const provider of state.providers) {
1096
+ wrap.append(providerChip(checkout, state, provider));
1097
+ }
1098
+ return wrap;
1099
+ }
1100
+ function providerChip(checkout, state, provider) {
1101
+ const chip = document.createElement("button");
1102
+ chip.type = "button";
1103
+ chip.className = "mm-provider-chip";
1104
+ chip.title = provider.name;
1105
+ chip.setAttribute("aria-label", provider.name);
1106
+ chip.setAttribute("aria-pressed", String(state.selectedProvider?.code === provider.code));
1107
+ if (state.selectedProvider?.code === provider.code) {
1108
+ chip.classList.add("is-selected");
1109
+ }
1110
+ if (provider.logo) {
1111
+ const img = document.createElement("img");
1112
+ img.src = provider.logo;
1113
+ img.alt = provider.name;
1114
+ img.loading = "lazy";
1115
+ chip.append(img);
1116
+ } else {
1117
+ chip.textContent = provider.name;
1118
+ }
1119
+ chip.addEventListener("click", () => {
1120
+ void checkout.selectProvider(provider.code).catch(() => void 0);
1121
+ });
1122
+ return chip;
1123
+ }
1124
+ function payButton(checkout, state) {
1125
+ const button = document.createElement("button");
1126
+ button.type = "button";
1127
+ button.className = "mm-pay";
1128
+ applyPayState(button, checkout, state);
1129
+ button.addEventListener("click", () => {
1130
+ void checkout.pay().catch(() => void 0);
1131
+ });
1132
+ return button;
1133
+ }
1134
+ function applyPayState(button, checkout, state) {
1135
+ const ready = state.canPay;
1136
+ button.disabled = !ready;
1137
+ const total = ready ? formatMoney(quote(state).pay, state, state.fees?.quote?.to_currency ?? state.currency) : "\u2014";
1138
+ button.textContent = `${checkout.t("pay")} ${total}`;
1139
+ }
1140
+ function refreshPayButton(checkout, from) {
1141
+ const button = from.closest(".mm-form")?.querySelector(".mm-pay") ?? null;
1142
+ if (button !== null) {
1143
+ applyPayState(button, checkout, checkout.getState());
1144
+ }
1145
+ }
1146
+ function quote(state) {
1147
+ const amount = state.amount.trim() === "" ? null : state.amount;
1148
+ const fees = state.fees;
1149
+ return {
1150
+ receives: fees?.merchant_receives ?? amount,
1151
+ fee: fees?.processing_fee ?? null,
1152
+ pay: fees?.total_to_pay ?? amount
1153
+ };
1154
+ }
1155
+ function formatMoney(value, state, currency = state.currency) {
1156
+ if (value === null || value === "" || currency === "") {
1157
+ return "\u2014";
1158
+ }
1159
+ const number = Number(value);
1160
+ if (!Number.isFinite(number)) {
1161
+ return "\u2014";
1162
+ }
1163
+ try {
1164
+ return new Intl.NumberFormat(state.locale, { style: "currency", currency }).format(number);
1165
+ } catch {
1166
+ return `${number.toFixed(2)} ${currency}`;
1167
+ }
1168
+ }
1169
+ function flagNode(country) {
1170
+ if (country?.flag) {
1171
+ const img = document.createElement("img");
1172
+ img.src = country.flag;
1173
+ img.alt = country.name;
1174
+ img.className = "mm-flag-image";
1175
+ return img;
1176
+ }
1177
+ return document.createTextNode(flagEmoji(country?.code));
1178
+ }
1179
+ function flagEmoji(code) {
1180
+ if (code === void 0 || !/^[A-Za-z]{2}$/.test(code)) {
1181
+ return "";
1182
+ }
1183
+ return [...code.toUpperCase()].map((char) => String.fromCodePoint(127462 + char.charCodeAt(0) - 65)).join("");
1184
+ }
1185
+ function appendError(parent, state) {
1186
+ if (state.error === void 0 || state.error === "") {
1187
+ return;
1188
+ }
1189
+ const error = document.createElement("div");
1190
+ error.className = "mm-error";
1191
+ error.setAttribute("role", "alert");
1192
+ error.textContent = state.error;
1193
+ parent.append(error);
1194
+ }
1195
+
1196
+ // src/mount.ts
1197
+ function layoutKey(state) {
1198
+ return JSON.stringify({
1199
+ ...state,
1200
+ colorScheme: void 0,
1201
+ resolvedColorScheme: void 0,
1202
+ identifier: void 0,
1203
+ customerEmail: void 0,
1204
+ amount: void 0,
1205
+ canPay: void 0
1206
+ });
1207
+ }
1208
+ function applyPalette(element, state) {
1209
+ applyColorScheme(element, state.colorScheme);
1210
+ element.setAttribute("data-mm-resolved-theme", state.resolvedColorScheme);
1211
+ }
1212
+ function mountCheckout(element, checkout, options = {}) {
1213
+ const logoUrl = options.logoUrl ?? DEFAULT_LOGO_URL;
1214
+ const useForm = (state) => (options.layout ?? (state.operation === "deposit" ? "form" : "wizard")) === "form";
1215
+ let preselecting = false;
1216
+ const renderForm = (state) => {
1217
+ if (state.step === "country" && state.countries.length > 0 && !preselecting) {
1218
+ preselecting = true;
1219
+ const preferred = state.countries.find((country) => country.code === options.defaultCountry) ?? state.countries[0];
1220
+ void checkout.selectCountry(preferred.code).catch(() => void 0).finally(() => {
1221
+ preselecting = false;
1222
+ });
1223
+ }
1224
+ let body;
1225
+ if (state.step === "confirming" || state.step === "polling") {
1226
+ body = confirmingStep(checkout, state, logoUrl);
1227
+ } else if (state.step === "ongoing") {
1228
+ body = messageStep(checkout.t("ongoing"));
1229
+ } else if (state.step === "error") {
1230
+ body = retryStep(checkout, state.error ?? checkout.t("failed"));
1231
+ } else if (state.step === "terminal") {
1232
+ const status = state.status?.status ?? "";
1233
+ body = status.toUpperCase() === "SUCCESS" ? messageStep(checkout.t("success")) : retryStep(checkout, checkout.t("failed"));
1234
+ } else {
1235
+ body = formBody(checkout, state);
1236
+ }
1237
+ element.append(formChrome(checkout, state, logoUrl, options.links ?? {}, body));
1238
+ };
1239
+ let renderedKey;
1240
+ let renderedScheme = "";
1241
+ const render = (state) => {
1242
+ element.setAttribute("data-mm-variant", options.variant ?? "default");
1243
+ const key = layoutKey(state);
1244
+ const scheme = `${state.colorScheme}/${state.resolvedColorScheme}`;
1245
+ if (key === renderedKey) {
1246
+ if (scheme !== renderedScheme) {
1247
+ applyPalette(element, state);
1248
+ renderedScheme = scheme;
1249
+ }
1250
+ return;
1251
+ }
1252
+ renderedKey = key;
1253
+ renderedScheme = scheme;
1254
+ const fields = Array.from(element.querySelectorAll("input"));
1255
+ const focused = fields.findIndex((field) => field === document.activeElement);
1256
+ const caret = focused >= 0 ? fields[focused].selectionStart : null;
1257
+ renderBody(state);
1258
+ applyPalette(element, state);
1259
+ if (focused >= 0) {
1260
+ const next = element.querySelectorAll("input")[focused];
1261
+ next?.focus();
1262
+ if (caret !== null && caret !== void 0) {
1263
+ try {
1264
+ next?.setSelectionRange(caret, caret);
1265
+ } catch {
1266
+ }
1267
+ }
1268
+ }
1269
+ };
1270
+ const renderBody = (state) => {
1271
+ applyTheme(element, state.theme);
1272
+ element.classList.add("mm-checkout");
1273
+ element.innerHTML = "";
1274
+ if (useForm(state)) {
1275
+ element.classList.add("mm-checkout-form");
1276
+ renderForm(state);
1277
+ return;
1278
+ }
1279
+ element.classList.remove("mm-checkout-form");
1280
+ if (state.step === "country") {
1281
+ element.append(countryStep(checkout, state));
1282
+ } else if (state.step === "details") {
1283
+ element.append(detailsStep(checkout, state));
1284
+ } else if (state.step === "overview") {
1285
+ element.append(overviewStep(checkout, state));
1286
+ } else if (state.step === "confirming" || state.step === "polling") {
1287
+ element.append(confirmingStep(checkout, state, logoUrl));
1288
+ } else if (state.step === "ongoing") {
1289
+ element.append(messageStep(checkout.t("ongoing")));
1290
+ } else if (state.step === "error") {
1291
+ element.append(retryStep(checkout, state.error ?? checkout.t("failed")));
1292
+ return;
1293
+ } else {
1294
+ const status = state.status?.status ?? "";
1295
+ element.append(
1296
+ status.toUpperCase() === "SUCCESS" ? messageStep(checkout.t("success")) : retryStep(checkout, checkout.t("failed"))
1297
+ );
1298
+ }
1299
+ appendError2(element, state);
1300
+ };
1301
+ render(checkout.getState());
1302
+ return checkout.subscribe(render);
1303
+ }
1304
+ function countryStep(checkout, state) {
1305
+ const wrap = document.createElement("div");
1306
+ wrap.append(label(checkout.t("country")));
1307
+ const select = document.createElement("select");
1308
+ select.className = "mm-select";
1309
+ const placeholder = document.createElement("option");
1310
+ placeholder.value = "";
1311
+ placeholder.textContent = checkout.t("selectCountry");
1312
+ select.append(placeholder);
1313
+ for (const country of state.countries) {
1314
+ const option = document.createElement("option");
1315
+ option.value = country.code;
1316
+ option.textContent = country.name;
1317
+ if (state.selectedCountry?.code === country.code) {
1318
+ option.selected = true;
1319
+ }
1320
+ select.append(option);
1321
+ }
1322
+ select.addEventListener("change", () => {
1323
+ if (select.value !== "") {
1324
+ run(() => checkout.selectCountry(select.value));
1325
+ }
1326
+ });
1327
+ wrap.append(select);
1328
+ return wrap;
1329
+ }
1330
+ function detailsStep(checkout, state) {
1331
+ const wrap = document.createElement("div");
1332
+ const phone = isPhoneIdentifier2(state.selectedProvider);
1333
+ wrap.append(providerList(checkout, state));
1334
+ wrap.append(identifierField(checkout, state, phone));
1335
+ if (state.customerName !== null) {
1336
+ wrap.append(overviewRow(checkout.t("customerName"), state.customerName));
1337
+ }
1338
+ wrap.append(amountField2(checkout, state));
1339
+ wrap.append(actions(checkout, () => run(() => checkout.goOverview()), checkout.t("next"), true));
1340
+ return wrap;
1341
+ }
1342
+ function overviewStep(checkout, state) {
1343
+ const wrap = document.createElement("div");
1344
+ const phone = isPhoneIdentifier2(state.selectedProvider);
1345
+ wrap.append(overviewRow(checkout.t("country"), state.selectedCountry?.name ?? ""));
1346
+ wrap.append(overviewRow(checkout.t("provider"), state.selectedProvider?.name ?? ""));
1347
+ wrap.append(overviewRow(phone ? checkout.t("phone") : checkout.t("account"), state.identifier));
1348
+ if (state.customerName !== null) {
1349
+ wrap.append(overviewRow(checkout.t("customerName"), state.customerName));
1350
+ }
1351
+ wrap.append(overviewRow(checkout.t("amount"), `${state.amount} ${state.currency}`));
1352
+ if (state.partnerFee !== null) {
1353
+ wrap.append(
1354
+ overviewRow(state.partnerFee.label ?? checkout.t("partnerFee"), `${state.partnerFee.amount} ${state.partnerFee.currency}`)
1355
+ );
1356
+ }
1357
+ wrap.append(actions(checkout, () => run(() => checkout.confirm()), checkout.t("confirm"), true));
1358
+ return wrap;
1359
+ }
1360
+ function confirmingStep(checkout, state, logoUrl) {
1361
+ const wrap = document.createElement("div");
1362
+ wrap.className = "mm-confirm";
1363
+ const spinner = document.createElement("div");
1364
+ spinner.className = "mm-spinner";
1365
+ const curveA = document.createElement("div");
1366
+ curveA.className = "mm-spinner-curve";
1367
+ const curveB = document.createElement("div");
1368
+ curveB.className = "mm-spinner-curve";
1369
+ const logo = document.createElement("img");
1370
+ logo.className = "mm-spinner-logo";
1371
+ logo.alt = "MainMoney";
1372
+ logo.src = logoUrl;
1373
+ spinner.append(curveA, curveB, logo);
1374
+ const caption = document.createElement("div");
1375
+ caption.textContent = state.step === "polling" ? checkout.t("polling") : checkout.t("confirming");
1376
+ wrap.append(spinner, caption);
1377
+ if (state.step === "polling" && state.connectionLost) {
1378
+ const lost = document.createElement("div");
1379
+ lost.className = "mm-confirm-slow mm-confirm-offline";
1380
+ lost.setAttribute("role", "status");
1381
+ lost.textContent = checkout.t("connectionLost");
1382
+ wrap.append(lost);
1383
+ }
1384
+ if (state.step === "polling" && state.slow) {
1385
+ const notice = document.createElement("div");
1386
+ notice.className = "mm-confirm-slow";
1387
+ notice.textContent = checkout.t("slow");
1388
+ wrap.append(notice);
1389
+ }
1390
+ return wrap;
1391
+ }
1392
+ function retryStep(checkout, message) {
1393
+ const wrap = document.createElement("div");
1394
+ wrap.className = "mm-confirm mm-retry";
1395
+ const error = document.createElement("div");
1396
+ error.className = "mm-error";
1397
+ error.setAttribute("role", "alert");
1398
+ error.textContent = message;
1399
+ const hint = document.createElement("p");
1400
+ hint.className = "mm-hint";
1401
+ hint.textContent = checkout.t("retryHint");
1402
+ const button = document.createElement("button");
1403
+ button.type = "button";
1404
+ button.className = "mm-pay";
1405
+ button.textContent = checkout.t("retry");
1406
+ button.addEventListener("click", () => {
1407
+ button.disabled = true;
1408
+ void checkout.retry().catch(() => {
1409
+ button.disabled = false;
1410
+ });
1411
+ });
1412
+ wrap.append(error, hint, button);
1413
+ return wrap;
1414
+ }
1415
+ function messageStep(text) {
1416
+ const wrap = document.createElement("div");
1417
+ wrap.className = "mm-confirm";
1418
+ wrap.textContent = text;
1419
+ return wrap;
1420
+ }
1421
+ function providerList(checkout, state) {
1422
+ const wrap = document.createElement("div");
1423
+ wrap.append(label(checkout.t("provider")));
1424
+ for (const provider of state.providers) {
1425
+ const row = document.createElement("button");
1426
+ row.type = "button";
1427
+ row.className = "mm-provider";
1428
+ if (state.selectedProvider?.code === provider.code) {
1429
+ row.classList.add("is-selected");
1430
+ }
1431
+ if (state.highlightedProviderCode === provider.code) {
1432
+ row.classList.add("is-highlighted");
1433
+ }
1434
+ row.textContent = provider.name;
1435
+ if (state.highlightedProviderCode === provider.code) {
1436
+ const badge = document.createElement("span");
1437
+ badge.className = "mm-badge";
1438
+ badge.textContent = checkout.t("highlighted");
1439
+ row.append(badge);
1440
+ }
1441
+ row.addEventListener("click", () => {
1442
+ run(() => checkout.selectProvider(provider.code));
1443
+ });
1444
+ wrap.append(row);
1445
+ }
1446
+ return wrap;
1447
+ }
1448
+ function identifierField(checkout, state, phone) {
1449
+ const field = document.createElement("div");
1450
+ field.className = "mm-field";
1451
+ field.append(label(phone ? checkout.t("phone") : checkout.t("account")));
1452
+ const input = document.createElement("input");
1453
+ input.className = "mm-input";
1454
+ input.value = state.identifier;
1455
+ input.addEventListener("input", () => checkout.setIdentifier(input.value));
1456
+ input.addEventListener("blur", () => {
1457
+ run(() => checkout.matchProvider());
1458
+ });
1459
+ if (phone && state.selectedCountry?.phone_code) {
1460
+ const row = document.createElement("div");
1461
+ row.className = "mm-phone";
1462
+ const prefix = document.createElement("div");
1463
+ prefix.className = "mm-phone-prefix";
1464
+ prefix.textContent = `+${state.selectedCountry.phone_code}`;
1465
+ row.append(prefix, input);
1466
+ field.append(row);
1467
+ } else {
1468
+ field.append(input);
1469
+ }
1470
+ return field;
1471
+ }
1472
+ function amountField2(checkout, state) {
1473
+ const field = document.createElement("div");
1474
+ field.className = "mm-field";
1475
+ field.append(label(checkout.t("amount")));
1476
+ const row = document.createElement("div");
1477
+ row.className = "mm-amount-row";
1478
+ const input = document.createElement("input");
1479
+ input.className = "mm-input";
1480
+ input.value = state.amount;
1481
+ input.inputMode = "decimal";
1482
+ if (state.lockAmount) {
1483
+ input.disabled = true;
1484
+ input.readOnly = true;
1485
+ } else {
1486
+ input.addEventListener("change", () => {
1487
+ run(() => checkout.setAmount(input.value));
1488
+ });
1489
+ }
1490
+ row.append(input);
1491
+ row.append(currencySuffix(checkout, state));
1492
+ field.append(row);
1493
+ return field;
1494
+ }
1495
+ function currencySuffix(checkout, state) {
1496
+ if (state.lockCurrency) {
1497
+ const badge = document.createElement("div");
1498
+ badge.className = "mm-currency-suffix";
1499
+ badge.textContent = state.currency;
1500
+ return badge;
1501
+ }
1502
+ const select = document.createElement("select");
1503
+ select.className = "mm-currency-suffix mm-select";
1504
+ for (const code of state.availableCurrencies) {
1505
+ const option = document.createElement("option");
1506
+ option.value = code;
1507
+ option.textContent = code;
1508
+ if (state.currency === code) {
1509
+ option.selected = true;
1510
+ }
1511
+ select.append(option);
1512
+ }
1513
+ select.addEventListener("change", () => {
1514
+ if (select.value !== "") {
1515
+ run(() => checkout.setCurrency(select.value));
1516
+ }
1517
+ });
1518
+ return select;
1519
+ }
1520
+ function overviewRow(labelText, value) {
1521
+ const row = document.createElement("div");
1522
+ row.className = "mm-overview-row";
1523
+ const left = document.createElement("span");
1524
+ left.textContent = labelText;
1525
+ const right = document.createElement("span");
1526
+ right.textContent = value;
1527
+ row.append(left, right);
1528
+ return row;
1529
+ }
1530
+ function actions(checkout, onPrimary, primaryLabel, showBack) {
1531
+ const row = document.createElement("div");
1532
+ row.className = "mm-actions";
1533
+ if (showBack) {
1534
+ const back = document.createElement("button");
1535
+ back.type = "button";
1536
+ back.className = "mm-button mm-button-secondary";
1537
+ back.textContent = checkout.t("back");
1538
+ back.addEventListener("click", () => checkout.goBack());
1539
+ row.append(back);
1540
+ }
1541
+ const primary = document.createElement("button");
1542
+ primary.type = "button";
1543
+ primary.className = "mm-button mm-button-primary";
1544
+ primary.textContent = primaryLabel;
1545
+ primary.addEventListener("click", onPrimary);
1546
+ row.append(primary);
1547
+ return row;
1548
+ }
1549
+ function label(text) {
1550
+ const node = document.createElement("label");
1551
+ node.className = "mm-label";
1552
+ node.textContent = text;
1553
+ return node;
1554
+ }
1555
+ function appendError2(parent, state) {
1556
+ if (state.error === void 0 || state.error === "") {
1557
+ return;
1558
+ }
1559
+ const error = document.createElement("div");
1560
+ error.className = "mm-error";
1561
+ error.textContent = state.error;
1562
+ parent.append(error);
1563
+ }
1564
+ function run(op) {
1565
+ void op().catch(() => void 0);
1566
+ }
1567
+ export {
1568
+ createCheckout,
1569
+ mountCheckout,
1570
+ pollStatus,
1571
+ toE164Digits
1572
+ };