@sunbay/checkout-sdk-js 0.0.4 → 0.0.7
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 +165 -40
- package/dist/index.cjs +355 -84
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +96 -19
- package/dist/index.d.ts +96 -19
- package/dist/index.global.js +318 -50
- package/dist/index.global.js.map +1 -1
- package/dist/index.mjs +324 -53
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -3,14 +3,62 @@ import {
|
|
|
3
3
|
decodeEnvelope,
|
|
4
4
|
encodeEnvelope,
|
|
5
5
|
PROTOCOL_MAJOR,
|
|
6
|
-
PROTOCOL_NAME
|
|
6
|
+
PROTOCOL_NAME as PROTOCOL_NAME2
|
|
7
7
|
} from "@sunbay/checkout-sdk-protocol";
|
|
8
8
|
import { ErrorCode } from "@sunbay/checkout-sdk-protocol";
|
|
9
|
+
|
|
10
|
+
// src/config/env.ts
|
|
11
|
+
var HOST_ORIGINS = {
|
|
12
|
+
dev: "https://checkout.sunbay-dev.com",
|
|
13
|
+
test: "https://checkout.sunbay-uat.us",
|
|
14
|
+
prod: "https://checkout.sunbay.us"
|
|
15
|
+
};
|
|
16
|
+
function resolveHostOrigin(env = "prod", override) {
|
|
17
|
+
if (override) {
|
|
18
|
+
return override.replace(/\/+$/, "");
|
|
19
|
+
}
|
|
20
|
+
return HOST_ORIGINS[env];
|
|
21
|
+
}
|
|
22
|
+
var RUNTIME_BASE_PATH = "/embed/sdk";
|
|
23
|
+
function runtimeUrl(hostOrigin, segment, type) {
|
|
24
|
+
const origin = hostOrigin.replace(/\/+$/, "");
|
|
25
|
+
const path = type ? `${segment}/${type}` : segment;
|
|
26
|
+
return `${origin}${RUNTIME_BASE_PATH}/${path}`;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// src/transport/hello.ts
|
|
30
|
+
import { PROTOCOL_NAME } from "@sunbay/checkout-sdk-protocol";
|
|
31
|
+
function isValidRuntimeHello(value) {
|
|
32
|
+
if (typeof value !== "object" || value === null) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
const r = value;
|
|
36
|
+
if (r.protocol !== PROTOCOL_NAME || r.kind !== "hello") {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
if (typeof r.runtimeId !== "string" || r.runtimeId.length === 0) {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
if (!Array.isArray(r.supportedMajorVersions) || !r.supportedMajorVersions.every((v) => typeof v === "number" && Number.isFinite(v))) {
|
|
43
|
+
return false;
|
|
44
|
+
}
|
|
45
|
+
if (r.capabilities !== void 0 && (!Array.isArray(r.capabilities) || !r.capabilities.every((c) => typeof c === "string"))) {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/transport/MainFrameTransport.ts
|
|
9
52
|
var DEFAULT_REQUEST_TIMEOUTS = {
|
|
10
53
|
default: 1e4,
|
|
11
54
|
initialize: 3e4,
|
|
12
55
|
handshake: 15e3
|
|
13
56
|
};
|
|
57
|
+
function rethrowAsync(err) {
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
throw err;
|
|
60
|
+
}, 0);
|
|
61
|
+
}
|
|
14
62
|
function genId(prefix) {
|
|
15
63
|
const rand = Math.random().toString(36).slice(2);
|
|
16
64
|
return `${prefix}_${Date.now().toString(36)}_${rand}`;
|
|
@@ -28,7 +76,7 @@ function isRuntimeHello(value) {
|
|
|
28
76
|
return false;
|
|
29
77
|
}
|
|
30
78
|
const record = value;
|
|
31
|
-
return record.protocol ===
|
|
79
|
+
return record.protocol === PROTOCOL_NAME2 && record.kind === "hello";
|
|
32
80
|
}
|
|
33
81
|
var MainFrameTransport = class {
|
|
34
82
|
constructor(options, timeouts = DEFAULT_REQUEST_TIMEOUTS) {
|
|
@@ -63,7 +111,7 @@ var MainFrameTransport = class {
|
|
|
63
111
|
return this.iframe;
|
|
64
112
|
}
|
|
65
113
|
const iframe = document.createElement("iframe");
|
|
66
|
-
iframe.src = `${this.hostOrigin}
|
|
114
|
+
iframe.src = `${runtimeUrl(this.hostOrigin, "main")}?checkoutId=${this.checkoutId}`;
|
|
67
115
|
iframe.setAttribute("aria-hidden", "true");
|
|
68
116
|
iframe.title = "sunbay-checkout-main";
|
|
69
117
|
iframe.style.position = "absolute";
|
|
@@ -102,7 +150,7 @@ var MainFrameTransport = class {
|
|
|
102
150
|
}
|
|
103
151
|
const requestId = genId("req");
|
|
104
152
|
const envelope = {
|
|
105
|
-
protocol:
|
|
153
|
+
protocol: PROTOCOL_NAME2,
|
|
106
154
|
major: PROTOCOL_MAJOR,
|
|
107
155
|
runtimeId: this.runtimeId,
|
|
108
156
|
checkoutId: this.checkoutId,
|
|
@@ -151,7 +199,8 @@ var MainFrameTransport = class {
|
|
|
151
199
|
}
|
|
152
200
|
/**
|
|
153
201
|
* Register the handshake (RuntimeHello) callback. The hello phase has no checkoutId yet and is
|
|
154
|
-
* handled separately.
|
|
202
|
+
* handled separately. The callback is one-shot: it is cleared after the first valid hello from
|
|
203
|
+
* this transport's own iframe; malformed hellos are ignored.
|
|
155
204
|
*
|
|
156
205
|
* @param handler - Called when a hello is received.
|
|
157
206
|
*/
|
|
@@ -255,6 +304,10 @@ var MainFrameTransport = class {
|
|
|
255
304
|
if (typeof event.data !== "string") {
|
|
256
305
|
return;
|
|
257
306
|
}
|
|
307
|
+
const mainWindow = this.iframe?.contentWindow;
|
|
308
|
+
if (!mainWindow || event.source !== mainWindow) {
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
258
311
|
let raw;
|
|
259
312
|
try {
|
|
260
313
|
raw = JSON.parse(event.data);
|
|
@@ -262,8 +315,17 @@ var MainFrameTransport = class {
|
|
|
262
315
|
return;
|
|
263
316
|
}
|
|
264
317
|
if (isRuntimeHello(raw)) {
|
|
265
|
-
if (
|
|
266
|
-
|
|
318
|
+
if (!isValidRuntimeHello(raw)) {
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
const handler = this.helloHandler;
|
|
322
|
+
this.helloHandler = null;
|
|
323
|
+
if (handler) {
|
|
324
|
+
try {
|
|
325
|
+
handler(raw);
|
|
326
|
+
} catch (err) {
|
|
327
|
+
rethrowAsync(err);
|
|
328
|
+
}
|
|
267
329
|
}
|
|
268
330
|
return;
|
|
269
331
|
}
|
|
@@ -276,6 +338,9 @@ var MainFrameTransport = class {
|
|
|
276
338
|
if (envelope.checkoutId !== this.checkoutId) {
|
|
277
339
|
return;
|
|
278
340
|
}
|
|
341
|
+
if (this.runtimeId !== "" && envelope.runtimeId !== this.runtimeId) {
|
|
342
|
+
return;
|
|
343
|
+
}
|
|
279
344
|
if (envelope.kind === "response") {
|
|
280
345
|
this.handleResponse(envelope);
|
|
281
346
|
} else if (envelope.kind === "event") {
|
|
@@ -302,8 +367,12 @@ var MainFrameTransport = class {
|
|
|
302
367
|
if (!set) {
|
|
303
368
|
return;
|
|
304
369
|
}
|
|
305
|
-
for (const handler of set) {
|
|
306
|
-
|
|
370
|
+
for (const handler of [...set]) {
|
|
371
|
+
try {
|
|
372
|
+
handler(envelope.payload);
|
|
373
|
+
} catch (err) {
|
|
374
|
+
rethrowAsync(err);
|
|
375
|
+
}
|
|
307
376
|
}
|
|
308
377
|
}
|
|
309
378
|
};
|
|
@@ -313,6 +382,7 @@ import {
|
|
|
313
382
|
ErrorCode as ErrorCode2,
|
|
314
383
|
isIframeResizeMessage
|
|
315
384
|
} from "@sunbay/checkout-sdk-protocol";
|
|
385
|
+
var DEFAULT_MOUNT_TIMEOUT_MS = 3e4;
|
|
316
386
|
var FIELD_TYPES = ["cardNumber", "cardExpiry", "cardCvc"];
|
|
317
387
|
var INPUT_TYPES = [
|
|
318
388
|
"cardNumber",
|
|
@@ -345,8 +415,15 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
345
415
|
/** Event handlers temporarily subscribed during mount (unbound after resolve/reject). */
|
|
346
416
|
this.readyHandler = null;
|
|
347
417
|
this.loadErrorHandler = null;
|
|
418
|
+
/**
|
|
419
|
+
* Cancels the in-flight mount (clears its timer/listeners and rejects its promise). Null when no
|
|
420
|
+
* mount is in flight. Used by `unmount()` / `destroy()` while the Element is still loading.
|
|
421
|
+
*/
|
|
422
|
+
this.abortMount = null;
|
|
348
423
|
/** expressCheckout only: the persistently subscribed expressAvailabilityChange handler (unbound on destroy). */
|
|
349
424
|
this.availabilityHandler = null;
|
|
425
|
+
/** Merchant listeners registered via `on`: event → (original handler → transport wrapper). Cleared on destroy. */
|
|
426
|
+
this.userListeners = /* @__PURE__ */ new Map();
|
|
350
427
|
/**
|
|
351
428
|
* iframe auto-height: the window 'message' listener that stays resident during mount (unbound
|
|
352
429
|
* when the iframe disappears/is destroyed). Keeps a reference so removeEventListener can be
|
|
@@ -387,13 +464,20 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
387
464
|
/**
|
|
388
465
|
* Mount at the merchant DOM target: insert a visible iframe, send createElement, await elementReady.
|
|
389
466
|
*
|
|
467
|
+
* Failure modes: a hosted `loadError` / command failure rejects with that error; waiting longer
|
|
468
|
+
* than `opts.timeoutMs` (default 30000ms) removes the iframe and rejects `RUNTIME_LOAD_FAILED`;
|
|
469
|
+
* calling `unmount()` while loading rejects with `RUNTIME_LOAD_FAILED` ("mount cancelled");
|
|
470
|
+
* calling `destroy()` (or destroying the instance) while loading rejects `INSTANCE_DESTROYED`.
|
|
471
|
+
* A late `elementReady` after any of these is ignored and never flips the status to ready.
|
|
472
|
+
*
|
|
390
473
|
* @param selectorOrEl - A CSS selector or an HTMLElement container; the container must be unique and empty.
|
|
474
|
+
* @param opts - Optional mount options (`timeoutMs`).
|
|
391
475
|
* @returns Resolves after elementReady; on failure removes the node and rejects (loadError / INVALID_OPTIONS).
|
|
392
476
|
* @throws INSTANCE_DESTROYED when already destroyed.
|
|
393
477
|
* @example
|
|
394
|
-
* await element.mount('#card-field');
|
|
478
|
+
* await element.mount('#card-field', { timeoutMs: 20000 });
|
|
395
479
|
*/
|
|
396
|
-
mount(selectorOrEl) {
|
|
480
|
+
mount(selectorOrEl, opts) {
|
|
397
481
|
this.assertAlive();
|
|
398
482
|
if (this.status === "loading" || this.status === "ready") {
|
|
399
483
|
return Promise.reject(
|
|
@@ -425,6 +509,10 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
425
509
|
iframe.src = this.resolveSrc();
|
|
426
510
|
iframe.title = `sunbay-checkout-${this.type}`;
|
|
427
511
|
iframe.setAttribute("data-element-id", this.elementId);
|
|
512
|
+
iframe.setAttribute(
|
|
513
|
+
"allow",
|
|
514
|
+
`payment 'self' ${this.hostOrigin}; publickey-credentials-get 'self' ${this.hostOrigin}`
|
|
515
|
+
);
|
|
428
516
|
iframe.style.border = "0";
|
|
429
517
|
iframe.style.width = "100%";
|
|
430
518
|
iframe.style.display = "block";
|
|
@@ -432,8 +520,18 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
432
520
|
container.appendChild(iframe);
|
|
433
521
|
this.iframe = iframe;
|
|
434
522
|
this.installResizeListener();
|
|
523
|
+
const requested = opts?.timeoutMs;
|
|
524
|
+
const timeoutMs = typeof requested === "number" && Number.isFinite(requested) && requested > 0 ? requested : DEFAULT_MOUNT_TIMEOUT_MS;
|
|
435
525
|
return new Promise((resolve, reject) => {
|
|
526
|
+
let settled = false;
|
|
527
|
+
let timer = null;
|
|
436
528
|
const cleanup = () => {
|
|
529
|
+
settled = true;
|
|
530
|
+
if (timer !== null) {
|
|
531
|
+
clearTimeout(timer);
|
|
532
|
+
timer = null;
|
|
533
|
+
}
|
|
534
|
+
this.abortMount = null;
|
|
437
535
|
if (this.readyHandler) {
|
|
438
536
|
this.transport.off("elementReady", this.readyHandler);
|
|
439
537
|
this.readyHandler = null;
|
|
@@ -444,7 +542,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
444
542
|
}
|
|
445
543
|
};
|
|
446
544
|
const onReady = (payload) => {
|
|
447
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
545
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
448
546
|
return;
|
|
449
547
|
}
|
|
450
548
|
cleanup();
|
|
@@ -456,12 +554,13 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
456
554
|
resolve();
|
|
457
555
|
};
|
|
458
556
|
const onLoadError = (payload) => {
|
|
459
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
557
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
460
558
|
return;
|
|
461
559
|
}
|
|
462
560
|
cleanup();
|
|
463
561
|
this.status = "loadError";
|
|
464
562
|
this.removeIframe();
|
|
563
|
+
this.releaseHostedElement();
|
|
465
564
|
const err = payload?.error;
|
|
466
565
|
reject(
|
|
467
566
|
err ?? makeError2(
|
|
@@ -475,18 +574,56 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
475
574
|
this.loadErrorHandler = onLoadError;
|
|
476
575
|
this.transport.on("elementReady", onReady);
|
|
477
576
|
this.transport.on("loadError", onLoadError);
|
|
577
|
+
this.abortMount = (reason) => {
|
|
578
|
+
if (settled) {
|
|
579
|
+
return;
|
|
580
|
+
}
|
|
581
|
+
cleanup();
|
|
582
|
+
reject(reason);
|
|
583
|
+
};
|
|
584
|
+
timer = setTimeout(() => {
|
|
585
|
+
if (settled) {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
cleanup();
|
|
589
|
+
this.status = "loadError";
|
|
590
|
+
this.removeIframe();
|
|
591
|
+
this.releaseHostedElement();
|
|
592
|
+
reject(
|
|
593
|
+
makeError2(
|
|
594
|
+
ErrorCode2.RUNTIME_LOAD_FAILED,
|
|
595
|
+
`Element did not become ready within ${timeoutMs}ms`,
|
|
596
|
+
this.elementId
|
|
597
|
+
)
|
|
598
|
+
);
|
|
599
|
+
}, timeoutMs);
|
|
478
600
|
this.transport.request("createElement", {
|
|
479
601
|
elementId: this.elementId,
|
|
480
602
|
type: this.type,
|
|
481
603
|
options: this.options
|
|
482
|
-
}).
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
604
|
+
}).then(
|
|
605
|
+
() => void 0,
|
|
606
|
+
(cmdErr) => {
|
|
607
|
+
if (settled) {
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
cleanup();
|
|
611
|
+
this.status = "loadError";
|
|
612
|
+
this.removeIframe();
|
|
613
|
+
reject(cmdErr);
|
|
614
|
+
}
|
|
615
|
+
);
|
|
488
616
|
});
|
|
489
617
|
}
|
|
618
|
+
/** Best-effort, fire-and-forget release of the hosted element registration; never throws. */
|
|
619
|
+
releaseHostedElement() {
|
|
620
|
+
try {
|
|
621
|
+
void Promise.resolve(
|
|
622
|
+
this.transport.request("destroyElement", { elementId: this.elementId })
|
|
623
|
+
).catch(() => void 0);
|
|
624
|
+
} catch {
|
|
625
|
+
}
|
|
626
|
+
}
|
|
490
627
|
/**
|
|
491
628
|
* Unmount: send destroyElement, remove the iframe; idempotent; keeps the handle and configuration.
|
|
492
629
|
*
|
|
@@ -498,6 +635,13 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
498
635
|
if (this.status !== "ready" && this.status !== "loading") {
|
|
499
636
|
return;
|
|
500
637
|
}
|
|
638
|
+
this.abortMount?.(
|
|
639
|
+
makeError2(
|
|
640
|
+
ErrorCode2.RUNTIME_LOAD_FAILED,
|
|
641
|
+
"Mount cancelled: the Element was unmounted while loading",
|
|
642
|
+
this.elementId
|
|
643
|
+
)
|
|
644
|
+
);
|
|
501
645
|
try {
|
|
502
646
|
await this.transport.request("destroyElement", { elementId: this.elementId });
|
|
503
647
|
} finally {
|
|
@@ -513,6 +657,14 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
513
657
|
if (this.status === "destroyed") {
|
|
514
658
|
return;
|
|
515
659
|
}
|
|
660
|
+
const wasHosted = this.status === "loading" || this.status === "ready";
|
|
661
|
+
this.abortMount?.(
|
|
662
|
+
makeError2(
|
|
663
|
+
ErrorCode2.INSTANCE_DESTROYED,
|
|
664
|
+
"Mount cancelled: the Element was destroyed while loading",
|
|
665
|
+
this.elementId
|
|
666
|
+
)
|
|
667
|
+
);
|
|
516
668
|
if (this.readyHandler) {
|
|
517
669
|
this.transport.off("elementReady", this.readyHandler);
|
|
518
670
|
this.readyHandler = null;
|
|
@@ -525,9 +677,23 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
525
677
|
this.transport.off("expressAvailabilityChange", this.availabilityHandler);
|
|
526
678
|
this.availabilityHandler = null;
|
|
527
679
|
}
|
|
680
|
+
for (const [event, wrappers] of this.userListeners) {
|
|
681
|
+
for (const wrapper of wrappers.values()) {
|
|
682
|
+
this.transport.off(event, wrapper);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
this.userListeners.clear();
|
|
528
686
|
this.removeIframe();
|
|
529
687
|
this.container = null;
|
|
530
688
|
this.status = "destroyed";
|
|
689
|
+
if (wasHosted) {
|
|
690
|
+
try {
|
|
691
|
+
void Promise.resolve(
|
|
692
|
+
this.transport.request("destroyElement", { elementId: this.elementId })
|
|
693
|
+
).catch(() => void 0);
|
|
694
|
+
} catch {
|
|
695
|
+
}
|
|
696
|
+
}
|
|
531
697
|
this.onDestroy?.(this.elementId);
|
|
532
698
|
}
|
|
533
699
|
/**
|
|
@@ -557,7 +723,24 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
557
723
|
*/
|
|
558
724
|
on(event, handler) {
|
|
559
725
|
this.assertAlive();
|
|
560
|
-
this.
|
|
726
|
+
let wrappers = this.userListeners.get(event);
|
|
727
|
+
if (!wrappers) {
|
|
728
|
+
wrappers = /* @__PURE__ */ new Map();
|
|
729
|
+
this.userListeners.set(event, wrappers);
|
|
730
|
+
}
|
|
731
|
+
if (wrappers.has(handler)) {
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const typed = handler;
|
|
735
|
+
const wrapper = (payload) => {
|
|
736
|
+
const target = payload?.elementId;
|
|
737
|
+
if (target !== void 0 && target !== this.elementId) {
|
|
738
|
+
return;
|
|
739
|
+
}
|
|
740
|
+
typed(payload);
|
|
741
|
+
};
|
|
742
|
+
wrappers.set(typed, wrapper);
|
|
743
|
+
this.transport.on(event, wrapper);
|
|
561
744
|
}
|
|
562
745
|
/**
|
|
563
746
|
* Unregister an event handler (still safe to call after destruction).
|
|
@@ -566,7 +749,13 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
566
749
|
* @param handler - The previously registered callback.
|
|
567
750
|
*/
|
|
568
751
|
off(event, handler) {
|
|
569
|
-
this.
|
|
752
|
+
const wrappers = this.userListeners.get(event);
|
|
753
|
+
const wrapper = wrappers?.get(handler);
|
|
754
|
+
if (!wrappers || !wrapper) {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
wrappers.delete(handler);
|
|
758
|
+
this.transport.off(event, wrapper);
|
|
570
759
|
}
|
|
571
760
|
/** Install input-class action methods (automatically called after construction). */
|
|
572
761
|
installInputActions() {
|
|
@@ -623,7 +812,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
623
812
|
return node instanceof HTMLElement ? node : null;
|
|
624
813
|
}
|
|
625
814
|
/**
|
|
626
|
-
* Resolve the iframe src: split fields go to /sdk/field/, the rest to /sdk/element/, with
|
|
815
|
+
* Resolve the iframe src: split fields go to /embed/sdk/field/, the rest to /embed/sdk/element/, with
|
|
627
816
|
* checkoutId and elementId; if an initial colorScheme is provided, append `&colorScheme=<value>`
|
|
628
817
|
* (no flash on the first frame); if the initial appearance contains variables or rules, append
|
|
629
818
|
* `&appearance=<encodeURIComponent(JSON.stringify(appearance))>` (takes effect on the first
|
|
@@ -655,7 +844,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
655
844
|
if (this.type === "expressCheckout") {
|
|
656
845
|
query += this.resolveExpressQuery();
|
|
657
846
|
}
|
|
658
|
-
return `${this.hostOrigin
|
|
847
|
+
return `${runtimeUrl(this.hostOrigin, segment, this.type)}${query}`;
|
|
659
848
|
}
|
|
660
849
|
/**
|
|
661
850
|
* Extract a per-element appearance (from the Element's own options, i.e. the second
|
|
@@ -767,6 +956,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
767
956
|
import {
|
|
768
957
|
ErrorCode as ErrorCode3
|
|
769
958
|
} from "@sunbay/checkout-sdk-protocol";
|
|
959
|
+
var CONFIRM_RESULT_DEADLINE_MS = 12e4;
|
|
770
960
|
var CARD_FIELD_TYPES = ["cardNumber", "cardExpiry", "cardCvc"];
|
|
771
961
|
function genId2(prefix) {
|
|
772
962
|
const rand = Math.random().toString(36).slice(2);
|
|
@@ -839,12 +1029,36 @@ var CheckoutInstance = class {
|
|
|
839
1029
|
return element;
|
|
840
1030
|
}
|
|
841
1031
|
/**
|
|
842
|
-
* Initiate a payment confirmation. Generates an operationId, sends the confirm command
|
|
843
|
-
*
|
|
844
|
-
*
|
|
1032
|
+
* Initiate a payment confirmation. Generates an operationId, sends the `confirm` command, and
|
|
1033
|
+
* returns a Promise fulfilled by `paymentResult` events with the same operationId.
|
|
1034
|
+
*
|
|
1035
|
+
* Behavior changes in 0.0.7: previously the Promise resolved on the first `paymentResult`,
|
|
1036
|
+
* including a non-final `pending` outcome. Now it resolves ONLY on a terminal outcome
|
|
1037
|
+
* (`succeeded` | `failed` | `canceled`); a `pending` outcome keeps it waiting.
|
|
1038
|
+
*
|
|
1039
|
+
* Watchdog: if no terminal outcome arrives within `resultTimeoutMs` (default
|
|
1040
|
+
* {@link CONFIRM_RESULT_DEADLINE_MS} = 120000ms) the Promise resolves
|
|
1041
|
+
* `{ status: 'pending', operationId, txnId? }`. This is NOT a failure: the payment may still
|
|
1042
|
+
* complete, so never treat it as failed and never retry blindly. Reconcile with
|
|
1043
|
+
* `getPaymentStatus()` or the `paymentResult` event.
|
|
845
1044
|
*
|
|
846
|
-
*
|
|
847
|
-
*
|
|
1045
|
+
* Slot semantics (duplicate-submit protection): the confirm slot stays occupied until a
|
|
1046
|
+
* terminal outcome arrives or `destroy()` is called. While it is occupied another `confirm()`
|
|
1047
|
+
* rejects with `CHECKOUT_BUSY`; this also holds after the watchdog resolved `pending`, and later
|
|
1048
|
+
* terminal outcomes are still delivered through `on('paymentResult', ...)`.
|
|
1049
|
+
*
|
|
1050
|
+
* If the `confirm` command is rejected by the runtime the payment was not started: the Promise
|
|
1051
|
+
* rejects with that error and the slot is freed. If the command itself times out
|
|
1052
|
+
* (`REQUEST_TIMEOUT`) the runtime may have started the payment: the Promise rejects with the
|
|
1053
|
+
* error augmented with `paymentStateKnown: false`, the slot stays occupied and a later terminal
|
|
1054
|
+
* `paymentResult` for this operation frees it. Do not retry; reconcile with `getPaymentStatus()`.
|
|
1055
|
+
*
|
|
1056
|
+
* `error` events with `paymentStateKnown === false` are informational and do not settle the
|
|
1057
|
+
* Promise.
|
|
1058
|
+
*
|
|
1059
|
+
* @param opts - Optional billing/shipping address, name, email, returnUrl, plus `onOperation`
|
|
1060
|
+
* and `resultTimeoutMs`.
|
|
1061
|
+
* @returns The terminal PaymentOutcome, or `{ status: 'pending' }` when the watchdog fires.
|
|
848
1062
|
* @throws rejects INSTANCE_DESTROYED when destroyed; rejects CHECKOUT_BUSY when in progress.
|
|
849
1063
|
*/
|
|
850
1064
|
confirm(opts) {
|
|
@@ -858,15 +1072,57 @@ var CheckoutInstance = class {
|
|
|
858
1072
|
makeError3(ErrorCode3.CHECKOUT_BUSY, "A payment confirmation is already in progress", "runtime")
|
|
859
1073
|
);
|
|
860
1074
|
}
|
|
1075
|
+
const { onOperation, resultTimeoutMs, ...rest } = opts ?? {};
|
|
861
1076
|
const operationId = genId2("op");
|
|
862
|
-
const payload = { ...
|
|
1077
|
+
const payload = { ...rest, operationId };
|
|
1078
|
+
const deadline = typeof resultTimeoutMs === "number" && Number.isFinite(resultTimeoutMs) && resultTimeoutMs > 0 ? resultTimeoutMs : CONFIRM_RESULT_DEADLINE_MS;
|
|
863
1079
|
return new Promise((resolve, reject) => {
|
|
864
|
-
|
|
1080
|
+
const entry = {
|
|
1081
|
+
operationId,
|
|
1082
|
+
settled: false,
|
|
1083
|
+
resolve,
|
|
1084
|
+
reject,
|
|
1085
|
+
watchdog: null,
|
|
1086
|
+
lastPendingTxnId: void 0
|
|
1087
|
+
};
|
|
1088
|
+
this.pendingConfirm = entry;
|
|
1089
|
+
entry.watchdog = setTimeout(() => {
|
|
1090
|
+
entry.watchdog = null;
|
|
1091
|
+
if (entry.settled) {
|
|
1092
|
+
return;
|
|
1093
|
+
}
|
|
1094
|
+
entry.settled = true;
|
|
1095
|
+
const outcome = entry.lastPendingTxnId !== void 0 ? { status: "pending", operationId, txnId: entry.lastPendingTxnId } : { status: "pending", operationId };
|
|
1096
|
+
resolve(outcome);
|
|
1097
|
+
}, deadline);
|
|
1098
|
+
if (typeof onOperation === "function") {
|
|
1099
|
+
try {
|
|
1100
|
+
onOperation(operationId);
|
|
1101
|
+
} catch (err) {
|
|
1102
|
+
setTimeout(() => {
|
|
1103
|
+
throw err;
|
|
1104
|
+
}, 0);
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
865
1107
|
this.transport.request("confirm", payload).catch((err) => {
|
|
866
|
-
if (this.pendingConfirm
|
|
867
|
-
|
|
1108
|
+
if (this.pendingConfirm !== entry) {
|
|
1109
|
+
return;
|
|
1110
|
+
}
|
|
1111
|
+
const error = err;
|
|
1112
|
+
if (error && error.code === ErrorCode3.REQUEST_TIMEOUT) {
|
|
1113
|
+
if (!entry.settled) {
|
|
1114
|
+
entry.settled = true;
|
|
1115
|
+
this.clearWatchdog(entry);
|
|
1116
|
+
reject({ ...error, paymentStateKnown: false });
|
|
1117
|
+
}
|
|
1118
|
+
return;
|
|
1119
|
+
}
|
|
1120
|
+
this.pendingConfirm = null;
|
|
1121
|
+
this.clearWatchdog(entry);
|
|
1122
|
+
if (!entry.settled) {
|
|
1123
|
+
entry.settled = true;
|
|
1124
|
+
reject(error);
|
|
868
1125
|
}
|
|
869
|
-
reject(err);
|
|
870
1126
|
});
|
|
871
1127
|
});
|
|
872
1128
|
}
|
|
@@ -952,10 +1208,15 @@ var CheckoutInstance = class {
|
|
|
952
1208
|
this.elements.clear();
|
|
953
1209
|
this.transport.off("paymentResult", this.paymentResultHandler);
|
|
954
1210
|
if (this.pendingConfirm) {
|
|
955
|
-
this.pendingConfirm
|
|
956
|
-
makeError3(ErrorCode3.INSTANCE_DESTROYED, "Checkout instance has been destroyed", "runtime")
|
|
957
|
-
);
|
|
1211
|
+
const entry = this.pendingConfirm;
|
|
958
1212
|
this.pendingConfirm = null;
|
|
1213
|
+
this.clearWatchdog(entry);
|
|
1214
|
+
if (!entry.settled) {
|
|
1215
|
+
entry.settled = true;
|
|
1216
|
+
entry.reject(
|
|
1217
|
+
makeError3(ErrorCode3.INSTANCE_DESTROYED, "Checkout instance has been destroyed", "runtime")
|
|
1218
|
+
);
|
|
1219
|
+
}
|
|
959
1220
|
}
|
|
960
1221
|
void Promise.resolve(this.transport.request("destroy", {})).catch(() => void 0);
|
|
961
1222
|
this.transport.destroy();
|
|
@@ -1043,26 +1304,32 @@ var CheckoutInstance = class {
|
|
|
1043
1304
|
if (evt.operationId !== pending.operationId) {
|
|
1044
1305
|
return;
|
|
1045
1306
|
}
|
|
1046
|
-
|
|
1307
|
+
const outcome = evt.outcome;
|
|
1308
|
+
if (!outcome) {
|
|
1309
|
+
return;
|
|
1310
|
+
}
|
|
1311
|
+
if (outcome.status === "pending") {
|
|
1312
|
+
pending.lastPendingTxnId = outcome.txnId ?? pending.lastPendingTxnId;
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (outcome.status !== "succeeded" && outcome.status !== "failed" && outcome.status !== "canceled") {
|
|
1047
1316
|
return;
|
|
1048
1317
|
}
|
|
1049
1318
|
this.pendingConfirm = null;
|
|
1050
|
-
|
|
1319
|
+
this.clearWatchdog(pending);
|
|
1320
|
+
if (!pending.settled) {
|
|
1321
|
+
pending.settled = true;
|
|
1322
|
+
pending.resolve(outcome);
|
|
1323
|
+
}
|
|
1051
1324
|
}
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
prod: "https://checkout.sunbay.us"
|
|
1059
|
-
};
|
|
1060
|
-
function resolveHostOrigin(env = "prod", override) {
|
|
1061
|
-
if (override) {
|
|
1062
|
-
return override;
|
|
1325
|
+
/** Clear a pending confirm's watchdog timer (idempotent). */
|
|
1326
|
+
clearWatchdog(entry) {
|
|
1327
|
+
if (entry.watchdog !== null) {
|
|
1328
|
+
clearTimeout(entry.watchdog);
|
|
1329
|
+
entry.watchdog = null;
|
|
1330
|
+
}
|
|
1063
1331
|
}
|
|
1064
|
-
|
|
1065
|
-
}
|
|
1332
|
+
};
|
|
1066
1333
|
|
|
1067
1334
|
// src/createCheckout.ts
|
|
1068
1335
|
import {
|
|
@@ -1084,6 +1351,9 @@ function makeError4(code, message) {
|
|
|
1084
1351
|
}
|
|
1085
1352
|
var defaultTransportFactory = ({ checkoutId, hostOrigin }) => new MainFrameTransport({ checkoutId, hostOrigin });
|
|
1086
1353
|
async function createCheckout(opts) {
|
|
1354
|
+
if (typeof opts.checkoutClientSecret !== "string" || opts.checkoutClientSecret.trim() === "") {
|
|
1355
|
+
throw makeError4(ErrorCode4.INVALID_OPTIONS, "checkoutClientSecret is required");
|
|
1356
|
+
}
|
|
1087
1357
|
const checkoutId = genId3("co");
|
|
1088
1358
|
const hostOrigin = resolveHostOrigin(opts.env, opts.hostOrigin);
|
|
1089
1359
|
const factory = opts.__transportFactory ?? defaultTransportFactory;
|
|
@@ -1106,6 +1376,7 @@ async function createCheckout(opts) {
|
|
|
1106
1376
|
transport.setRuntimeId(hello.runtimeId);
|
|
1107
1377
|
const initReq = {
|
|
1108
1378
|
sessionId: opts.sessionId,
|
|
1379
|
+
checkoutClientSecret: opts.checkoutClientSecret,
|
|
1109
1380
|
locale: opts.locale,
|
|
1110
1381
|
appearance: opts.appearance,
|
|
1111
1382
|
paymentMethods: opts.paymentMethods
|
|
@@ -1138,7 +1409,7 @@ function waitForHello(transport) {
|
|
|
1138
1409
|
reject(makeError4(ErrorCode4.RUNTIME_LOAD_FAILED, "Timed out waiting for hosted runtime handshake"));
|
|
1139
1410
|
}, HANDSHAKE_TIMEOUT_MS);
|
|
1140
1411
|
transport.onHello((raw) => {
|
|
1141
|
-
if (settled) {
|
|
1412
|
+
if (settled || !isValidRuntimeHello(raw)) {
|
|
1142
1413
|
return;
|
|
1143
1414
|
}
|
|
1144
1415
|
settled = true;
|
|
@@ -1156,7 +1427,7 @@ if (typeof window !== "undefined") {
|
|
|
1156
1427
|
const w = window;
|
|
1157
1428
|
w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
|
|
1158
1429
|
}
|
|
1159
|
-
var version = true ? "0.0.
|
|
1430
|
+
var version = true ? "0.0.7" : "0.0.0";
|
|
1160
1431
|
export {
|
|
1161
1432
|
CheckoutElement,
|
|
1162
1433
|
CheckoutInstance,
|