@paynext/sdk 0.0.170 → 0.0.172
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/checkout.html +17 -2
- package/dist/index.es.js +117 -55
- package/dist/index.umd.js +81 -19
- package/package.json +1 -1
package/dist/checkout.html
CHANGED
|
@@ -124,8 +124,23 @@
|
|
|
124
124
|
if (data.payload?.config) {
|
|
125
125
|
config = data.payload.config
|
|
126
126
|
|
|
127
|
-
//
|
|
128
|
-
|
|
127
|
+
// Load SDK from CDN
|
|
128
|
+
// CDN version exports to window.PayNextSDK
|
|
129
|
+
if (!window.PayNextSDK) {
|
|
130
|
+
await new Promise((resolve, reject) => {
|
|
131
|
+
const script = document.createElement('script')
|
|
132
|
+
script.src = './index.cdn.js'
|
|
133
|
+
script.onload = resolve
|
|
134
|
+
script.onerror = reject
|
|
135
|
+
document.head.appendChild(script)
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const { PayNextCheckout } = window.PayNextSDK || {}
|
|
140
|
+
|
|
141
|
+
if (!PayNextCheckout) {
|
|
142
|
+
throw new Error('PayNextCheckout not found in window.PayNextSDK')
|
|
143
|
+
}
|
|
129
144
|
|
|
130
145
|
checkout = new PayNextCheckout()
|
|
131
146
|
|
package/dist/index.es.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
var w = Object.defineProperty;
|
|
2
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
|
|
4
|
-
const
|
|
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", C = {
|
|
5
5
|
develop: "https://cdn-sdk-dev.paynext.com",
|
|
6
6
|
production: "https://cdn-sdk.paynext.com"
|
|
7
7
|
};
|
|
8
|
-
function
|
|
8
|
+
function P(a) {
|
|
9
9
|
return `<!DOCTYPE html>
|
|
10
10
|
<html lang="en">
|
|
11
11
|
<head>
|
|
@@ -20,6 +20,7 @@ function k(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 k(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,59 @@ function k(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
|
+
// Load SDK from CDN
|
|
59
|
+
// CDN version exports to window.PayNextSDK
|
|
60
|
+
if (!window.PayNextSDK) {
|
|
61
|
+
// Load SDK script if not already loaded
|
|
62
|
+
await new Promise((resolve, reject) => {
|
|
63
|
+
const script = document.createElement('script')
|
|
64
|
+
script.src = '${a}'
|
|
65
|
+
script.onload = resolve
|
|
66
|
+
script.onerror = reject
|
|
67
|
+
document.head.appendChild(script)
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const { PayNextCheckout } = window.PayNextSDK || {}
|
|
72
|
+
|
|
73
|
+
if (!PayNextCheckout) {
|
|
74
|
+
throw new Error('PayNextCheckout not found in window.PayNextSDK')
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
checkout = new PayNextCheckout()
|
|
78
|
+
await checkout.mount('checkout-container', {
|
|
79
|
+
...config,
|
|
80
|
+
onCheckoutLoaded: (result) => {
|
|
81
|
+
sendToParent(MessageType.LOADED, result, data.requestId)
|
|
82
|
+
notifyResize()
|
|
83
|
+
},
|
|
84
|
+
onCheckoutAttempt: (result) => sendToParent(MessageType.ATTEMPT, result),
|
|
85
|
+
onCheckoutComplete: (result) => sendToParent(MessageType.COMPLETE, result),
|
|
86
|
+
onCheckoutFail: (error) => sendToParent(MessageType.FAIL, error)
|
|
87
|
+
})
|
|
88
|
+
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
89
|
+
setupResizeObserver()
|
|
90
|
+
} catch (importError) {
|
|
91
|
+
console.error('[Iframe] Failed to load SDK:', importError)
|
|
92
|
+
showError('Failed to load PayNext SDK: ' + importError.message)
|
|
93
|
+
sendToParent(MessageType.ERROR, {
|
|
94
|
+
message: 'SDK import failed: ' + importError.message,
|
|
95
|
+
stack: importError.stack
|
|
96
|
+
}, data.requestId)
|
|
97
|
+
}
|
|
61
98
|
}
|
|
62
99
|
break
|
|
100
|
+
|
|
63
101
|
case MessageType.UNMOUNT:
|
|
64
|
-
if (checkout) {
|
|
102
|
+
if (checkout) {
|
|
103
|
+
await checkout.unmount()
|
|
104
|
+
checkout = null
|
|
105
|
+
}
|
|
65
106
|
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
66
107
|
break
|
|
108
|
+
|
|
67
109
|
case MessageType.UPDATE_CONFIG:
|
|
68
110
|
if (checkout && data.payload?.config) {
|
|
69
111
|
await checkout.updateConfig(data.payload.config)
|
|
@@ -72,38 +114,58 @@ function k(a) {
|
|
|
72
114
|
break
|
|
73
115
|
}
|
|
74
116
|
} catch (error) {
|
|
75
|
-
|
|
117
|
+
console.error('[Iframe] Error:', error)
|
|
118
|
+
sendToParent(MessageType.ERROR, {
|
|
119
|
+
message: error.message,
|
|
120
|
+
stack: error.stack
|
|
121
|
+
}, data.requestId)
|
|
76
122
|
}
|
|
77
123
|
})
|
|
78
124
|
|
|
79
125
|
function setupResizeObserver() {
|
|
80
126
|
new ResizeObserver(entries => {
|
|
81
|
-
for (const entry of entries)
|
|
127
|
+
for (const entry of entries) {
|
|
128
|
+
sendToParent(MessageType.RESIZE, { height: entry.contentRect.height })
|
|
129
|
+
}
|
|
82
130
|
}).observe(document.body)
|
|
83
131
|
}
|
|
84
132
|
|
|
85
|
-
function notifyResize() {
|
|
133
|
+
function notifyResize() {
|
|
134
|
+
sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight })
|
|
135
|
+
}
|
|
86
136
|
|
|
87
137
|
window.addEventListener('load', () => {
|
|
88
|
-
|
|
138
|
+
console.log('[Iframe] Iframe loaded, sending ready message')
|
|
139
|
+
setTimeout(() => {
|
|
140
|
+
window.parent.postMessage(
|
|
141
|
+
{ type: MessageType.READY, payload: { success: true } },
|
|
142
|
+
'*'
|
|
143
|
+
)
|
|
144
|
+
}, 100)
|
|
89
145
|
})
|
|
90
146
|
|
|
91
147
|
window.addEventListener('error', (event) => {
|
|
92
|
-
|
|
148
|
+
console.error('[Iframe] Global error:', event.error)
|
|
149
|
+
sendToParent(MessageType.ERROR, {
|
|
150
|
+
message: event.error?.message || 'Unknown error',
|
|
151
|
+
stack: event.error?.stack
|
|
152
|
+
})
|
|
93
153
|
})
|
|
154
|
+
|
|
155
|
+
console.log('[Iframe] PayNext Checkout iframe initialized, SDK URL: ${a}')
|
|
94
156
|
<\/script>
|
|
95
157
|
</body>
|
|
96
158
|
</html>`;
|
|
97
159
|
}
|
|
98
160
|
class f {
|
|
99
161
|
constructor() {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
162
|
+
r(this, "iframe", null);
|
|
163
|
+
r(this, "container", null);
|
|
164
|
+
r(this, "config", null);
|
|
165
|
+
r(this, "messageHandlers", /* @__PURE__ */ new Map());
|
|
166
|
+
r(this, "pendingRequests", /* @__PURE__ */ new Map());
|
|
167
|
+
r(this, "isReady", !1);
|
|
168
|
+
r(this, "allowedOrigin", "");
|
|
107
169
|
this.handleMessage = this.handleMessage.bind(this);
|
|
108
170
|
}
|
|
109
171
|
// generate unique request ID
|
|
@@ -117,10 +179,10 @@ class f {
|
|
|
117
179
|
n(new Error("Iframe not initialized"));
|
|
118
180
|
return;
|
|
119
181
|
}
|
|
120
|
-
const i = this.generateRequestId(),
|
|
182
|
+
const i = this.generateRequestId(), s = { type: t, payload: e, requestId: i };
|
|
121
183
|
this.pendingRequests.set(i, { resolve: o, reject: n }), setTimeout(() => {
|
|
122
184
|
this.pendingRequests.has(i) && (this.pendingRequests.delete(i), n(new Error(`Request timeout: ${t}`)));
|
|
123
|
-
}, 3e4), this.iframe.contentWindow.postMessage(
|
|
185
|
+
}, 3e4), this.iframe.contentWindow.postMessage(s, this.allowedOrigin);
|
|
124
186
|
});
|
|
125
187
|
}
|
|
126
188
|
// handle messages from iframe
|
|
@@ -167,8 +229,8 @@ class f {
|
|
|
167
229
|
createIframe(t, e = !1) {
|
|
168
230
|
const o = t.environment?.toLowerCase()?.includes("develop") || t.environment?.toLowerCase()?.includes("staging") ? "develop" : "production";
|
|
169
231
|
this.allowedOrigin = e ? window.location.origin : C[o];
|
|
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:",
|
|
232
|
+
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";
|
|
233
|
+
return e ? (console.log("[PayNext Iframe] Using inline iframe mode (embedded HTML)"), console.log("[PayNext Iframe] SDK will load from:", s), n.srcdoc = P(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
234
|
"allow-scripts",
|
|
173
235
|
// required for SDK execution
|
|
174
236
|
"allow-same-origin",
|
|
@@ -199,27 +261,27 @@ class f {
|
|
|
199
261
|
if (!o)
|
|
200
262
|
throw new Error(`Container element "${t}" not found`);
|
|
201
263
|
this.container = o, this.config = e, this.iframe && await this.unmount();
|
|
202
|
-
const n = e.environment?.toLowerCase()?.includes("develop") || e.environment?.toLowerCase()?.includes("staging") ? "develop" : "production", i = n === "develop" ?
|
|
203
|
-
let l =
|
|
204
|
-
if (!
|
|
264
|
+
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");
|
|
265
|
+
let l = s;
|
|
266
|
+
if (!s && n === "develop")
|
|
205
267
|
try {
|
|
206
|
-
const
|
|
268
|
+
const m = new AbortController(), u = setTimeout(() => m.abort(), 2e3);
|
|
207
269
|
await fetch(i, {
|
|
208
270
|
method: "HEAD",
|
|
209
|
-
signal:
|
|
271
|
+
signal: m.signal,
|
|
210
272
|
cache: "no-cache"
|
|
211
|
-
}), clearTimeout(
|
|
273
|
+
}), clearTimeout(u), l = !1, console.log("[PayNext Iframe] CDN is available, using remote iframe");
|
|
212
274
|
} catch {
|
|
213
275
|
l = !0, console.warn("[PayNext Iframe] CDN not available, using inline fallback mode");
|
|
214
276
|
}
|
|
215
|
-
l && (console.log("[PayNext Iframe] Using inline fallback mode"), console.log("[PayNext Iframe] Reason:",
|
|
277
|
+
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) => {
|
|
216
278
|
const y = setTimeout(() => {
|
|
217
|
-
|
|
279
|
+
u(new Error("Iframe load timeout"));
|
|
218
280
|
}, 1e4);
|
|
219
281
|
this.iframe.onload = () => {
|
|
220
|
-
clearTimeout(y),
|
|
282
|
+
clearTimeout(y), m();
|
|
221
283
|
}, this.iframe.onerror = () => {
|
|
222
|
-
clearTimeout(y),
|
|
284
|
+
clearTimeout(y), u(new Error("Iframe failed to load"));
|
|
223
285
|
};
|
|
224
286
|
}), await this.waitForReady(), await this.sendMessage("paynext:mount", {
|
|
225
287
|
config: {
|
|
@@ -263,15 +325,15 @@ class f {
|
|
|
263
325
|
}
|
|
264
326
|
// preload iframe (optional optimization)
|
|
265
327
|
static async preload(t) {
|
|
266
|
-
const o = (t?.toLowerCase()?.includes("develop") || t?.toLowerCase()?.includes("staging") ? "develop" : "production") === "develop" ?
|
|
328
|
+
const o = (t?.toLowerCase()?.includes("develop") || t?.toLowerCase()?.includes("staging") ? "develop" : "production") === "develop" ? h : p, n = document.createElement("link");
|
|
267
329
|
n.rel = "preconnect", n.href = new URL(o).origin, document.head.appendChild(n);
|
|
268
330
|
const i = document.createElement("link");
|
|
269
331
|
i.rel = "dns-prefetch", i.href = new URL(o).origin, document.head.appendChild(i);
|
|
270
332
|
}
|
|
271
333
|
}
|
|
272
|
-
const
|
|
334
|
+
const R = {
|
|
273
335
|
preload: f.preload
|
|
274
|
-
},
|
|
336
|
+
}, k = "https://cdn-sdk-dev.paynext.com/index.cdn.js", I = "https://cdn-sdk.paynext.com/index.cdn.js";
|
|
275
337
|
let d = null, c = null;
|
|
276
338
|
async function g(a) {
|
|
277
339
|
if (c)
|
|
@@ -289,7 +351,7 @@ async function g(a) {
|
|
|
289
351
|
return;
|
|
290
352
|
}
|
|
291
353
|
const n = document.createElement("script");
|
|
292
|
-
n.src = t === "develop" ?
|
|
354
|
+
n.src = t === "develop" ? k : I, n.crossOrigin = "*", n.async = !0, n.onload = () => {
|
|
293
355
|
const i = window.PayNextSDK;
|
|
294
356
|
if (!i) {
|
|
295
357
|
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"));
|
|
@@ -301,12 +363,12 @@ async function g(a) {
|
|
|
301
363
|
}, document.head.appendChild(n);
|
|
302
364
|
}), d;
|
|
303
365
|
}
|
|
304
|
-
class
|
|
366
|
+
class E {
|
|
305
367
|
constructor() {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
368
|
+
r(this, "cdnInstance", null);
|
|
369
|
+
r(this, "iframeInstance", null);
|
|
370
|
+
r(this, "initPromise", null);
|
|
371
|
+
r(this, "mode", "direct");
|
|
310
372
|
}
|
|
311
373
|
// initialize
|
|
312
374
|
async initialize(t) {
|
|
@@ -348,14 +410,14 @@ class v {
|
|
|
348
410
|
e?.iframeMode ? await f.preload(t) : await g(t);
|
|
349
411
|
}
|
|
350
412
|
}
|
|
351
|
-
const
|
|
352
|
-
preload:
|
|
413
|
+
const D = {
|
|
414
|
+
preload: E.preload
|
|
353
415
|
};
|
|
354
416
|
var N = /* @__PURE__ */ ((a) => (a.PAYPAL = "PAYPAL", a.APPLE_PAY = "APPLEPAY", a.GOOGLE_PAY = "GPAY", a.CARD = "CARD", a.VENMO = "VENMO", a))(N || {});
|
|
355
417
|
export {
|
|
356
|
-
|
|
418
|
+
E as PayNextCheckout,
|
|
357
419
|
f as PayNextCheckoutIframe,
|
|
358
|
-
|
|
359
|
-
|
|
420
|
+
D as PayNextSDK,
|
|
421
|
+
R as PayNextSDKIframe,
|
|
360
422
|
N as PaymentMethod
|
|
361
423
|
};
|
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 D=Object.defineProperty;var R=(i,r,c)=>r in i?D(i,r,{enumerable:!0,configurable:!0,writable:!0,value:c}):i[r]=c;var d=(i,r,c)=>R(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",C={develop:"https://cdn-sdk-dev.paynext.com",production:"https://cdn-sdk.paynext.com"};function k(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,59 @@
|
|
|
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
|
+
// Load SDK from CDN
|
|
51
|
+
// CDN version exports to window.PayNextSDK
|
|
52
|
+
if (!window.PayNextSDK) {
|
|
53
|
+
// Load SDK script if not already loaded
|
|
54
|
+
await new Promise((resolve, reject) => {
|
|
55
|
+
const script = document.createElement('script')
|
|
56
|
+
script.src = '${s}'
|
|
57
|
+
script.onload = resolve
|
|
58
|
+
script.onerror = reject
|
|
59
|
+
document.head.appendChild(script)
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const { PayNextCheckout } = window.PayNextSDK || {}
|
|
64
|
+
|
|
65
|
+
if (!PayNextCheckout) {
|
|
66
|
+
throw new Error('PayNextCheckout not found in window.PayNextSDK')
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
checkout = new PayNextCheckout()
|
|
70
|
+
await checkout.mount('checkout-container', {
|
|
71
|
+
...config,
|
|
72
|
+
onCheckoutLoaded: (result) => {
|
|
73
|
+
sendToParent(MessageType.LOADED, result, data.requestId)
|
|
74
|
+
notifyResize()
|
|
75
|
+
},
|
|
76
|
+
onCheckoutAttempt: (result) => sendToParent(MessageType.ATTEMPT, result),
|
|
77
|
+
onCheckoutComplete: (result) => sendToParent(MessageType.COMPLETE, result),
|
|
78
|
+
onCheckoutFail: (error) => sendToParent(MessageType.FAIL, error)
|
|
79
|
+
})
|
|
80
|
+
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
81
|
+
setupResizeObserver()
|
|
82
|
+
} catch (importError) {
|
|
83
|
+
console.error('[Iframe] Failed to load SDK:', importError)
|
|
84
|
+
showError('Failed to load PayNext SDK: ' + importError.message)
|
|
85
|
+
sendToParent(MessageType.ERROR, {
|
|
86
|
+
message: 'SDK import failed: ' + importError.message,
|
|
87
|
+
stack: importError.stack
|
|
88
|
+
}, data.requestId)
|
|
89
|
+
}
|
|
53
90
|
}
|
|
54
91
|
break
|
|
92
|
+
|
|
55
93
|
case MessageType.UNMOUNT:
|
|
56
|
-
if (checkout) {
|
|
94
|
+
if (checkout) {
|
|
95
|
+
await checkout.unmount()
|
|
96
|
+
checkout = null
|
|
97
|
+
}
|
|
57
98
|
sendToParent(MessageType.READY, { success: true }, data.requestId)
|
|
58
99
|
break
|
|
100
|
+
|
|
59
101
|
case MessageType.UPDATE_CONFIG:
|
|
60
102
|
if (checkout && data.payload?.config) {
|
|
61
103
|
await checkout.updateConfig(data.payload.config)
|
|
@@ -64,25 +106,45 @@
|
|
|
64
106
|
break
|
|
65
107
|
}
|
|
66
108
|
} catch (error) {
|
|
67
|
-
|
|
109
|
+
console.error('[Iframe] Error:', error)
|
|
110
|
+
sendToParent(MessageType.ERROR, {
|
|
111
|
+
message: error.message,
|
|
112
|
+
stack: error.stack
|
|
113
|
+
}, data.requestId)
|
|
68
114
|
}
|
|
69
115
|
})
|
|
70
116
|
|
|
71
117
|
function setupResizeObserver() {
|
|
72
118
|
new ResizeObserver(entries => {
|
|
73
|
-
for (const entry of entries)
|
|
119
|
+
for (const entry of entries) {
|
|
120
|
+
sendToParent(MessageType.RESIZE, { height: entry.contentRect.height })
|
|
121
|
+
}
|
|
74
122
|
}).observe(document.body)
|
|
75
123
|
}
|
|
76
124
|
|
|
77
|
-
function notifyResize() {
|
|
125
|
+
function notifyResize() {
|
|
126
|
+
sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight })
|
|
127
|
+
}
|
|
78
128
|
|
|
79
129
|
window.addEventListener('load', () => {
|
|
80
|
-
|
|
130
|
+
console.log('[Iframe] Iframe loaded, sending ready message')
|
|
131
|
+
setTimeout(() => {
|
|
132
|
+
window.parent.postMessage(
|
|
133
|
+
{ type: MessageType.READY, payload: { success: true } },
|
|
134
|
+
'*'
|
|
135
|
+
)
|
|
136
|
+
}, 100)
|
|
81
137
|
})
|
|
82
138
|
|
|
83
139
|
window.addEventListener('error', (event) => {
|
|
84
|
-
|
|
140
|
+
console.error('[Iframe] Global error:', event.error)
|
|
141
|
+
sendToParent(MessageType.ERROR, {
|
|
142
|
+
message: event.error?.message || 'Unknown error',
|
|
143
|
+
stack: event.error?.stack
|
|
144
|
+
})
|
|
85
145
|
})
|
|
146
|
+
|
|
147
|
+
console.log('[Iframe] PayNext Checkout iframe initialized, SDK URL: ${s}')
|
|
86
148
|
<\/script>
|
|
87
149
|
</body>
|
|
88
|
-
</html>`}class
|
|
150
|
+
</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:C[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=k(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",v="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:v,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 E={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=E,i.PayNextSDKIframe=I,i.PaymentMethod=x,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|