@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.global.js
CHANGED
|
@@ -137,12 +137,57 @@ var SunbayCheckout = (() => {
|
|
|
137
137
|
return msg.type === IFRAME_RESIZE_MESSAGE_TYPE && typeof msg.elementId === "string" && msg.elementId.length > 0 && typeof msg.height === "number" && Number.isFinite(msg.height) && msg.height >= 0;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
+
// src/config/env.ts
|
|
141
|
+
var HOST_ORIGINS = {
|
|
142
|
+
dev: "https://checkout.sunbay-dev.com",
|
|
143
|
+
test: "https://checkout.sunbay-uat.us",
|
|
144
|
+
prod: "https://checkout.sunbay.us"
|
|
145
|
+
};
|
|
146
|
+
function resolveHostOrigin(env = "prod", override) {
|
|
147
|
+
if (override) {
|
|
148
|
+
return override.replace(/\/+$/, "");
|
|
149
|
+
}
|
|
150
|
+
return HOST_ORIGINS[env];
|
|
151
|
+
}
|
|
152
|
+
var RUNTIME_BASE_PATH = "/embed/sdk";
|
|
153
|
+
function runtimeUrl(hostOrigin, segment, type) {
|
|
154
|
+
const origin = hostOrigin.replace(/\/+$/, "");
|
|
155
|
+
const path = type ? `${segment}/${type}` : segment;
|
|
156
|
+
return `${origin}${RUNTIME_BASE_PATH}/${path}`;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// src/transport/hello.ts
|
|
160
|
+
function isValidRuntimeHello(value) {
|
|
161
|
+
if (typeof value !== "object" || value === null) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
const r = value;
|
|
165
|
+
if (r.protocol !== PROTOCOL_NAME || r.kind !== "hello") {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
if (typeof r.runtimeId !== "string" || r.runtimeId.length === 0) {
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
if (!Array.isArray(r.supportedMajorVersions) || !r.supportedMajorVersions.every((v) => typeof v === "number" && Number.isFinite(v))) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
if (r.capabilities !== void 0 && (!Array.isArray(r.capabilities) || !r.capabilities.every((c) => typeof c === "string"))) {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
|
|
140
180
|
// src/transport/MainFrameTransport.ts
|
|
141
181
|
var DEFAULT_REQUEST_TIMEOUTS = {
|
|
142
182
|
default: 1e4,
|
|
143
183
|
initialize: 3e4,
|
|
144
184
|
handshake: 15e3
|
|
145
185
|
};
|
|
186
|
+
function rethrowAsync(err) {
|
|
187
|
+
setTimeout(() => {
|
|
188
|
+
throw err;
|
|
189
|
+
}, 0);
|
|
190
|
+
}
|
|
146
191
|
function genId(prefix) {
|
|
147
192
|
const rand = Math.random().toString(36).slice(2);
|
|
148
193
|
return `${prefix}_${Date.now().toString(36)}_${rand}`;
|
|
@@ -195,7 +240,7 @@ var SunbayCheckout = (() => {
|
|
|
195
240
|
return this.iframe;
|
|
196
241
|
}
|
|
197
242
|
const iframe = document.createElement("iframe");
|
|
198
|
-
iframe.src = `${this.hostOrigin}
|
|
243
|
+
iframe.src = `${runtimeUrl(this.hostOrigin, "main")}?checkoutId=${this.checkoutId}`;
|
|
199
244
|
iframe.setAttribute("aria-hidden", "true");
|
|
200
245
|
iframe.title = "sunbay-checkout-main";
|
|
201
246
|
iframe.style.position = "absolute";
|
|
@@ -283,7 +328,8 @@ var SunbayCheckout = (() => {
|
|
|
283
328
|
}
|
|
284
329
|
/**
|
|
285
330
|
* Register the handshake (RuntimeHello) callback. The hello phase has no checkoutId yet and is
|
|
286
|
-
* handled separately.
|
|
331
|
+
* handled separately. The callback is one-shot: it is cleared after the first valid hello from
|
|
332
|
+
* this transport's own iframe; malformed hellos are ignored.
|
|
287
333
|
*
|
|
288
334
|
* @param handler - Called when a hello is received.
|
|
289
335
|
*/
|
|
@@ -387,6 +433,10 @@ var SunbayCheckout = (() => {
|
|
|
387
433
|
if (typeof event.data !== "string") {
|
|
388
434
|
return;
|
|
389
435
|
}
|
|
436
|
+
const mainWindow = this.iframe?.contentWindow;
|
|
437
|
+
if (!mainWindow || event.source !== mainWindow) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
390
440
|
let raw;
|
|
391
441
|
try {
|
|
392
442
|
raw = JSON.parse(event.data);
|
|
@@ -394,8 +444,17 @@ var SunbayCheckout = (() => {
|
|
|
394
444
|
return;
|
|
395
445
|
}
|
|
396
446
|
if (isRuntimeHello(raw)) {
|
|
397
|
-
if (
|
|
398
|
-
|
|
447
|
+
if (!isValidRuntimeHello(raw)) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const handler = this.helloHandler;
|
|
451
|
+
this.helloHandler = null;
|
|
452
|
+
if (handler) {
|
|
453
|
+
try {
|
|
454
|
+
handler(raw);
|
|
455
|
+
} catch (err) {
|
|
456
|
+
rethrowAsync(err);
|
|
457
|
+
}
|
|
399
458
|
}
|
|
400
459
|
return;
|
|
401
460
|
}
|
|
@@ -408,6 +467,9 @@ var SunbayCheckout = (() => {
|
|
|
408
467
|
if (envelope.checkoutId !== this.checkoutId) {
|
|
409
468
|
return;
|
|
410
469
|
}
|
|
470
|
+
if (this.runtimeId !== "" && envelope.runtimeId !== this.runtimeId) {
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
411
473
|
if (envelope.kind === "response") {
|
|
412
474
|
this.handleResponse(envelope);
|
|
413
475
|
} else if (envelope.kind === "event") {
|
|
@@ -434,13 +496,18 @@ var SunbayCheckout = (() => {
|
|
|
434
496
|
if (!set) {
|
|
435
497
|
return;
|
|
436
498
|
}
|
|
437
|
-
for (const handler of set) {
|
|
438
|
-
|
|
499
|
+
for (const handler of [...set]) {
|
|
500
|
+
try {
|
|
501
|
+
handler(envelope.payload);
|
|
502
|
+
} catch (err) {
|
|
503
|
+
rethrowAsync(err);
|
|
504
|
+
}
|
|
439
505
|
}
|
|
440
506
|
}
|
|
441
507
|
};
|
|
442
508
|
|
|
443
509
|
// src/Element.ts
|
|
510
|
+
var DEFAULT_MOUNT_TIMEOUT_MS = 3e4;
|
|
444
511
|
var FIELD_TYPES = ["cardNumber", "cardExpiry", "cardCvc"];
|
|
445
512
|
var INPUT_TYPES = [
|
|
446
513
|
"cardNumber",
|
|
@@ -473,8 +540,15 @@ var SunbayCheckout = (() => {
|
|
|
473
540
|
/** Event handlers temporarily subscribed during mount (unbound after resolve/reject). */
|
|
474
541
|
this.readyHandler = null;
|
|
475
542
|
this.loadErrorHandler = null;
|
|
543
|
+
/**
|
|
544
|
+
* Cancels the in-flight mount (clears its timer/listeners and rejects its promise). Null when no
|
|
545
|
+
* mount is in flight. Used by `unmount()` / `destroy()` while the Element is still loading.
|
|
546
|
+
*/
|
|
547
|
+
this.abortMount = null;
|
|
476
548
|
/** expressCheckout only: the persistently subscribed expressAvailabilityChange handler (unbound on destroy). */
|
|
477
549
|
this.availabilityHandler = null;
|
|
550
|
+
/** Merchant listeners registered via `on`: event → (original handler → transport wrapper). Cleared on destroy. */
|
|
551
|
+
this.userListeners = /* @__PURE__ */ new Map();
|
|
478
552
|
/**
|
|
479
553
|
* iframe auto-height: the window 'message' listener that stays resident during mount (unbound
|
|
480
554
|
* when the iframe disappears/is destroyed). Keeps a reference so removeEventListener can be
|
|
@@ -515,13 +589,20 @@ var SunbayCheckout = (() => {
|
|
|
515
589
|
/**
|
|
516
590
|
* Mount at the merchant DOM target: insert a visible iframe, send createElement, await elementReady.
|
|
517
591
|
*
|
|
592
|
+
* Failure modes: a hosted `loadError` / command failure rejects with that error; waiting longer
|
|
593
|
+
* than `opts.timeoutMs` (default 30000ms) removes the iframe and rejects `RUNTIME_LOAD_FAILED`;
|
|
594
|
+
* calling `unmount()` while loading rejects with `RUNTIME_LOAD_FAILED` ("mount cancelled");
|
|
595
|
+
* calling `destroy()` (or destroying the instance) while loading rejects `INSTANCE_DESTROYED`.
|
|
596
|
+
* A late `elementReady` after any of these is ignored and never flips the status to ready.
|
|
597
|
+
*
|
|
518
598
|
* @param selectorOrEl - A CSS selector or an HTMLElement container; the container must be unique and empty.
|
|
599
|
+
* @param opts - Optional mount options (`timeoutMs`).
|
|
519
600
|
* @returns Resolves after elementReady; on failure removes the node and rejects (loadError / INVALID_OPTIONS).
|
|
520
601
|
* @throws INSTANCE_DESTROYED when already destroyed.
|
|
521
602
|
* @example
|
|
522
|
-
* await element.mount('#card-field');
|
|
603
|
+
* await element.mount('#card-field', { timeoutMs: 20000 });
|
|
523
604
|
*/
|
|
524
|
-
mount(selectorOrEl) {
|
|
605
|
+
mount(selectorOrEl, opts) {
|
|
525
606
|
this.assertAlive();
|
|
526
607
|
if (this.status === "loading" || this.status === "ready") {
|
|
527
608
|
return Promise.reject(
|
|
@@ -553,6 +634,10 @@ var SunbayCheckout = (() => {
|
|
|
553
634
|
iframe.src = this.resolveSrc();
|
|
554
635
|
iframe.title = `sunbay-checkout-${this.type}`;
|
|
555
636
|
iframe.setAttribute("data-element-id", this.elementId);
|
|
637
|
+
iframe.setAttribute(
|
|
638
|
+
"allow",
|
|
639
|
+
`payment 'self' ${this.hostOrigin}; publickey-credentials-get 'self' ${this.hostOrigin}`
|
|
640
|
+
);
|
|
556
641
|
iframe.style.border = "0";
|
|
557
642
|
iframe.style.width = "100%";
|
|
558
643
|
iframe.style.display = "block";
|
|
@@ -560,8 +645,18 @@ var SunbayCheckout = (() => {
|
|
|
560
645
|
container.appendChild(iframe);
|
|
561
646
|
this.iframe = iframe;
|
|
562
647
|
this.installResizeListener();
|
|
648
|
+
const requested = opts?.timeoutMs;
|
|
649
|
+
const timeoutMs = typeof requested === "number" && Number.isFinite(requested) && requested > 0 ? requested : DEFAULT_MOUNT_TIMEOUT_MS;
|
|
563
650
|
return new Promise((resolve, reject) => {
|
|
651
|
+
let settled = false;
|
|
652
|
+
let timer = null;
|
|
564
653
|
const cleanup = () => {
|
|
654
|
+
settled = true;
|
|
655
|
+
if (timer !== null) {
|
|
656
|
+
clearTimeout(timer);
|
|
657
|
+
timer = null;
|
|
658
|
+
}
|
|
659
|
+
this.abortMount = null;
|
|
565
660
|
if (this.readyHandler) {
|
|
566
661
|
this.transport.off("elementReady", this.readyHandler);
|
|
567
662
|
this.readyHandler = null;
|
|
@@ -572,7 +667,7 @@ var SunbayCheckout = (() => {
|
|
|
572
667
|
}
|
|
573
668
|
};
|
|
574
669
|
const onReady = (payload) => {
|
|
575
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
670
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
576
671
|
return;
|
|
577
672
|
}
|
|
578
673
|
cleanup();
|
|
@@ -584,12 +679,13 @@ var SunbayCheckout = (() => {
|
|
|
584
679
|
resolve();
|
|
585
680
|
};
|
|
586
681
|
const onLoadError = (payload) => {
|
|
587
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
682
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
588
683
|
return;
|
|
589
684
|
}
|
|
590
685
|
cleanup();
|
|
591
686
|
this.status = "loadError";
|
|
592
687
|
this.removeIframe();
|
|
688
|
+
this.releaseHostedElement();
|
|
593
689
|
const err = payload?.error;
|
|
594
690
|
reject(
|
|
595
691
|
err ?? makeError2(
|
|
@@ -603,18 +699,56 @@ var SunbayCheckout = (() => {
|
|
|
603
699
|
this.loadErrorHandler = onLoadError;
|
|
604
700
|
this.transport.on("elementReady", onReady);
|
|
605
701
|
this.transport.on("loadError", onLoadError);
|
|
702
|
+
this.abortMount = (reason) => {
|
|
703
|
+
if (settled) {
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
cleanup();
|
|
707
|
+
reject(reason);
|
|
708
|
+
};
|
|
709
|
+
timer = setTimeout(() => {
|
|
710
|
+
if (settled) {
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
cleanup();
|
|
714
|
+
this.status = "loadError";
|
|
715
|
+
this.removeIframe();
|
|
716
|
+
this.releaseHostedElement();
|
|
717
|
+
reject(
|
|
718
|
+
makeError2(
|
|
719
|
+
ErrorCode.RUNTIME_LOAD_FAILED,
|
|
720
|
+
`Element did not become ready within ${timeoutMs}ms`,
|
|
721
|
+
this.elementId
|
|
722
|
+
)
|
|
723
|
+
);
|
|
724
|
+
}, timeoutMs);
|
|
606
725
|
this.transport.request("createElement", {
|
|
607
726
|
elementId: this.elementId,
|
|
608
727
|
type: this.type,
|
|
609
728
|
options: this.options
|
|
610
|
-
}).
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
729
|
+
}).then(
|
|
730
|
+
() => void 0,
|
|
731
|
+
(cmdErr) => {
|
|
732
|
+
if (settled) {
|
|
733
|
+
return;
|
|
734
|
+
}
|
|
735
|
+
cleanup();
|
|
736
|
+
this.status = "loadError";
|
|
737
|
+
this.removeIframe();
|
|
738
|
+
reject(cmdErr);
|
|
739
|
+
}
|
|
740
|
+
);
|
|
616
741
|
});
|
|
617
742
|
}
|
|
743
|
+
/** Best-effort, fire-and-forget release of the hosted element registration; never throws. */
|
|
744
|
+
releaseHostedElement() {
|
|
745
|
+
try {
|
|
746
|
+
void Promise.resolve(
|
|
747
|
+
this.transport.request("destroyElement", { elementId: this.elementId })
|
|
748
|
+
).catch(() => void 0);
|
|
749
|
+
} catch {
|
|
750
|
+
}
|
|
751
|
+
}
|
|
618
752
|
/**
|
|
619
753
|
* Unmount: send destroyElement, remove the iframe; idempotent; keeps the handle and configuration.
|
|
620
754
|
*
|
|
@@ -626,6 +760,13 @@ var SunbayCheckout = (() => {
|
|
|
626
760
|
if (this.status !== "ready" && this.status !== "loading") {
|
|
627
761
|
return;
|
|
628
762
|
}
|
|
763
|
+
this.abortMount?.(
|
|
764
|
+
makeError2(
|
|
765
|
+
ErrorCode.RUNTIME_LOAD_FAILED,
|
|
766
|
+
"Mount cancelled: the Element was unmounted while loading",
|
|
767
|
+
this.elementId
|
|
768
|
+
)
|
|
769
|
+
);
|
|
629
770
|
try {
|
|
630
771
|
await this.transport.request("destroyElement", { elementId: this.elementId });
|
|
631
772
|
} finally {
|
|
@@ -641,6 +782,14 @@ var SunbayCheckout = (() => {
|
|
|
641
782
|
if (this.status === "destroyed") {
|
|
642
783
|
return;
|
|
643
784
|
}
|
|
785
|
+
const wasHosted = this.status === "loading" || this.status === "ready";
|
|
786
|
+
this.abortMount?.(
|
|
787
|
+
makeError2(
|
|
788
|
+
ErrorCode.INSTANCE_DESTROYED,
|
|
789
|
+
"Mount cancelled: the Element was destroyed while loading",
|
|
790
|
+
this.elementId
|
|
791
|
+
)
|
|
792
|
+
);
|
|
644
793
|
if (this.readyHandler) {
|
|
645
794
|
this.transport.off("elementReady", this.readyHandler);
|
|
646
795
|
this.readyHandler = null;
|
|
@@ -653,9 +802,23 @@ var SunbayCheckout = (() => {
|
|
|
653
802
|
this.transport.off("expressAvailabilityChange", this.availabilityHandler);
|
|
654
803
|
this.availabilityHandler = null;
|
|
655
804
|
}
|
|
805
|
+
for (const [event, wrappers] of this.userListeners) {
|
|
806
|
+
for (const wrapper of wrappers.values()) {
|
|
807
|
+
this.transport.off(event, wrapper);
|
|
808
|
+
}
|
|
809
|
+
}
|
|
810
|
+
this.userListeners.clear();
|
|
656
811
|
this.removeIframe();
|
|
657
812
|
this.container = null;
|
|
658
813
|
this.status = "destroyed";
|
|
814
|
+
if (wasHosted) {
|
|
815
|
+
try {
|
|
816
|
+
void Promise.resolve(
|
|
817
|
+
this.transport.request("destroyElement", { elementId: this.elementId })
|
|
818
|
+
).catch(() => void 0);
|
|
819
|
+
} catch {
|
|
820
|
+
}
|
|
821
|
+
}
|
|
659
822
|
this.onDestroy?.(this.elementId);
|
|
660
823
|
}
|
|
661
824
|
/**
|
|
@@ -685,7 +848,24 @@ var SunbayCheckout = (() => {
|
|
|
685
848
|
*/
|
|
686
849
|
on(event, handler) {
|
|
687
850
|
this.assertAlive();
|
|
688
|
-
this.
|
|
851
|
+
let wrappers = this.userListeners.get(event);
|
|
852
|
+
if (!wrappers) {
|
|
853
|
+
wrappers = /* @__PURE__ */ new Map();
|
|
854
|
+
this.userListeners.set(event, wrappers);
|
|
855
|
+
}
|
|
856
|
+
if (wrappers.has(handler)) {
|
|
857
|
+
return;
|
|
858
|
+
}
|
|
859
|
+
const typed = handler;
|
|
860
|
+
const wrapper = (payload) => {
|
|
861
|
+
const target = payload?.elementId;
|
|
862
|
+
if (target !== void 0 && target !== this.elementId) {
|
|
863
|
+
return;
|
|
864
|
+
}
|
|
865
|
+
typed(payload);
|
|
866
|
+
};
|
|
867
|
+
wrappers.set(typed, wrapper);
|
|
868
|
+
this.transport.on(event, wrapper);
|
|
689
869
|
}
|
|
690
870
|
/**
|
|
691
871
|
* Unregister an event handler (still safe to call after destruction).
|
|
@@ -694,7 +874,13 @@ var SunbayCheckout = (() => {
|
|
|
694
874
|
* @param handler - The previously registered callback.
|
|
695
875
|
*/
|
|
696
876
|
off(event, handler) {
|
|
697
|
-
this.
|
|
877
|
+
const wrappers = this.userListeners.get(event);
|
|
878
|
+
const wrapper = wrappers?.get(handler);
|
|
879
|
+
if (!wrappers || !wrapper) {
|
|
880
|
+
return;
|
|
881
|
+
}
|
|
882
|
+
wrappers.delete(handler);
|
|
883
|
+
this.transport.off(event, wrapper);
|
|
698
884
|
}
|
|
699
885
|
/** Install input-class action methods (automatically called after construction). */
|
|
700
886
|
installInputActions() {
|
|
@@ -751,7 +937,7 @@ var SunbayCheckout = (() => {
|
|
|
751
937
|
return node instanceof HTMLElement ? node : null;
|
|
752
938
|
}
|
|
753
939
|
/**
|
|
754
|
-
* Resolve the iframe src: split fields go to /sdk/field/, the rest to /sdk/element/, with
|
|
940
|
+
* Resolve the iframe src: split fields go to /embed/sdk/field/, the rest to /embed/sdk/element/, with
|
|
755
941
|
* checkoutId and elementId; if an initial colorScheme is provided, append `&colorScheme=<value>`
|
|
756
942
|
* (no flash on the first frame); if the initial appearance contains variables or rules, append
|
|
757
943
|
* `&appearance=<encodeURIComponent(JSON.stringify(appearance))>` (takes effect on the first
|
|
@@ -783,7 +969,7 @@ var SunbayCheckout = (() => {
|
|
|
783
969
|
if (this.type === "expressCheckout") {
|
|
784
970
|
query += this.resolveExpressQuery();
|
|
785
971
|
}
|
|
786
|
-
return `${this.hostOrigin
|
|
972
|
+
return `${runtimeUrl(this.hostOrigin, segment, this.type)}${query}`;
|
|
787
973
|
}
|
|
788
974
|
/**
|
|
789
975
|
* Extract a per-element appearance (from the Element's own options, i.e. the second
|
|
@@ -892,6 +1078,7 @@ var SunbayCheckout = (() => {
|
|
|
892
1078
|
};
|
|
893
1079
|
|
|
894
1080
|
// src/CheckoutInstance.ts
|
|
1081
|
+
var CONFIRM_RESULT_DEADLINE_MS = 12e4;
|
|
895
1082
|
var CARD_FIELD_TYPES = ["cardNumber", "cardExpiry", "cardCvc"];
|
|
896
1083
|
function genId2(prefix) {
|
|
897
1084
|
const rand = Math.random().toString(36).slice(2);
|
|
@@ -964,12 +1151,36 @@ var SunbayCheckout = (() => {
|
|
|
964
1151
|
return element;
|
|
965
1152
|
}
|
|
966
1153
|
/**
|
|
967
|
-
* Initiate a payment confirmation. Generates an operationId, sends the confirm command
|
|
968
|
-
*
|
|
969
|
-
*
|
|
1154
|
+
* Initiate a payment confirmation. Generates an operationId, sends the `confirm` command, and
|
|
1155
|
+
* returns a Promise fulfilled by `paymentResult` events with the same operationId.
|
|
1156
|
+
*
|
|
1157
|
+
* Behavior changes in 0.0.7: previously the Promise resolved on the first `paymentResult`,
|
|
1158
|
+
* including a non-final `pending` outcome. Now it resolves ONLY on a terminal outcome
|
|
1159
|
+
* (`succeeded` | `failed` | `canceled`); a `pending` outcome keeps it waiting.
|
|
1160
|
+
*
|
|
1161
|
+
* Watchdog: if no terminal outcome arrives within `resultTimeoutMs` (default
|
|
1162
|
+
* {@link CONFIRM_RESULT_DEADLINE_MS} = 120000ms) the Promise resolves
|
|
1163
|
+
* `{ status: 'pending', operationId, txnId? }`. This is NOT a failure: the payment may still
|
|
1164
|
+
* complete, so never treat it as failed and never retry blindly. Reconcile with
|
|
1165
|
+
* `getPaymentStatus()` or the `paymentResult` event.
|
|
970
1166
|
*
|
|
971
|
-
*
|
|
972
|
-
*
|
|
1167
|
+
* Slot semantics (duplicate-submit protection): the confirm slot stays occupied until a
|
|
1168
|
+
* terminal outcome arrives or `destroy()` is called. While it is occupied another `confirm()`
|
|
1169
|
+
* rejects with `CHECKOUT_BUSY`; this also holds after the watchdog resolved `pending`, and later
|
|
1170
|
+
* terminal outcomes are still delivered through `on('paymentResult', ...)`.
|
|
1171
|
+
*
|
|
1172
|
+
* If the `confirm` command is rejected by the runtime the payment was not started: the Promise
|
|
1173
|
+
* rejects with that error and the slot is freed. If the command itself times out
|
|
1174
|
+
* (`REQUEST_TIMEOUT`) the runtime may have started the payment: the Promise rejects with the
|
|
1175
|
+
* error augmented with `paymentStateKnown: false`, the slot stays occupied and a later terminal
|
|
1176
|
+
* `paymentResult` for this operation frees it. Do not retry; reconcile with `getPaymentStatus()`.
|
|
1177
|
+
*
|
|
1178
|
+
* `error` events with `paymentStateKnown === false` are informational and do not settle the
|
|
1179
|
+
* Promise.
|
|
1180
|
+
*
|
|
1181
|
+
* @param opts - Optional billing/shipping address, name, email, returnUrl, plus `onOperation`
|
|
1182
|
+
* and `resultTimeoutMs`.
|
|
1183
|
+
* @returns The terminal PaymentOutcome, or `{ status: 'pending' }` when the watchdog fires.
|
|
973
1184
|
* @throws rejects INSTANCE_DESTROYED when destroyed; rejects CHECKOUT_BUSY when in progress.
|
|
974
1185
|
*/
|
|
975
1186
|
confirm(opts) {
|
|
@@ -983,15 +1194,57 @@ var SunbayCheckout = (() => {
|
|
|
983
1194
|
makeError3(ErrorCode.CHECKOUT_BUSY, "A payment confirmation is already in progress", "runtime")
|
|
984
1195
|
);
|
|
985
1196
|
}
|
|
1197
|
+
const { onOperation, resultTimeoutMs, ...rest } = opts ?? {};
|
|
986
1198
|
const operationId = genId2("op");
|
|
987
|
-
const payload = { ...
|
|
1199
|
+
const payload = { ...rest, operationId };
|
|
1200
|
+
const deadline = typeof resultTimeoutMs === "number" && Number.isFinite(resultTimeoutMs) && resultTimeoutMs > 0 ? resultTimeoutMs : CONFIRM_RESULT_DEADLINE_MS;
|
|
988
1201
|
return new Promise((resolve, reject) => {
|
|
989
|
-
|
|
1202
|
+
const entry = {
|
|
1203
|
+
operationId,
|
|
1204
|
+
settled: false,
|
|
1205
|
+
resolve,
|
|
1206
|
+
reject,
|
|
1207
|
+
watchdog: null,
|
|
1208
|
+
lastPendingTxnId: void 0
|
|
1209
|
+
};
|
|
1210
|
+
this.pendingConfirm = entry;
|
|
1211
|
+
entry.watchdog = setTimeout(() => {
|
|
1212
|
+
entry.watchdog = null;
|
|
1213
|
+
if (entry.settled) {
|
|
1214
|
+
return;
|
|
1215
|
+
}
|
|
1216
|
+
entry.settled = true;
|
|
1217
|
+
const outcome = entry.lastPendingTxnId !== void 0 ? { status: "pending", operationId, txnId: entry.lastPendingTxnId } : { status: "pending", operationId };
|
|
1218
|
+
resolve(outcome);
|
|
1219
|
+
}, deadline);
|
|
1220
|
+
if (typeof onOperation === "function") {
|
|
1221
|
+
try {
|
|
1222
|
+
onOperation(operationId);
|
|
1223
|
+
} catch (err) {
|
|
1224
|
+
setTimeout(() => {
|
|
1225
|
+
throw err;
|
|
1226
|
+
}, 0);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
990
1229
|
this.transport.request("confirm", payload).catch((err) => {
|
|
991
|
-
if (this.pendingConfirm
|
|
992
|
-
|
|
1230
|
+
if (this.pendingConfirm !== entry) {
|
|
1231
|
+
return;
|
|
1232
|
+
}
|
|
1233
|
+
const error = err;
|
|
1234
|
+
if (error && error.code === ErrorCode.REQUEST_TIMEOUT) {
|
|
1235
|
+
if (!entry.settled) {
|
|
1236
|
+
entry.settled = true;
|
|
1237
|
+
this.clearWatchdog(entry);
|
|
1238
|
+
reject({ ...error, paymentStateKnown: false });
|
|
1239
|
+
}
|
|
1240
|
+
return;
|
|
1241
|
+
}
|
|
1242
|
+
this.pendingConfirm = null;
|
|
1243
|
+
this.clearWatchdog(entry);
|
|
1244
|
+
if (!entry.settled) {
|
|
1245
|
+
entry.settled = true;
|
|
1246
|
+
reject(error);
|
|
993
1247
|
}
|
|
994
|
-
reject(err);
|
|
995
1248
|
});
|
|
996
1249
|
});
|
|
997
1250
|
}
|
|
@@ -1077,10 +1330,15 @@ var SunbayCheckout = (() => {
|
|
|
1077
1330
|
this.elements.clear();
|
|
1078
1331
|
this.transport.off("paymentResult", this.paymentResultHandler);
|
|
1079
1332
|
if (this.pendingConfirm) {
|
|
1080
|
-
this.pendingConfirm
|
|
1081
|
-
makeError3(ErrorCode.INSTANCE_DESTROYED, "Checkout instance has been destroyed", "runtime")
|
|
1082
|
-
);
|
|
1333
|
+
const entry = this.pendingConfirm;
|
|
1083
1334
|
this.pendingConfirm = null;
|
|
1335
|
+
this.clearWatchdog(entry);
|
|
1336
|
+
if (!entry.settled) {
|
|
1337
|
+
entry.settled = true;
|
|
1338
|
+
entry.reject(
|
|
1339
|
+
makeError3(ErrorCode.INSTANCE_DESTROYED, "Checkout instance has been destroyed", "runtime")
|
|
1340
|
+
);
|
|
1341
|
+
}
|
|
1084
1342
|
}
|
|
1085
1343
|
void Promise.resolve(this.transport.request("destroy", {})).catch(() => void 0);
|
|
1086
1344
|
this.transport.destroy();
|
|
@@ -1168,26 +1426,32 @@ var SunbayCheckout = (() => {
|
|
|
1168
1426
|
if (evt.operationId !== pending.operationId) {
|
|
1169
1427
|
return;
|
|
1170
1428
|
}
|
|
1171
|
-
|
|
1429
|
+
const outcome = evt.outcome;
|
|
1430
|
+
if (!outcome) {
|
|
1431
|
+
return;
|
|
1432
|
+
}
|
|
1433
|
+
if (outcome.status === "pending") {
|
|
1434
|
+
pending.lastPendingTxnId = outcome.txnId ?? pending.lastPendingTxnId;
|
|
1435
|
+
return;
|
|
1436
|
+
}
|
|
1437
|
+
if (outcome.status !== "succeeded" && outcome.status !== "failed" && outcome.status !== "canceled") {
|
|
1172
1438
|
return;
|
|
1173
1439
|
}
|
|
1174
1440
|
this.pendingConfirm = null;
|
|
1175
|
-
|
|
1441
|
+
this.clearWatchdog(pending);
|
|
1442
|
+
if (!pending.settled) {
|
|
1443
|
+
pending.settled = true;
|
|
1444
|
+
pending.resolve(outcome);
|
|
1445
|
+
}
|
|
1176
1446
|
}
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
prod: "https://checkout.sunbay.us"
|
|
1184
|
-
};
|
|
1185
|
-
function resolveHostOrigin(env = "prod", override) {
|
|
1186
|
-
if (override) {
|
|
1187
|
-
return override;
|
|
1447
|
+
/** Clear a pending confirm's watchdog timer (idempotent). */
|
|
1448
|
+
clearWatchdog(entry) {
|
|
1449
|
+
if (entry.watchdog !== null) {
|
|
1450
|
+
clearTimeout(entry.watchdog);
|
|
1451
|
+
entry.watchdog = null;
|
|
1452
|
+
}
|
|
1188
1453
|
}
|
|
1189
|
-
|
|
1190
|
-
}
|
|
1454
|
+
};
|
|
1191
1455
|
|
|
1192
1456
|
// src/createCheckout.ts
|
|
1193
1457
|
var HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
@@ -1205,6 +1469,9 @@ var SunbayCheckout = (() => {
|
|
|
1205
1469
|
}
|
|
1206
1470
|
var defaultTransportFactory = ({ checkoutId, hostOrigin }) => new MainFrameTransport({ checkoutId, hostOrigin });
|
|
1207
1471
|
async function createCheckout(opts) {
|
|
1472
|
+
if (typeof opts.checkoutClientSecret !== "string" || opts.checkoutClientSecret.trim() === "") {
|
|
1473
|
+
throw makeError4(ErrorCode.INVALID_OPTIONS, "checkoutClientSecret is required");
|
|
1474
|
+
}
|
|
1208
1475
|
const checkoutId = genId3("co");
|
|
1209
1476
|
const hostOrigin = resolveHostOrigin(opts.env, opts.hostOrigin);
|
|
1210
1477
|
const factory = opts.__transportFactory ?? defaultTransportFactory;
|
|
@@ -1227,6 +1494,7 @@ var SunbayCheckout = (() => {
|
|
|
1227
1494
|
transport.setRuntimeId(hello.runtimeId);
|
|
1228
1495
|
const initReq = {
|
|
1229
1496
|
sessionId: opts.sessionId,
|
|
1497
|
+
checkoutClientSecret: opts.checkoutClientSecret,
|
|
1230
1498
|
locale: opts.locale,
|
|
1231
1499
|
appearance: opts.appearance,
|
|
1232
1500
|
paymentMethods: opts.paymentMethods
|
|
@@ -1259,7 +1527,7 @@ var SunbayCheckout = (() => {
|
|
|
1259
1527
|
reject(makeError4(ErrorCode.RUNTIME_LOAD_FAILED, "Timed out waiting for hosted runtime handshake"));
|
|
1260
1528
|
}, HANDSHAKE_TIMEOUT_MS);
|
|
1261
1529
|
transport.onHello((raw) => {
|
|
1262
|
-
if (settled) {
|
|
1530
|
+
if (settled || !isValidRuntimeHello(raw)) {
|
|
1263
1531
|
return;
|
|
1264
1532
|
}
|
|
1265
1533
|
settled = true;
|
|
@@ -1274,7 +1542,7 @@ var SunbayCheckout = (() => {
|
|
|
1274
1542
|
const w = window;
|
|
1275
1543
|
w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
|
|
1276
1544
|
}
|
|
1277
|
-
var version = true ? "0.0.
|
|
1545
|
+
var version = true ? "0.0.7" : "0.0.0";
|
|
1278
1546
|
return __toCommonJS(index_exports);
|
|
1279
1547
|
})();
|
|
1280
1548
|
//# sourceMappingURL=index.global.js.map
|