@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/README.md CHANGED
@@ -59,7 +59,7 @@ with a CSS selector or an empty `HTMLElement`.
59
59
  | `expressCheckout` | `paymentMethods.applePay` / `googlePay` (`false` hides; or `{ buttonTheme, buttonType }`), `layout.maxColumns`, `buttonHeight` (30-64, default 40) | Singleton. Wallet buttons. |
60
60
 
61
61
  Handle methods: `mount`, `unmount`, `update(patch)`, `destroy`, `getStatus`,
62
- `getAvailableWallets` (express), `on`, `off`. A conflicting `create` throws `ELEMENT_CONFLICT`.
62
+ `getAvailableWallets` (express), `getState` (inputs), `on`, `off`. A conflicting `create` throws `ELEMENT_CONFLICT`.
63
63
 
64
64
  ## Events
65
65
 
@@ -177,7 +177,7 @@ challenges: it only emits the event. Your page must handle the action.
177
177
  (`RUNTIME_LOAD_FAILED` / `INSTANCE_DESTROYED`). If you call `mount()` without
178
178
  `catch`/`await`, that surfaces as an **unhandled rejection**; always handle it.
179
179
  - `checkout.updateCheckout({ locale?, appearance?, billingAddress?, shippingAddress? })` hot-switches locale/appearance and replaces the host-provided addresses.
180
- - `getSession()`, `refresh()`, `getPaymentStatus()` read session/payment state.
180
+ - `getSession()`, `refresh()`, `getPaymentStatus()` read session/payment state. `getPaymentMethods()` splits the methods into `express` (wallets, with device `available`) and `regular`.
181
181
  - `checkout.destroy()` is idempotent, removes all iframes, and later calls reject/throw
182
182
  `INSTANCE_DESTROYED`. Calling `destroy()` while a `confirm()` is pending rejects that
183
183
  `confirm()` with `INSTANCE_DESTROYED`.
package/dist/index.cjs CHANGED
@@ -445,6 +445,10 @@ var CheckoutElement = class _CheckoutElement {
445
445
  this.abortMount = null;
446
446
  /** expressCheckout only: the persistently subscribed expressAvailabilityChange handler (unbound on destroy). */
447
447
  this.availabilityHandler = null;
448
+ /** Input Elements only: the last `change` snapshot, tracked from construction (never replayed to `on`). */
449
+ this.state = { empty: true, complete: false };
450
+ /** Input Elements only: the persistently subscribed `change` handler (unbound on destroy). */
451
+ this.stateHandler = null;
448
452
  /** Merchant listeners registered via `on`: event → (original handler → transport wrapper). Cleared on destroy. */
449
453
  this.userListeners = /* @__PURE__ */ new Map();
450
454
  /**
@@ -463,10 +467,12 @@ var CheckoutElement = class _CheckoutElement {
463
467
  this.options = opts.initialOptions;
464
468
  this.colorScheme = opts.colorScheme;
465
469
  this.appearance = opts.appearance;
470
+ this.locale = opts.locale;
466
471
  this.mode = opts.initialOptions?.mode;
467
472
  this.onDestroy = opts.onDestroy;
468
473
  this.installInputActions();
469
474
  this.installExpressAvailability();
475
+ this.installInputState();
470
476
  }
471
477
  /** The current status (read-only snapshot, for external/test observation). */
472
478
  getStatus() {
@@ -484,6 +490,21 @@ var CheckoutElement = class _CheckoutElement {
484
490
  getAvailableWallets() {
485
491
  return this.availableWallets;
486
492
  }
493
+ /**
494
+ * Input Elements only: the last known `change` state (`empty`, `complete`, `brand?`, `error?`).
495
+ *
496
+ * `change` events are not replayed, so a handler registered after `mount()` resolves can miss
497
+ * changes (e.g. browser autofill). Read this to get the current value instead; it is tracked from
498
+ * construction. Starts as `{ empty: true, complete: false }`. Never contains values.
499
+ *
500
+ * @returns A copy of the snapshot; undefined for `expressCheckout`. Still readable after destroy.
501
+ */
502
+ getState() {
503
+ if (!INPUT_TYPES.includes(this.type)) {
504
+ return void 0;
505
+ }
506
+ return { ...this.state };
507
+ }
487
508
  /**
488
509
  * Mount at the merchant DOM target: insert a visible iframe, send createElement, await elementReady.
489
510
  *
@@ -700,6 +721,10 @@ var CheckoutElement = class _CheckoutElement {
700
721
  this.transport.off("expressAvailabilityChange", this.availabilityHandler);
701
722
  this.availabilityHandler = null;
702
723
  }
724
+ if (this.stateHandler) {
725
+ this.transport.off("change", this.stateHandler);
726
+ this.stateHandler = null;
727
+ }
703
728
  for (const [event, wrappers] of this.userListeners) {
704
729
  for (const wrapper of wrappers.values()) {
705
730
  this.transport.off(event, wrapper);
@@ -811,6 +836,31 @@ var CheckoutElement = class _CheckoutElement {
811
836
  this.availabilityHandler = handler;
812
837
  this.transport.on("expressAvailabilityChange", handler);
813
838
  }
839
+ /** Input Elements only: keep the `change` snapshot current so `getState()` works without a listener. */
840
+ installInputState() {
841
+ if (!INPUT_TYPES.includes(this.type)) {
842
+ return;
843
+ }
844
+ const handler = (payload) => {
845
+ const change = payload;
846
+ if (!change || change.elementId !== this.elementId) {
847
+ return;
848
+ }
849
+ const next = {
850
+ empty: change.empty === true,
851
+ complete: change.complete === true
852
+ };
853
+ if (change.brand !== void 0) {
854
+ next.brand = change.brand;
855
+ }
856
+ if (change.error !== void 0) {
857
+ next.error = change.error;
858
+ }
859
+ this.state = next;
860
+ };
861
+ this.stateHandler = handler;
862
+ this.transport.on("change", handler);
863
+ }
814
864
  /** Send one input-class action command (synchronously throws INSTANCE_DESTROYED after destruction). */
815
865
  sendAction(name) {
816
866
  this.assertAlive();
@@ -840,7 +890,8 @@ var CheckoutElement = class _CheckoutElement {
840
890
  * (no flash on the first frame); if the initial appearance contains variables or rules, append
841
891
  * `&appearance=<encodeURIComponent(JSON.stringify(appearance))>` (takes effect on the first
842
892
  * frame, no flash). An appearance containing only colorScheme (no variables/rules) is not
843
- * appended, to avoid an empty appearance string. For expressCheckout, additionally append the
893
+ * appended, to avoid an empty appearance string. If an initial locale is provided, append
894
+ * `&locale=<value>` (first frame renders in that language). For expressCheckout, additionally append the
844
895
  * wallet toggles / per-wallet buttonType / global buttonHeight (see {@link resolveExpressQuery}).
845
896
  *
846
897
  * elementId is sent with the src so the hosted child echoes it verbatim in iframe-resize
@@ -863,6 +914,9 @@ var CheckoutElement = class _CheckoutElement {
863
914
  if (this.colorScheme) {
864
915
  query += `&colorScheme=${encodeURIComponent(this.colorScheme)}`;
865
916
  }
917
+ if (this.locale) {
918
+ query += `&locale=${encodeURIComponent(this.locale)}`;
919
+ }
866
920
  const effectiveAppearance = this.perElementAppearance() ?? this.appearance;
867
921
  if (effectiveAppearance && (effectiveAppearance.variables || effectiveAppearance.rules)) {
868
922
  query += `&appearance=${encodeURIComponent(JSON.stringify(effectiveAppearance))}`;
@@ -1016,6 +1070,7 @@ var CheckoutInstance = class {
1016
1070
  this.transport = opts.transport;
1017
1071
  this.session = opts.session;
1018
1072
  this.appearance = opts.appearance;
1073
+ this.locale = opts.locale;
1019
1074
  this.paymentResultHandler = (payload) => {
1020
1075
  this.handlePaymentResult(payload);
1021
1076
  };
@@ -1047,6 +1102,7 @@ var CheckoutInstance = class {
1047
1102
  initialOptions: options,
1048
1103
  colorScheme: this.appearance?.colorScheme,
1049
1104
  appearance: this.appearance,
1105
+ locale: this.locale,
1050
1106
  onDestroy: (id) => this.releaseElement(id)
1051
1107
  };
1052
1108
  const element = CheckoutElement.create(elementOpts);
@@ -1163,6 +1219,35 @@ var CheckoutInstance = class {
1163
1219
  this.session = resp;
1164
1220
  return resp;
1165
1221
  }
1222
+ /**
1223
+ * The session's payment methods split into quick wallets (`express`) and everything else
1224
+ * (`regular`), from the local session snapshot (already filtered by `paymentMethods`).
1225
+ *
1226
+ * Each express entry carries `available` from the `expressCheckout` Element's availability
1227
+ * snapshot; it is `undefined` while no such Element exists or it has not reported yet.
1228
+ *
1229
+ * @returns The split view.
1230
+ * @throws INSTANCE_DESTROYED when destroyed.
1231
+ */
1232
+ getPaymentMethods() {
1233
+ this.assertAlive();
1234
+ let wallets;
1235
+ for (const element of this.elements.values()) {
1236
+ if (element.type === "expressCheckout") {
1237
+ wallets = element.getAvailableWallets();
1238
+ }
1239
+ }
1240
+ const view = { express: [], regular: [] };
1241
+ for (const method of this.session.paymentMethods) {
1242
+ if (method.method === "APPLE_PAY" || method.method === "GOOGLE_PAY") {
1243
+ const available = wallets === void 0 ? void 0 : method.method === "APPLE_PAY" ? wallets.applePay : wallets.googlePay;
1244
+ view.express.push({ ...method, available });
1245
+ } else {
1246
+ view.regular.push({ ...method });
1247
+ }
1248
+ }
1249
+ return view;
1250
+ }
1166
1251
  /**
1167
1252
  * Refresh the session (passes through the refresh command and writes back the local snapshot).
1168
1253
  *
@@ -1432,7 +1517,8 @@ async function createCheckout(opts) {
1432
1517
  capabilities: initResp.capabilities,
1433
1518
  session: initResp.session,
1434
1519
  initialWarnings: initResp.warnings,
1435
- appearance: opts.appearance
1520
+ appearance: opts.appearance,
1521
+ locale: opts.locale
1436
1522
  });
1437
1523
  }
1438
1524
  function waitForHello(transport) {
@@ -1462,7 +1548,7 @@ if (typeof window !== "undefined") {
1462
1548
  const w = window;
1463
1549
  w.SunbayCheckout = { ...w.SunbayCheckout ?? {}, createCheckout };
1464
1550
  }
1465
- var version = true ? "0.1.0" : "0.0.0";
1551
+ var version = true ? "0.2.0" : "0.0.0";
1466
1552
  // Annotate the CommonJS export names for ESM import in node:
1467
1553
  0 && (module.exports = {
1468
1554
  CheckoutElement,