@paynext/sdk 0.0.170 → 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 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 s = (a, t, e) => x(a, typeof t != "symbol" ? t + "" : t, e);
4
- const m = "https://cdn-sdk-dev.paynext.com/checkout.html", p = "https://cdn-sdk.paynext.com/checkout.html", C = {
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
  };
8
- function k(a) {
8
+ function C(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,47 @@ function k(a) {
47
53
  case MessageType.MOUNT:
48
54
  if (data.payload?.config) {
49
55
  config = data.payload.config
50
- const { PayNextCheckout } = await import('${a}')
51
- checkout = new PayNextCheckout()
52
- await checkout.mount('checkout-container', {
53
- ...config,
54
- onCheckoutLoaded: (result) => { sendToParent(MessageType.LOADED, result, data.requestId); notifyResize() },
55
- onCheckoutAttempt: (result) => sendToParent(MessageType.ATTEMPT, result),
56
- onCheckoutComplete: (result) => sendToParent(MessageType.COMPLETE, result),
57
- onCheckoutFail: (error) => sendToParent(MessageType.FAIL, error)
58
- })
59
- sendToParent(MessageType.READY, { success: true }, data.requestId)
60
- setupResizeObserver()
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) { await checkout.unmount(); checkout = null }
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 k(a) {
72
102
  break
73
103
  }
74
104
  } catch (error) {
75
- sendToParent(MessageType.ERROR, { message: error.message, stack: error.stack }, data.requestId)
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) sendToParent(MessageType.RESIZE, { height: entry.contentRect.height })
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() { sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight }) }
121
+ function notifyResize() {
122
+ sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight })
123
+ }
86
124
 
87
125
  window.addEventListener('load', () => {
88
- setTimeout(() => window.parent.postMessage({ type: MessageType.READY, payload: { success: true } }, '*'), 100)
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
- sendToParent(MessageType.ERROR, { message: event.error?.message || 'Unknown error', stack: event.error?.stack })
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
148
  class f {
99
149
  constructor() {
100
- s(this, "iframe", null);
101
- s(this, "container", null);
102
- s(this, "config", null);
103
- s(this, "messageHandlers", /* @__PURE__ */ new Map());
104
- s(this, "pendingRequests", /* @__PURE__ */ new Map());
105
- s(this, "isReady", !1);
106
- s(this, "allowedOrigin", "");
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 f {
117
167
  n(new Error("Iframe not initialized"));
118
168
  return;
119
169
  }
120
- const i = this.generateRequestId(), r = { type: t, payload: e, requestId: i };
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(r, this.allowedOrigin);
173
+ }, 3e4), this.iframe.contentWindow.postMessage(s, this.allowedOrigin);
124
174
  });
125
175
  }
126
176
  // handle messages from iframe
@@ -166,9 +216,9 @@ class f {
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 : C[o];
170
- const n = document.createElement("iframe"), i = o === "develop" ? m : p, r = o === "develop" ? "https://cdn-sdk-dev.paynext.com/index.cdn.js" : "https://cdn-sdk.paynext.com/index.cdn.js";
171
- return e ? (console.log("[PayNext Iframe] Using inline iframe mode (embedded HTML)"), console.log("[PayNext Iframe] SDK will load from:", r), n.srcdoc = k(r)) : (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(
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,27 +249,27 @@ class f {
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 n = e.environment?.toLowerCase()?.includes("develop") || e.environment?.toLowerCase()?.includes("staging") ? "develop" : "production", i = n === "develop" ? m : p, r = window.location.hostname === "localhost" || window.location.hostname === "127.0.0.1" || window.location.hostname.startsWith("192.168.") || window.location.hostname.endsWith(".local");
203
- let l = r;
204
- if (!r && n === "develop")
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")
205
255
  try {
206
- const u = new AbortController(), h = setTimeout(() => u.abort(), 2e3);
256
+ const m = new AbortController(), u = setTimeout(() => m.abort(), 2e3);
207
257
  await fetch(i, {
208
258
  method: "HEAD",
209
- signal: u.signal,
259
+ signal: m.signal,
210
260
  cache: "no-cache"
211
- }), clearTimeout(h), l = !1, console.log("[PayNext Iframe] CDN is available, using remote iframe");
261
+ }), clearTimeout(u), l = !1, console.log("[PayNext Iframe] CDN is available, using remote iframe");
212
262
  } catch {
213
263
  l = !0, console.warn("[PayNext Iframe] CDN not available, using inline fallback mode");
214
264
  }
215
- l && (console.log("[PayNext Iframe] Using inline fallback mode"), console.log("[PayNext Iframe] Reason:", r ? "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((u, h) => {
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) => {
216
266
  const y = setTimeout(() => {
217
- h(new Error("Iframe load timeout"));
267
+ u(new Error("Iframe load timeout"));
218
268
  }, 1e4);
219
269
  this.iframe.onload = () => {
220
- clearTimeout(y), u();
270
+ clearTimeout(y), m();
221
271
  }, this.iframe.onerror = () => {
222
- clearTimeout(y), h(new Error("Iframe failed to load"));
272
+ clearTimeout(y), u(new Error("Iframe failed to load"));
223
273
  };
224
274
  }), await this.waitForReady(), await this.sendMessage("paynext:mount", {
225
275
  config: {
@@ -263,13 +313,13 @@ class f {
263
313
  }
264
314
  // preload iframe (optional optimization)
265
315
  static async preload(t) {
266
- const o = (t?.toLowerCase()?.includes("develop") || t?.toLowerCase()?.includes("staging") ? "develop" : "production") === "develop" ? m : p, n = document.createElement("link");
316
+ const o = (t?.toLowerCase()?.includes("develop") || t?.toLowerCase()?.includes("staging") ? "develop" : "production") === "develop" ? h : p, n = document.createElement("link");
267
317
  n.rel = "preconnect", n.href = new URL(o).origin, document.head.appendChild(n);
268
318
  const i = document.createElement("link");
269
319
  i.rel = "dns-prefetch", i.href = new URL(o).origin, document.head.appendChild(i);
270
320
  }
271
321
  }
272
- const E = {
322
+ const R = {
273
323
  preload: f.preload
274
324
  }, P = "https://cdn-sdk-dev.paynext.com/index.cdn.js", I = "https://cdn-sdk.paynext.com/index.cdn.js";
275
325
  let d = null, c = null;
@@ -301,12 +351,12 @@ async function g(a) {
301
351
  }, document.head.appendChild(n);
302
352
  }), d;
303
353
  }
304
- class v {
354
+ class E {
305
355
  constructor() {
306
- s(this, "cdnInstance", null);
307
- s(this, "iframeInstance", null);
308
- s(this, "initPromise", null);
309
- s(this, "mode", "direct");
356
+ r(this, "cdnInstance", null);
357
+ r(this, "iframeInstance", null);
358
+ r(this, "initPromise", null);
359
+ r(this, "mode", "direct");
310
360
  }
311
361
  // initialize
312
362
  async initialize(t) {
@@ -348,14 +398,14 @@ class v {
348
398
  e?.iframeMode ? await f.preload(t) : await g(t);
349
399
  }
350
400
  }
351
- const T = {
352
- preload: v.preload
401
+ const D = {
402
+ preload: E.preload
353
403
  };
354
- var N = /* @__PURE__ */ ((a) => (a.PAYPAL = "PAYPAL", a.APPLE_PAY = "APPLEPAY", a.GOOGLE_PAY = "GPAY", a.CARD = "CARD", a.VENMO = "VENMO", a))(N || {});
404
+ var v = /* @__PURE__ */ ((a) => (a.PAYPAL = "PAYPAL", a.APPLE_PAY = "APPLEPAY", a.GOOGLE_PAY = "GPAY", a.CARD = "CARD", a.VENMO = "VENMO", a))(v || {});
355
405
  export {
356
- v as PayNextCheckout,
406
+ E as PayNextCheckout,
357
407
  f as PayNextCheckoutIframe,
358
- T as PayNextSDK,
359
- E as PayNextSDKIframe,
360
- N as PaymentMethod
408
+ D as PayNextSDK,
409
+ R as PayNextSDKIframe,
410
+ v as PaymentMethod
361
411
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(i,s){typeof exports=="object"&&typeof module<"u"?s(exports):typeof define=="function"&&define.amd?define(["exports"],s):(i=typeof globalThis<"u"?globalThis:i||self,s(i["PayNext SDK"]={}))})(this,(function(i){"use strict";var E=Object.defineProperty;var T=(i,s,c)=>s in i?E(i,s,{enumerable:!0,configurable:!0,writable:!0,value:c}):i[s]=c;var d=(i,s,c)=>T(i,typeof s!="symbol"?s+"":s,c);const s="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(r){return`<!DOCTYPE html>
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
- const { PayNextCheckout } = await import('${r}')
43
- checkout = new PayNextCheckout()
44
- await checkout.mount('checkout-container', {
45
- ...config,
46
- onCheckoutLoaded: (result) => { sendToParent(MessageType.LOADED, result, data.requestId); notifyResize() },
47
- onCheckoutAttempt: (result) => sendToParent(MessageType.ATTEMPT, result),
48
- onCheckoutComplete: (result) => sendToParent(MessageType.COMPLETE, result),
49
- onCheckoutFail: (error) => sendToParent(MessageType.FAIL, error)
50
- })
51
- sendToParent(MessageType.READY, { success: true }, data.requestId)
52
- setupResizeObserver()
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) { await checkout.unmount(); checkout = null }
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
- sendToParent(MessageType.ERROR, { message: error.message, stack: error.stack }, data.requestId)
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) sendToParent(MessageType.RESIZE, { height: entry.contentRect.height })
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() { sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight }) }
113
+ function notifyResize() {
114
+ sendToParent(MessageType.RESIZE, { height: document.body.scrollHeight })
115
+ }
78
116
 
79
117
  window.addEventListener('load', () => {
80
- setTimeout(() => window.parent.postMessage({ type: MessageType.READY, payload: { success: true } }, '*'), 100)
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
- sendToParent(MessageType.ERROR, { message: event.error?.message || 'Unknown error', stack: event.error?.stack })
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 m{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"?s: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"?s: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"?s: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:m.preload},N="https://cdn-sdk-dev.paynext.com/index.cdn.js",v="https://cdn-sdk.paynext.com/index.cdn.js";let u=null,h=null;async function g(r){if(h)return h;if(u)return u;const t=r?.toLowerCase()?.includes("develop")||r?.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"),h=window.PayNextSDK,e(h);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}h=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 m),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 m.preload(t):await g(t)}}const R={preload:w.preload};var x=(r=>(r.PAYPAL="PAYPAL",r.APPLE_PAY="APPLEPAY",r.GOOGLE_PAY="GPAY",r.CARD="CARD",r.VENMO="VENMO",r))(x||{});i.PayNextCheckout=w,i.PayNextCheckoutIframe=m,i.PayNextSDK=R,i.PayNextSDKIframe=I,i.PaymentMethod=x,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
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"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@paynext/sdk",
3
- "version": "0.0.170",
3
+ "version": "0.0.171",
4
4
  "description": "PayNext SDK - Payment processing with automatic CDN loading",
5
5
  "type": "module",
6
6
  "main": "dist/index.es.js",