@moonpay/platform-sdk-web 0.0.0 → 0.2.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/dist/index.cjs +690 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +163 -0
- package/dist/index.d.ts +163 -0
- package/dist/index.js +673 -0
- package/dist/index.js.map +1 -0
- package/package.json +37 -5
- package/README.md +0 -3
- package/index.js +0 -1
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,690 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
IframeTransport: () => IframeTransport,
|
|
24
|
+
createClient: () => createClient
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(src_exports);
|
|
27
|
+
|
|
28
|
+
// src/client.ts
|
|
29
|
+
var import_platform_protocol = require("@moonpay/platform-protocol");
|
|
30
|
+
var import_platform_sdk_core = require("@moonpay/platform-sdk-core");
|
|
31
|
+
|
|
32
|
+
// src/iframe-transport.ts
|
|
33
|
+
var ALLOWED_ORIGIN_PATTERNS = [
|
|
34
|
+
/^https:\/\/([a-zA-Z0-9-]+\.)*moonpay\.com$/,
|
|
35
|
+
/^https:\/\/([a-zA-Z0-9-]+\.)*moonpaycloud\.com$/,
|
|
36
|
+
/^https:\/\/([a-zA-Z0-9-]+\.)*preview\.moonpay-dev\.com$/,
|
|
37
|
+
/^http:\/\/localhost(:\d+)?$/
|
|
38
|
+
];
|
|
39
|
+
function isAllowedOrigin(origin) {
|
|
40
|
+
return ALLOWED_ORIGIN_PATTERNS.some((pattern) => pattern.test(origin));
|
|
41
|
+
}
|
|
42
|
+
var IframeTransport = class {
|
|
43
|
+
iframe = null;
|
|
44
|
+
container = null;
|
|
45
|
+
trustedOrigin = null;
|
|
46
|
+
handlers = [];
|
|
47
|
+
boundListener = null;
|
|
48
|
+
create(url, options) {
|
|
49
|
+
const container = options.container;
|
|
50
|
+
this.container = container;
|
|
51
|
+
const iframe = document.createElement("iframe");
|
|
52
|
+
iframe.src = url;
|
|
53
|
+
iframe.style.border = "none";
|
|
54
|
+
if (options.hidden) {
|
|
55
|
+
iframe.style.width = "0";
|
|
56
|
+
iframe.style.height = "0";
|
|
57
|
+
iframe.style.position = "absolute";
|
|
58
|
+
iframe.style.overflow = "hidden";
|
|
59
|
+
} else {
|
|
60
|
+
iframe.style.width = "100%";
|
|
61
|
+
iframe.style.height = "100%";
|
|
62
|
+
}
|
|
63
|
+
iframe.allow = "accelerometer; autoplay; camera; encrypted-media; gyroscope; payment";
|
|
64
|
+
this.iframe = iframe;
|
|
65
|
+
this.boundListener = this.handleMessage.bind(this);
|
|
66
|
+
window.addEventListener("message", this.boundListener);
|
|
67
|
+
container.appendChild(iframe);
|
|
68
|
+
}
|
|
69
|
+
sendMessage(message) {
|
|
70
|
+
if (!this.iframe?.contentWindow) {
|
|
71
|
+
throw new Error("Cannot send message: iframe not initialized");
|
|
72
|
+
}
|
|
73
|
+
if (!this.trustedOrigin) {
|
|
74
|
+
throw new Error("Cannot send message: no trusted origin established");
|
|
75
|
+
}
|
|
76
|
+
this.iframe.contentWindow.postMessage(JSON.stringify(message), this.trustedOrigin);
|
|
77
|
+
}
|
|
78
|
+
onMessage(handler) {
|
|
79
|
+
this.handlers.push(handler);
|
|
80
|
+
return () => {
|
|
81
|
+
const idx = this.handlers.indexOf(handler);
|
|
82
|
+
if (idx >= 0)
|
|
83
|
+
this.handlers.splice(idx, 1);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
dispose() {
|
|
87
|
+
if (this.boundListener) {
|
|
88
|
+
window.removeEventListener("message", this.boundListener);
|
|
89
|
+
this.boundListener = null;
|
|
90
|
+
}
|
|
91
|
+
this.handlers.length = 0;
|
|
92
|
+
if (this.iframe && this.container?.contains(this.iframe)) {
|
|
93
|
+
this.iframe.remove();
|
|
94
|
+
}
|
|
95
|
+
this.iframe = null;
|
|
96
|
+
this.container = null;
|
|
97
|
+
this.trustedOrigin = null;
|
|
98
|
+
}
|
|
99
|
+
// ---------------------------------------------------------------------------
|
|
100
|
+
// Private
|
|
101
|
+
// ---------------------------------------------------------------------------
|
|
102
|
+
handleMessage(event) {
|
|
103
|
+
if (!isAllowedOrigin(event.origin))
|
|
104
|
+
return;
|
|
105
|
+
let msg;
|
|
106
|
+
try {
|
|
107
|
+
msg = typeof event.data === "string" ? JSON.parse(event.data) : event.data;
|
|
108
|
+
if (!msg.kind)
|
|
109
|
+
return;
|
|
110
|
+
} catch {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
if (!this.trustedOrigin) {
|
|
114
|
+
this.trustedOrigin = event.origin;
|
|
115
|
+
} else if (event.origin !== this.trustedOrigin) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
for (const handler of this.handlers) {
|
|
119
|
+
handler(msg);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// src/client.ts
|
|
125
|
+
function createClient(options) {
|
|
126
|
+
const { sessionToken, apiBaseUrl, frameBaseUrl } = options;
|
|
127
|
+
const core = (0, import_platform_sdk_core.createClientCore)({
|
|
128
|
+
apiBaseUrl,
|
|
129
|
+
frameBaseUrl,
|
|
130
|
+
createTransport: () => new IframeTransport()
|
|
131
|
+
});
|
|
132
|
+
async function getConnection() {
|
|
133
|
+
const { privateKey, publicKey } = (0, import_platform_sdk_core.generateKeyPair)();
|
|
134
|
+
const channelId = `check-${crypto.randomUUID()}`;
|
|
135
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
136
|
+
import_platform_sdk_core.FRAME_PATHS.checkConnection,
|
|
137
|
+
{ sessionToken, channelId, publicKey },
|
|
138
|
+
{ frameBaseUrl }
|
|
139
|
+
);
|
|
140
|
+
const transport = core.createTransport();
|
|
141
|
+
try {
|
|
142
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
143
|
+
transport,
|
|
144
|
+
channelId,
|
|
145
|
+
url,
|
|
146
|
+
container: document.body,
|
|
147
|
+
hidden: true,
|
|
148
|
+
handshakeTimeout: 1e4
|
|
149
|
+
});
|
|
150
|
+
return new Promise((resolve) => {
|
|
151
|
+
const timeout = setTimeout(() => {
|
|
152
|
+
handle.dispose();
|
|
153
|
+
resolve((0, import_platform_protocol.err)({ message: "Get connection timed out" }));
|
|
154
|
+
}, 1e4);
|
|
155
|
+
handle.onMessage((msg) => {
|
|
156
|
+
if (msg.kind === "error") {
|
|
157
|
+
clearTimeout(timeout);
|
|
158
|
+
handle.dispose();
|
|
159
|
+
const payload = msg.payload;
|
|
160
|
+
resolve((0, import_platform_protocol.err)({ message: payload.message }));
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
if (msg.kind === "complete") {
|
|
164
|
+
clearTimeout(timeout);
|
|
165
|
+
handle.dispose();
|
|
166
|
+
const connection = msg.payload;
|
|
167
|
+
if (connection.status === import_platform_protocol.ConnectionStatus.active) {
|
|
168
|
+
const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
|
|
169
|
+
core.context.setAccessToken(creds.accessToken);
|
|
170
|
+
core.context.setClientToken(creds.clientToken);
|
|
171
|
+
} else if (connection.status === import_platform_protocol.ConnectionStatus.connectionRequired) {
|
|
172
|
+
const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
|
|
173
|
+
core.context.setAccessToken(creds.accessToken);
|
|
174
|
+
core.context.setClientToken(creds.clientToken);
|
|
175
|
+
}
|
|
176
|
+
resolve((0, import_platform_protocol.ok)(connection));
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
} catch (e) {
|
|
181
|
+
return (0, import_platform_protocol.err)({
|
|
182
|
+
message: e instanceof Error ? e.message : "Failed to connect"
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function connect(opts) {
|
|
187
|
+
const { privateKey, publicKey } = (0, import_platform_sdk_core.generateKeyPair)();
|
|
188
|
+
const channelId = `connect-${crypto.randomUUID()}`;
|
|
189
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
190
|
+
import_platform_sdk_core.FRAME_PATHS.connect,
|
|
191
|
+
{
|
|
192
|
+
sessionToken,
|
|
193
|
+
channelId,
|
|
194
|
+
publicKey,
|
|
195
|
+
...opts.theme?.appearance && { appearance: opts.theme.appearance }
|
|
196
|
+
},
|
|
197
|
+
{ frameBaseUrl }
|
|
198
|
+
);
|
|
199
|
+
const transport = core.createTransport();
|
|
200
|
+
try {
|
|
201
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
202
|
+
transport,
|
|
203
|
+
channelId,
|
|
204
|
+
url,
|
|
205
|
+
container: opts.container
|
|
206
|
+
});
|
|
207
|
+
return new Promise((resolve) => {
|
|
208
|
+
handle.onMessage((msg) => {
|
|
209
|
+
if (msg.kind === "ready") {
|
|
210
|
+
opts.onEvent?.({ kind: "ready" });
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
if (msg.kind === "error") {
|
|
214
|
+
const payload = msg.payload;
|
|
215
|
+
opts.onEvent?.({ kind: "error", payload });
|
|
216
|
+
handle.dispose();
|
|
217
|
+
resolve((0, import_platform_protocol.err)({ message: "Connection error" }));
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if (msg.kind === "complete") {
|
|
221
|
+
const connection = msg.payload;
|
|
222
|
+
if (connection.status === import_platform_protocol.ConnectionStatus.active) {
|
|
223
|
+
const creds = (0, import_platform_sdk_core.decryptCredentials)(connection.credentials, privateKey);
|
|
224
|
+
core.context.setAccessToken(creds.accessToken);
|
|
225
|
+
core.context.setClientToken(creds.clientToken);
|
|
226
|
+
}
|
|
227
|
+
opts.onEvent?.({ kind: "complete", payload: connection });
|
|
228
|
+
resolve((0, import_platform_protocol.ok)({ dispose: () => handle.dispose() }));
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
} catch (e) {
|
|
233
|
+
return (0, import_platform_protocol.err)({
|
|
234
|
+
message: e instanceof Error ? e.message : "Connect failed"
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
async function setupWidget(opts) {
|
|
239
|
+
const channelId = `widget-${crypto.randomUUID()}`;
|
|
240
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
241
|
+
import_platform_sdk_core.FRAME_PATHS.widget,
|
|
242
|
+
{
|
|
243
|
+
flow: "buy",
|
|
244
|
+
clientToken: core.context.clientToken,
|
|
245
|
+
quoteSignature: opts.quote,
|
|
246
|
+
channelId
|
|
247
|
+
},
|
|
248
|
+
{ frameBaseUrl }
|
|
249
|
+
);
|
|
250
|
+
const transport = core.createTransport();
|
|
251
|
+
try {
|
|
252
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
253
|
+
transport,
|
|
254
|
+
channelId,
|
|
255
|
+
url,
|
|
256
|
+
container: opts.container
|
|
257
|
+
});
|
|
258
|
+
handle.onMessage((msg) => {
|
|
259
|
+
switch (msg.kind) {
|
|
260
|
+
case "ready":
|
|
261
|
+
opts.onEvent?.({ kind: "ready" });
|
|
262
|
+
break;
|
|
263
|
+
case "transactionCreated":
|
|
264
|
+
opts.onEvent?.({
|
|
265
|
+
kind: "transactionCreated",
|
|
266
|
+
payload: msg.payload
|
|
267
|
+
});
|
|
268
|
+
break;
|
|
269
|
+
case "complete":
|
|
270
|
+
opts.onEvent?.({
|
|
271
|
+
kind: "complete",
|
|
272
|
+
payload: msg.payload
|
|
273
|
+
});
|
|
274
|
+
break;
|
|
275
|
+
case "error": {
|
|
276
|
+
const payload = msg.payload;
|
|
277
|
+
opts.onEvent?.({
|
|
278
|
+
kind: "error",
|
|
279
|
+
payload: {
|
|
280
|
+
code: payload.code,
|
|
281
|
+
message: payload.message
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case "close":
|
|
287
|
+
opts.onEvent?.({ kind: "close" });
|
|
288
|
+
break;
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
return (0, import_platform_protocol.ok)({ dispose: () => handle.dispose() });
|
|
292
|
+
} catch (e) {
|
|
293
|
+
return (0, import_platform_protocol.err)({
|
|
294
|
+
kind: "genericError",
|
|
295
|
+
message: e instanceof Error ? e.message : "Widget setup failed"
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async function setupApplePay(opts) {
|
|
300
|
+
const channelId = `applepay-${crypto.randomUUID()}`;
|
|
301
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
302
|
+
import_platform_sdk_core.FRAME_PATHS.applePay,
|
|
303
|
+
{
|
|
304
|
+
clientToken: core.context.clientToken,
|
|
305
|
+
signature: opts.quote,
|
|
306
|
+
channelId
|
|
307
|
+
},
|
|
308
|
+
{ frameBaseUrl }
|
|
309
|
+
);
|
|
310
|
+
const transport = core.createTransport();
|
|
311
|
+
try {
|
|
312
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
313
|
+
transport,
|
|
314
|
+
channelId,
|
|
315
|
+
url,
|
|
316
|
+
container: opts.container
|
|
317
|
+
});
|
|
318
|
+
const setQuote = (signature) => {
|
|
319
|
+
handle.sendMessage({
|
|
320
|
+
version: 2,
|
|
321
|
+
meta: { channelId },
|
|
322
|
+
kind: "setQuote",
|
|
323
|
+
payload: { quote: { signature } }
|
|
324
|
+
});
|
|
325
|
+
};
|
|
326
|
+
handle.onMessage((msg) => {
|
|
327
|
+
switch (msg.kind) {
|
|
328
|
+
case "ready":
|
|
329
|
+
opts.onEvent?.({ kind: "ready" });
|
|
330
|
+
break;
|
|
331
|
+
case "complete":
|
|
332
|
+
opts.onEvent?.({
|
|
333
|
+
kind: "complete",
|
|
334
|
+
payload: msg.payload
|
|
335
|
+
});
|
|
336
|
+
break;
|
|
337
|
+
case "error": {
|
|
338
|
+
const payload = msg.payload;
|
|
339
|
+
if (payload.code === "quoteExpired") {
|
|
340
|
+
opts.onEvent?.({
|
|
341
|
+
kind: "quoteExpired",
|
|
342
|
+
payload: { setQuote }
|
|
343
|
+
});
|
|
344
|
+
} else if (payload.code === "applePayUnavailable") {
|
|
345
|
+
opts.onEvent?.({ kind: "unsupported" });
|
|
346
|
+
} else {
|
|
347
|
+
const kind = payload.code === "generic" ? "genericError" : payload.code;
|
|
348
|
+
opts.onEvent?.({
|
|
349
|
+
kind: "error",
|
|
350
|
+
payload: { kind, message: payload.message }
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
});
|
|
357
|
+
return (0, import_platform_protocol.ok)({ setQuote, dispose: () => handle.dispose() });
|
|
358
|
+
} catch (e) {
|
|
359
|
+
return (0, import_platform_protocol.err)({
|
|
360
|
+
kind: "genericError",
|
|
361
|
+
message: e instanceof Error ? e.message : "Apple Pay setup failed"
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
async function setupGooglePay(opts) {
|
|
366
|
+
const channelId = `googlepay-${crypto.randomUUID()}`;
|
|
367
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
368
|
+
import_platform_sdk_core.FRAME_PATHS.googlePay,
|
|
369
|
+
{
|
|
370
|
+
clientToken: core.context.clientToken,
|
|
371
|
+
signature: opts.quote,
|
|
372
|
+
channelId,
|
|
373
|
+
...opts.externalTransactionId && { externalTransactionId: opts.externalTransactionId }
|
|
374
|
+
},
|
|
375
|
+
{ frameBaseUrl }
|
|
376
|
+
);
|
|
377
|
+
const transport = core.createTransport();
|
|
378
|
+
try {
|
|
379
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
380
|
+
transport,
|
|
381
|
+
channelId,
|
|
382
|
+
url,
|
|
383
|
+
container: opts.container
|
|
384
|
+
});
|
|
385
|
+
const setQuote = (signature) => {
|
|
386
|
+
handle.sendMessage({
|
|
387
|
+
version: 2,
|
|
388
|
+
meta: { channelId },
|
|
389
|
+
kind: "setQuote",
|
|
390
|
+
payload: { quote: { signature } }
|
|
391
|
+
});
|
|
392
|
+
};
|
|
393
|
+
handle.onMessage((msg) => {
|
|
394
|
+
switch (msg.kind) {
|
|
395
|
+
case "ready":
|
|
396
|
+
opts.onEvent?.({ kind: "ready" });
|
|
397
|
+
break;
|
|
398
|
+
case "complete":
|
|
399
|
+
opts.onEvent?.({
|
|
400
|
+
kind: "complete",
|
|
401
|
+
payload: msg.payload
|
|
402
|
+
});
|
|
403
|
+
break;
|
|
404
|
+
case "challenge":
|
|
405
|
+
opts.onEvent?.({
|
|
406
|
+
kind: "challenge",
|
|
407
|
+
payload: msg.payload
|
|
408
|
+
});
|
|
409
|
+
break;
|
|
410
|
+
case "error": {
|
|
411
|
+
const payload = msg.payload;
|
|
412
|
+
if (payload.code === "quoteExpired") {
|
|
413
|
+
opts.onEvent?.({
|
|
414
|
+
kind: "quoteExpired",
|
|
415
|
+
payload: { setQuote }
|
|
416
|
+
});
|
|
417
|
+
} else if (payload.code === "googlePayUnavailable") {
|
|
418
|
+
opts.onEvent?.({ kind: "unsupported" });
|
|
419
|
+
} else {
|
|
420
|
+
const kind = payload.code === "generic" ? "genericError" : payload.code;
|
|
421
|
+
opts.onEvent?.({
|
|
422
|
+
kind: "error",
|
|
423
|
+
payload: { kind, message: payload.message }
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
return (0, import_platform_protocol.ok)({ setQuote, dispose: () => handle.dispose() });
|
|
431
|
+
} catch (e) {
|
|
432
|
+
return (0, import_platform_protocol.err)({
|
|
433
|
+
kind: "genericError",
|
|
434
|
+
message: e instanceof Error ? e.message : "Google Pay setup failed"
|
|
435
|
+
});
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
async function resetConnection() {
|
|
439
|
+
const channelId = `reset-${crypto.randomUUID()}`;
|
|
440
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(import_platform_sdk_core.FRAME_PATHS.reset, { sessionToken, channelId }, { frameBaseUrl });
|
|
441
|
+
const transport = core.createTransport();
|
|
442
|
+
try {
|
|
443
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
444
|
+
transport,
|
|
445
|
+
channelId,
|
|
446
|
+
url,
|
|
447
|
+
container: document.body,
|
|
448
|
+
hidden: true,
|
|
449
|
+
handshakeTimeout: 5e3
|
|
450
|
+
});
|
|
451
|
+
return new Promise((resolve) => {
|
|
452
|
+
const timeout = setTimeout(() => {
|
|
453
|
+
handle.dispose();
|
|
454
|
+
resolve((0, import_platform_protocol.ok)(void 0));
|
|
455
|
+
}, 5e3);
|
|
456
|
+
handle.onMessage((msg) => {
|
|
457
|
+
if (msg.kind === "complete" || msg.kind === "error") {
|
|
458
|
+
clearTimeout(timeout);
|
|
459
|
+
handle.dispose();
|
|
460
|
+
resolve((0, import_platform_protocol.ok)(void 0));
|
|
461
|
+
}
|
|
462
|
+
});
|
|
463
|
+
});
|
|
464
|
+
} catch {
|
|
465
|
+
return (0, import_platform_protocol.ok)(void 0);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
async function setupBuy(opts) {
|
|
469
|
+
const channelId = `buy-${crypto.randomUUID()}`;
|
|
470
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
471
|
+
import_platform_sdk_core.FRAME_PATHS.buy,
|
|
472
|
+
{
|
|
473
|
+
channelId,
|
|
474
|
+
clientToken: core.context.clientToken,
|
|
475
|
+
signature: opts.quote,
|
|
476
|
+
...opts.externalTransactionId && { externalTransactionId: opts.externalTransactionId }
|
|
477
|
+
},
|
|
478
|
+
{ frameBaseUrl }
|
|
479
|
+
);
|
|
480
|
+
const transport = core.createTransport();
|
|
481
|
+
try {
|
|
482
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
483
|
+
transport,
|
|
484
|
+
channelId,
|
|
485
|
+
url,
|
|
486
|
+
container: opts.container,
|
|
487
|
+
hidden: true
|
|
488
|
+
});
|
|
489
|
+
const setQuote = (signature) => {
|
|
490
|
+
handle.sendMessage({
|
|
491
|
+
version: 2,
|
|
492
|
+
meta: { channelId },
|
|
493
|
+
kind: "setQuote",
|
|
494
|
+
payload: { quote: { signature } }
|
|
495
|
+
});
|
|
496
|
+
};
|
|
497
|
+
handle.onMessage((msg) => {
|
|
498
|
+
switch (msg.kind) {
|
|
499
|
+
case "ready":
|
|
500
|
+
opts.onEvent?.({ kind: "ready" });
|
|
501
|
+
break;
|
|
502
|
+
case "complete":
|
|
503
|
+
opts.onEvent?.({
|
|
504
|
+
kind: "complete",
|
|
505
|
+
payload: msg.payload
|
|
506
|
+
});
|
|
507
|
+
break;
|
|
508
|
+
case "challenge":
|
|
509
|
+
opts.onEvent?.({
|
|
510
|
+
kind: "challenge",
|
|
511
|
+
payload: msg.payload
|
|
512
|
+
});
|
|
513
|
+
break;
|
|
514
|
+
case "error":
|
|
515
|
+
opts.onEvent?.({
|
|
516
|
+
kind: "error",
|
|
517
|
+
payload: msg.payload
|
|
518
|
+
});
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
return (0, import_platform_protocol.ok)({ setQuote, dispose: () => handle.dispose() });
|
|
523
|
+
} catch (e) {
|
|
524
|
+
return (0, import_platform_protocol.err)({
|
|
525
|
+
kind: "genericError",
|
|
526
|
+
message: e instanceof Error ? e.message : "Buy setup failed"
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
async function setupBuyButton(opts) {
|
|
531
|
+
const channelId = `buy-button-${crypto.randomUUID()}`;
|
|
532
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
533
|
+
import_platform_sdk_core.FRAME_PATHS.buyButton,
|
|
534
|
+
{
|
|
535
|
+
channelId,
|
|
536
|
+
clientToken: core.context.clientToken,
|
|
537
|
+
signature: opts.quote
|
|
538
|
+
},
|
|
539
|
+
{ frameBaseUrl }
|
|
540
|
+
);
|
|
541
|
+
const transport = core.createTransport();
|
|
542
|
+
try {
|
|
543
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
544
|
+
transport,
|
|
545
|
+
channelId,
|
|
546
|
+
url,
|
|
547
|
+
container: opts.container
|
|
548
|
+
});
|
|
549
|
+
const setQuote = (signature) => {
|
|
550
|
+
handle.sendMessage({
|
|
551
|
+
version: 2,
|
|
552
|
+
meta: { channelId },
|
|
553
|
+
kind: "setQuote",
|
|
554
|
+
payload: { quote: { signature } }
|
|
555
|
+
});
|
|
556
|
+
};
|
|
557
|
+
handle.onMessage((msg) => {
|
|
558
|
+
switch (msg.kind) {
|
|
559
|
+
case "complete":
|
|
560
|
+
opts.onEvent?.({
|
|
561
|
+
kind: "complete",
|
|
562
|
+
payload: msg.payload
|
|
563
|
+
});
|
|
564
|
+
break;
|
|
565
|
+
case "challenge":
|
|
566
|
+
opts.onEvent?.({
|
|
567
|
+
kind: "challenge",
|
|
568
|
+
payload: msg.payload
|
|
569
|
+
});
|
|
570
|
+
break;
|
|
571
|
+
case "error":
|
|
572
|
+
opts.onEvent?.({
|
|
573
|
+
kind: "error",
|
|
574
|
+
payload: msg.payload
|
|
575
|
+
});
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
return (0, import_platform_protocol.ok)({ setQuote, dispose: () => handle.dispose() });
|
|
580
|
+
} catch (e) {
|
|
581
|
+
return (0, import_platform_protocol.err)({
|
|
582
|
+
kind: "genericError",
|
|
583
|
+
message: e instanceof Error ? e.message : "Buy button setup failed"
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
async function setupChallenge(opts) {
|
|
588
|
+
let channelId;
|
|
589
|
+
try {
|
|
590
|
+
channelId = new URL(opts.url).searchParams.get("channelId") ?? "";
|
|
591
|
+
if (!channelId)
|
|
592
|
+
throw new Error("missing channelId");
|
|
593
|
+
} catch {
|
|
594
|
+
return (0, import_platform_protocol.err)({ kind: "configurationError", message: "Challenge URL is missing channelId" });
|
|
595
|
+
}
|
|
596
|
+
const transport = core.createTransport();
|
|
597
|
+
try {
|
|
598
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
599
|
+
transport,
|
|
600
|
+
channelId,
|
|
601
|
+
url: opts.url,
|
|
602
|
+
container: opts.container
|
|
603
|
+
});
|
|
604
|
+
handle.onMessage((msg) => {
|
|
605
|
+
switch (msg.kind) {
|
|
606
|
+
case "ready":
|
|
607
|
+
opts.onEvent?.({ kind: "ready" });
|
|
608
|
+
break;
|
|
609
|
+
case "complete":
|
|
610
|
+
opts.onEvent?.({ kind: "complete", payload: msg.payload });
|
|
611
|
+
break;
|
|
612
|
+
case "cancelled":
|
|
613
|
+
opts.onEvent?.({ kind: "cancelled", payload: msg.payload });
|
|
614
|
+
break;
|
|
615
|
+
case "error":
|
|
616
|
+
opts.onEvent?.({
|
|
617
|
+
kind: "error",
|
|
618
|
+
payload: msg.payload
|
|
619
|
+
});
|
|
620
|
+
break;
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
return (0, import_platform_protocol.ok)({ dispose: () => handle.dispose() });
|
|
624
|
+
} catch (e) {
|
|
625
|
+
return (0, import_platform_protocol.err)({
|
|
626
|
+
kind: "genericError",
|
|
627
|
+
message: e instanceof Error ? e.message : "Challenge setup failed"
|
|
628
|
+
});
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
async function setupAddCard(opts) {
|
|
632
|
+
const channelId = `add-card-${crypto.randomUUID()}`;
|
|
633
|
+
const url = (0, import_platform_sdk_core.buildFrameUrl)(
|
|
634
|
+
import_platform_sdk_core.FRAME_PATHS.addCard,
|
|
635
|
+
{ channelId, clientToken: core.context.clientToken },
|
|
636
|
+
{ frameBaseUrl }
|
|
637
|
+
);
|
|
638
|
+
const transport = core.createTransport();
|
|
639
|
+
try {
|
|
640
|
+
const handle = await (0, import_platform_sdk_core.createFrameOrchestrator)({
|
|
641
|
+
transport,
|
|
642
|
+
channelId,
|
|
643
|
+
url,
|
|
644
|
+
container: opts.container
|
|
645
|
+
});
|
|
646
|
+
handle.onMessage((msg) => {
|
|
647
|
+
switch (msg.kind) {
|
|
648
|
+
case "complete":
|
|
649
|
+
opts.onEvent?.({ kind: "complete", payload: msg.payload });
|
|
650
|
+
break;
|
|
651
|
+
case "error":
|
|
652
|
+
opts.onEvent?.({
|
|
653
|
+
kind: "error",
|
|
654
|
+
payload: msg.payload
|
|
655
|
+
});
|
|
656
|
+
break;
|
|
657
|
+
}
|
|
658
|
+
});
|
|
659
|
+
return (0, import_platform_protocol.ok)({ dispose: () => handle.dispose() });
|
|
660
|
+
} catch (e) {
|
|
661
|
+
return (0, import_platform_protocol.err)({
|
|
662
|
+
kind: "genericError",
|
|
663
|
+
message: e instanceof Error ? e.message : "Add card setup failed"
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
return {
|
|
668
|
+
getConnection,
|
|
669
|
+
connect,
|
|
670
|
+
getPaymentMethods: () => core.getPaymentMethods(),
|
|
671
|
+
getQuote: (params) => core.getQuote(params),
|
|
672
|
+
listTransactions: (params) => core.listTransactions(params),
|
|
673
|
+
getTransaction: (id) => core.getTransaction(id),
|
|
674
|
+
deletePaymentMethod: (id) => core.deletePaymentMethod(id),
|
|
675
|
+
resetConnection,
|
|
676
|
+
setupWidget,
|
|
677
|
+
setupApplePay,
|
|
678
|
+
setupGooglePay,
|
|
679
|
+
setupBuy,
|
|
680
|
+
setupBuyButton,
|
|
681
|
+
setupChallenge,
|
|
682
|
+
setupAddCard
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
686
|
+
0 && (module.exports = {
|
|
687
|
+
IframeTransport,
|
|
688
|
+
createClient
|
|
689
|
+
});
|
|
690
|
+
//# sourceMappingURL=index.cjs.map
|