@sunbay/checkout-sdk-js 0.0.5 → 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 +351 -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 +314 -50
- package/dist/index.global.js.map +1 -1
- package/dist/index.mjs +320 -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(
|
|
@@ -436,8 +520,18 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
436
520
|
container.appendChild(iframe);
|
|
437
521
|
this.iframe = iframe;
|
|
438
522
|
this.installResizeListener();
|
|
523
|
+
const requested = opts?.timeoutMs;
|
|
524
|
+
const timeoutMs = typeof requested === "number" && Number.isFinite(requested) && requested > 0 ? requested : DEFAULT_MOUNT_TIMEOUT_MS;
|
|
439
525
|
return new Promise((resolve, reject) => {
|
|
526
|
+
let settled = false;
|
|
527
|
+
let timer = null;
|
|
440
528
|
const cleanup = () => {
|
|
529
|
+
settled = true;
|
|
530
|
+
if (timer !== null) {
|
|
531
|
+
clearTimeout(timer);
|
|
532
|
+
timer = null;
|
|
533
|
+
}
|
|
534
|
+
this.abortMount = null;
|
|
441
535
|
if (this.readyHandler) {
|
|
442
536
|
this.transport.off("elementReady", this.readyHandler);
|
|
443
537
|
this.readyHandler = null;
|
|
@@ -448,7 +542,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
448
542
|
}
|
|
449
543
|
};
|
|
450
544
|
const onReady = (payload) => {
|
|
451
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
545
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
452
546
|
return;
|
|
453
547
|
}
|
|
454
548
|
cleanup();
|
|
@@ -460,12 +554,13 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
460
554
|
resolve();
|
|
461
555
|
};
|
|
462
556
|
const onLoadError = (payload) => {
|
|
463
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
557
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
464
558
|
return;
|
|
465
559
|
}
|
|
466
560
|
cleanup();
|
|
467
561
|
this.status = "loadError";
|
|
468
562
|
this.removeIframe();
|
|
563
|
+
this.releaseHostedElement();
|
|
469
564
|
const err = payload?.error;
|
|
470
565
|
reject(
|
|
471
566
|
err ?? makeError2(
|
|
@@ -479,18 +574,56 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
479
574
|
this.loadErrorHandler = onLoadError;
|
|
480
575
|
this.transport.on("elementReady", onReady);
|
|
481
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);
|
|
482
600
|
this.transport.request("createElement", {
|
|
483
601
|
elementId: this.elementId,
|
|
484
602
|
type: this.type,
|
|
485
603
|
options: this.options
|
|
486
|
-
}).
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
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
|
+
);
|
|
492
616
|
});
|
|
493
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
|
+
}
|
|
494
627
|
/**
|
|
495
628
|
* Unmount: send destroyElement, remove the iframe; idempotent; keeps the handle and configuration.
|
|
496
629
|
*
|
|
@@ -502,6 +635,13 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
502
635
|
if (this.status !== "ready" && this.status !== "loading") {
|
|
503
636
|
return;
|
|
504
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
|
+
);
|
|
505
645
|
try {
|
|
506
646
|
await this.transport.request("destroyElement", { elementId: this.elementId });
|
|
507
647
|
} finally {
|
|
@@ -517,6 +657,14 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
517
657
|
if (this.status === "destroyed") {
|
|
518
658
|
return;
|
|
519
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
|
+
);
|
|
520
668
|
if (this.readyHandler) {
|
|
521
669
|
this.transport.off("elementReady", this.readyHandler);
|
|
522
670
|
this.readyHandler = null;
|
|
@@ -529,9 +677,23 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
529
677
|
this.transport.off("expressAvailabilityChange", this.availabilityHandler);
|
|
530
678
|
this.availabilityHandler = null;
|
|
531
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();
|
|
532
686
|
this.removeIframe();
|
|
533
687
|
this.container = null;
|
|
534
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
|
+
}
|
|
535
697
|
this.onDestroy?.(this.elementId);
|
|
536
698
|
}
|
|
537
699
|
/**
|
|
@@ -561,7 +723,24 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
561
723
|
*/
|
|
562
724
|
on(event, handler) {
|
|
563
725
|
this.assertAlive();
|
|
564
|
-
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);
|
|
565
744
|
}
|
|
566
745
|
/**
|
|
567
746
|
* Unregister an event handler (still safe to call after destruction).
|
|
@@ -570,7 +749,13 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
570
749
|
* @param handler - The previously registered callback.
|
|
571
750
|
*/
|
|
572
751
|
off(event, handler) {
|
|
573
|
-
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);
|
|
574
759
|
}
|
|
575
760
|
/** Install input-class action methods (automatically called after construction). */
|
|
576
761
|
installInputActions() {
|
|
@@ -627,7 +812,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
627
812
|
return node instanceof HTMLElement ? node : null;
|
|
628
813
|
}
|
|
629
814
|
/**
|
|
630
|
-
* 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
|
|
631
816
|
* checkoutId and elementId; if an initial colorScheme is provided, append `&colorScheme=<value>`
|
|
632
817
|
* (no flash on the first frame); if the initial appearance contains variables or rules, append
|
|
633
818
|
* `&appearance=<encodeURIComponent(JSON.stringify(appearance))>` (takes effect on the first
|
|
@@ -659,7 +844,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
659
844
|
if (this.type === "expressCheckout") {
|
|
660
845
|
query += this.resolveExpressQuery();
|
|
661
846
|
}
|
|
662
|
-
return `${this.hostOrigin
|
|
847
|
+
return `${runtimeUrl(this.hostOrigin, segment, this.type)}${query}`;
|
|
663
848
|
}
|
|
664
849
|
/**
|
|
665
850
|
* Extract a per-element appearance (from the Element's own options, i.e. the second
|
|
@@ -771,6 +956,7 @@ var CheckoutElement = class _CheckoutElement {
|
|
|
771
956
|
import {
|
|
772
957
|
ErrorCode as ErrorCode3
|
|
773
958
|
} from "@sunbay/checkout-sdk-protocol";
|
|
959
|
+
var CONFIRM_RESULT_DEADLINE_MS = 12e4;
|
|
774
960
|
var CARD_FIELD_TYPES = ["cardNumber", "cardExpiry", "cardCvc"];
|
|
775
961
|
function genId2(prefix) {
|
|
776
962
|
const rand = Math.random().toString(36).slice(2);
|
|
@@ -843,12 +1029,36 @@ var CheckoutInstance = class {
|
|
|
843
1029
|
return element;
|
|
844
1030
|
}
|
|
845
1031
|
/**
|
|
846
|
-
* Initiate a payment confirmation. Generates an operationId, sends the confirm command
|
|
847
|
-
*
|
|
848
|
-
*
|
|
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.
|
|
849
1044
|
*
|
|
850
|
-
*
|
|
851
|
-
*
|
|
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.
|
|
852
1062
|
* @throws rejects INSTANCE_DESTROYED when destroyed; rejects CHECKOUT_BUSY when in progress.
|
|
853
1063
|
*/
|
|
854
1064
|
confirm(opts) {
|
|
@@ -862,15 +1072,57 @@ var CheckoutInstance = class {
|
|
|
862
1072
|
makeError3(ErrorCode3.CHECKOUT_BUSY, "A payment confirmation is already in progress", "runtime")
|
|
863
1073
|
);
|
|
864
1074
|
}
|
|
1075
|
+
const { onOperation, resultTimeoutMs, ...rest } = opts ?? {};
|
|
865
1076
|
const operationId = genId2("op");
|
|
866
|
-
const payload = { ...
|
|
1077
|
+
const payload = { ...rest, operationId };
|
|
1078
|
+
const deadline = typeof resultTimeoutMs === "number" && Number.isFinite(resultTimeoutMs) && resultTimeoutMs > 0 ? resultTimeoutMs : CONFIRM_RESULT_DEADLINE_MS;
|
|
867
1079
|
return new Promise((resolve, reject) => {
|
|
868
|
-
|
|
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
|
+
}
|
|
869
1107
|
this.transport.request("confirm", payload).catch((err) => {
|
|
870
|
-
if (this.pendingConfirm
|
|
871
|
-
|
|
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);
|
|
872
1125
|
}
|
|
873
|
-
reject(err);
|
|
874
1126
|
});
|
|
875
1127
|
});
|
|
876
1128
|
}
|
|
@@ -956,10 +1208,15 @@ var CheckoutInstance = class {
|
|
|
956
1208
|
this.elements.clear();
|
|
957
1209
|
this.transport.off("paymentResult", this.paymentResultHandler);
|
|
958
1210
|
if (this.pendingConfirm) {
|
|
959
|
-
this.pendingConfirm
|
|
960
|
-
makeError3(ErrorCode3.INSTANCE_DESTROYED, "Checkout instance has been destroyed", "runtime")
|
|
961
|
-
);
|
|
1211
|
+
const entry = this.pendingConfirm;
|
|
962
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
|
+
}
|
|
963
1220
|
}
|
|
964
1221
|
void Promise.resolve(this.transport.request("destroy", {})).catch(() => void 0);
|
|
965
1222
|
this.transport.destroy();
|
|
@@ -1047,26 +1304,32 @@ var CheckoutInstance = class {
|
|
|
1047
1304
|
if (evt.operationId !== pending.operationId) {
|
|
1048
1305
|
return;
|
|
1049
1306
|
}
|
|
1050
|
-
|
|
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") {
|
|
1051
1316
|
return;
|
|
1052
1317
|
}
|
|
1053
1318
|
this.pendingConfirm = null;
|
|
1054
|
-
|
|
1319
|
+
this.clearWatchdog(pending);
|
|
1320
|
+
if (!pending.settled) {
|
|
1321
|
+
pending.settled = true;
|
|
1322
|
+
pending.resolve(outcome);
|
|
1323
|
+
}
|
|
1055
1324
|
}
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
prod: "https://checkout.sunbay.us"
|
|
1063
|
-
};
|
|
1064
|
-
function resolveHostOrigin(env = "prod", override) {
|
|
1065
|
-
if (override) {
|
|
1066
|
-
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
|
+
}
|
|
1067
1331
|
}
|
|
1068
|
-
|
|
1069
|
-
}
|
|
1332
|
+
};
|
|
1070
1333
|
|
|
1071
1334
|
// src/createCheckout.ts
|
|
1072
1335
|
import {
|
|
@@ -1088,6 +1351,9 @@ function makeError4(code, message) {
|
|
|
1088
1351
|
}
|
|
1089
1352
|
var defaultTransportFactory = ({ checkoutId, hostOrigin }) => new MainFrameTransport({ checkoutId, hostOrigin });
|
|
1090
1353
|
async function createCheckout(opts) {
|
|
1354
|
+
if (typeof opts.checkoutClientSecret !== "string" || opts.checkoutClientSecret.trim() === "") {
|
|
1355
|
+
throw makeError4(ErrorCode4.INVALID_OPTIONS, "checkoutClientSecret is required");
|
|
1356
|
+
}
|
|
1091
1357
|
const checkoutId = genId3("co");
|
|
1092
1358
|
const hostOrigin = resolveHostOrigin(opts.env, opts.hostOrigin);
|
|
1093
1359
|
const factory = opts.__transportFactory ?? defaultTransportFactory;
|
|
@@ -1110,6 +1376,7 @@ async function createCheckout(opts) {
|
|
|
1110
1376
|
transport.setRuntimeId(hello.runtimeId);
|
|
1111
1377
|
const initReq = {
|
|
1112
1378
|
sessionId: opts.sessionId,
|
|
1379
|
+
checkoutClientSecret: opts.checkoutClientSecret,
|
|
1113
1380
|
locale: opts.locale,
|
|
1114
1381
|
appearance: opts.appearance,
|
|
1115
1382
|
paymentMethods: opts.paymentMethods
|
|
@@ -1142,7 +1409,7 @@ function waitForHello(transport) {
|
|
|
1142
1409
|
reject(makeError4(ErrorCode4.RUNTIME_LOAD_FAILED, "Timed out waiting for hosted runtime handshake"));
|
|
1143
1410
|
}, HANDSHAKE_TIMEOUT_MS);
|
|
1144
1411
|
transport.onHello((raw) => {
|
|
1145
|
-
if (settled) {
|
|
1412
|
+
if (settled || !isValidRuntimeHello(raw)) {
|
|
1146
1413
|
return;
|
|
1147
1414
|
}
|
|
1148
1415
|
settled = true;
|
|
@@ -1160,7 +1427,7 @@ if (typeof window !== "undefined") {
|
|
|
1160
1427
|
const w = window;
|
|
1161
1428
|
w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
|
|
1162
1429
|
}
|
|
1163
|
-
var version = true ? "0.0.
|
|
1430
|
+
var version = true ? "0.0.7" : "0.0.0";
|
|
1164
1431
|
export {
|
|
1165
1432
|
CheckoutElement,
|
|
1166
1433
|
CheckoutInstance,
|