@sunbay/checkout-sdk-js 0.1.1 → 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.mjs CHANGED
@@ -422,6 +422,10 @@ var CheckoutElement = class _CheckoutElement {
422
422
  this.abortMount = null;
423
423
  /** expressCheckout only: the persistently subscribed expressAvailabilityChange handler (unbound on destroy). */
424
424
  this.availabilityHandler = null;
425
+ /** Input Elements only: the last `change` snapshot, tracked from construction (never replayed to `on`). */
426
+ this.state = { empty: true, complete: false };
427
+ /** Input Elements only: the persistently subscribed `change` handler (unbound on destroy). */
428
+ this.stateHandler = null;
425
429
  /** Merchant listeners registered via `on`: event → (original handler → transport wrapper). Cleared on destroy. */
426
430
  this.userListeners = /* @__PURE__ */ new Map();
427
431
  /**
@@ -445,6 +449,7 @@ var CheckoutElement = class _CheckoutElement {
445
449
  this.onDestroy = opts.onDestroy;
446
450
  this.installInputActions();
447
451
  this.installExpressAvailability();
452
+ this.installInputState();
448
453
  }
449
454
  /** The current status (read-only snapshot, for external/test observation). */
450
455
  getStatus() {
@@ -462,6 +467,21 @@ var CheckoutElement = class _CheckoutElement {
462
467
  getAvailableWallets() {
463
468
  return this.availableWallets;
464
469
  }
470
+ /**
471
+ * Input Elements only: the last known `change` state (`empty`, `complete`, `brand?`, `error?`).
472
+ *
473
+ * `change` events are not replayed, so a handler registered after `mount()` resolves can miss
474
+ * changes (e.g. browser autofill). Read this to get the current value instead; it is tracked from
475
+ * construction. Starts as `{ empty: true, complete: false }`. Never contains values.
476
+ *
477
+ * @returns A copy of the snapshot; undefined for `expressCheckout`. Still readable after destroy.
478
+ */
479
+ getState() {
480
+ if (!INPUT_TYPES.includes(this.type)) {
481
+ return void 0;
482
+ }
483
+ return { ...this.state };
484
+ }
465
485
  /**
466
486
  * Mount at the merchant DOM target: insert a visible iframe, send createElement, await elementReady.
467
487
  *
@@ -678,6 +698,10 @@ var CheckoutElement = class _CheckoutElement {
678
698
  this.transport.off("expressAvailabilityChange", this.availabilityHandler);
679
699
  this.availabilityHandler = null;
680
700
  }
701
+ if (this.stateHandler) {
702
+ this.transport.off("change", this.stateHandler);
703
+ this.stateHandler = null;
704
+ }
681
705
  for (const [event, wrappers] of this.userListeners) {
682
706
  for (const wrapper of wrappers.values()) {
683
707
  this.transport.off(event, wrapper);
@@ -789,6 +813,31 @@ var CheckoutElement = class _CheckoutElement {
789
813
  this.availabilityHandler = handler;
790
814
  this.transport.on("expressAvailabilityChange", handler);
791
815
  }
816
+ /** Input Elements only: keep the `change` snapshot current so `getState()` works without a listener. */
817
+ installInputState() {
818
+ if (!INPUT_TYPES.includes(this.type)) {
819
+ return;
820
+ }
821
+ const handler = (payload) => {
822
+ const change = payload;
823
+ if (!change || change.elementId !== this.elementId) {
824
+ return;
825
+ }
826
+ const next = {
827
+ empty: change.empty === true,
828
+ complete: change.complete === true
829
+ };
830
+ if (change.brand !== void 0) {
831
+ next.brand = change.brand;
832
+ }
833
+ if (change.error !== void 0) {
834
+ next.error = change.error;
835
+ }
836
+ this.state = next;
837
+ };
838
+ this.stateHandler = handler;
839
+ this.transport.on("change", handler);
840
+ }
792
841
  /** Send one input-class action command (synchronously throws INSTANCE_DESTROYED after destruction). */
793
842
  sendAction(name) {
794
843
  this.assertAlive();
@@ -1149,6 +1198,35 @@ var CheckoutInstance = class {
1149
1198
  this.session = resp;
1150
1199
  return resp;
1151
1200
  }
1201
+ /**
1202
+ * The session's payment methods split into quick wallets (`express`) and everything else
1203
+ * (`regular`), from the local session snapshot (already filtered by `paymentMethods`).
1204
+ *
1205
+ * Each express entry carries `available` from the `expressCheckout` Element's availability
1206
+ * snapshot; it is `undefined` while no such Element exists or it has not reported yet.
1207
+ *
1208
+ * @returns The split view.
1209
+ * @throws INSTANCE_DESTROYED when destroyed.
1210
+ */
1211
+ getPaymentMethods() {
1212
+ this.assertAlive();
1213
+ let wallets;
1214
+ for (const element of this.elements.values()) {
1215
+ if (element.type === "expressCheckout") {
1216
+ wallets = element.getAvailableWallets();
1217
+ }
1218
+ }
1219
+ const view = { express: [], regular: [] };
1220
+ for (const method of this.session.paymentMethods) {
1221
+ if (method.method === "APPLE_PAY" || method.method === "GOOGLE_PAY") {
1222
+ const available = wallets === void 0 ? void 0 : method.method === "APPLE_PAY" ? wallets.applePay : wallets.googlePay;
1223
+ view.express.push({ ...method, available });
1224
+ } else {
1225
+ view.regular.push({ ...method });
1226
+ }
1227
+ }
1228
+ return view;
1229
+ }
1152
1230
  /**
1153
1231
  * Refresh the session (passes through the refresh command and writes back the local snapshot).
1154
1232
  *
@@ -1455,7 +1533,7 @@ if (typeof window !== "undefined") {
1455
1533
  const w = window;
1456
1534
  w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
1457
1535
  }
1458
- var version = true ? "0.1.1" : "0.0.0";
1536
+ var version = true ? "0.2.0" : "0.0.0";
1459
1537
  export {
1460
1538
  CheckoutElement,
1461
1539
  CheckoutInstance,