// @ts-check // Pure decision logic, kept separate from the run.js adapter so it's // unit-testable directly with plain Vitest (see // tests/unit/payment-customization.test.ts at the repo root) with no WASM // build. // // Study §5.2 / DS parity: "payment options can be adjusted based on the // fulfillment method chosen — e.g. hiding cash-on-delivery for a shipped // order." A shipped order leaves the store before it's paid in person, so // cash-on-delivery / pay-in-store gateways make no sense for it; pickup and // local delivery keep every method. /** Matches the common names Shopify merchants give manual "pay later / in person" gateways. */ export const DEFAULT_HIDE_PATTERN = /cash on delivery|\bc\.?o\.?d\.?\b|pay on delivery|pay on pickup|pay in store|pay in person/i; /** * @param {Record} attributes The cart's dd_* attributes. * @param {string[]} paymentMethodNames Names of every payment method offered at checkout. * @param {{ pattern?: RegExp }} [options] * @returns {string[]} the subset of `paymentMethodNames` that should be hidden. */ export function paymentMethodNamesToHide(attributes, paymentMethodNames, options = {}) { const pattern = options.pattern ?? DEFAULT_HIDE_PATTERN; // Only shipped orders lose the pay-in-person options. No selection yet, or // pickup/local delivery → touch nothing. if (attributes.dd_method !== "SHIPPING") return []; return paymentMethodNames.filter((name) => typeof name === "string" && pattern.test(name)); }