@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.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(
|
|
@@ -564,8 +645,18 @@ var SunbayCheckout = (() => {
|
|
|
564
645
|
container.appendChild(iframe);
|
|
565
646
|
this.iframe = iframe;
|
|
566
647
|
this.installResizeListener();
|
|
648
|
+
const requested = opts?.timeoutMs;
|
|
649
|
+
const timeoutMs = typeof requested === "number" && Number.isFinite(requested) && requested > 0 ? requested : DEFAULT_MOUNT_TIMEOUT_MS;
|
|
567
650
|
return new Promise((resolve, reject) => {
|
|
651
|
+
let settled = false;
|
|
652
|
+
let timer = null;
|
|
568
653
|
const cleanup = () => {
|
|
654
|
+
settled = true;
|
|
655
|
+
if (timer !== null) {
|
|
656
|
+
clearTimeout(timer);
|
|
657
|
+
timer = null;
|
|
658
|
+
}
|
|
659
|
+
this.abortMount = null;
|
|
569
660
|
if (this.readyHandler) {
|
|
570
661
|
this.transport.off("elementReady", this.readyHandler);
|
|
571
662
|
this.readyHandler = null;
|
|
@@ -576,7 +667,7 @@ var SunbayCheckout = (() => {
|
|
|
576
667
|
}
|
|
577
668
|
};
|
|
578
669
|
const onReady = (payload) => {
|
|
579
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
670
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
580
671
|
return;
|
|
581
672
|
}
|
|
582
673
|
cleanup();
|
|
@@ -588,12 +679,13 @@ var SunbayCheckout = (() => {
|
|
|
588
679
|
resolve();
|
|
589
680
|
};
|
|
590
681
|
const onLoadError = (payload) => {
|
|
591
|
-
if (!matchesElement(payload, this.elementId)) {
|
|
682
|
+
if (settled || !matchesElement(payload, this.elementId)) {
|
|
592
683
|
return;
|
|
593
684
|
}
|
|
594
685
|
cleanup();
|
|
595
686
|
this.status = "loadError";
|
|
596
687
|
this.removeIframe();
|
|
688
|
+
this.releaseHostedElement();
|
|
597
689
|
const err = payload?.error;
|
|
598
690
|
reject(
|
|
599
691
|
err ?? makeError2(
|
|
@@ -607,18 +699,56 @@ var SunbayCheckout = (() => {
|
|
|
607
699
|
this.loadErrorHandler = onLoadError;
|
|
608
700
|
this.transport.on("elementReady", onReady);
|
|
609
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);
|
|
610
725
|
this.transport.request("createElement", {
|
|
611
726
|
elementId: this.elementId,
|
|
612
727
|
type: this.type,
|
|
613
728
|
options: this.options
|
|
614
|
-
}).
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
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
|
+
);
|
|
620
741
|
});
|
|
621
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
|
+
}
|
|
622
752
|
/**
|
|
623
753
|
* Unmount: send destroyElement, remove the iframe; idempotent; keeps the handle and configuration.
|
|
624
754
|
*
|
|
@@ -630,6 +760,13 @@ var SunbayCheckout = (() => {
|
|
|
630
760
|
if (this.status !== "ready" && this.status !== "loading") {
|
|
631
761
|
return;
|
|
632
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
|
+
);
|
|
633
770
|
try {
|
|
634
771
|
await this.transport.request("destroyElement", { elementId: this.elementId });
|
|
635
772
|
} finally {
|
|
@@ -645,6 +782,14 @@ var SunbayCheckout = (() => {
|
|
|
645
782
|
if (this.status === "destroyed") {
|
|
646
783
|
return;
|
|
647
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
|
+
);
|
|
648
793
|
if (this.readyHandler) {
|
|
649
794
|
this.transport.off("elementReady", this.readyHandler);
|
|
650
795
|
this.readyHandler = null;
|
|
@@ -657,9 +802,23 @@ var SunbayCheckout = (() => {
|
|
|
657
802
|
this.transport.off("expressAvailabilityChange", this.availabilityHandler);
|
|
658
803
|
this.availabilityHandler = null;
|
|
659
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();
|
|
660
811
|
this.removeIframe();
|
|
661
812
|
this.container = null;
|
|
662
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
|
+
}
|
|
663
822
|
this.onDestroy?.(this.elementId);
|
|
664
823
|
}
|
|
665
824
|
/**
|
|
@@ -689,7 +848,24 @@ var SunbayCheckout = (() => {
|
|
|
689
848
|
*/
|
|
690
849
|
on(event, handler) {
|
|
691
850
|
this.assertAlive();
|
|
692
|
-
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);
|
|
693
869
|
}
|
|
694
870
|
/**
|
|
695
871
|
* Unregister an event handler (still safe to call after destruction).
|
|
@@ -698,7 +874,13 @@ var SunbayCheckout = (() => {
|
|
|
698
874
|
* @param handler - The previously registered callback.
|
|
699
875
|
*/
|
|
700
876
|
off(event, handler) {
|
|
701
|
-
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);
|
|
702
884
|
}
|
|
703
885
|
/** Install input-class action methods (automatically called after construction). */
|
|
704
886
|
installInputActions() {
|
|
@@ -755,7 +937,7 @@ var SunbayCheckout = (() => {
|
|
|
755
937
|
return node instanceof HTMLElement ? node : null;
|
|
756
938
|
}
|
|
757
939
|
/**
|
|
758
|
-
* 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
|
|
759
941
|
* checkoutId and elementId; if an initial colorScheme is provided, append `&colorScheme=<value>`
|
|
760
942
|
* (no flash on the first frame); if the initial appearance contains variables or rules, append
|
|
761
943
|
* `&appearance=<encodeURIComponent(JSON.stringify(appearance))>` (takes effect on the first
|
|
@@ -787,7 +969,7 @@ var SunbayCheckout = (() => {
|
|
|
787
969
|
if (this.type === "expressCheckout") {
|
|
788
970
|
query += this.resolveExpressQuery();
|
|
789
971
|
}
|
|
790
|
-
return `${this.hostOrigin
|
|
972
|
+
return `${runtimeUrl(this.hostOrigin, segment, this.type)}${query}`;
|
|
791
973
|
}
|
|
792
974
|
/**
|
|
793
975
|
* Extract a per-element appearance (from the Element's own options, i.e. the second
|
|
@@ -896,6 +1078,7 @@ var SunbayCheckout = (() => {
|
|
|
896
1078
|
};
|
|
897
1079
|
|
|
898
1080
|
// src/CheckoutInstance.ts
|
|
1081
|
+
var CONFIRM_RESULT_DEADLINE_MS = 12e4;
|
|
899
1082
|
var CARD_FIELD_TYPES = ["cardNumber", "cardExpiry", "cardCvc"];
|
|
900
1083
|
function genId2(prefix) {
|
|
901
1084
|
const rand = Math.random().toString(36).slice(2);
|
|
@@ -968,12 +1151,36 @@ var SunbayCheckout = (() => {
|
|
|
968
1151
|
return element;
|
|
969
1152
|
}
|
|
970
1153
|
/**
|
|
971
|
-
* Initiate a payment confirmation. Generates an operationId, sends the confirm command
|
|
972
|
-
*
|
|
973
|
-
*
|
|
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.
|
|
974
1166
|
*
|
|
975
|
-
*
|
|
976
|
-
*
|
|
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.
|
|
977
1184
|
* @throws rejects INSTANCE_DESTROYED when destroyed; rejects CHECKOUT_BUSY when in progress.
|
|
978
1185
|
*/
|
|
979
1186
|
confirm(opts) {
|
|
@@ -987,15 +1194,57 @@ var SunbayCheckout = (() => {
|
|
|
987
1194
|
makeError3(ErrorCode.CHECKOUT_BUSY, "A payment confirmation is already in progress", "runtime")
|
|
988
1195
|
);
|
|
989
1196
|
}
|
|
1197
|
+
const { onOperation, resultTimeoutMs, ...rest } = opts ?? {};
|
|
990
1198
|
const operationId = genId2("op");
|
|
991
|
-
const payload = { ...
|
|
1199
|
+
const payload = { ...rest, operationId };
|
|
1200
|
+
const deadline = typeof resultTimeoutMs === "number" && Number.isFinite(resultTimeoutMs) && resultTimeoutMs > 0 ? resultTimeoutMs : CONFIRM_RESULT_DEADLINE_MS;
|
|
992
1201
|
return new Promise((resolve, reject) => {
|
|
993
|
-
|
|
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
|
+
}
|
|
994
1229
|
this.transport.request("confirm", payload).catch((err) => {
|
|
995
|
-
if (this.pendingConfirm
|
|
996
|
-
|
|
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);
|
|
997
1247
|
}
|
|
998
|
-
reject(err);
|
|
999
1248
|
});
|
|
1000
1249
|
});
|
|
1001
1250
|
}
|
|
@@ -1081,10 +1330,15 @@ var SunbayCheckout = (() => {
|
|
|
1081
1330
|
this.elements.clear();
|
|
1082
1331
|
this.transport.off("paymentResult", this.paymentResultHandler);
|
|
1083
1332
|
if (this.pendingConfirm) {
|
|
1084
|
-
this.pendingConfirm
|
|
1085
|
-
makeError3(ErrorCode.INSTANCE_DESTROYED, "Checkout instance has been destroyed", "runtime")
|
|
1086
|
-
);
|
|
1333
|
+
const entry = this.pendingConfirm;
|
|
1087
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
|
+
}
|
|
1088
1342
|
}
|
|
1089
1343
|
void Promise.resolve(this.transport.request("destroy", {})).catch(() => void 0);
|
|
1090
1344
|
this.transport.destroy();
|
|
@@ -1172,26 +1426,32 @@ var SunbayCheckout = (() => {
|
|
|
1172
1426
|
if (evt.operationId !== pending.operationId) {
|
|
1173
1427
|
return;
|
|
1174
1428
|
}
|
|
1175
|
-
|
|
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") {
|
|
1176
1438
|
return;
|
|
1177
1439
|
}
|
|
1178
1440
|
this.pendingConfirm = null;
|
|
1179
|
-
|
|
1441
|
+
this.clearWatchdog(pending);
|
|
1442
|
+
if (!pending.settled) {
|
|
1443
|
+
pending.settled = true;
|
|
1444
|
+
pending.resolve(outcome);
|
|
1445
|
+
}
|
|
1180
1446
|
}
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
prod: "https://checkout.sunbay.us"
|
|
1188
|
-
};
|
|
1189
|
-
function resolveHostOrigin(env = "prod", override) {
|
|
1190
|
-
if (override) {
|
|
1191
|
-
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
|
+
}
|
|
1192
1453
|
}
|
|
1193
|
-
|
|
1194
|
-
}
|
|
1454
|
+
};
|
|
1195
1455
|
|
|
1196
1456
|
// src/createCheckout.ts
|
|
1197
1457
|
var HANDSHAKE_TIMEOUT_MS = 15e3;
|
|
@@ -1209,6 +1469,9 @@ var SunbayCheckout = (() => {
|
|
|
1209
1469
|
}
|
|
1210
1470
|
var defaultTransportFactory = ({ checkoutId, hostOrigin }) => new MainFrameTransport({ checkoutId, hostOrigin });
|
|
1211
1471
|
async function createCheckout(opts) {
|
|
1472
|
+
if (typeof opts.checkoutClientSecret !== "string" || opts.checkoutClientSecret.trim() === "") {
|
|
1473
|
+
throw makeError4(ErrorCode.INVALID_OPTIONS, "checkoutClientSecret is required");
|
|
1474
|
+
}
|
|
1212
1475
|
const checkoutId = genId3("co");
|
|
1213
1476
|
const hostOrigin = resolveHostOrigin(opts.env, opts.hostOrigin);
|
|
1214
1477
|
const factory = opts.__transportFactory ?? defaultTransportFactory;
|
|
@@ -1231,6 +1494,7 @@ var SunbayCheckout = (() => {
|
|
|
1231
1494
|
transport.setRuntimeId(hello.runtimeId);
|
|
1232
1495
|
const initReq = {
|
|
1233
1496
|
sessionId: opts.sessionId,
|
|
1497
|
+
checkoutClientSecret: opts.checkoutClientSecret,
|
|
1234
1498
|
locale: opts.locale,
|
|
1235
1499
|
appearance: opts.appearance,
|
|
1236
1500
|
paymentMethods: opts.paymentMethods
|
|
@@ -1263,7 +1527,7 @@ var SunbayCheckout = (() => {
|
|
|
1263
1527
|
reject(makeError4(ErrorCode.RUNTIME_LOAD_FAILED, "Timed out waiting for hosted runtime handshake"));
|
|
1264
1528
|
}, HANDSHAKE_TIMEOUT_MS);
|
|
1265
1529
|
transport.onHello((raw) => {
|
|
1266
|
-
if (settled) {
|
|
1530
|
+
if (settled || !isValidRuntimeHello(raw)) {
|
|
1267
1531
|
return;
|
|
1268
1532
|
}
|
|
1269
1533
|
settled = true;
|
|
@@ -1278,7 +1542,7 @@ var SunbayCheckout = (() => {
|
|
|
1278
1542
|
const w = window;
|
|
1279
1543
|
w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
|
|
1280
1544
|
}
|
|
1281
|
-
var version = true ? "0.0.
|
|
1545
|
+
var version = true ? "0.0.7" : "0.0.0";
|
|
1282
1546
|
return __toCommonJS(index_exports);
|
|
1283
1547
|
})();
|
|
1284
1548
|
//# sourceMappingURL=index.global.js.map
|