@cimplify/sdk 0.106.1 → 0.107.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.
Files changed (58) hide show
  1. package/dist/advanced.d.mts +3 -3
  2. package/dist/advanced.d.ts +3 -3
  3. package/dist/advanced.js +20 -20
  4. package/dist/advanced.mjs +1 -1
  5. package/dist/{catalogue-ChBjPPME.d.ts → catalogue-BohD-jU_.d.mts} +44 -38
  6. package/dist/{catalogue-CVKH9faR.d.mts → catalogue-C4xKpirp.d.ts} +44 -38
  7. package/dist/{chunk-YHR6LFQ7.mjs → chunk-2I6YKE6Q.mjs} +55 -94
  8. package/dist/{chunk-LJSQ7TUV.js → chunk-73HY36Z7.js} +4 -4
  9. package/dist/{chunk-SFG2LMRD.js → chunk-HKCB73AJ.js} +55 -94
  10. package/dist/{chunk-IWXVKW22.mjs → chunk-IWZKCL3P.mjs} +2 -2
  11. package/dist/{chunk-L4XKIMUO.mjs → chunk-MKZXZDHE.mjs} +17 -0
  12. package/dist/{chunk-NJK3THWT.js → chunk-NX5YBQZP.js} +17 -0
  13. package/dist/{chunk-ABAK4YVY.mjs → chunk-P4HYAV3L.mjs} +102 -82
  14. package/dist/{chunk-QKL5L5IU.js → chunk-W4ZFZPPY.js} +181 -160
  15. package/dist/{client-BfKFK7cn.d.mts → client-BBFfAtn4.d.mts} +2 -2
  16. package/dist/{client-DoczTJbs.d.ts → client-ByURQZM9.d.ts} +2 -2
  17. package/dist/{checkout-XoV4HNg2.d.mts → elements-Dtqe9Ekd.d.mts} +348 -361
  18. package/dist/{checkout-XoV4HNg2.d.ts → elements-Dtqe9Ekd.d.ts} +348 -361
  19. package/dist/{index-DEUorPUO.d.mts → index-7oz-hVVA.d.mts} +2 -2
  20. package/dist/{index-DRMBo26v.d.mts → index-B6mukQYB.d.mts} +2 -2
  21. package/dist/{index-Dn6xCpDe.d.ts → index-CvLuAMjQ.d.ts} +2 -2
  22. package/dist/{index-DweR60Cv.d.ts → index-DFqDU6yU.d.ts} +2 -2
  23. package/dist/index.d.mts +7 -7
  24. package/dist/index.d.ts +7 -7
  25. package/dist/index.js +71 -67
  26. package/dist/index.mjs +3 -3
  27. package/dist/mock/cli.mjs +17 -0
  28. package/dist/mock/library.js +17 -0
  29. package/dist/mock/library.mjs +17 -0
  30. package/dist/mock/msw.js +17 -0
  31. package/dist/mock/msw.mjs +17 -0
  32. package/dist/{price-De-sxcuj.d.ts → price-B4jL81ef.d.ts} +1 -1
  33. package/dist/{price-CUGIYRtD.d.mts → price-Pyq-vlrm.d.mts} +1 -1
  34. package/dist/react.d.mts +3 -3
  35. package/dist/react.d.ts +3 -3
  36. package/dist/react.js +34 -34
  37. package/dist/react.mjs +5 -5
  38. package/dist/{server-7soMMUgP.d.ts → server-Cowuvfbr.d.ts} +1 -1
  39. package/dist/{server-DDEJ7e9s.d.mts → server-KilvZJU0.d.mts} +1 -1
  40. package/dist/server.d.mts +3 -3
  41. package/dist/server.d.ts +3 -3
  42. package/dist/server.js +7 -7
  43. package/dist/server.mjs +2 -2
  44. package/dist/testing/msw.d.mts +2 -2
  45. package/dist/testing/msw.d.ts +2 -2
  46. package/dist/testing/msw.js +2 -2
  47. package/dist/testing/msw.mjs +1 -1
  48. package/dist/testing/suite.d.mts +5 -5
  49. package/dist/testing/suite.d.ts +5 -5
  50. package/dist/testing/suite.js +24 -24
  51. package/dist/testing/suite.mjs +4 -4
  52. package/dist/testing.d.mts +4 -4
  53. package/dist/testing.d.ts +4 -4
  54. package/dist/testing.js +116 -116
  55. package/dist/testing.mjs +5 -5
  56. package/dist/utils.d.mts +3 -3
  57. package/dist/utils.d.ts +3 -3
  58. package/package.json +1 -1
@@ -2405,7 +2405,7 @@ var LinkService = class {
2405
2405
  this.client.activity.refreshIdentity();
2406
2406
  return result;
2407
2407
  }
2408
- const claim = await this.client.support.claimConversation();
2408
+ const claim = await this.client.session.claim();
2409
2409
  if (fence.generation !== this.activationGeneration || fence.publicKeyEpoch !== this.client.getPublicKeyEpoch() || this.client.getAccessToken() !== accessToken) {
2410
2410
  return this.supersededActivation();
2411
2411
  }
@@ -2629,7 +2629,7 @@ var AuthService = class {
2629
2629
  if (result.ok && result.value.session_token) {
2630
2630
  this.client.setAccessToken(result.value.session_token);
2631
2631
  this.client.activity.refreshIdentity();
2632
- this.client.autoClaimChatIdentity();
2632
+ this.client.autoClaimSession();
2633
2633
  }
2634
2634
  return result;
2635
2635
  }
@@ -2922,13 +2922,10 @@ var ELEMENT_TYPES = {
2922
2922
  };
2923
2923
  var MESSAGE_TYPES = {
2924
2924
  INIT: "init",
2925
- SET_TOKEN: "set_token",
2926
- SET_SESSION: "set_session",
2925
+ IDENTITY: "identity",
2927
2926
  SESSION_CLEARED: "session_cleared",
2928
2927
  SET_CART: "set_cart",
2929
2928
  GET_DATA: "get_data",
2930
- NAVIGATE: "navigate",
2931
- REFRESH_TOKEN: "refresh_token",
2932
2929
  LOGOUT: "logout",
2933
2930
  PROCESS_CHECKOUT: "process_checkout",
2934
2931
  ABORT_CHECKOUT: "abort_checkout",
@@ -2941,7 +2938,6 @@ var MESSAGE_TYPES = {
2941
2938
  LOGOUT_COMPLETE: "logout_complete",
2942
2939
  AUTH_REQUESTED: "auth_requested",
2943
2940
  SESSION_ESTABLISHED: "session_established",
2944
- SESSION_ESTABLISHMENT_FAILED: "session_establishment_failed",
2945
2941
  CONTINUE_WITHOUT_SAVED_DETAILS: "continue_without_saved_details",
2946
2942
  CONTACT_PROVIDED: "contact_provided",
2947
2943
  CHECKOUT_STATUS: "checkout_status",
@@ -3345,7 +3341,6 @@ function toCheckoutError(code, message, recoverable) {
3345
3341
  };
3346
3342
  }
3347
3343
  var DEFAULT_LINK_URL = "https://link.cimplify.io";
3348
- var AUTH_ESTABLISHMENT_WAIT_MS = 8e3;
3349
3344
  var AUTH_CONTACT_TYPES = new Set(Object.values(ELEMENTS_AUTH_CONTACT_TYPES));
3350
3345
  var AUTH_MODES = new Set(Object.values(ELEMENTS_AUTH_MODES));
3351
3346
  var AUTH_PROMPTS = new Set(Object.values(ELEMENTS_AUTH_PROMPTS));
@@ -3376,6 +3371,11 @@ function decodeJwtSub(token) {
3376
3371
  return "";
3377
3372
  }
3378
3373
  }
3374
+ function isClaimedCustomer(value) {
3375
+ if (!value || typeof value !== "object") return false;
3376
+ const record = value;
3377
+ return typeof record.id === "string" && typeof record.name === "string" && (record.email === null || typeof record.email === "string") && (record.phone === null || typeof record.phone === "string");
3378
+ }
3379
3379
  function isAllowedOrigin(origin) {
3380
3380
  try {
3381
3381
  const url = new URL(origin);
@@ -3432,8 +3432,10 @@ function parseIframeMessage(data) {
3432
3432
  }
3433
3433
  break;
3434
3434
  case MESSAGE_TYPES.SESSION_ESTABLISHED:
3435
+ if (typeof record.sessionToken !== "string" || !record.sessionToken.trim()) return null;
3435
3436
  if (typeof record.accessToken !== "string" || !record.accessToken.trim()) return null;
3436
3437
  if (record.refreshToken !== void 0 && typeof record.refreshToken !== "string") return null;
3438
+ if (!isClaimedCustomer(record.customer)) return null;
3437
3439
  break;
3438
3440
  case MESSAGE_TYPES.ADDRESS_CHANGED:
3439
3441
  case MESSAGE_TYPES.ADDRESS_SELECTED:
@@ -3484,7 +3486,6 @@ var CimplifyElements = class {
3484
3486
  this.paymentData = null;
3485
3487
  this.checkoutInProgress = false;
3486
3488
  this.authInProgress = false;
3487
- this.authEstablishmentInProgress = false;
3488
3489
  this.authGeneration = 0;
3489
3490
  this.activeCheckoutAbort = null;
3490
3491
  this.businessIdResolvePromise = null;
@@ -3499,7 +3500,7 @@ var CimplifyElements = class {
3499
3500
  window.addEventListener("message", this.boundHandleMessage);
3500
3501
  }
3501
3502
  this.stopSessionSync = client.events.on(SESSION_CLAIMED, () => {
3502
- if (!this.authInProgress && !this.authEstablishmentInProgress) this.syncHostSession();
3503
+ if (!this.authInProgress) this._syncHostSession();
3503
3504
  });
3504
3505
  void this.restoreSession();
3505
3506
  }
@@ -3508,20 +3509,24 @@ var CimplifyElements = class {
3508
3509
  console.debug(...args);
3509
3510
  }
3510
3511
  }
3511
- syncHostSession(element) {
3512
- if (this.authEstablishmentInProgress) return;
3512
+ _syncHostSession(element) {
3513
3513
  const sessionToken = this.client.getSessionToken();
3514
- const token = this.client.getAccessToken() ?? this.accessToken;
3514
+ if (!sessionToken) return;
3515
+ const identity = {
3516
+ type: MESSAGE_TYPES.IDENTITY,
3517
+ sessionToken,
3518
+ accessToken: this.client.getAccessToken() ?? this.accessToken,
3519
+ customer: this.identityCustomer()
3520
+ };
3515
3521
  const targets = element ? [element] : Array.from(this.elements.values());
3516
3522
  for (const target of targets) {
3517
- if (sessionToken) {
3518
- target.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
3519
- }
3520
- if (token) {
3521
- target.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
3522
- }
3523
+ target.sendMessage(identity);
3523
3524
  }
3524
3525
  }
3526
+ identityCustomer() {
3527
+ if (!this.accessToken || !this.customerId || !this.customerData) return null;
3528
+ return { id: this.customerId, ...this.customerData };
3529
+ }
3525
3530
  create(type, options = {}) {
3526
3531
  const existing = this.elements.get(type);
3527
3532
  if (existing) return existing;
@@ -3540,11 +3545,7 @@ var CimplifyElements = class {
3540
3545
  }
3541
3546
  destroy() {
3542
3547
  this.authGeneration += 1;
3543
- if (this.authEstablishmentInProgress) {
3544
- this.client.setAccessToken(null);
3545
- }
3546
3548
  this.authInProgress = false;
3547
- this.authEstablishmentInProgress = false;
3548
3549
  this.stopSessionSync();
3549
3550
  this.stopSessionSync = () => {
3550
3551
  };
@@ -3604,17 +3605,6 @@ var CimplifyElements = class {
3604
3605
  );
3605
3606
  }
3606
3607
  this.checkoutInProgress = true;
3607
- if (this.authEstablishmentInProgress) {
3608
- await this.waitForAuthEstablishment(AUTH_ESTABLISHMENT_WAIT_MS);
3609
- if (this.authEstablishmentInProgress) {
3610
- this.checkoutInProgress = false;
3611
- return toCheckoutError(
3612
- ErrorCode.AUTH_INCOMPLETE,
3613
- "Authentication must finish establishing the checkout session.",
3614
- true
3615
- );
3616
- }
3617
- }
3618
3608
  if (!options.cart_id && !options.intent_id) {
3619
3609
  this.debug("[cimplify:checkout] BLOCKED: no cart_id or intent_id");
3620
3610
  this.checkoutInProgress = false;
@@ -3819,7 +3809,7 @@ var CimplifyElements = class {
3819
3809
  hasAddress: !!processMessage.address,
3820
3810
  isUnifiedCheckout
3821
3811
  });
3822
- this.syncHostSession(checkoutElement);
3812
+ this._syncHostSession(checkoutElement);
3823
3813
  checkoutElement.sendMessage(processMessage);
3824
3814
  this.debug("[cimplify:checkout] postMessage sent, waiting for checkout_complete...");
3825
3815
  });
@@ -3930,7 +3920,6 @@ var CimplifyElements = class {
3930
3920
  this.customerData = null;
3931
3921
  this.addressData = null;
3932
3922
  this.paymentData = null;
3933
- this.authEstablishmentInProgress = false;
3934
3923
  this.client.clearSession();
3935
3924
  this.broadcastClearedSession();
3936
3925
  }
@@ -3966,14 +3955,12 @@ var CimplifyElements = class {
3966
3955
  this.broadcastClearedSession();
3967
3956
  return;
3968
3957
  }
3969
- if (application.kind !== "authenticated" || !this.broadcastAccessToken(result.value.session_token, generation)) {
3970
- return;
3971
- }
3958
+ if (application.kind !== "authenticated") return;
3959
+ this._syncHostSession();
3972
3960
  this.elements.forEach((element) => element._emitAuthenticated(application.data));
3973
3961
  }
3974
3962
  async applyAccessToken(token, generation) {
3975
3963
  if (this.authGeneration !== generation) return { kind: "incomplete" };
3976
- this.authEstablishmentInProgress = true;
3977
3964
  this.accessToken = null;
3978
3965
  this.accountId = null;
3979
3966
  this.customerId = null;
@@ -3981,7 +3968,7 @@ var CimplifyElements = class {
3981
3968
  this.addressData = null;
3982
3969
  this.paymentData = null;
3983
3970
  this.client.setAccessToken(token);
3984
- const claim = await this.client.support.claimConversation();
3971
+ const claim = await this.client.session.claim();
3985
3972
  if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
3986
3973
  if (!claim.ok && claim.error.code !== "already_claimed") {
3987
3974
  if (this.client.hasClaimedSessionAuthority()) {
@@ -3989,7 +3976,6 @@ var CimplifyElements = class {
3989
3976
  } else {
3990
3977
  this.client.setAccessToken(null);
3991
3978
  }
3992
- this.authEstablishmentInProgress = false;
3993
3979
  return {
3994
3980
  kind: "claim_failed",
3995
3981
  code: claim.error.code,
@@ -4002,7 +3988,6 @@ var CimplifyElements = class {
4002
3988
  return { kind: "incomplete" };
4003
3989
  }
4004
3990
  if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
4005
- this.authEstablishmentInProgress = false;
4006
3991
  return {
4007
3992
  kind: "authenticated",
4008
3993
  data: {
@@ -4013,24 +3998,30 @@ var CimplifyElements = class {
4013
3998
  }
4014
3999
  };
4015
4000
  }
4016
- async waitForAuthEstablishment(timeoutMs) {
4017
- const deadline = Date.now() + timeoutMs;
4018
- while (this.authEstablishmentInProgress && Date.now() < deadline) {
4019
- await new Promise((resolve) => setTimeout(resolve, 100));
4020
- }
4021
- }
4022
- broadcastAccessToken(token, generation) {
4023
- if (!this.isAuthGenerationCurrent(generation, token) || this.accessToken !== token || this.authEstablishmentInProgress) {
4024
- return false;
4025
- }
4026
- const sessionToken = this.client.getSessionToken();
4027
- this.elements.forEach((element) => {
4028
- if (sessionToken) {
4029
- element.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
4030
- }
4031
- element.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
4001
+ adoptEstablishedSession(sourceElement, session) {
4002
+ this.authGeneration += 1;
4003
+ this.authInProgress = false;
4004
+ const { id, ...customer } = session.customer;
4005
+ this.accessToken = session.accessToken;
4006
+ this.accountId = decodeJwtSub(session.accessToken);
4007
+ this.customerId = id;
4008
+ this.customerData = customer;
4009
+ this.addressData = null;
4010
+ this.paymentData = null;
4011
+ this.client.setAccessToken(session.accessToken);
4012
+ this.persistRefreshToken(session.refreshToken);
4013
+ this.client.session.adopt(session.accessToken, {
4014
+ sessionToken: session.sessionToken,
4015
+ customerName: session.customer.name,
4016
+ customer: session.customer,
4017
+ cart: null
4018
+ });
4019
+ sourceElement._emitAuthenticated({
4020
+ accountId: this.accountId,
4021
+ customerId: id,
4022
+ token: session.accessToken,
4023
+ customer
4032
4024
  });
4033
- return true;
4034
4025
  }
4035
4026
  broadcastClearedSession() {
4036
4027
  const sessionToken = this.client.getSessionToken();
@@ -4077,7 +4068,8 @@ var CimplifyElements = class {
4077
4068
  return;
4078
4069
  }
4079
4070
  const application = await this.applyAccessToken(accessToken, generation);
4080
- if (application.kind === "authenticated" && this.broadcastAccessToken(accessToken, generation)) {
4071
+ if (application.kind === "authenticated") {
4072
+ this._syncHostSession();
4081
4073
  sourceElement._emitAuthenticated(application.data);
4082
4074
  } else if (application.kind === "claim_failed") {
4083
4075
  sourceElement._emitError({
@@ -4116,29 +4108,7 @@ var CimplifyElements = class {
4116
4108
  this.startEmbeddedAuth(sourceElement, message);
4117
4109
  break;
4118
4110
  case MESSAGE_TYPES.SESSION_ESTABLISHED:
4119
- {
4120
- this.authInProgress = false;
4121
- const generation = ++this.authGeneration;
4122
- void this.applyAccessToken(message.accessToken, generation).then((application) => {
4123
- if (this.authGeneration !== generation) return;
4124
- if (application.kind === "claim_failed") {
4125
- this.clearPersistedSession();
4126
- sourceElement.sendMessage({
4127
- type: MESSAGE_TYPES.SESSION_ESTABLISHMENT_FAILED,
4128
- accessToken: message.accessToken,
4129
- code: application.code,
4130
- message: application.message
4131
- });
4132
- this.broadcastClearedSession();
4133
- return;
4134
- }
4135
- if (application.kind !== "authenticated" || !this.broadcastAccessToken(message.accessToken, generation)) {
4136
- return;
4137
- }
4138
- this.persistRefreshToken(message.refreshToken);
4139
- sourceElement._emitAuthenticated(application.data);
4140
- });
4141
- }
4111
+ this.adoptEstablishedSession(sourceElement, message);
4142
4112
  break;
4143
4113
  case MESSAGE_TYPES.ADDRESS_CHANGED:
4144
4114
  case MESSAGE_TYPES.ADDRESS_SELECTED:
@@ -4160,7 +4130,6 @@ var CimplifyElements = class {
4160
4130
  case MESSAGE_TYPES.CONTINUE_WITHOUT_SAVED_DETAILS: {
4161
4131
  this.authGeneration += 1;
4162
4132
  this.authInProgress = false;
4163
- this.authEstablishmentInProgress = false;
4164
4133
  const carriedIdentityAuthority = Boolean(this.client.getAccessToken()) || this.client.hasClaimedSessionAuthority();
4165
4134
  this.accessToken = null;
4166
4135
  this.accountId = null;
@@ -4185,7 +4154,6 @@ var CimplifyElements = class {
4185
4154
  case MESSAGE_TYPES.LOGOUT_COMPLETE:
4186
4155
  this.authGeneration += 1;
4187
4156
  this.authInProgress = false;
4188
- this.authEstablishmentInProgress = false;
4189
4157
  if (this.checkoutInProgress && this.activeCheckoutAbort) {
4190
4158
  this.activeCheckoutAbort();
4191
4159
  }
@@ -4434,14 +4402,7 @@ var CimplifyElement = class {
4434
4402
  hostedSessionId: this.options.hostedSessionId,
4435
4403
  initialTerms: this.options.initialTerms
4436
4404
  });
4437
- const token = this.parent.getAccessToken();
4438
- const sessionToken = this.parent.getSessionToken();
4439
- if (sessionToken) {
4440
- this.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
4441
- }
4442
- if (token) {
4443
- this.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
4444
- }
4405
+ this.parent._syncHostSession(this);
4445
4406
  this.pendingInit = null;
4446
4407
  }
4447
4408
  this.flushPending();
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var chunkNJK3THWT_js = require('./chunk-NJK3THWT.js');
4
- var chunkQKL5L5IU_js = require('./chunk-QKL5L5IU.js');
3
+ var chunkNX5YBQZP_js = require('./chunk-NX5YBQZP.js');
4
+ var chunkW4ZFZPPY_js = require('./chunk-W4ZFZPPY.js');
5
5
  var chunkEV2EVOYS_js = require('./chunk-EV2EVOYS.js');
6
6
  var chunkAVXA4F6G_js = require('./chunk-AVXA4F6G.js');
7
7
  var zod = require('zod');
@@ -1185,7 +1185,7 @@ async function toForwardedRequest(input, init) {
1185
1185
  };
1186
1186
  }
1187
1187
  function createTestClient(options = {}) {
1188
- const mock = chunkNJK3THWT_js.createMockApp(options);
1188
+ const mock = chunkNX5YBQZP_js.createMockApp(options);
1189
1189
  let sessionToken;
1190
1190
  const fetchImpl = async (input, init) => {
1191
1191
  const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
@@ -1204,7 +1204,7 @@ function createTestClient(options = {}) {
1204
1204
  if (echoed) sessionToken = echoed;
1205
1205
  return res;
1206
1206
  };
1207
- const client = chunkQKL5L5IU_js.createCimplifyClient({
1207
+ const client = chunkW4ZFZPPY_js.createCimplifyClient({
1208
1208
  baseUrl: TEST_BASE_URL,
1209
1209
  // Link mock lives at /v1/link on the same Hono app, so point the SDK's
1210
1210
  // separate Link transport at the same TEST_BASE_URL.
@@ -2407,7 +2407,7 @@ var LinkService = class {
2407
2407
  this.client.activity.refreshIdentity();
2408
2408
  return result;
2409
2409
  }
2410
- const claim = await this.client.support.claimConversation();
2410
+ const claim = await this.client.session.claim();
2411
2411
  if (fence.generation !== this.activationGeneration || fence.publicKeyEpoch !== this.client.getPublicKeyEpoch() || this.client.getAccessToken() !== accessToken) {
2412
2412
  return this.supersededActivation();
2413
2413
  }
@@ -2631,7 +2631,7 @@ var AuthService = class {
2631
2631
  if (result.ok && result.value.session_token) {
2632
2632
  this.client.setAccessToken(result.value.session_token);
2633
2633
  this.client.activity.refreshIdentity();
2634
- this.client.autoClaimChatIdentity();
2634
+ this.client.autoClaimSession();
2635
2635
  }
2636
2636
  return result;
2637
2637
  }
@@ -2924,13 +2924,10 @@ var ELEMENT_TYPES = {
2924
2924
  };
2925
2925
  var MESSAGE_TYPES = {
2926
2926
  INIT: "init",
2927
- SET_TOKEN: "set_token",
2928
- SET_SESSION: "set_session",
2927
+ IDENTITY: "identity",
2929
2928
  SESSION_CLEARED: "session_cleared",
2930
2929
  SET_CART: "set_cart",
2931
2930
  GET_DATA: "get_data",
2932
- NAVIGATE: "navigate",
2933
- REFRESH_TOKEN: "refresh_token",
2934
2931
  LOGOUT: "logout",
2935
2932
  PROCESS_CHECKOUT: "process_checkout",
2936
2933
  ABORT_CHECKOUT: "abort_checkout",
@@ -2943,7 +2940,6 @@ var MESSAGE_TYPES = {
2943
2940
  LOGOUT_COMPLETE: "logout_complete",
2944
2941
  AUTH_REQUESTED: "auth_requested",
2945
2942
  SESSION_ESTABLISHED: "session_established",
2946
- SESSION_ESTABLISHMENT_FAILED: "session_establishment_failed",
2947
2943
  CONTINUE_WITHOUT_SAVED_DETAILS: "continue_without_saved_details",
2948
2944
  CONTACT_PROVIDED: "contact_provided",
2949
2945
  CHECKOUT_STATUS: "checkout_status",
@@ -3347,7 +3343,6 @@ function toCheckoutError(code, message, recoverable) {
3347
3343
  };
3348
3344
  }
3349
3345
  var DEFAULT_LINK_URL = "https://link.cimplify.io";
3350
- var AUTH_ESTABLISHMENT_WAIT_MS = 8e3;
3351
3346
  var AUTH_CONTACT_TYPES = new Set(Object.values(ELEMENTS_AUTH_CONTACT_TYPES));
3352
3347
  var AUTH_MODES = new Set(Object.values(ELEMENTS_AUTH_MODES));
3353
3348
  var AUTH_PROMPTS = new Set(Object.values(ELEMENTS_AUTH_PROMPTS));
@@ -3378,6 +3373,11 @@ function decodeJwtSub(token) {
3378
3373
  return "";
3379
3374
  }
3380
3375
  }
3376
+ function isClaimedCustomer(value) {
3377
+ if (!value || typeof value !== "object") return false;
3378
+ const record = value;
3379
+ return typeof record.id === "string" && typeof record.name === "string" && (record.email === null || typeof record.email === "string") && (record.phone === null || typeof record.phone === "string");
3380
+ }
3381
3381
  function isAllowedOrigin(origin) {
3382
3382
  try {
3383
3383
  const url = new URL(origin);
@@ -3434,8 +3434,10 @@ function parseIframeMessage(data) {
3434
3434
  }
3435
3435
  break;
3436
3436
  case MESSAGE_TYPES.SESSION_ESTABLISHED:
3437
+ if (typeof record.sessionToken !== "string" || !record.sessionToken.trim()) return null;
3437
3438
  if (typeof record.accessToken !== "string" || !record.accessToken.trim()) return null;
3438
3439
  if (record.refreshToken !== void 0 && typeof record.refreshToken !== "string") return null;
3440
+ if (!isClaimedCustomer(record.customer)) return null;
3439
3441
  break;
3440
3442
  case MESSAGE_TYPES.ADDRESS_CHANGED:
3441
3443
  case MESSAGE_TYPES.ADDRESS_SELECTED:
@@ -3486,7 +3488,6 @@ var CimplifyElements = class {
3486
3488
  this.paymentData = null;
3487
3489
  this.checkoutInProgress = false;
3488
3490
  this.authInProgress = false;
3489
- this.authEstablishmentInProgress = false;
3490
3491
  this.authGeneration = 0;
3491
3492
  this.activeCheckoutAbort = null;
3492
3493
  this.businessIdResolvePromise = null;
@@ -3501,7 +3502,7 @@ var CimplifyElements = class {
3501
3502
  window.addEventListener("message", this.boundHandleMessage);
3502
3503
  }
3503
3504
  this.stopSessionSync = client.events.on(SESSION_CLAIMED, () => {
3504
- if (!this.authInProgress && !this.authEstablishmentInProgress) this.syncHostSession();
3505
+ if (!this.authInProgress) this._syncHostSession();
3505
3506
  });
3506
3507
  void this.restoreSession();
3507
3508
  }
@@ -3510,20 +3511,24 @@ var CimplifyElements = class {
3510
3511
  console.debug(...args);
3511
3512
  }
3512
3513
  }
3513
- syncHostSession(element) {
3514
- if (this.authEstablishmentInProgress) return;
3514
+ _syncHostSession(element) {
3515
3515
  const sessionToken = this.client.getSessionToken();
3516
- const token = this.client.getAccessToken() ?? this.accessToken;
3516
+ if (!sessionToken) return;
3517
+ const identity = {
3518
+ type: MESSAGE_TYPES.IDENTITY,
3519
+ sessionToken,
3520
+ accessToken: this.client.getAccessToken() ?? this.accessToken,
3521
+ customer: this.identityCustomer()
3522
+ };
3517
3523
  const targets = element ? [element] : Array.from(this.elements.values());
3518
3524
  for (const target of targets) {
3519
- if (sessionToken) {
3520
- target.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
3521
- }
3522
- if (token) {
3523
- target.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
3524
- }
3525
+ target.sendMessage(identity);
3525
3526
  }
3526
3527
  }
3528
+ identityCustomer() {
3529
+ if (!this.accessToken || !this.customerId || !this.customerData) return null;
3530
+ return { id: this.customerId, ...this.customerData };
3531
+ }
3527
3532
  create(type, options = {}) {
3528
3533
  const existing = this.elements.get(type);
3529
3534
  if (existing) return existing;
@@ -3542,11 +3547,7 @@ var CimplifyElements = class {
3542
3547
  }
3543
3548
  destroy() {
3544
3549
  this.authGeneration += 1;
3545
- if (this.authEstablishmentInProgress) {
3546
- this.client.setAccessToken(null);
3547
- }
3548
3550
  this.authInProgress = false;
3549
- this.authEstablishmentInProgress = false;
3550
3551
  this.stopSessionSync();
3551
3552
  this.stopSessionSync = () => {
3552
3553
  };
@@ -3606,17 +3607,6 @@ var CimplifyElements = class {
3606
3607
  );
3607
3608
  }
3608
3609
  this.checkoutInProgress = true;
3609
- if (this.authEstablishmentInProgress) {
3610
- await this.waitForAuthEstablishment(AUTH_ESTABLISHMENT_WAIT_MS);
3611
- if (this.authEstablishmentInProgress) {
3612
- this.checkoutInProgress = false;
3613
- return toCheckoutError(
3614
- chunkAVXA4F6G_js.ErrorCode.AUTH_INCOMPLETE,
3615
- "Authentication must finish establishing the checkout session.",
3616
- true
3617
- );
3618
- }
3619
- }
3620
3610
  if (!options.cart_id && !options.intent_id) {
3621
3611
  this.debug("[cimplify:checkout] BLOCKED: no cart_id or intent_id");
3622
3612
  this.checkoutInProgress = false;
@@ -3821,7 +3811,7 @@ var CimplifyElements = class {
3821
3811
  hasAddress: !!processMessage.address,
3822
3812
  isUnifiedCheckout
3823
3813
  });
3824
- this.syncHostSession(checkoutElement);
3814
+ this._syncHostSession(checkoutElement);
3825
3815
  checkoutElement.sendMessage(processMessage);
3826
3816
  this.debug("[cimplify:checkout] postMessage sent, waiting for checkout_complete...");
3827
3817
  });
@@ -3932,7 +3922,6 @@ var CimplifyElements = class {
3932
3922
  this.customerData = null;
3933
3923
  this.addressData = null;
3934
3924
  this.paymentData = null;
3935
- this.authEstablishmentInProgress = false;
3936
3925
  this.client.clearSession();
3937
3926
  this.broadcastClearedSession();
3938
3927
  }
@@ -3968,14 +3957,12 @@ var CimplifyElements = class {
3968
3957
  this.broadcastClearedSession();
3969
3958
  return;
3970
3959
  }
3971
- if (application.kind !== "authenticated" || !this.broadcastAccessToken(result.value.session_token, generation)) {
3972
- return;
3973
- }
3960
+ if (application.kind !== "authenticated") return;
3961
+ this._syncHostSession();
3974
3962
  this.elements.forEach((element) => element._emitAuthenticated(application.data));
3975
3963
  }
3976
3964
  async applyAccessToken(token, generation) {
3977
3965
  if (this.authGeneration !== generation) return { kind: "incomplete" };
3978
- this.authEstablishmentInProgress = true;
3979
3966
  this.accessToken = null;
3980
3967
  this.accountId = null;
3981
3968
  this.customerId = null;
@@ -3983,7 +3970,7 @@ var CimplifyElements = class {
3983
3970
  this.addressData = null;
3984
3971
  this.paymentData = null;
3985
3972
  this.client.setAccessToken(token);
3986
- const claim = await this.client.support.claimConversation();
3973
+ const claim = await this.client.session.claim();
3987
3974
  if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
3988
3975
  if (!claim.ok && claim.error.code !== "already_claimed") {
3989
3976
  if (this.client.hasClaimedSessionAuthority()) {
@@ -3991,7 +3978,6 @@ var CimplifyElements = class {
3991
3978
  } else {
3992
3979
  this.client.setAccessToken(null);
3993
3980
  }
3994
- this.authEstablishmentInProgress = false;
3995
3981
  return {
3996
3982
  kind: "claim_failed",
3997
3983
  code: claim.error.code,
@@ -4004,7 +3990,6 @@ var CimplifyElements = class {
4004
3990
  return { kind: "incomplete" };
4005
3991
  }
4006
3992
  if (!this.isAuthGenerationCurrent(generation, token)) return { kind: "incomplete" };
4007
- this.authEstablishmentInProgress = false;
4008
3993
  return {
4009
3994
  kind: "authenticated",
4010
3995
  data: {
@@ -4015,24 +4000,30 @@ var CimplifyElements = class {
4015
4000
  }
4016
4001
  };
4017
4002
  }
4018
- async waitForAuthEstablishment(timeoutMs) {
4019
- const deadline = Date.now() + timeoutMs;
4020
- while (this.authEstablishmentInProgress && Date.now() < deadline) {
4021
- await new Promise((resolve) => setTimeout(resolve, 100));
4022
- }
4023
- }
4024
- broadcastAccessToken(token, generation) {
4025
- if (!this.isAuthGenerationCurrent(generation, token) || this.accessToken !== token || this.authEstablishmentInProgress) {
4026
- return false;
4027
- }
4028
- const sessionToken = this.client.getSessionToken();
4029
- this.elements.forEach((element) => {
4030
- if (sessionToken) {
4031
- element.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
4032
- }
4033
- element.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
4003
+ adoptEstablishedSession(sourceElement, session) {
4004
+ this.authGeneration += 1;
4005
+ this.authInProgress = false;
4006
+ const { id, ...customer } = session.customer;
4007
+ this.accessToken = session.accessToken;
4008
+ this.accountId = decodeJwtSub(session.accessToken);
4009
+ this.customerId = id;
4010
+ this.customerData = customer;
4011
+ this.addressData = null;
4012
+ this.paymentData = null;
4013
+ this.client.setAccessToken(session.accessToken);
4014
+ this.persistRefreshToken(session.refreshToken);
4015
+ this.client.session.adopt(session.accessToken, {
4016
+ sessionToken: session.sessionToken,
4017
+ customerName: session.customer.name,
4018
+ customer: session.customer,
4019
+ cart: null
4020
+ });
4021
+ sourceElement._emitAuthenticated({
4022
+ accountId: this.accountId,
4023
+ customerId: id,
4024
+ token: session.accessToken,
4025
+ customer
4034
4026
  });
4035
- return true;
4036
4027
  }
4037
4028
  broadcastClearedSession() {
4038
4029
  const sessionToken = this.client.getSessionToken();
@@ -4079,7 +4070,8 @@ var CimplifyElements = class {
4079
4070
  return;
4080
4071
  }
4081
4072
  const application = await this.applyAccessToken(accessToken, generation);
4082
- if (application.kind === "authenticated" && this.broadcastAccessToken(accessToken, generation)) {
4073
+ if (application.kind === "authenticated") {
4074
+ this._syncHostSession();
4083
4075
  sourceElement._emitAuthenticated(application.data);
4084
4076
  } else if (application.kind === "claim_failed") {
4085
4077
  sourceElement._emitError({
@@ -4118,29 +4110,7 @@ var CimplifyElements = class {
4118
4110
  this.startEmbeddedAuth(sourceElement, message);
4119
4111
  break;
4120
4112
  case MESSAGE_TYPES.SESSION_ESTABLISHED:
4121
- {
4122
- this.authInProgress = false;
4123
- const generation = ++this.authGeneration;
4124
- void this.applyAccessToken(message.accessToken, generation).then((application) => {
4125
- if (this.authGeneration !== generation) return;
4126
- if (application.kind === "claim_failed") {
4127
- this.clearPersistedSession();
4128
- sourceElement.sendMessage({
4129
- type: MESSAGE_TYPES.SESSION_ESTABLISHMENT_FAILED,
4130
- accessToken: message.accessToken,
4131
- code: application.code,
4132
- message: application.message
4133
- });
4134
- this.broadcastClearedSession();
4135
- return;
4136
- }
4137
- if (application.kind !== "authenticated" || !this.broadcastAccessToken(message.accessToken, generation)) {
4138
- return;
4139
- }
4140
- this.persistRefreshToken(message.refreshToken);
4141
- sourceElement._emitAuthenticated(application.data);
4142
- });
4143
- }
4113
+ this.adoptEstablishedSession(sourceElement, message);
4144
4114
  break;
4145
4115
  case MESSAGE_TYPES.ADDRESS_CHANGED:
4146
4116
  case MESSAGE_TYPES.ADDRESS_SELECTED:
@@ -4162,7 +4132,6 @@ var CimplifyElements = class {
4162
4132
  case MESSAGE_TYPES.CONTINUE_WITHOUT_SAVED_DETAILS: {
4163
4133
  this.authGeneration += 1;
4164
4134
  this.authInProgress = false;
4165
- this.authEstablishmentInProgress = false;
4166
4135
  const carriedIdentityAuthority = Boolean(this.client.getAccessToken()) || this.client.hasClaimedSessionAuthority();
4167
4136
  this.accessToken = null;
4168
4137
  this.accountId = null;
@@ -4187,7 +4156,6 @@ var CimplifyElements = class {
4187
4156
  case MESSAGE_TYPES.LOGOUT_COMPLETE:
4188
4157
  this.authGeneration += 1;
4189
4158
  this.authInProgress = false;
4190
- this.authEstablishmentInProgress = false;
4191
4159
  if (this.checkoutInProgress && this.activeCheckoutAbort) {
4192
4160
  this.activeCheckoutAbort();
4193
4161
  }
@@ -4436,14 +4404,7 @@ var CimplifyElement = class {
4436
4404
  hostedSessionId: this.options.hostedSessionId,
4437
4405
  initialTerms: this.options.initialTerms
4438
4406
  });
4439
- const token = this.parent.getAccessToken();
4440
- const sessionToken = this.parent.getSessionToken();
4441
- if (sessionToken) {
4442
- this.sendMessage({ type: MESSAGE_TYPES.SET_SESSION, sessionToken });
4443
- }
4444
- if (token) {
4445
- this.sendMessage({ type: MESSAGE_TYPES.SET_TOKEN, token });
4446
- }
4407
+ this.parent._syncHostSession(this);
4447
4408
  this.pendingInit = null;
4448
4409
  }
4449
4410
  this.flushPending();