@cimplify/sdk 0.3.6 → 0.4.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.js CHANGED
@@ -521,6 +521,15 @@ var AUTHORIZATION_TYPE = {
521
521
  BIRTHDAY: "birthday",
522
522
  ADDRESS: "address"
523
523
  };
524
+ var DEVICE_TYPE = {
525
+ MOBILE: "mobile",
526
+ DESKTOP: "desktop",
527
+ TABLET: "tablet"
528
+ };
529
+ var CONTACT_TYPE = {
530
+ PHONE: "phone",
531
+ EMAIL: "email"
532
+ };
524
533
  var LINK_QUERY = {
525
534
  DATA: "link.data",
526
535
  ADDRESSES: "link.addresses",
@@ -1221,6 +1230,297 @@ var LiteService = class {
1221
1230
  }
1222
1231
  };
1223
1232
 
1233
+ // src/types/elements.ts
1234
+ var ELEMENT_TYPES = {
1235
+ AUTH: "auth",
1236
+ ADDRESS: "address",
1237
+ PAYMENT: "payment"
1238
+ };
1239
+ var MESSAGE_TYPES = {
1240
+ // Parent → Iframe
1241
+ INIT: "init",
1242
+ SET_TOKEN: "set_token",
1243
+ GET_DATA: "get_data",
1244
+ REFRESH_TOKEN: "refresh_token",
1245
+ LOGOUT: "logout",
1246
+ // Iframe → Parent
1247
+ READY: "ready",
1248
+ HEIGHT_CHANGE: "height_change",
1249
+ AUTHENTICATED: "authenticated",
1250
+ REQUIRES_OTP: "requires_otp",
1251
+ ERROR: "error",
1252
+ ADDRESS_CHANGED: "address_changed",
1253
+ ADDRESS_SELECTED: "address_selected",
1254
+ PAYMENT_METHOD_SELECTED: "payment_method_selected",
1255
+ TOKEN_REFRESHED: "token_refreshed",
1256
+ LOGOUT_COMPLETE: "logout_complete"
1257
+ };
1258
+ var EVENT_TYPES = {
1259
+ READY: "ready",
1260
+ AUTHENTICATED: "authenticated",
1261
+ REQUIRES_OTP: "requires_otp",
1262
+ ERROR: "error",
1263
+ CHANGE: "change",
1264
+ BLUR: "blur",
1265
+ FOCUS: "focus"
1266
+ };
1267
+
1268
+ // src/elements.ts
1269
+ var DEFAULT_LINK_URL = "https://link.cimplify.io";
1270
+ var CimplifyElements = class {
1271
+ constructor(client, businessId, options = {}) {
1272
+ this.elements = /* @__PURE__ */ new Map();
1273
+ this.accessToken = null;
1274
+ this.accountId = null;
1275
+ this.customerId = null;
1276
+ this.addressData = null;
1277
+ this.paymentData = null;
1278
+ this.client = client;
1279
+ this.businessId = businessId;
1280
+ this.linkUrl = options.linkUrl || DEFAULT_LINK_URL;
1281
+ this.options = options;
1282
+ if (typeof window !== "undefined") {
1283
+ window.addEventListener("message", this.handleMessage.bind(this));
1284
+ }
1285
+ }
1286
+ create(type, options = {}) {
1287
+ const existing = this.elements.get(type);
1288
+ if (existing) return existing;
1289
+ const element = new CimplifyElement(type, this.businessId, this.linkUrl, options, this);
1290
+ this.elements.set(type, element);
1291
+ return element;
1292
+ }
1293
+ getElement(type) {
1294
+ return this.elements.get(type);
1295
+ }
1296
+ destroy() {
1297
+ this.elements.forEach((element) => element.destroy());
1298
+ this.elements.clear();
1299
+ if (typeof window !== "undefined") {
1300
+ window.removeEventListener("message", this.handleMessage.bind(this));
1301
+ }
1302
+ }
1303
+ async submitCheckout(data) {
1304
+ const addressElement = this.elements.get(ELEMENT_TYPES.ADDRESS);
1305
+ if (addressElement) await addressElement.getData();
1306
+ const paymentElement = this.elements.get(ELEMENT_TYPES.PAYMENT);
1307
+ if (paymentElement) await paymentElement.getData();
1308
+ const checkoutData = {
1309
+ ...data,
1310
+ customer: this.accountId ? { account_id: this.accountId, customer_id: this.customerId } : void 0,
1311
+ address: this.addressData,
1312
+ payment_method: this.paymentData
1313
+ };
1314
+ const result = await this.client.checkout.process(checkoutData);
1315
+ if (result.ok) {
1316
+ return { success: true, order: result.value };
1317
+ }
1318
+ return {
1319
+ success: false,
1320
+ error: {
1321
+ code: result.error.code || "CHECKOUT_FAILED",
1322
+ message: result.error.message || "Checkout failed"
1323
+ }
1324
+ };
1325
+ }
1326
+ isAuthenticated() {
1327
+ return this.accessToken !== null;
1328
+ }
1329
+ getAccessToken() {
1330
+ return this.accessToken;
1331
+ }
1332
+ handleMessage(event) {
1333
+ if (!event.origin.includes("cimplify.io") && !event.origin.includes("localhost")) {
1334
+ return;
1335
+ }
1336
+ const message = event.data;
1337
+ if (!message?.type) return;
1338
+ switch (message.type) {
1339
+ case MESSAGE_TYPES.AUTHENTICATED:
1340
+ this.accessToken = message.token;
1341
+ this.accountId = message.accountId;
1342
+ this.customerId = message.customerId;
1343
+ this.elements.forEach((element, type) => {
1344
+ if (type !== ELEMENT_TYPES.AUTH) {
1345
+ element.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token: message.token });
1346
+ }
1347
+ });
1348
+ break;
1349
+ case MESSAGE_TYPES.TOKEN_REFRESHED:
1350
+ this.accessToken = message.token;
1351
+ break;
1352
+ case MESSAGE_TYPES.ADDRESS_CHANGED:
1353
+ case MESSAGE_TYPES.ADDRESS_SELECTED:
1354
+ this.addressData = message.address;
1355
+ break;
1356
+ case MESSAGE_TYPES.PAYMENT_METHOD_SELECTED:
1357
+ this.paymentData = message.method;
1358
+ break;
1359
+ case MESSAGE_TYPES.LOGOUT_COMPLETE:
1360
+ this.accessToken = null;
1361
+ this.accountId = null;
1362
+ this.customerId = null;
1363
+ this.addressData = null;
1364
+ this.paymentData = null;
1365
+ break;
1366
+ }
1367
+ }
1368
+ _setAddressData(data) {
1369
+ this.addressData = data;
1370
+ }
1371
+ _setPaymentData(data) {
1372
+ this.paymentData = data;
1373
+ }
1374
+ };
1375
+ var CimplifyElement = class {
1376
+ constructor(type, businessId, linkUrl, options, parent) {
1377
+ this.iframe = null;
1378
+ this.container = null;
1379
+ this.mounted = false;
1380
+ this.eventHandlers = /* @__PURE__ */ new Map();
1381
+ this.resolvers = /* @__PURE__ */ new Map();
1382
+ this.type = type;
1383
+ this.businessId = businessId;
1384
+ this.linkUrl = linkUrl;
1385
+ this.options = options;
1386
+ this.parent = parent;
1387
+ if (typeof window !== "undefined") {
1388
+ window.addEventListener("message", this.handleMessage.bind(this));
1389
+ }
1390
+ }
1391
+ mount(container) {
1392
+ if (this.mounted) {
1393
+ console.warn(`Element ${this.type} is already mounted`);
1394
+ return;
1395
+ }
1396
+ const target = typeof container === "string" ? document.querySelector(container) : container;
1397
+ if (!target) {
1398
+ console.error(`Container not found: ${container}`);
1399
+ return;
1400
+ }
1401
+ this.container = target;
1402
+ this.createIframe();
1403
+ this.mounted = true;
1404
+ }
1405
+ destroy() {
1406
+ if (this.iframe) {
1407
+ this.iframe.remove();
1408
+ this.iframe = null;
1409
+ }
1410
+ this.container = null;
1411
+ this.mounted = false;
1412
+ this.eventHandlers.clear();
1413
+ if (typeof window !== "undefined") {
1414
+ window.removeEventListener("message", this.handleMessage.bind(this));
1415
+ }
1416
+ }
1417
+ on(event, handler) {
1418
+ if (!this.eventHandlers.has(event)) {
1419
+ this.eventHandlers.set(event, /* @__PURE__ */ new Set());
1420
+ }
1421
+ this.eventHandlers.get(event).add(handler);
1422
+ }
1423
+ off(event, handler) {
1424
+ this.eventHandlers.get(event)?.delete(handler);
1425
+ }
1426
+ async getData() {
1427
+ return new Promise((resolve) => {
1428
+ const id = Math.random().toString(36).slice(2);
1429
+ this.resolvers.set(id, resolve);
1430
+ this.sendMessage({ type: MESSAGE_TYPES.GET_DATA });
1431
+ setTimeout(() => {
1432
+ if (this.resolvers.has(id)) {
1433
+ this.resolvers.delete(id);
1434
+ resolve(null);
1435
+ }
1436
+ }, 5e3);
1437
+ });
1438
+ }
1439
+ sendMessage(message) {
1440
+ if (this.iframe?.contentWindow) {
1441
+ this.iframe.contentWindow.postMessage(message, this.linkUrl);
1442
+ }
1443
+ }
1444
+ createIframe() {
1445
+ if (!this.container) return;
1446
+ const iframe = document.createElement("iframe");
1447
+ const url = new URL(`${this.linkUrl}/elements/${this.type}`);
1448
+ url.searchParams.set("businessId", this.businessId);
1449
+ if (this.options.prefillEmail) url.searchParams.set("email", this.options.prefillEmail);
1450
+ if (this.options.mode) url.searchParams.set("mode", this.options.mode);
1451
+ iframe.src = url.toString();
1452
+ iframe.style.border = "none";
1453
+ iframe.style.width = "100%";
1454
+ iframe.style.display = "block";
1455
+ iframe.style.overflow = "hidden";
1456
+ iframe.setAttribute("allowtransparency", "true");
1457
+ iframe.setAttribute("frameborder", "0");
1458
+ iframe.setAttribute("sandbox", "allow-scripts allow-same-origin allow-forms allow-popups");
1459
+ this.iframe = iframe;
1460
+ this.container.appendChild(iframe);
1461
+ iframe.onload = () => {
1462
+ this.sendMessage({
1463
+ type: MESSAGE_TYPES.INIT,
1464
+ businessId: this.businessId,
1465
+ prefillEmail: this.options.prefillEmail
1466
+ });
1467
+ const token = this.parent.getAccessToken();
1468
+ if (token && this.type !== ELEMENT_TYPES.AUTH) {
1469
+ this.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
1470
+ }
1471
+ };
1472
+ }
1473
+ handleMessage(event) {
1474
+ if (!event.origin.includes("cimplify.io") && !event.origin.includes("localhost")) {
1475
+ return;
1476
+ }
1477
+ const message = event.data;
1478
+ if (!message?.type) return;
1479
+ switch (message.type) {
1480
+ case MESSAGE_TYPES.READY:
1481
+ this.emit(EVENT_TYPES.READY, { height: message.height });
1482
+ break;
1483
+ case MESSAGE_TYPES.HEIGHT_CHANGE:
1484
+ if (this.iframe) this.iframe.style.height = `${message.height}px`;
1485
+ break;
1486
+ case MESSAGE_TYPES.AUTHENTICATED:
1487
+ this.emit(EVENT_TYPES.AUTHENTICATED, {
1488
+ accountId: message.accountId,
1489
+ customerId: message.customerId,
1490
+ token: message.token
1491
+ });
1492
+ break;
1493
+ case MESSAGE_TYPES.REQUIRES_OTP:
1494
+ this.emit(EVENT_TYPES.REQUIRES_OTP, { contactMasked: message.contactMasked });
1495
+ break;
1496
+ case MESSAGE_TYPES.ERROR:
1497
+ this.emit(EVENT_TYPES.ERROR, { code: message.code, message: message.message });
1498
+ break;
1499
+ case MESSAGE_TYPES.ADDRESS_CHANGED:
1500
+ case MESSAGE_TYPES.ADDRESS_SELECTED:
1501
+ this.parent._setAddressData(message.address);
1502
+ this.emit(EVENT_TYPES.CHANGE, { address: message.address });
1503
+ this.resolveData(message.address);
1504
+ break;
1505
+ case MESSAGE_TYPES.PAYMENT_METHOD_SELECTED:
1506
+ this.parent._setPaymentData(message.method);
1507
+ this.emit(EVENT_TYPES.CHANGE, { paymentMethod: message.method });
1508
+ this.resolveData(message.method);
1509
+ break;
1510
+ }
1511
+ }
1512
+ emit(event, data) {
1513
+ this.eventHandlers.get(event)?.forEach((handler) => handler(data));
1514
+ }
1515
+ resolveData(data) {
1516
+ this.resolvers.forEach((resolve) => resolve(data));
1517
+ this.resolvers.clear();
1518
+ }
1519
+ };
1520
+ function createElements(client, businessId, options) {
1521
+ return new CimplifyElements(client, businessId, options);
1522
+ }
1523
+
1224
1524
  // src/client.ts
1225
1525
  var SESSION_TOKEN_HEADER = "x-session-token";
1226
1526
  var SESSION_STORAGE_KEY = "cimplify_session_token";
@@ -1632,6 +1932,23 @@ var CimplifyClient = class {
1632
1932
  }
1633
1933
  return this._lite;
1634
1934
  }
1935
+ /**
1936
+ * Create a CimplifyElements instance for embedding checkout components.
1937
+ * Like Stripe's stripe.elements().
1938
+ *
1939
+ * @param businessId - The business ID for checkout context
1940
+ * @param options - Optional configuration for elements
1941
+ *
1942
+ * @example
1943
+ * ```ts
1944
+ * const elements = client.elements('bus_xxx');
1945
+ * const authElement = elements.create('auth');
1946
+ * authElement.mount('#auth-container');
1947
+ * ```
1948
+ */
1949
+ elements(businessId, options) {
1950
+ return createElements(this, businessId, options);
1951
+ }
1635
1952
  };
1636
1953
  function createCimplifyClient(config) {
1637
1954
  return new CimplifyClient(config);
@@ -2195,21 +2512,28 @@ exports.BusinessService = BusinessService;
2195
2512
  exports.CHECKOUT_MODE = CHECKOUT_MODE;
2196
2513
  exports.CHECKOUT_MUTATION = CHECKOUT_MUTATION;
2197
2514
  exports.CHECKOUT_STEP = CHECKOUT_STEP;
2515
+ exports.CONTACT_TYPE = CONTACT_TYPE;
2198
2516
  exports.CURRENCY_SYMBOLS = CURRENCY_SYMBOLS;
2199
2517
  exports.CartOperations = CartOperations;
2200
2518
  exports.CatalogueQueries = CatalogueQueries;
2201
2519
  exports.CheckoutOperations = CheckoutService;
2202
2520
  exports.CheckoutService = CheckoutService;
2203
2521
  exports.CimplifyClient = CimplifyClient;
2522
+ exports.CimplifyElement = CimplifyElement;
2523
+ exports.CimplifyElements = CimplifyElements;
2204
2524
  exports.CimplifyError = CimplifyError;
2205
2525
  exports.DEFAULT_COUNTRY = DEFAULT_COUNTRY;
2206
2526
  exports.DEFAULT_CURRENCY = DEFAULT_CURRENCY;
2527
+ exports.DEVICE_TYPE = DEVICE_TYPE;
2528
+ exports.ELEMENT_TYPES = ELEMENT_TYPES;
2529
+ exports.EVENT_TYPES = EVENT_TYPES;
2207
2530
  exports.ErrorCode = ErrorCode;
2208
2531
  exports.InventoryService = InventoryService;
2209
2532
  exports.LINK_MUTATION = LINK_MUTATION;
2210
2533
  exports.LINK_QUERY = LINK_QUERY;
2211
2534
  exports.LinkService = LinkService;
2212
2535
  exports.LiteService = LiteService;
2536
+ exports.MESSAGE_TYPES = MESSAGE_TYPES;
2213
2537
  exports.MOBILE_MONEY_PROVIDER = MOBILE_MONEY_PROVIDER;
2214
2538
  exports.MOBILE_MONEY_PROVIDERS = MOBILE_MONEY_PROVIDERS;
2215
2539
  exports.ORDER_MUTATION = ORDER_MUTATION;
@@ -2225,6 +2549,7 @@ exports.categorizePaymentError = categorizePaymentError;
2225
2549
  exports.combine = combine;
2226
2550
  exports.combineObject = combineObject;
2227
2551
  exports.createCimplifyClient = createCimplifyClient;
2552
+ exports.createElements = createElements;
2228
2553
  exports.detectMobileMoneyProvider = detectMobileMoneyProvider;
2229
2554
  exports.err = err;
2230
2555
  exports.extractPriceInfo = extractPriceInfo;