@paynext/sdk 0.0.169 → 0.0.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +122 -70
- package/dist/index.umd.js +69 -19
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
1
|
+
var w = Object.defineProperty;
|
|
2
|
+
var x = (a, t, e) => t in a ? w(a, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : a[t] = e;
|
|
3
|
+
var r = (a, t, e) => x(a, typeof t != "symbol" ? t + "" : t, e);
|
|
4
|
+
const h = "https://cdn-sdk-dev.paynext.com/checkout.html", p = "https://cdn-sdk.paynext.com/checkout.html", k = {
|
|
5
5
|
develop: "https://cdn-sdk-dev.paynext.com",
|
|
6
6
|
production: "https://cdn-sdk.paynext.com"
|
|
7
7
|
};
|
|
@@ -20,6 +20,7 @@ function C(a) {
|
|
|
20
20
|
.loading { display: flex; align-items: center; justify-content: center; min-height: 400px; color: #666; font-size: 14px; }
|
|
21
21
|
.loading::after { content: '...'; animation: dots 1.5s steps(4, end) infinite; }
|
|
22
22
|
@keyframes dots { 0%, 20% { content: '.'; } 40% { content: '..'; } 60%, 100% { content: '...'; } }
|
|
23
|
+
.error { color: #e53e3e; padding: 20px; text-align: center; }
|
|
23
24
|
</style>
|
|
24
25
|
</head>
|
|
25
26
|
<body>
|
|
@@ -37,6 +38,11 @@ function C(a) {
|
|
|
37
38
|
window.parent.postMessage({ type, payload, requestId }, parentOrigin)
|
|
38
39
|
}
|
|
39
40
|
|
|
41
|
+
function showError(message) {
|
|
42
|
+
document.getElementById('checkout-container').innerHTML =
|
|
43
|
+
'<div class="error"><strong>Error:</strong> ' + message + '</div>'
|
|
44
|
+
}
|
|
45
|
+
|
|
40
46
|
window.addEventListener('message', async (event) => {
|
|
41
47
|
const data = event.data
|
|
42
48
|
if (!parentOrigin) parentOrigin = event.origin
|
|
@@ -47,23 +53,47 @@ function C(a) {
|
|
|
47
53
|
case MessageType.MOUNT:
|
|
48
54
|
if (data.payload?.config) {
|
|
49
55
|
config = data.payload.config
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
// Import SDK from CDN
|
|
59
|
+
const { PayNextCheckout } = await import('${a}')
|
|
60
|
+
|
|
61
|
+
if (!PayNextCheckout) {
|
|
62
|
+
throw new Error('PayNextCheckout not found in SDK module')
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
checkout = new PayNextCheckout()
|
|
66
|
+
await checkout.mount('checkout-container', {
|
|
67
|
+
...config,
|
|
68
|
+
onCheckoutLoaded: (result) => {
|
|
69
|
+
sendToParent(MessageType.LOADED, result, data.requestId)
|
|
70
|
+
notifyResize()
|
|
71
|
+
},
|
|
72
|
+
onCheckoutAttempt: (result) => sendToParent(MessageType.ATTEMPT, result),
|
|
73
|
+
onCheckoutComplete: (result) => sendToParent(MessageType.COMPLETE, result),
|
|
74
|
+
onCheckoutFail: (error) => sendToParent(MessageType.FAIL, error)
|
|
75
|
+
})
|
|
76
|
+
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
77
|
+
setupResizeObserver()
|
|
78
|
+
} catch (importError) {
|
|
79
|
+
console.error('[Iframe] Failed to load SDK:', importError)
|
|
80
|
+
showError('Failed to load PayNext SDK: ' + importError.message)
|
|
81
|
+
sendToParent(MessageType.ERROR, {
|
|
82
|
+
message: 'SDK import failed: ' + importError.message,
|
|
83
|
+
stack: importError.stack
|
|
84
|
+
}, data.requestId)
|
|
85
|
+
}
|
|
61
86
|
}
|
|
62
87
|
break
|
|
88
|
+
|
|
63
89
|
case MessageType.UNMOUNT:
|
|
64
|
-
if (checkout) {
|
|
90
|
+
if (checkout) {
|
|
91
|
+
await checkout.unmount()
|
|
92
|
+
checkout = null
|
|
93
|
+
}
|
|
65
94
|
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
66
95
|
break
|
|
96
|
+
|
|
67
97
|
case MessageType.UPDATE_CONFIG:
|
|
68
98
|
if (checkout && data.payload?.config) {
|
|
69
99
|
await checkout.updateConfig(data.payload.config)
|
|
@@ -72,38 +102,58 @@ function C(a) {
|
|
|
72
102
|
break
|
|
73
103
|
}
|
|
74
104
|
} catch (error) {
|
|
75
|
-
|
|
105
|
+
console.error('[Iframe] Error:', error)
|
|
106
|
+
sendToParent(MessageType.ERROR, {
|
|
107
|
+
message: error.message,
|
|
108
|
+
stack: error.stack
|
|
109
|
+
}, data.requestId)
|
|
76
110
|
}
|
|
77
111
|
})
|
|
78
112
|
|
|
79
113
|
function setupResizeObserver() {
|
|
80
114
|
new ResizeObserver(entries => {
|
|
81
|
-
for (const entry of entries)
|
|
115
|
+
for (const entry of entries) {
|
|
116
|
+
sendToParent(MessageType.RESIZE, { height: entry.contentRect.height })
|
|
117
|
+
}
|
|
82
118
|
}).observe(document.body)
|
|
83
119
|
}
|
|
84
120
|
|
|
85
|
-
function notifyResize() {
|
|
121
|
+
function notifyResize() {
|
|
122
|
+
sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight })
|
|
123
|
+
}
|
|
86
124
|
|
|
87
125
|
window.addEventListener('load', () => {
|
|
88
|
-
|
|
126
|
+
console.log('[Iframe] Iframe loaded, sending ready message')
|
|
127
|
+
setTimeout(() => {
|
|
128
|
+
window.parent.postMessage(
|
|
129
|
+
{ type: MessageType.READY, payload: { success: true } },
|
|
130
|
+
'*'
|
|
131
|
+
)
|
|
132
|
+
}, 100)
|
|
89
133
|
})
|
|
90
134
|
|
|
91
135
|
window.addEventListener('error', (event) => {
|
|
92
|
-
|
|
136
|
+
console.error('[Iframe] Global error:', event.error)
|
|
137
|
+
sendToParent(MessageType.ERROR, {
|
|
138
|
+
message: event.error?.message || 'Unknown error',
|
|
139
|
+
stack: event.error?.stack
|
|
140
|
+
})
|
|
93
141
|
})
|
|
142
|
+
|
|
143
|
+
console.log('[Iframe] PayNext Checkout iframe initialized, SDK URL: ${a}')
|
|
94
144
|
<\/script>
|
|
95
145
|
</body>
|
|
96
146
|
</html>`;
|
|
97
147
|
}
|
|
98
|
-
class
|
|
148
|
+
class f {
|
|
99
149
|
constructor() {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
150
|
+
r(this, "iframe", null);
|
|
151
|
+
r(this, "container", null);
|
|
152
|
+
r(this, "config", null);
|
|
153
|
+
r(this, "messageHandlers", /* @__PURE__ */ new Map());
|
|
154
|
+
r(this, "pendingRequests", /* @__PURE__ */ new Map());
|
|
155
|
+
r(this, "isReady", !1);
|
|
156
|
+
r(this, "allowedOrigin", "");
|
|
107
157
|
this.handleMessage = this.handleMessage.bind(this);
|
|
108
158
|
}
|
|
109
159
|
// generate unique request ID
|
|
@@ -117,10 +167,10 @@ class h {
|
|
|
117
167
|
n(new Error("Iframe not initialized"));
|
|
118
168
|
return;
|
|
119
169
|
}
|
|
120
|
-
const i = this.generateRequestId(),
|
|
170
|
+
const i = this.generateRequestId(), s = { type: t, payload: e, requestId: i };
|
|
121
171
|
this.pendingRequests.set(i, { resolve: o, reject: n }), setTimeout(() => {
|
|
122
172
|
this.pendingRequests.has(i) && (this.pendingRequests.delete(i), n(new Error(`Request timeout: ${t}`)));
|
|
123
|
-
}, 3e4), this.iframe.contentWindow.postMessage(
|
|
173
|
+
}, 3e4), this.iframe.contentWindow.postMessage(s, this.allowedOrigin);
|
|
124
174
|
});
|
|
125
175
|
}
|
|
126
176
|
// handle messages from iframe
|
|
@@ -166,9 +216,9 @@ class h {
|
|
|
166
216
|
// create and configure iframe
|
|
167
217
|
createIframe(t, e = !1) {
|
|
168
218
|
const o = t.environment?.toLowerCase()?.includes("develop") || t.environment?.toLowerCase()?.includes("staging") ? "develop" : "production";
|
|
169
|
-
this.allowedOrigin = e ? window.location.origin :
|
|
170
|
-
const n = document.createElement("iframe"), i = o === "develop" ?
|
|
171
|
-
return e ? (console.log("[PayNext Iframe] Using inline iframe mode (embedded HTML)"), console.log("[PayNext Iframe] SDK will load from:",
|
|
219
|
+
this.allowedOrigin = e ? window.location.origin : k[o];
|
|
220
|
+
const n = document.createElement("iframe"), i = o === "develop" ? h : p, s = o === "develop" ? "https://cdn-sdk-dev.paynext.com/index.cdn.js" : "https://cdn-sdk.paynext.com/index.cdn.js";
|
|
221
|
+
return e ? (console.log("[PayNext Iframe] Using inline iframe mode (embedded HTML)"), console.log("[PayNext Iframe] SDK will load from:", s), n.srcdoc = C(s)) : (console.log("[PayNext Iframe] Using CDN iframe mode"), console.log("[PayNext Iframe] Loading iframe from:", i), n.src = i), n.style.border = "none", n.style.width = "100%", n.style.minHeight = "400px", n.style.display = "block", n.allow = "payment *", n.sandbox.add(
|
|
172
222
|
"allow-scripts",
|
|
173
223
|
// required for SDK execution
|
|
174
224
|
"allow-same-origin",
|
|
@@ -199,25 +249,27 @@ class h {
|
|
|
199
249
|
if (!o)
|
|
200
250
|
throw new Error(`Container element "${t}" not found`);
|
|
201
251
|
this.container = o, this.config = e, this.iframe && await this.unmount();
|
|
202
|
-
const
|
|
203
|
-
let
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
252
|
+
const n = e.environment?.toLowerCase()?.includes("develop") || e.environment?.toLowerCase()?.includes("staging") ? "develop" : "production", i = n === "develop" ? h : p, s = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" || window.location.hostname.startsWith("192.168.") || window.location.hostname.endsWith(".local");
|
|
253
|
+
let l = s;
|
|
254
|
+
if (!s && n === "develop")
|
|
255
|
+
try {
|
|
256
|
+
const m = new AbortController(), u = setTimeout(() => m.abort(), 2e3);
|
|
257
|
+
await fetch(i, {
|
|
258
|
+
method: "HEAD",
|
|
259
|
+
signal: m.signal,
|
|
260
|
+
cache: "no-cache"
|
|
261
|
+
}), clearTimeout(u), l = !1, console.log("[PayNext Iframe] CDN is available, using remote iframe");
|
|
262
|
+
} catch {
|
|
263
|
+
l = !0, console.warn("[PayNext Iframe] CDN not available, using inline fallback mode");
|
|
264
|
+
}
|
|
265
|
+
l && (console.log("[PayNext Iframe] Using inline fallback mode"), console.log("[PayNext Iframe] Reason:", s ? "localhost detected" : "CDN not available")), this.iframe = this.createIframe(e, l), window.addEventListener("message", this.handleMessage), o.innerHTML = "", o.appendChild(this.iframe), await new Promise((m, u) => {
|
|
266
|
+
const y = setTimeout(() => {
|
|
267
|
+
u(new Error("Iframe load timeout"));
|
|
216
268
|
}, 1e4);
|
|
217
269
|
this.iframe.onload = () => {
|
|
218
|
-
clearTimeout(
|
|
270
|
+
clearTimeout(y), m();
|
|
219
271
|
}, this.iframe.onerror = () => {
|
|
220
|
-
clearTimeout(
|
|
272
|
+
clearTimeout(y), u(new Error("Iframe failed to load"));
|
|
221
273
|
};
|
|
222
274
|
}), await this.waitForReady(), await this.sendMessage("paynext:mount", {
|
|
223
275
|
config: {
|
|
@@ -261,17 +313,17 @@ class h {
|
|
|
261
313
|
}
|
|
262
314
|
// preload iframe (optional optimization)
|
|
263
315
|
static async preload(t) {
|
|
264
|
-
const o = (t?.toLowerCase()?.includes("develop") || t?.toLowerCase()?.includes("staging") ? "develop" : "production") === "develop" ?
|
|
316
|
+
const o = (t?.toLowerCase()?.includes("develop") || t?.toLowerCase()?.includes("staging") ? "develop" : "production") === "develop" ? h : p, n = document.createElement("link");
|
|
265
317
|
n.rel = "preconnect", n.href = new URL(o).origin, document.head.appendChild(n);
|
|
266
318
|
const i = document.createElement("link");
|
|
267
319
|
i.rel = "dns-prefetch", i.href = new URL(o).origin, document.head.appendChild(i);
|
|
268
320
|
}
|
|
269
321
|
}
|
|
270
|
-
const
|
|
271
|
-
preload:
|
|
272
|
-
},
|
|
322
|
+
const R = {
|
|
323
|
+
preload: f.preload
|
|
324
|
+
}, P = "https://cdn-sdk-dev.paynext.com/index.cdn.js", I = "https://cdn-sdk.paynext.com/index.cdn.js";
|
|
273
325
|
let d = null, c = null;
|
|
274
|
-
async function
|
|
326
|
+
async function g(a) {
|
|
275
327
|
if (c)
|
|
276
328
|
return c;
|
|
277
329
|
if (d)
|
|
@@ -287,7 +339,7 @@ async function f(a) {
|
|
|
287
339
|
return;
|
|
288
340
|
}
|
|
289
341
|
const n = document.createElement("script");
|
|
290
|
-
n.src = t === "develop" ?
|
|
342
|
+
n.src = t === "develop" ? P : I, n.crossOrigin = "*", n.async = !0, n.onload = () => {
|
|
291
343
|
const i = window.PayNextSDK;
|
|
292
344
|
if (!i) {
|
|
293
345
|
console.error("[PayNext CDN] PayNextSDK not found in window after load"), d = null, document.head.removeChild(n), o(new Error("PayNextSDK not found in global scope after loading"));
|
|
@@ -299,17 +351,17 @@ async function f(a) {
|
|
|
299
351
|
}, document.head.appendChild(n);
|
|
300
352
|
}), d;
|
|
301
353
|
}
|
|
302
|
-
class
|
|
354
|
+
class E {
|
|
303
355
|
constructor() {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
356
|
+
r(this, "cdnInstance", null);
|
|
357
|
+
r(this, "iframeInstance", null);
|
|
358
|
+
r(this, "initPromise", null);
|
|
359
|
+
r(this, "mode", "direct");
|
|
308
360
|
}
|
|
309
361
|
// initialize
|
|
310
362
|
async initialize(t) {
|
|
311
363
|
try {
|
|
312
|
-
if (this.initPromise = await
|
|
364
|
+
if (this.initPromise = await g(t), !this.initPromise.PayNextCheckout)
|
|
313
365
|
throw new Error("PayNextCheckout not found in CDN module");
|
|
314
366
|
this.cdnInstance = new this.initPromise.PayNextCheckout();
|
|
315
367
|
} catch (e) {
|
|
@@ -325,7 +377,7 @@ class I {
|
|
|
325
377
|
}
|
|
326
378
|
// mount with iframe mode support
|
|
327
379
|
async mount(t, e) {
|
|
328
|
-
return e.iframeMode === !0 ? (this.mode = "iframe", this.iframeInstance || (this.iframeInstance = new
|
|
380
|
+
return e.iframeMode === !0 ? (this.mode = "iframe", this.iframeInstance || (this.iframeInstance = new f()), this.iframeInstance.mount(t, e)) : (this.mode = "direct", await this.initialize(e.environment), this.waitForReady(), this.cdnInstance?.mount(t, e));
|
|
329
381
|
}
|
|
330
382
|
// unmount with mode detection
|
|
331
383
|
async unmount() {
|
|
@@ -343,17 +395,17 @@ class I {
|
|
|
343
395
|
}
|
|
344
396
|
// preload (optional optimization)
|
|
345
397
|
static async preload(t, e) {
|
|
346
|
-
e?.iframeMode ? await
|
|
398
|
+
e?.iframeMode ? await f.preload(t) : await g(t);
|
|
347
399
|
}
|
|
348
400
|
}
|
|
349
|
-
const
|
|
350
|
-
preload:
|
|
401
|
+
const D = {
|
|
402
|
+
preload: E.preload
|
|
351
403
|
};
|
|
352
404
|
var v = /* @__PURE__ */ ((a) => (a.PAYPAL = "PAYPAL", a.APPLE_PAY = "APPLEPAY", a.GOOGLE_PAY = "GPAY", a.CARD = "CARD", a.VENMO = "VENMO", a))(v || {});
|
|
353
405
|
export {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
406
|
+
E as PayNextCheckout,
|
|
407
|
+
f as PayNextCheckoutIframe,
|
|
408
|
+
D as PayNextSDK,
|
|
409
|
+
R as PayNextSDKIframe,
|
|
358
410
|
v as PaymentMethod
|
|
359
411
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
(function(i,
|
|
1
|
+
(function(i,r){typeof exports=="object"&&typeof module<"u"?r(exports):typeof define=="function"&&define.amd?define(["exports"],r):(i=typeof globalThis<"u"?globalThis:i||self,r(i["PayNext SDK"]={}))})(this,(function(i){"use strict";var R=Object.defineProperty;var D=(i,r,c)=>r in i?R(i,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):i[r]=c;var d=(i,r,c)=>D(i,typeof r!="symbol"?r+"":r,c);const r="https://cdn-sdk-dev.paynext.com/checkout.html",c="https://cdn-sdk.paynext.com/checkout.html",k={develop:"https://cdn-sdk-dev.paynext.com",production:"https://cdn-sdk.paynext.com"};function C(s){return`<!DOCTYPE html>
|
|
2
2
|
<html lang="en">
|
|
3
3
|
<head>
|
|
4
4
|
<meta charset="UTF-8">
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
.loading { display: flex; align-items: center; justify-content: center; min-height: 400px; color: #666; font-size: 14px; }
|
|
13
13
|
.loading::after { content: '...'; animation: dots 1.5s steps(4, end) infinite; }
|
|
14
14
|
@keyframes dots { 0%, 20% { content: '.'; } 40% { content: '..'; } 60%, 100% { content: '...'; } }
|
|
15
|
+
.error { color: #e53e3e; padding: 20px; text-align: center; }
|
|
15
16
|
</style>
|
|
16
17
|
</head>
|
|
17
18
|
<body>
|
|
@@ -29,6 +30,11 @@
|
|
|
29
30
|
window.parent.postMessage({ type, payload, requestId }, parentOrigin)
|
|
30
31
|
}
|
|
31
32
|
|
|
33
|
+
function showError(message) {
|
|
34
|
+
document.getElementById('checkout-container').innerHTML =
|
|
35
|
+
'<div class="error"><strong>Error:</strong> ' + message + '</div>'
|
|
36
|
+
}
|
|
37
|
+
|
|
32
38
|
window.addEventListener('message', async (event) => {
|
|
33
39
|
const data = event.data
|
|
34
40
|
if (!parentOrigin) parentOrigin = event.origin
|
|
@@ -39,23 +45,47 @@
|
|
|
39
45
|
case MessageType.MOUNT:
|
|
40
46
|
if (data.payload?.config) {
|
|
41
47
|
config = data.payload.config
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
// Import SDK from CDN
|
|
51
|
+
const { PayNextCheckout } = await import('${s}')
|
|
52
|
+
|
|
53
|
+
if (!PayNextCheckout) {
|
|
54
|
+
throw new Error('PayNextCheckout not found in SDK module')
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
checkout = new PayNextCheckout()
|
|
58
|
+
await checkout.mount('checkout-container', {
|
|
59
|
+
...config,
|
|
60
|
+
onCheckoutLoaded: (result) => {
|
|
61
|
+
sendToParent(MessageType.LOADED, result, data.requestId)
|
|
62
|
+
notifyResize()
|
|
63
|
+
},
|
|
64
|
+
onCheckoutAttempt: (result) => sendToParent(MessageType.ATTEMPT, result),
|
|
65
|
+
onCheckoutComplete: (result) => sendToParent(MessageType.COMPLETE, result),
|
|
66
|
+
onCheckoutFail: (error) => sendToParent(MessageType.FAIL, error)
|
|
67
|
+
})
|
|
68
|
+
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
69
|
+
setupResizeObserver()
|
|
70
|
+
} catch (importError) {
|
|
71
|
+
console.error('[Iframe] Failed to load SDK:', importError)
|
|
72
|
+
showError('Failed to load PayNext SDK: ' + importError.message)
|
|
73
|
+
sendToParent(MessageType.ERROR, {
|
|
74
|
+
message: 'SDK import failed: ' + importError.message,
|
|
75
|
+
stack: importError.stack
|
|
76
|
+
}, data.requestId)
|
|
77
|
+
}
|
|
53
78
|
}
|
|
54
79
|
break
|
|
80
|
+
|
|
55
81
|
case MessageType.UNMOUNT:
|
|
56
|
-
if (checkout) {
|
|
82
|
+
if (checkout) {
|
|
83
|
+
await checkout.unmount()
|
|
84
|
+
checkout = null
|
|
85
|
+
}
|
|
57
86
|
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
58
87
|
break
|
|
88
|
+
|
|
59
89
|
case MessageType.UPDATE_CONFIG:
|
|
60
90
|
if (checkout && data.payload?.config) {
|
|
61
91
|
await checkout.updateConfig(data.payload.config)
|
|
@@ -64,25 +94,45 @@
|
|
|
64
94
|
break
|
|
65
95
|
}
|
|
66
96
|
} catch (error) {
|
|
67
|
-
|
|
97
|
+
console.error('[Iframe] Error:', error)
|
|
98
|
+
sendToParent(MessageType.ERROR, {
|
|
99
|
+
message: error.message,
|
|
100
|
+
stack: error.stack
|
|
101
|
+
}, data.requestId)
|
|
68
102
|
}
|
|
69
103
|
})
|
|
70
104
|
|
|
71
105
|
function setupResizeObserver() {
|
|
72
106
|
new ResizeObserver(entries => {
|
|
73
|
-
for (const entry of entries)
|
|
107
|
+
for (const entry of entries) {
|
|
108
|
+
sendToParent(MessageType.RESIZE, { height: entry.contentRect.height })
|
|
109
|
+
}
|
|
74
110
|
}).observe(document.body)
|
|
75
111
|
}
|
|
76
112
|
|
|
77
|
-
function notifyResize() {
|
|
113
|
+
function notifyResize() {
|
|
114
|
+
sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight })
|
|
115
|
+
}
|
|
78
116
|
|
|
79
117
|
window.addEventListener('load', () => {
|
|
80
|
-
|
|
118
|
+
console.log('[Iframe] Iframe loaded, sending ready message')
|
|
119
|
+
setTimeout(() => {
|
|
120
|
+
window.parent.postMessage(
|
|
121
|
+
{ type: MessageType.READY, payload: { success: true } },
|
|
122
|
+
'*'
|
|
123
|
+
)
|
|
124
|
+
}, 100)
|
|
81
125
|
})
|
|
82
126
|
|
|
83
127
|
window.addEventListener('error', (event) => {
|
|
84
|
-
|
|
128
|
+
console.error('[Iframe] Global error:', event.error)
|
|
129
|
+
sendToParent(MessageType.ERROR, {
|
|
130
|
+
message: event.error?.message || 'Unknown error',
|
|
131
|
+
stack: event.error?.stack
|
|
132
|
+
})
|
|
85
133
|
})
|
|
134
|
+
|
|
135
|
+
console.log('[Iframe] PayNext Checkout iframe initialized, SDK URL: ${s}')
|
|
86
136
|
<\/script>
|
|
87
137
|
</body>
|
|
88
|
-
</html>`}class
|
|
138
|
+
</html>`}class h{constructor(){d(this,"iframe",null);d(this,"container",null);d(this,"config",null);d(this,"messageHandlers",new Map);d(this,"pendingRequests",new Map);d(this,"isReady",!1);d(this,"allowedOrigin","");this.handleMessage=this.handleMessage.bind(this)}generateRequestId(){return`${Date.now()}-${Math.random().toString(36).substr(2,9)}`}sendMessage(t,e){return new Promise((o,n)=>{if(!this.iframe||!this.iframe.contentWindow){n(new Error("Iframe not initialized"));return}const a=this.generateRequestId(),l={type:t,payload:e,requestId:a};this.pendingRequests.set(a,{resolve:o,reject:n}),setTimeout(()=>{this.pendingRequests.has(a)&&(this.pendingRequests.delete(a),n(new Error(`Request timeout: ${t}`)))},3e4),this.iframe.contentWindow.postMessage(l,this.allowedOrigin)})}handleMessage(t){if(t.origin!==this.allowedOrigin){console.warn("[PayNext Iframe] Message from unauthorized origin:",t.origin);return}const e=t.data;if(!(!e.type||!e.type.startsWith("paynext:"))){if(e.requestId&&this.pendingRequests.has(e.requestId)){const{resolve:o}=this.pendingRequests.get(e.requestId);this.pendingRequests.delete(e.requestId),o(e.payload);return}switch(e.type){case"paynext:ready":this.isReady=!0,console.log("[PayNext Iframe] SDK ready");break;case"paynext:loaded":this.config?.onCheckoutLoaded&&this.config.onCheckoutLoaded(e.payload);break;case"paynext:attempt":this.config?.onCheckoutAttempt&&this.config.onCheckoutAttempt(e.payload);break;case"paynext:complete":this.config?.onCheckoutComplete&&this.config.onCheckoutComplete(e.payload);break;case"paynext:fail":this.config?.onCheckoutFail&&this.config.onCheckoutFail(e.payload);break;case"paynext:error":console.error("[PayNext Iframe] Error:",e.payload);break;case"paynext:resize":this.iframe&&e.payload?.height&&(this.iframe.style.height=`${e.payload.height}px`);break;default:console.warn("[PayNext Iframe] Unknown message type:",e.type)}}}createIframe(t,e=!1){const o=t.environment?.toLowerCase()?.includes("develop")||t.environment?.toLowerCase()?.includes("staging")?"develop":"production";this.allowedOrigin=e?window.location.origin:k[o];const n=document.createElement("iframe"),a=o==="develop"?r:c,l=o==="develop"?"https://cdn-sdk-dev.paynext.com/index.cdn.js":"https://cdn-sdk.paynext.com/index.cdn.js";return e?(console.log("[PayNext Iframe] Using inline iframe mode (embedded HTML)"),console.log("[PayNext Iframe] SDK will load from:",l),n.srcdoc=C(l)):(console.log("[PayNext Iframe] Using CDN iframe mode"),console.log("[PayNext Iframe] Loading iframe from:",a),n.src=a),n.style.border="none",n.style.width="100%",n.style.minHeight="400px",n.style.display="block",n.allow="payment *",n.sandbox.add("allow-scripts","allow-same-origin","allow-forms","allow-popups","allow-popups-to-escape-sandbox","allow-top-navigation-by-user-activation"),n.setAttribute("referrerpolicy","strict-origin-when-cross-origin"),n.setAttribute("importance","high"),n}async waitForReady(t=1e4){const e=Date.now();for(;!this.isReady;){if(Date.now()-e>t)throw new Error("Iframe initialization timeout");await new Promise(o=>setTimeout(o,100))}}async mount(t,e){try{const o=document.getElementById(t);if(!o)throw new Error(`Container element "${t}" not found`);this.container=o,this.config=e,this.iframe&&await this.unmount();const n=e.environment?.toLowerCase()?.includes("develop")||e.environment?.toLowerCase()?.includes("staging")?"develop":"production",a=n==="develop"?r:c,l=window.location.hostname==="localhost"||window.location.hostname==="127.0.0.1"||window.location.hostname.startsWith("192.168.")||window.location.hostname.endsWith(".local");let p=l;if(!l&&n==="develop")try{const f=new AbortController,y=setTimeout(()=>f.abort(),2e3);await fetch(a,{method:"HEAD",signal:f.signal,cache:"no-cache"}),clearTimeout(y),p=!1,console.log("[PayNext Iframe] CDN is available, using remote iframe")}catch{p=!0,console.warn("[PayNext Iframe] CDN not available, using inline fallback mode")}p&&(console.log("[PayNext Iframe] Using inline fallback mode"),console.log("[PayNext Iframe] Reason:",l?"localhost detected":"CDN not available")),this.iframe=this.createIframe(e,p),window.addEventListener("message",this.handleMessage),o.innerHTML="",o.appendChild(this.iframe),await new Promise((f,y)=>{const P=setTimeout(()=>{y(new Error("Iframe load timeout"))},1e4);this.iframe.onload=()=>{clearTimeout(P),f()},this.iframe.onerror=()=>{clearTimeout(P),y(new Error("Iframe failed to load"))}}),await this.waitForReady(),await this.sendMessage("paynext:mount",{config:{...e,onCheckoutLoaded:void 0,onCheckoutAttempt:void 0,onCheckoutComplete:void 0,onCheckoutFail:void 0}}),console.log("[PayNext Iframe] Checkout mounted successfully")}catch(o){throw console.error("[PayNext Iframe] Mount failed:",o),await this.unmount(),o}}async unmount(){try{this.iframe&&(this.isReady&&await this.sendMessage("paynext:unmount").catch(()=>{}),this.iframe.parentNode&&this.iframe.parentNode.removeChild(this.iframe),this.iframe=null),window.removeEventListener("message",this.handleMessage),this.container&&(this.container.innerHTML="",this.container=null),this.isReady=!1,this.config=null,this.pendingRequests.clear(),this.messageHandlers.clear(),console.log("[PayNext Iframe] Checkout unmounted")}catch(t){console.error("[PayNext Iframe] Unmount failed:",t)}}async updateConfig(t){if(!this.isReady)throw new Error("Iframe not ready");this.config={...this.config,...t},await this.sendMessage("paynext:updateConfig",{config:{...t,onCheckoutLoaded:void 0,onCheckoutAttempt:void 0,onCheckoutComplete:void 0,onCheckoutFail:void 0}})}static async preload(t){const o=(t?.toLowerCase()?.includes("develop")||t?.toLowerCase()?.includes("staging")?"develop":"production")==="develop"?r:c,n=document.createElement("link");n.rel="preconnect",n.href=new URL(o).origin,document.head.appendChild(n);const a=document.createElement("link");a.rel="dns-prefetch",a.href=new URL(o).origin,document.head.appendChild(a)}}const I={preload:h.preload},N="https://cdn-sdk-dev.paynext.com/index.cdn.js",E="https://cdn-sdk.paynext.com/index.cdn.js";let u=null,m=null;async function g(s){if(m)return m;if(u)return u;const t=s?.toLowerCase()?.includes("develop")||s?.toLowerCase()?.includes("staging")?"develop":"production";return u=new Promise((e,o)=>{if(typeof window>"u"){o(new Error("PayNext SDK can only be loaded in browser environment"));return}if(window.PayNextSDK){console.log("[PayNext CDN] SDK already loaded from cache"),m=window.PayNextSDK,e(m);return}const n=document.createElement("script");n.src=t==="develop"?N:E,n.crossOrigin="*",n.async=!0,n.onload=()=>{const a=window.PayNextSDK;if(!a){console.error("[PayNext CDN] PayNextSDK not found in window after load"),u=null,document.head.removeChild(n),o(new Error("PayNextSDK not found in global scope after loading"));return}m=a,e(a)},n.onerror=a=>{console.error("[PayNext CDN] Failed to load script:",a),u=null,document.head.removeChild(n),o(new Error("Failed to load PayNext SDK script from CDN"))},document.head.appendChild(n)}),u}class w{constructor(){d(this,"cdnInstance",null);d(this,"iframeInstance",null);d(this,"initPromise",null);d(this,"mode","direct")}async initialize(t){try{if(this.initPromise=await g(t),!this.initPromise.PayNextCheckout)throw new Error("PayNextCheckout not found in CDN module");this.cdnInstance=new this.initPromise.PayNextCheckout}catch(e){throw console.error("[PayNext CDN] Failed to initialize:",e),e}}waitForReady(){if(!this.initPromise)throw new Error("SDK initialization not started");if(!this.cdnInstance)throw new Error("SDK instance not created")}async mount(t,e){return e.iframeMode===!0?(this.mode="iframe",this.iframeInstance||(this.iframeInstance=new h),this.iframeInstance.mount(t,e)):(this.mode="direct",await this.initialize(e.environment),this.waitForReady(),this.cdnInstance?.mount(t,e))}async unmount(){if(this.mode==="iframe"&&this.iframeInstance)return this.iframeInstance.unmount();if(this.waitForReady(),this.cdnInstance?.unmount)return this.cdnInstance.unmount()}async updateConfig(t){if(this.mode==="iframe"&&this.iframeInstance)return this.iframeInstance.updateConfig(t);if(this.waitForReady(),this.cdnInstance?.updateConfig)return this.cdnInstance.updateConfig(t)}static async preload(t,e){e?.iframeMode?await h.preload(t):await g(t)}}const v={preload:w.preload};var x=(s=>(s.PAYPAL="PAYPAL",s.APPLE_PAY="APPLEPAY",s.GOOGLE_PAY="GPAY",s.CARD="CARD",s.VENMO="VENMO",s))(x||{});i.PayNextCheckout=w,i.PayNextCheckoutIframe=h,i.PayNextSDK=v,i.PayNextSDKIframe=I,i.PaymentMethod=x,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|