@ozura/elements 1.3.1-next.74 → 1.3.1-next.76

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.
@@ -1067,6 +1067,8 @@ class OzVault {
1067
1067
  this.bankTokenizeResolvers = new Map();
1068
1068
  // Track completion state per element for auto-advance (only fire on transition)
1069
1069
  this.completionState = new Map();
1070
+ // Latest per-field state for getFieldStates(), keyed by frameId.
1071
+ this.fieldStates = new Map();
1070
1072
  this.tokenizerFrame = null;
1071
1073
  this.tokenizerWindow = null;
1072
1074
  this.tokenizerReady = false;
@@ -1282,6 +1284,24 @@ class OzVault {
1282
1284
  return false;
1283
1285
  return els.every(el => this.completionState.get(el.frameId) === true);
1284
1286
  }
1287
+ /**
1288
+ * Snapshot of every created field's latest state, keyed by element type.
1289
+ * Card and bank fields are combined (their type keys never collide). Useful for
1290
+ * gating UI, rendering per-field errors, or showing the card brand without
1291
+ * wiring an onChange listener per element. Returns shallow copies so callers
1292
+ * cannot mutate the vault's internal state.
1293
+ */
1294
+ getFieldStates() {
1295
+ var _a, _b;
1296
+ const snapshot = {};
1297
+ for (const [type, el] of this.elementsByType) {
1298
+ snapshot[type] = Object.assign({}, ((_a = this.fieldStates.get(el.frameId)) !== null && _a !== void 0 ? _a : { empty: true, complete: false, valid: false }));
1299
+ }
1300
+ for (const [type, el] of this.bankElementsByType) {
1301
+ snapshot[type] = Object.assign({}, ((_b = this.fieldStates.get(el.frameId)) !== null && _b !== void 0 ? _b : { empty: true, complete: false, valid: false }));
1302
+ }
1303
+ return snapshot;
1304
+ }
1285
1305
  /**
1286
1306
  * `true` while a `createToken()` or `createBankToken()` call is in progress
1287
1307
  * (including the transparent wax-key refresh phase). Use this to keep the pay
@@ -1334,6 +1354,7 @@ class OzVault {
1334
1354
  if (existing) {
1335
1355
  this.elements.delete(existing.frameId);
1336
1356
  this.completionState.delete(existing.frameId);
1357
+ this.fieldStates.delete(existing.frameId);
1337
1358
  existing.destroy();
1338
1359
  }
1339
1360
  const el = new OzElement(type, options, this.vaultId, this.frameBaseUrl, this.fonts, this.resolvedAppearance, () => {
@@ -1341,9 +1362,11 @@ class OzVault {
1341
1362
  // don't grow unboundedly in SPA scenarios with repeated mount/unmount cycles.
1342
1363
  this.elements.delete(el.frameId);
1343
1364
  this.completionState.delete(el.frameId);
1365
+ this.fieldStates.delete(el.frameId);
1344
1366
  }, this._debug);
1345
1367
  this.elements.set(el.frameId, el);
1346
1368
  typeMap.set(type, el);
1369
+ this.fieldStates.set(el.frameId, { empty: true, complete: false, valid: false });
1347
1370
  return el;
1348
1371
  }
1349
1372
  /**
@@ -1643,6 +1666,10 @@ class OzVault {
1643
1666
  for (const frameId of this.completionState.keys()) {
1644
1667
  this.completionState.set(frameId, false);
1645
1668
  }
1669
+ // Mirror for field states: every created field returns to its default.
1670
+ for (const frameId of this.fieldStates.keys()) {
1671
+ this.fieldStates.set(frameId, { empty: true, complete: false, valid: false });
1672
+ }
1646
1673
  // NOTE: _tokenizeSuccessCount is intentionally NOT reset.
1647
1674
  // It reflects real server-side wax key budget consumption. Zeroing it
1648
1675
  // would desync the proactive refresh logic from the vault's state and
@@ -1690,6 +1717,7 @@ class OzVault {
1690
1717
  this.elementsByType.clear();
1691
1718
  this.bankElementsByType.clear();
1692
1719
  this.completionState.clear();
1720
+ this.fieldStates.clear();
1693
1721
  this._tokenizeSuccessCount = 0;
1694
1722
  (_a = this.tokenizerFrame) === null || _a === void 0 ? void 0 : _a.remove();
1695
1723
  this.tokenizerFrame = null;
@@ -1851,6 +1879,10 @@ class OzVault {
1851
1879
  // the previous session and justCompleted never fires, breaking auto-advance.
1852
1880
  if (msg.type === 'OZ_FRAME_READY') {
1853
1881
  this.completionState.set(frameId, false);
1882
+ // Mirror the completion reset: a reloaded iframe starts empty, so the
1883
+ // field state must return to default rather than serving pre-reload data
1884
+ // (e.g. a stale card brand) until the next OZ_CHANGE arrives.
1885
+ this.fieldStates.set(frameId, { empty: true, complete: false, valid: false });
1854
1886
  if (msg.__ozVersion !== PROTOCOL_VERSION) {
1855
1887
  console.warn(`[OzVault] Protocol version mismatch on element frame "${frameId}" — ` +
1856
1888
  `SDK expects v${PROTOCOL_VERSION}, frame reported v${typeof msg.__ozVersion === 'number' ? msg.__ozVersion : '(none)'}. ` +
@@ -1892,8 +1924,14 @@ class OzVault {
1892
1924
  var _a, _b, _c;
1893
1925
  const complete = msg.complete;
1894
1926
  const valid = msg.valid;
1927
+ // Narrow rather than blind-cast: a malformed/legacy frame that omits `empty`
1928
+ // must not write `undefined` into the public FieldState contract.
1929
+ const empty = typeof msg.empty === 'boolean' ? msg.empty : true;
1895
1930
  const wasComplete = (_a = this.completionState.get(el.frameId)) !== null && _a !== void 0 ? _a : false;
1896
1931
  this.completionState.set(el.frameId, complete && valid);
1932
+ this.fieldStates.set(el.frameId, Object.assign(Object.assign({ empty,
1933
+ complete,
1934
+ valid }, (typeof msg.error === 'string' ? { error: msg.error } : {})), (typeof msg.cardBrand === 'string' ? { cardBrand: msg.cardBrand } : {})));
1897
1935
  // Require valid too — avoids advancing at 13 digits for unknown-brand cards
1898
1936
  // where isComplete() fires before the user has finished typing.
1899
1937
  const justCompleted = complete && valid && !wasComplete;
@@ -2482,6 +2520,21 @@ function useOzElements() {
2482
2520
  const isTokenizing = (_c = vault === null || vault === void 0 ? void 0 : vault.isTokenizing) !== null && _c !== void 0 ? _c : false;
2483
2521
  return { createToken, createBankToken, reset, ready, initError, tokenizeCount, isComplete, isBankComplete, isTokenizing };
2484
2522
  }
2523
+ /**
2524
+ * Reactive snapshot of every created field's state, keyed by element type
2525
+ * (e.g. `cardNumber`, `cvv`, `accountNumber`). Each entry is
2526
+ * `{ empty, complete, valid, error?, cardBrand? }`. Recomputes whenever any
2527
+ * field fires a change event. Must be called inside an `<OzElements>` provider;
2528
+ * returns `{}` otherwise.
2529
+ *
2530
+ * @example
2531
+ * const states = useFieldStates();
2532
+ * if (states.cvv?.error) showCvvError(states.cvv.error);
2533
+ */
2534
+ function useFieldStates() {
2535
+ const { vault, changeTick } = react.useContext(OzContext);
2536
+ return react.useMemo(() => { var _a; return (_a = vault === null || vault === void 0 ? void 0 : vault.getFieldStates()) !== null && _a !== void 0 ? _a : {}; }, [vault, changeTick]);
2537
+ }
2485
2538
  const SKELETON_STYLE = {
2486
2539
  height: 46,
2487
2540
  borderRadius: 6,
@@ -2789,5 +2842,6 @@ exports.OzElements = OzElements;
2789
2842
  exports.OzExpiry = OzExpiry;
2790
2843
  exports.createFetchWaxKey = createSessionFetcher;
2791
2844
  exports.createSessionFetcher = createSessionFetcher;
2845
+ exports.useFieldStates = useFieldStates;
2792
2846
  exports.useOzElements = useOzElements;
2793
2847
  //# sourceMappingURL=index.cjs.js.map