@final-commerce/common 2.11.1-online.7 → 2.11.1-online.8

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
@@ -1847,6 +1847,7 @@ var FULFILLMENT_STATES = [
1847
1847
  "in_progress",
1848
1848
  "fulfilled",
1849
1849
  "partially_fulfilled",
1850
+ "return_pending",
1850
1851
  "returned",
1851
1852
  "partially_returned",
1852
1853
  "cancelled"
@@ -1872,10 +1873,7 @@ var CONDITION_OPERATORS = [
1872
1873
  function isConditionOperator(value) {
1873
1874
  return CONDITION_OPERATORS.includes(value);
1874
1875
  }
1875
- var PATH_MODES = [
1876
- "strict",
1877
- "permissive"
1878
- ];
1876
+ var PATH_MODES = ["strict", "permissive"];
1879
1877
 
1880
1878
  // order-state-machine/state-schema.ts
1881
1879
  var PAYMENT_DEFS = [
@@ -1896,6 +1894,9 @@ var FULFILLMENT_DEFS = [
1896
1894
  { id: "in_progress", label: "In Progress", terminal: false },
1897
1895
  { id: "fulfilled", label: "Fulfilled", terminal: true },
1898
1896
  { id: "partially_fulfilled", label: "Partially Fulfilled", terminal: false },
1897
+ // The store accepted the shopper's return request: the goods are on their way back, no money has moved yet.
1898
+ // The refund lands `partially_returned` / `returned` as it does from `fulfilled`.
1899
+ { id: "return_pending", label: "Return Accepted", terminal: false },
1899
1900
  { id: "returned", label: "Returned", terminal: true },
1900
1901
  { id: "partially_returned", label: "Partially Returned", terminal: false },
1901
1902
  { id: "cancelled", label: "Cancelled", terminal: true }
@@ -1925,6 +1926,8 @@ var STATE_SCHEMA = Object.freeze({
1925
1926
  var REFUND_ALLOWED_FULFILLMENT = /* @__PURE__ */ new Set(["fulfilled", "returned", "partially_returned", "cancelled"]);
1926
1927
  var PAYMENT_COLLECTION_ORDER = ["unpaid", "partially_paid", "paid"];
1927
1928
  var UNRETURNED_MONEY_STATES = /* @__PURE__ */ new Set(["partially_paid", "paid", "partially_refunded"]);
1929
+ var RETURN_FROM_FULFILLMENT = /* @__PURE__ */ new Set(["fulfilled", "partially_fulfilled", "partially_returned"]);
1930
+ var RETURN_PENDING_PAYMENTS = /* @__PURE__ */ new Set(["paid", "partially_refunded"]);
1928
1931
  var REFUNDED_FULFILLMENT_LANDINGS = /* @__PURE__ */ new Set(["returned", "cancelled"]);
1929
1932
  var FINANCIAL_INVARIANTS = [
1930
1933
  {
@@ -1972,6 +1975,11 @@ var FINANCIAL_INVARIANTS = [
1972
1975
  description: "A voided order exists only at cancelled fulfillment: a voided order cannot re-enter fulfillment, and a voided \xD7 non-cancelled pair cannot be materialized from nothing (complements no-leave-voided, which only freezes the payment axis). Moves INTO voided from a live payment state are the void flow\u2019s concern, not this rule\u2019s",
1973
1976
  check: (from, to) => (from === null || from.payment === "voided") && to.payment === "voided" && to.fulfillment !== "cancelled"
1974
1977
  },
1978
+ {
1979
+ id: "return-pending-needs-goods-out",
1980
+ description: "Fulfillment enters return_pending (return accepted) only from fulfilled, partially_fulfilled or partially_returned, and holds it only while payment is paid or partially_refunded \u2014 goods must have gone out and money must still be held",
1981
+ check: (from, to) => to.fulfillment === "return_pending" && (!RETURN_PENDING_PAYMENTS.has(to.payment) || from?.fulfillment !== "return_pending" && !(from && RETURN_FROM_FULFILLMENT.has(from.fulfillment)))
1982
+ },
1975
1983
  {
1976
1984
  id: "no-reopen-refunded-fulfillment",
1977
1985
  description: "A fully refunded order cannot re-enter fulfillment \u2014 it may only relabel between its canonical landings, returned and cancelled (complements no-leave-refunded, which only freezes the payment axis)",
@@ -2063,7 +2071,9 @@ var DISPLAY_LABEL_OVERRIDES = {
2063
2071
  "paid|on_hold": "Parked - Paid",
2064
2072
  "paid|fulfilled": "Completed",
2065
2073
  "partially_refunded|fulfilled": "Partially Refunded",
2066
- "partially_refunded|partially_returned": "Partially Refunded"
2074
+ "partially_refunded|partially_returned": "Partially Refunded",
2075
+ "paid|return_pending": "Return Accepted",
2076
+ "partially_refunded|return_pending": "Return Accepted"
2067
2077
  };
2068
2078
  var TERMINAL_LABEL_OVERRIDES = {
2069
2079
  voided: "Cancelled"
@@ -2205,6 +2215,7 @@ function buildOrderContext(order, extras) {
2205
2215
  }
2206
2216
 
2207
2217
  // order-state-machine/display-state.ts
2218
+ var ATOMS_ADDED_AFTER_SNAPSHOT = /* @__PURE__ */ new Set(["return_pending"]);
2208
2219
  function atomLabel(atom, defs) {
2209
2220
  return defs.find((d) => d.id === atom)?.label ?? atom;
2210
2221
  }
@@ -2218,6 +2229,10 @@ function deriveDisplayState(payment, fulfillment, config) {
2218
2229
  return { label: rule.label, color: rule.color, icon: rule.icon };
2219
2230
  }
2220
2231
  }
2232
+ if (ATOMS_ADDED_AFTER_SNAPSHOT.has(fulfillment)) {
2233
+ const fallback = DEFAULT_DISPLAY_STATE_MAP.find((rule) => displayRuleMatchesPair(rule, payment, fulfillment));
2234
+ if (fallback) return { label: fallback.label, color: fallback.color, icon: fallback.icon };
2235
+ }
2221
2236
  return {
2222
2237
  label: `${atomLabel(payment, STATE_SCHEMA.paymentStates)} / ${atomLabel(fulfillment, STATE_SCHEMA.fulfillmentStates)}`
2223
2238
  };
@@ -4278,12 +4293,38 @@ function cardFromProcessorData(processorData) {
4278
4293
  const text = (...keys) => keys.map((key) => fields.get(key)).find((value) => typeof value === "string" && !!value.trim());
4279
4294
  const brand = text("Brand", "brand")?.trim();
4280
4295
  const digits = text("Card Number", "card", "card_number")?.replace(/\D/g, "");
4296
+ const issuer = text("Issuer", "issuer")?.trim();
4281
4297
  return {
4282
4298
  ...brand ? { cardBrand: brand.charAt(0).toUpperCase() + brand.slice(1) } : {},
4283
- ...digits && digits.length >= 4 ? { cardLastFour: digits.slice(-4) } : {}
4299
+ ...digits && digits.length >= 4 ? { cardLastFour: digits.slice(-4) } : {},
4300
+ ...issuer ? { cardIssuer: issuer } : {}
4284
4301
  };
4285
4302
  }
4286
4303
 
4304
+ // shopper/payments.ts
4305
+ var CARD_TYPES = /* @__PURE__ */ new Set(["card", "terminal", "taptopay", "moto", "cloud", "bluetooth"]);
4306
+ function kindOf(paymentType) {
4307
+ if (paymentType === "cash") return "cash";
4308
+ if (paymentType === "redeem") return "gift_card";
4309
+ if (paymentType === "pay_later") return "pay_later";
4310
+ if (CARD_TYPES.has(paymentType)) return "card";
4311
+ return "other";
4312
+ }
4313
+ function shopperPaymentsOf(legs) {
4314
+ return (legs ?? []).map((leg) => {
4315
+ const type = (leg.paymentType ?? "").trim();
4316
+ const kind = kindOf(type.toLowerCase());
4317
+ const card = kind === "card" ? cardFromProcessorData(leg.emv) : {};
4318
+ return {
4319
+ kind,
4320
+ // Cards: the issuer's name, else the brand. Other tenders the store named itself keep that name.
4321
+ name: kind === "card" ? card.cardIssuer ?? card.cardBrand ?? null : kind === "other" ? type || null : null,
4322
+ lastFour: card.cardLastFour ?? null,
4323
+ amount: (leg.amount ?? 0) + (leg.tip?.amount ?? 0)
4324
+ };
4325
+ });
4326
+ }
4327
+
4287
4328
  // constants/adyen.constants.ts
4288
4329
  var ADYEN_ROLLOUT_COUNTRY_CODES = /* @__PURE__ */ new Set(["CA"]);
4289
4330
  function isAdyenRolloutCountry(alpha2) {
@@ -4628,6 +4669,7 @@ export {
4628
4669
  shipsToCountry,
4629
4670
  shopDayKey,
4630
4671
  shopDayRange,
4672
+ shopperPaymentsOf,
4631
4673
  signReportForDisplay,
4632
4674
  slotsForWindow,
4633
4675
  stringifyMinorUnits,