@sunbay/checkout-sdk-js 0.1.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.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
  /**
@@ -440,10 +444,12 @@ var CheckoutElement = class _CheckoutElement {
440
444
  this.options = opts.initialOptions;
441
445
  this.colorScheme = opts.colorScheme;
442
446
  this.appearance = opts.appearance;
447
+ this.locale = opts.locale;
443
448
  this.mode = opts.initialOptions?.mode;
444
449
  this.onDestroy = opts.onDestroy;
445
450
  this.installInputActions();
446
451
  this.installExpressAvailability();
452
+ this.installInputState();
447
453
  }
448
454
  /** The current status (read-only snapshot, for external/test observation). */
449
455
  getStatus() {
@@ -461,6 +467,21 @@ var CheckoutElement = class _CheckoutElement {
461
467
  getAvailableWallets() {
462
468
  return this.availableWallets;
463
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
+ }
464
485
  /**
465
486
  * Mount at the merchant DOM target: insert a visible iframe, send createElement, await elementReady.
466
487
  *
@@ -677,6 +698,10 @@ var CheckoutElement = class _CheckoutElement {
677
698
  this.transport.off("expressAvailabilityChange", this.availabilityHandler);
678
699
  this.availabilityHandler = null;
679
700
  }
701
+ if (this.stateHandler) {
702
+ this.transport.off("change", this.stateHandler);
703
+ this.stateHandler = null;
704
+ }
680
705
  for (const [event, wrappers] of this.userListeners) {
681
706
  for (const wrapper of wrappers.values()) {
682
707
  this.transport.off(event, wrapper);
@@ -788,6 +813,31 @@ var CheckoutElement = class _CheckoutElement {
788
813
  this.availabilityHandler = handler;
789
814
  this.transport.on("expressAvailabilityChange", handler);
790
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
+ }
791
841
  /** Send one input-class action command (synchronously throws INSTANCE_DESTROYED after destruction). */
792
842
  sendAction(name) {
793
843
  this.assertAlive();
@@ -817,7 +867,8 @@ var CheckoutElement = class _CheckoutElement {
817
867
  * (no flash on the first frame); if the initial appearance contains variables or rules, append
818
868
  * `&appearance=<encodeURIComponent(JSON.stringify(appearance))>` (takes effect on the first
819
869
  * frame, no flash). An appearance containing only colorScheme (no variables/rules) is not
820
- * appended, to avoid an empty appearance string. For expressCheckout, additionally append the
870
+ * appended, to avoid an empty appearance string. If an initial locale is provided, append
871
+ * `&locale=<value>` (first frame renders in that language). For expressCheckout, additionally append the
821
872
  * wallet toggles / per-wallet buttonType / global buttonHeight (see {@link resolveExpressQuery}).
822
873
  *
823
874
  * elementId is sent with the src so the hosted child echoes it verbatim in iframe-resize
@@ -840,6 +891,9 @@ var CheckoutElement = class _CheckoutElement {
840
891
  if (this.colorScheme) {
841
892
  query += `&colorScheme=${encodeURIComponent(this.colorScheme)}`;
842
893
  }
894
+ if (this.locale) {
895
+ query += `&locale=${encodeURIComponent(this.locale)}`;
896
+ }
843
897
  const effectiveAppearance = this.perElementAppearance() ?? this.appearance;
844
898
  if (effectiveAppearance && (effectiveAppearance.variables || effectiveAppearance.rules)) {
845
899
  query += `&appearance=${encodeURIComponent(JSON.stringify(effectiveAppearance))}`;
@@ -995,6 +1049,7 @@ var CheckoutInstance = class {
995
1049
  this.transport = opts.transport;
996
1050
  this.session = opts.session;
997
1051
  this.appearance = opts.appearance;
1052
+ this.locale = opts.locale;
998
1053
  this.paymentResultHandler = (payload) => {
999
1054
  this.handlePaymentResult(payload);
1000
1055
  };
@@ -1026,6 +1081,7 @@ var CheckoutInstance = class {
1026
1081
  initialOptions: options,
1027
1082
  colorScheme: this.appearance?.colorScheme,
1028
1083
  appearance: this.appearance,
1084
+ locale: this.locale,
1029
1085
  onDestroy: (id) => this.releaseElement(id)
1030
1086
  };
1031
1087
  const element = CheckoutElement.create(elementOpts);
@@ -1142,6 +1198,35 @@ var CheckoutInstance = class {
1142
1198
  this.session = resp;
1143
1199
  return resp;
1144
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
+ }
1145
1230
  /**
1146
1231
  * Refresh the session (passes through the refresh command and writes back the local snapshot).
1147
1232
  *
@@ -1414,7 +1499,8 @@ async function createCheckout(opts) {
1414
1499
  capabilities: initResp.capabilities,
1415
1500
  session: initResp.session,
1416
1501
  initialWarnings: initResp.warnings,
1417
- appearance: opts.appearance
1502
+ appearance: opts.appearance,
1503
+ locale: opts.locale
1418
1504
  });
1419
1505
  }
1420
1506
  function waitForHello(transport) {
@@ -1447,7 +1533,7 @@ if (typeof window !== "undefined") {
1447
1533
  const w = window;
1448
1534
  w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
1449
1535
  }
1450
- var version = true ? "0.1.0" : "0.0.0";
1536
+ var version = true ? "0.2.0" : "0.0.0";
1451
1537
  export {
1452
1538
  CheckoutElement,
1453
1539
  CheckoutInstance,