@moonpay/platform-sdk-web 1.6.0 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -48,8 +48,9 @@ await client.connect({ container, theme: { appearance: 'light' } });
48
48
 
49
49
  - **Connection** — `getConnection()`, `connect()`, `setupAuth()`, `resetConnection()`
50
50
  - **Payments data** — `getQuote()`, `getPaymentMethods()`, `deletePaymentMethod()`, `listTransactions()`, `getTransaction()`
51
- - **Frames** — `setupWidget()`, `setupApplePay()`, `setupGooglePay()`, `setupBuy()`, `setupBuyButton()`, `setupAddCard()`, `setupChallenge()`
52
- - **Identity** — `createIdentity()`, `getIdentity()`, `updateIdentity()`, `verifyIdentity()` and file upload helpers for headless integrations
51
+ - **Frames** — `setupWidget()`, `setupApplePay()`, `setupGooglePay()`, `setupBuy()`, `setupBuyButton()`, `setupAddCard()`, `setupChallenge()`, `setupCustomerExport()`
52
+ - **Customer** — `getCustomer()`, `submitCustomerKyc()`, `getCustomerUploadUrl()`, `submitCustomerFiles()` for headless integrations
53
+ - **Identity** *(deprecated, use Customer above)* — `createIdentity()`, `getIdentity()`, `updateIdentity()`, `verifyIdentity()` and file upload helpers
53
54
 
54
55
  ## Related packages
55
56
 
package/dist/index.cjs CHANGED
@@ -18,30 +18,22 @@ var __copyProps = (to, from, except, desc) => {
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
19
 
20
20
  // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
23
  ConnectionStatus: () => import_connection2.ConnectionStatus,
24
24
  IframeTransport: () => IframeTransport,
25
25
  createClient: () => createClient
26
26
  });
27
- module.exports = __toCommonJS(src_exports);
27
+ module.exports = __toCommonJS(index_exports);
28
28
  var import_connection2 = require("@moonpay/platform-protocol/models/connection");
29
29
 
30
30
  // src/client.ts
31
31
  var import_connection = require("@moonpay/platform-protocol/models/connection");
32
32
  var import_result = require("@moonpay/platform-protocol/result");
33
- var import_platform_sdk_core = require("@moonpay/platform-sdk-core");
33
+ var import_platform_sdk_core2 = require("@moonpay/platform-sdk-core");
34
34
 
35
35
  // src/iframe-transport.ts
36
- var ALLOWED_ORIGIN_PATTERNS = [
37
- /^https:\/\/([a-zA-Z0-9-]+\.)*moonpay\.com$/,
38
- /^https:\/\/([a-zA-Z0-9-]+\.)*moonpaycloud\.com$/,
39
- /^https:\/\/([a-zA-Z0-9-]+\.)*preview\.moonpay-dev\.com$/,
40
- /^http:\/\/localhost(:\d+)?$/
41
- ];
42
- function isAllowedOrigin(origin) {
43
- return ALLOWED_ORIGIN_PATTERNS.some((pattern) => pattern.test(origin));
44
- }
36
+ var import_platform_sdk_core = require("@moonpay/platform-sdk-core");
45
37
  var IframeTransport = class {
46
38
  iframe = null;
47
39
  container = null;
@@ -49,6 +41,9 @@ var IframeTransport = class {
49
41
  handlers = [];
50
42
  boundListener = null;
51
43
  create(url, options) {
44
+ if (!(0, import_platform_sdk_core.isTrustedFrameUrl)(url)) {
45
+ throw new Error("Refusing to load frame: URL is not a trusted MoonPay origin");
46
+ }
52
47
  const container = options.container;
53
48
  this.container = container;
54
49
  const iframe = document.createElement("iframe");
@@ -82,8 +77,7 @@ var IframeTransport = class {
82
77
  this.handlers.push(handler);
83
78
  return () => {
84
79
  const idx = this.handlers.indexOf(handler);
85
- if (idx >= 0)
86
- this.handlers.splice(idx, 1);
80
+ if (idx >= 0) this.handlers.splice(idx, 1);
87
81
  };
88
82
  }
89
83
  dispose() {
@@ -103,13 +97,11 @@ var IframeTransport = class {
103
97
  // Private
104
98
  // ---------------------------------------------------------------------------
105
99
  handleMessage(event) {
106
- if (!isAllowedOrigin(event.origin))
107
- return;
100
+ if (!(0, import_platform_sdk_core.isTrustedFrameOrigin)(event.origin)) return;
108
101
  let msg;
109
102
  try {
110
103
  msg = typeof event.data === "string" ? JSON.parse(event.data) : event.data;
111
- if (!msg.kind)
112
- return;
104
+ if (!msg.kind) return;
113
105
  } catch {
114
106
  return;
115
107
  }
@@ -127,16 +119,16 @@ var IframeTransport = class {
127
119
  // src/client.ts
128
120
  function createClient(options) {
129
121
  const { sessionToken, apiBaseUrl, frameBaseUrl, theme } = options;
130
- const core = (0, import_platform_sdk_core.createClientCore)({
122
+ const core = (0, import_platform_sdk_core2.createClientCore)({
131
123
  apiBaseUrl,
132
124
  frameBaseUrl,
133
125
  createTransport: () => new IframeTransport()
134
126
  });
135
127
  async function getConnection(options2 = {}) {
136
- const { privateKey, publicKey } = (0, import_platform_sdk_core.generateKeyPair)();
128
+ const { privateKey, publicKey } = (0, import_platform_sdk_core2.generateKeyPair)();
137
129
  const channelId = `check-${crypto.randomUUID()}`;
138
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
139
- import_platform_sdk_core.FRAME_PATHS.checkConnection,
130
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
131
+ import_platform_sdk_core2.FRAME_PATHS.checkConnection,
140
132
  {
141
133
  sessionToken,
142
134
  channelId,
@@ -147,7 +139,7 @@ function createClient(options) {
147
139
  );
148
140
  const transport = core.createTransport();
149
141
  try {
150
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
142
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
151
143
  transport,
152
144
  channelId,
153
145
  url,
@@ -173,11 +165,11 @@ function createClient(options) {
173
165
  handle.dispose();
174
166
  const connection = msg.payload;
175
167
  if (connection.status === import_connection.ConnectionStatus.active) {
176
- const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
168
+ const creds = (0, import_platform_sdk_core2.decryptCredentials)(connection.credentials, privateKey);
177
169
  core.context.setAccessToken(creds.accessToken);
178
170
  core.context.setClientToken(creds.clientToken);
179
171
  } else if (connection.status === import_connection.ConnectionStatus.connectionRequired) {
180
- const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
172
+ const creds = (0, import_platform_sdk_core2.decryptCredentials)(connection.credentials, privateKey);
181
173
  core.context.setAccessToken(creds.accessToken);
182
174
  core.context.setClientToken(creds.clientToken);
183
175
  }
@@ -192,22 +184,22 @@ function createClient(options) {
192
184
  }
193
185
  }
194
186
  async function connect(opts) {
195
- const { privateKey, publicKey } = (0, import_platform_sdk_core.generateKeyPair)();
187
+ const { privateKey, publicKey } = (0, import_platform_sdk_core2.generateKeyPair)();
196
188
  const channelId = `connect-${crypto.randomUUID()}`;
197
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
198
- import_platform_sdk_core.FRAME_PATHS.connect,
189
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
190
+ import_platform_sdk_core2.FRAME_PATHS.connect,
199
191
  {
200
192
  sessionToken,
201
193
  channelId,
202
194
  publicKey,
203
195
  // Per-call theme wins; otherwise fall back to the client-level theme.
204
- ...(0, import_platform_sdk_core.themeParams)(opts.theme ?? theme)
196
+ ...(0, import_platform_sdk_core2.themeParams)(opts.theme ?? theme)
205
197
  },
206
198
  { frameBaseUrl }
207
199
  );
208
200
  const transport = core.createTransport();
209
201
  try {
210
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
202
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
211
203
  transport,
212
204
  channelId,
213
205
  url,
@@ -229,7 +221,7 @@ function createClient(options) {
229
221
  if (msg.kind === "complete") {
230
222
  const connection = msg.payload;
231
223
  if (connection.status === import_connection.ConnectionStatus.active) {
232
- const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
224
+ const creds = (0, import_platform_sdk_core2.decryptCredentials)(connection.credentials, privateKey);
233
225
  core.context.setAccessToken(creds.accessToken);
234
226
  core.context.setClientToken(creds.clientToken);
235
227
  }
@@ -252,16 +244,16 @@ function createClient(options) {
252
244
  message: 'No clientToken in context \u2014 call getConnection() first and ensure it resolved with status "connectionRequired".'
253
245
  });
254
246
  }
255
- const { privateKey, publicKey } = (0, import_platform_sdk_core.generateKeyPair)();
247
+ const { privateKey, publicKey } = (0, import_platform_sdk_core2.generateKeyPair)();
256
248
  const channelId = `auth-${crypto.randomUUID()}`;
257
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
258
- import_platform_sdk_core.FRAME_PATHS.auth,
259
- { clientToken, channelId, publicKey, ...(0, import_platform_sdk_core.themeParams)(theme) },
249
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
250
+ import_platform_sdk_core2.FRAME_PATHS.auth,
251
+ { clientToken, channelId, publicKey, ...(0, import_platform_sdk_core2.themeParams)(theme) },
260
252
  { frameBaseUrl }
261
253
  );
262
254
  const transport = core.createTransport();
263
255
  try {
264
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
256
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
265
257
  transport,
266
258
  channelId,
267
259
  url,
@@ -283,7 +275,7 @@ function createClient(options) {
283
275
  if (msg.kind === "complete") {
284
276
  const connection = msg.payload;
285
277
  if (connection.status === import_connection.ConnectionStatus.active) {
286
- const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
278
+ const creds = (0, import_platform_sdk_core2.decryptCredentials)(connection.credentials, privateKey);
287
279
  core.context.setAccessToken(creds.accessToken);
288
280
  core.context.setClientToken(creds.clientToken);
289
281
  }
@@ -303,20 +295,20 @@ function createClient(options) {
303
295
  }
304
296
  async function setupWidget(opts) {
305
297
  const channelId = `widget-${crypto.randomUUID()}`;
306
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
307
- import_platform_sdk_core.FRAME_PATHS.widget,
298
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
299
+ import_platform_sdk_core2.FRAME_PATHS.widget,
308
300
  {
309
301
  flow: "buy",
310
302
  clientToken: core.context.clientToken,
311
303
  quoteSignature: opts.quote,
312
304
  channelId,
313
- ...(0, import_platform_sdk_core.themeParams)(theme)
305
+ ...(0, import_platform_sdk_core2.themeParams)(theme)
314
306
  },
315
307
  { frameBaseUrl }
316
308
  );
317
309
  const transport = core.createTransport();
318
310
  try {
319
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
311
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
320
312
  transport,
321
313
  channelId,
322
314
  url,
@@ -365,19 +357,19 @@ function createClient(options) {
365
357
  }
366
358
  async function setupApplePay(opts) {
367
359
  const channelId = `applepay-${crypto.randomUUID()}`;
368
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
369
- import_platform_sdk_core.FRAME_PATHS.applePay,
360
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
361
+ import_platform_sdk_core2.FRAME_PATHS.applePay,
370
362
  {
371
363
  clientToken: core.context.clientToken,
372
364
  signature: opts.quote,
373
365
  channelId,
374
- ...(0, import_platform_sdk_core.themeParams)(theme)
366
+ ...(0, import_platform_sdk_core2.themeParams)(theme)
375
367
  },
376
368
  { frameBaseUrl }
377
369
  );
378
370
  const transport = core.createTransport();
379
371
  try {
380
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
372
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
381
373
  transport,
382
374
  channelId,
383
375
  url,
@@ -438,20 +430,20 @@ function createClient(options) {
438
430
  }
439
431
  async function setupGooglePay(opts) {
440
432
  const channelId = `googlepay-${crypto.randomUUID()}`;
441
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
442
- import_platform_sdk_core.FRAME_PATHS.googlePay,
433
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
434
+ import_platform_sdk_core2.FRAME_PATHS.googlePay,
443
435
  {
444
436
  clientToken: core.context.clientToken,
445
437
  signature: opts.quote,
446
438
  channelId,
447
439
  ...opts.externalTransactionId && { externalTransactionId: opts.externalTransactionId },
448
- ...(0, import_platform_sdk_core.themeParams)(theme)
440
+ ...(0, import_platform_sdk_core2.themeParams)(theme)
449
441
  },
450
442
  { frameBaseUrl }
451
443
  );
452
444
  const transport = core.createTransport();
453
445
  try {
454
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
446
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
455
447
  transport,
456
448
  channelId,
457
449
  url,
@@ -512,10 +504,10 @@ function createClient(options) {
512
504
  }
513
505
  async function resetConnection() {
514
506
  const channelId = `reset-${crypto.randomUUID()}`;
515
- const url = (0, import_platform_sdk_core.buildFrameUrl)(import_platform_sdk_core.FRAME_PATHS.reset, { sessionToken, channelId }, { frameBaseUrl });
507
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(import_platform_sdk_core2.FRAME_PATHS.reset, { sessionToken, channelId }, { frameBaseUrl });
516
508
  const transport = core.createTransport();
517
509
  try {
518
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
510
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
519
511
  transport,
520
512
  channelId,
521
513
  url,
@@ -542,8 +534,8 @@ function createClient(options) {
542
534
  }
543
535
  async function setupBuy(opts) {
544
536
  const channelId = `buy-${crypto.randomUUID()}`;
545
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
546
- import_platform_sdk_core.FRAME_PATHS.buy,
537
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
538
+ import_platform_sdk_core2.FRAME_PATHS.buy,
547
539
  {
548
540
  channelId,
549
541
  clientToken: core.context.clientToken,
@@ -554,7 +546,7 @@ function createClient(options) {
554
546
  );
555
547
  const transport = core.createTransport();
556
548
  try {
557
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
549
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
558
550
  transport,
559
551
  channelId,
560
552
  url,
@@ -604,19 +596,19 @@ function createClient(options) {
604
596
  }
605
597
  async function setupBuyButton(opts) {
606
598
  const channelId = `buy-button-${crypto.randomUUID()}`;
607
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
608
- import_platform_sdk_core.FRAME_PATHS.buyButton,
599
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
600
+ import_platform_sdk_core2.FRAME_PATHS.buyButton,
609
601
  {
610
602
  channelId,
611
603
  clientToken: core.context.clientToken,
612
604
  signature: opts.quote,
613
- ...(0, import_platform_sdk_core.themeParams)(theme)
605
+ ...(0, import_platform_sdk_core2.themeParams)(theme)
614
606
  },
615
607
  { frameBaseUrl }
616
608
  );
617
609
  const transport = core.createTransport();
618
610
  try {
619
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
611
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
620
612
  transport,
621
613
  channelId,
622
614
  url,
@@ -670,6 +662,12 @@ function createClient(options) {
670
662
  } catch {
671
663
  return (0, import_result.err)({ kind: "configurationError", message: "Invalid challenge URL" });
672
664
  }
665
+ if (!(0, import_platform_sdk_core2.isTrustedFrameOrigin)(url.origin)) {
666
+ return (0, import_result.err)({
667
+ kind: "configurationError",
668
+ message: "Challenge URL must be served from a trusted MoonPay origin"
669
+ });
670
+ }
673
671
  let channelId = url.searchParams.get("channelId") ?? "";
674
672
  if (!channelId) {
675
673
  channelId = `challenge-${crypto.randomUUID()}`;
@@ -677,10 +675,10 @@ function createClient(options) {
677
675
  }
678
676
  const transport = core.createTransport();
679
677
  try {
680
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
678
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
681
679
  transport,
682
680
  channelId,
683
- url: (0, import_platform_sdk_core.applyThemeParam)(url.toString(), theme),
681
+ url: (0, import_platform_sdk_core2.applyThemeParam)(url.toString(), theme),
684
682
  container: opts.container
685
683
  });
686
684
  handle.onMessage((msg) => {
@@ -718,14 +716,14 @@ function createClient(options) {
718
716
  }
719
717
  async function setupAddCard(opts) {
720
718
  const channelId = `add-card-${crypto.randomUUID()}`;
721
- const url = (0, import_platform_sdk_core.buildFrameUrl)(
722
- import_platform_sdk_core.FRAME_PATHS.addCard,
723
- { channelId, clientToken: core.context.clientToken, ...(0, import_platform_sdk_core.themeParams)(theme) },
719
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
720
+ import_platform_sdk_core2.FRAME_PATHS.addCard,
721
+ { channelId, clientToken: core.context.clientToken, ...(0, import_platform_sdk_core2.themeParams)(theme) },
724
722
  { frameBaseUrl }
725
723
  );
726
724
  const transport = core.createTransport();
727
725
  try {
728
- const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
726
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
729
727
  transport,
730
728
  channelId,
731
729
  url,
@@ -758,6 +756,51 @@ function createClient(options) {
758
756
  });
759
757
  }
760
758
  }
759
+ async function setupCustomerExport(opts) {
760
+ const channelId = `customer-export-${crypto.randomUUID()}`;
761
+ const url = (0, import_platform_sdk_core2.buildFrameUrl)(
762
+ import_platform_sdk_core2.FRAME_PATHS.customerExport,
763
+ { channelId, clientToken: core.context.clientToken, ...(0, import_platform_sdk_core2.themeParams)(theme) },
764
+ { frameBaseUrl }
765
+ );
766
+ const transport = core.createTransport();
767
+ try {
768
+ const handle = await (0, import_platform_sdk_core2.createFrameOrchestrator)({
769
+ transport,
770
+ channelId,
771
+ url,
772
+ container: opts.container
773
+ });
774
+ handle.onMessage((msg) => {
775
+ switch (msg.kind) {
776
+ case "ready":
777
+ opts.onEvent?.({ kind: "ready" });
778
+ break;
779
+ case "complete":
780
+ opts.onEvent?.({
781
+ kind: "complete",
782
+ payload: msg.payload
783
+ });
784
+ break;
785
+ case "error":
786
+ opts.onEvent?.({
787
+ kind: "error",
788
+ payload: msg.payload
789
+ });
790
+ break;
791
+ case "close":
792
+ opts.onEvent?.({ kind: "close" });
793
+ break;
794
+ }
795
+ });
796
+ return (0, import_result.ok)({ dispose: () => handle.dispose() });
797
+ } catch (e) {
798
+ return (0, import_result.err)({
799
+ kind: "genericError",
800
+ message: e instanceof Error ? e.message : "Customer export setup failed"
801
+ });
802
+ }
803
+ }
761
804
  return {
762
805
  getConnection,
763
806
  connect,
@@ -776,6 +819,11 @@ function createClient(options) {
776
819
  setupBuyButton,
777
820
  setupChallenge,
778
821
  setupAddCard,
822
+ setupCustomerExport,
823
+ getCustomer: (id) => core.getCustomer(id),
824
+ submitCustomerKyc: (id, body) => core.submitCustomerKyc(id, body),
825
+ getCustomerUploadUrl: (id, body) => core.getCustomerUploadUrl(id, body),
826
+ submitCustomerFiles: (id, body) => core.submitCustomerFiles(id, body),
779
827
  createIdentity: (body) => core.createIdentity(body),
780
828
  getIdentity: (id) => core.getIdentity(id),
781
829
  updateIdentity: (id, body) => core.updateIdentity(id, body),