@aglyn/plugins-commerce 1.33.2 → 1.34.1

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.
Files changed (107) hide show
  1. package/package.json +13 -13
  2. package/src/lib/components/cart.js +3 -1
  3. package/src/lib/components/cart.js.map +1 -1
  4. package/src/lib/components/console/catalog-organization-card.component.js +82 -69
  5. package/src/lib/components/console/catalog-organization-card.component.js.map +1 -1
  6. package/src/lib/components/console/discounts-card.component.js +46 -25
  7. package/src/lib/components/console/discounts-card.component.js.map +1 -1
  8. package/src/lib/components/console/gift-cards-card.component.js +81 -55
  9. package/src/lib/components/console/gift-cards-card.component.js.map +1 -1
  10. package/src/lib/components/console/host-coupons-card.component.js +36 -25
  11. package/src/lib/components/console/host-coupons-card.component.js.map +1 -1
  12. package/src/lib/components/console/locations-card.component.js +53 -32
  13. package/src/lib/components/console/locations-card.component.js.map +1 -1
  14. package/src/lib/components/console/member-posts-card.component.js +35 -32
  15. package/src/lib/components/console/member-posts-card.component.js.map +1 -1
  16. package/src/lib/components/console/new-store-tax-question.component.d.ts +17 -0
  17. package/src/lib/components/console/new-store-tax-question.component.js +197 -0
  18. package/src/lib/components/console/new-store-tax-question.component.js.map +1 -0
  19. package/src/lib/components/console/payments-settings-card.component.js +132 -5
  20. package/src/lib/components/console/payments-settings-card.component.js.map +1 -1
  21. package/src/lib/components/console/pos/pos-api.d.ts +6 -0
  22. package/src/lib/components/console/pos/pos-api.js.map +1 -1
  23. package/src/lib/components/console/pos/pos-tax-notice.component.d.ts +21 -0
  24. package/src/lib/components/console/pos/pos-tax-notice.component.js +59 -0
  25. package/src/lib/components/console/pos/pos-tax-notice.component.js.map +1 -0
  26. package/src/lib/components/console/pos-page.component.js +65 -17
  27. package/src/lib/components/console/pos-page.component.js.map +1 -1
  28. package/src/lib/components/console/registers-card.component.js +21 -13
  29. package/src/lib/components/console/registers-card.component.js.map +1 -1
  30. package/src/lib/components/console/reviews-moderation-card.component.js +108 -68
  31. package/src/lib/components/console/reviews-moderation-card.component.js.map +1 -1
  32. package/src/lib/components/console/site-member-purchases.component.js +5 -3
  33. package/src/lib/components/console/site-member-purchases.component.js.map +1 -1
  34. package/src/lib/components/console/subscription-detail-dialog.component.d.ts +2 -0
  35. package/src/lib/components/console/subscription-detail-dialog.component.js +1 -1
  36. package/src/lib/components/console/subscription-detail-dialog.component.js.map +1 -1
  37. package/src/lib/components/console/subscriptions-card.component.js +13 -5
  38. package/src/lib/components/console/subscriptions-card.component.js.map +1 -1
  39. package/src/lib/components/console/suppliers-card.component.js +47 -40
  40. package/src/lib/components/console/suppliers-card.component.js.map +1 -1
  41. package/src/lib/components/product-choice-cards.js +2 -1
  42. package/src/lib/components/product-choice-cards.js.map +1 -1
  43. package/src/lib/components/product-detail.js +9 -4
  44. package/src/lib/components/product-detail.js.map +1 -1
  45. package/src/lib/components/product-grid.js +3 -1
  46. package/src/lib/components/product-grid.js.map +1 -1
  47. package/src/lib/components/related-products.js +2 -1
  48. package/src/lib/components/related-products.js.map +1 -1
  49. package/src/lib/components/store-image-sizing.d.ts +46 -0
  50. package/src/lib/components/store-image-sizing.js +53 -0
  51. package/src/lib/components/store-image-sizing.js.map +1 -0
  52. package/src/lib/components/wishlist.js +2 -1
  53. package/src/lib/components/wishlist.js.map +1 -1
  54. package/src/lib/declarations.console-server.js +10 -0
  55. package/src/lib/declarations.console-server.js.map +1 -1
  56. package/src/lib/model/commerce-tax-step.d.ts +123 -0
  57. package/src/lib/model/commerce-tax-step.js +185 -0
  58. package/src/lib/model/commerce-tax-step.js.map +1 -0
  59. package/src/lib/model/index.d.ts +1 -0
  60. package/src/lib/model/index.js +1 -0
  61. package/src/lib/model/index.js.map +1 -1
  62. package/src/lib/model/payments-setup-status.d.ts +40 -0
  63. package/src/lib/model/payments-setup-status.js +63 -0
  64. package/src/lib/model/payments-setup-status.js.map +1 -1
  65. package/src/lib/model/subscription-view.d.ts +19 -0
  66. package/src/lib/model/subscription-view.js +16 -0
  67. package/src/lib/model/subscription-view.js.map +1 -1
  68. package/src/lib/model/tap-to-pay-launch.d.ts +103 -0
  69. package/src/lib/model/tap-to-pay-launch.js +148 -0
  70. package/src/lib/model/tap-to-pay-launch.js.map +1 -0
  71. package/src/lib/plugin.js +12 -0
  72. package/src/lib/plugin.js.map +1 -1
  73. package/src/lib/server/billing-webhook.js +140 -116
  74. package/src/lib/server/billing-webhook.js.map +1 -1
  75. package/src/lib/server/checkout.js +15 -4
  76. package/src/lib/server/checkout.js.map +1 -1
  77. package/src/lib/server/connect-return.d.ts +83 -0
  78. package/src/lib/server/connect-return.js +105 -0
  79. package/src/lib/server/connect-return.js.map +1 -0
  80. package/src/lib/server/connect.d.ts +18 -0
  81. package/src/lib/server/connect.js +60 -6
  82. package/src/lib/server/connect.js.map +1 -1
  83. package/src/lib/server/new-store-tax.d.ts +10 -0
  84. package/src/lib/server/new-store-tax.js +43 -0
  85. package/src/lib/server/new-store-tax.js.map +1 -0
  86. package/src/lib/server/pos-payment.js +3 -2
  87. package/src/lib/server/pos-payment.js.map +1 -1
  88. package/src/lib/server/pos-tax-step.d.ts +43 -0
  89. package/src/lib/server/pos-tax-step.js +55 -0
  90. package/src/lib/server/pos-tax-step.js.map +1 -0
  91. package/src/lib/server/pos-terminal-connection-token.d.ts +10 -1
  92. package/src/lib/server/pos-terminal-connection-token.js +18 -7
  93. package/src/lib/server/pos-terminal-connection-token.js.map +1 -1
  94. package/src/lib/server/pos-terminal.d.ts +9 -0
  95. package/src/lib/server/pos-terminal.js +17 -1
  96. package/src/lib/server/pos-terminal.js.map +1 -1
  97. package/src/lib/server/site-page-resolver.d.ts +8 -0
  98. package/src/lib/server/site-page-resolver.js +21 -3
  99. package/src/lib/server/site-page-resolver.js.map +1 -1
  100. package/src/lib/server/tap-to-pay-launch-admin.d.ts +18 -0
  101. package/src/lib/server/tap-to-pay-launch-admin.js +204 -0
  102. package/src/lib/server/tap-to-pay-launch-admin.js.map +1 -0
  103. package/src/lib/server/tap-to-pay-launch.d.ts +114 -0
  104. package/src/lib/server/tap-to-pay-launch.js +408 -0
  105. package/src/lib/server/tap-to-pay-launch.js.map +1 -0
  106. package/src/lib/server.js +15 -0
  107. package/src/lib/server.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/pos-terminal-connection-token.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { firebaseAdmin } from '@aglyn/tenant-data-admin'\nimport type { PluginApiHandler } from '@aglyn/aglyn/server'\nimport { authorizePosStaff, posRequestBody, type PosStaff } from './pos-auth'\nimport { posStripeTestMode, posTerminalAvailable } from './pos-stripe'\nimport {\n createPosTerminalConnectionToken,\n ensurePosTerminalLocation,\n posMerchantAccount,\n posTerminalLocationId,\n posTerminalRecordedLocationId,\n type PosTerminalAddress,\n} from './pos-terminal'\n\n/**\n * `POST /api/commerce/pos-terminal-connection-token` (AGL-3618): what the\n * native Aglyn POS app's Stripe Terminal SDK (iOS and Android) needs to drive\n * Tap to Pay and Bluetooth readers for ONE site.\n *\n * Gated exactly like a register sale (`authorizePosStaff`): a site role that\n * may sell, `managePos`, and the `pos` entitlement. The app holds no key and\n * no privileged path. Every token is scoped to the site's own Terminal\n * Location, so the SDK can connect only to readers in that Location, and the\n * PaymentIntents it collects are the server's own (`card-present-sdk`).\n *\n * Actions:\n * - `token` (the default): the secret, the Location id, the merchant's\n * account id and display name, and test mode; or a 409 whose `code` names\n * what is missing: `terminal-unavailable`, `merchant-not-ready`,\n * `location-required`.\n * - `status`: the same readiness as booleans, minting nothing.\n * - `location`: registers the site's Location from an address, once.\n */\nexport const posTerminalConnectionTokenHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' })\n const body = posRequestBody(req)\n const gate = await authorizePosStaff(req, String(body['hostId'] ?? ''))\n if ('error' in gate) return res.status(gate.status).json({ error: gate.error })\n const staff = gate.staff\n const action = String(body['action'] ?? 'token')\n try {\n switch (action) {\n case 'token':\n return await mintToken(staff, res)\n case 'status':\n return res.status(200).json(await terminalReadiness(staff))\n case 'location':\n return await registerLocation(staff, body, res)\n default:\n return res.status(400).json({ error: 'Unknown action' })\n }\n } catch (error) {\n console.error('[pos-terminal-connection-token]', action, error)\n return res.status(500).json({ error: 'Card readers could not be reached. Try again.' })\n }\n}\n\nasync function hostName(hostId: string): Promise<string> {\n const host = await firebaseAdmin.app().firestore().collection('hosts').doc(hostId).get()\n return String(host.get('name') ?? host.get('title') ?? '').trim().slice(0, 100) || 'Store'\n}\n\n/** Readiness as the native POS app's readers panel draws it. */\nexport async function terminalReadiness(staff: PosStaff) {\n const available = posTerminalAvailable()\n // Any recorded Location counts: one made on the platform before the store\n // moved to its own Stripe account is made again there at the next token.\n const [merchant, locationId] = await Promise.all([\n posMerchantAccount(staff.hostId, staff.org),\n posTerminalRecordedLocationId(staff.hostId),\n ])\n return {\n available,\n testMode: posStripeTestMode(),\n merchantReady: Boolean(merchant),\n locationReady: Boolean(locationId),\n }\n}\n\ntype Res = Parameters<PluginApiHandler>[1]\n\nasync function mintToken(staff: PosStaff, res: Res) {\n if (!posTerminalAvailable()) {\n return res\n .status(409)\n .json({ error: 'Card readers are not available yet.', code: 'terminal-unavailable' })\n }\n // A reader that connects for a store that cannot take card payments would\n // only fail at the first sale, in front of a customer.\n const merchant = await posMerchantAccount(staff.hostId, staff.org)\n if (!merchant) {\n return res.status(409).json({\n error: 'Finish setting up payments in the console before taking cards.',\n code: 'merchant-not-ready',\n })\n }\n let token = await createPosTerminalConnectionToken(staff.hostId)\n if (!token && !(await posTerminalLocationId(staff.hostId))) {\n // A Location made on the platform before the store moved to its own\n // Stripe account is made again there, at the same address, so the app\n // keeps working without asking the store for it again.\n const moved = await ensurePosTerminalLocation({\n hostId: staff.hostId,\n orgId: staff.orgId,\n displayName: await hostName(staff.hostId),\n })\n if (moved.ok) token = await createPosTerminalConnectionToken(staff.hostId)\n }\n if (!token) {\n const locationId = await posTerminalLocationId(staff.hostId)\n return locationId\n ? res.status(502).json({ error: 'Stripe did not answer. Try again.' })\n : res.status(409).json({\n error: 'Add the store address card readers are used at.',\n code: 'location-required',\n })\n }\n return res.status(200).json({\n secret: token.secret,\n locationId: token.locationId,\n // Identifies the merchant only. It is never the SDK's `onBehalfOf`: a\n // destination store's card-present charges settle on Aglyn's platform\n // account (AGL-3607), and a direct store's token above was minted ON its\n // own account, so the SDK already works there.\n merchantAccountId: merchant.accountId,\n merchantDisplayName: await hostName(staff.hostId),\n testMode: posStripeTestMode(),\n })\n}\n\n/** An address from the native POS app, bounded the way `pos-readers` bounds it. */\nexport function readPosTerminalAddress(raw: unknown): PosTerminalAddress | undefined {\n if (!raw || typeof raw !== 'object') return undefined\n const address = raw as Record<string, unknown>\n const text = (value: unknown, max: number) => String(value ?? '').trim().slice(0, max)\n return {\n line1: text(address['line1'], 200),\n ...(text(address['line2'], 200) ? { line2: text(address['line2'], 200) } : {}),\n city: text(address['city'], 100),\n ...(text(address['state'], 100) ? { state: text(address['state'], 100) } : {}),\n postalCode: text(address['postalCode'], 20),\n country: text(address['country'], 2).toUpperCase(),\n }\n}\n\nasync function registerLocation(staff: PosStaff, body: Record<string, unknown>, res: Res) {\n if (!posTerminalAvailable()) {\n return res\n .status(409)\n .json({ error: 'Card readers are not available yet.', code: 'terminal-unavailable' })\n }\n const address = readPosTerminalAddress(body['address'])\n const location = await ensurePosTerminalLocation({\n hostId: staff.hostId,\n orgId: staff.orgId,\n displayName: await hostName(staff.hostId),\n ...(address ? { address } : {}),\n })\n if ('error' in location) return res.status(location.status).json({ error: location.error })\n return res.status(200).json({ locationId: location.locationId })\n}\n"],"names":["firebaseAdmin","authorizePosStaff","posRequestBody","posStripeTestMode","posTerminalAvailable","createPosTerminalConnectionToken","ensurePosTerminalLocation","posMerchantAccount","posTerminalLocationId","posTerminalRecordedLocationId","posTerminalConnectionTokenHandler","req","res","body","method","status","json","error","gate","String","staff","action","mintToken","terminalReadiness","registerLocation","console","hostName","hostId","host","app","firestore","collection","doc","get","trim","slice","available","merchant","locationId","Promise","all","org","testMode","merchantReady","Boolean","locationReady","code","token","moved","orgId","displayName","ok","secret","merchantAccountId","accountId","merchantDisplayName","readPosTerminalAddress","raw","undefined","address","text","value","max","line1","line2","city","state","postalCode","country","toUpperCase","location"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,aAAa,QAAQ,2BAA0B;AAExD,SAASC,iBAAiB,EAAEC,cAAc,QAAuB,gBAAY;AAC7E,SAASC,iBAAiB,EAAEC,oBAAoB,QAAQ,kBAAc;AACtE,SACEC,gCAAgC,EAChCC,yBAAyB,EACzBC,kBAAkB,EAClBC,qBAAqB,EACrBC,6BAA6B,QAExB,oBAAgB;AAEvB;;;;;;;;;;;;;;;;;;CAkBC,GACD,OAAO,MAAMC,oCAAsD,OAAOC,KAAKC;QAG5BC,cAG3BA;IALtB,IAAIF,IAAIG,MAAM,KAAK,QAAQ,OAAOF,IAAIG,MAAM,CAAC,KAAKC,IAAI,CAAC;QAAEC,OAAO;IAAqB;IACrF,MAAMJ,OAAOX,eAAeS;IAC5B,MAAMO,OAAO,MAAMjB,kBAAkBU,KAAKQ,QAAON,eAAAA,IAAI,CAAC,SAAS,YAAdA,eAAkB;IACnE,IAAI,WAAWK,MAAM,OAAON,IAAIG,MAAM,CAACG,KAAKH,MAAM,EAAEC,IAAI,CAAC;QAAEC,OAAOC,KAAKD,KAAK;IAAC;IAC7E,MAAMG,QAAQF,KAAKE,KAAK;IACxB,MAAMC,SAASF,QAAON,eAAAA,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U;IAAC;AAChE"}
1
+ {"version":3,"sources":["../../../../../../../libs/plugins/commerce/src/lib/server/pos-terminal-connection-token.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { firebaseAdmin } from '@aglyn/tenant-data-admin'\nimport type { PluginApiHandler } from '@aglyn/aglyn/server'\nimport { authorizePosStaff, posRequestBody, type PosStaff } from './pos-auth'\nimport { posStripeTestMode, posTerminalAvailable } from './pos-stripe'\nimport {\n createPosTerminalConnectionToken,\n ensurePosTerminalLocation,\n posMerchantAccount,\n posMerchantStanding,\n posTerminalLocationId,\n posTerminalRecordedLocationId,\n type PosTerminalAddress,\n} from './pos-terminal'\nimport { posTaxStep } from './pos-tax-step'\n\n/**\n * `POST /api/commerce/pos-terminal-connection-token` (AGL-3618): what the\n * native Aglyn POS app's Stripe Terminal SDK (iOS and Android) needs to drive\n * Tap to Pay and Bluetooth readers for ONE site.\n *\n * Gated exactly like a register sale (`authorizePosStaff`): a site role that\n * may sell, `managePos`, and the `pos` entitlement. The app holds no key and\n * no privileged path. Every token is scoped to the site's own Terminal\n * Location, so the SDK can connect only to readers in that Location, and the\n * PaymentIntents it collects are the server's own (`card-present-sdk`).\n *\n * Actions:\n * - `token` (the default): the secret, the Location id, the merchant's\n * account id and display name, and test mode; or a 409 whose `code` names\n * what is missing: `terminal-unavailable`, `merchant-not-ready`,\n * `location-required`.\n * - `status`: the same readiness as booleans, minting nothing, and\n * `merchantAccount`: whether the store has a Stripe account at all, ready\n * or not, so an app that finds it not ready asks Stripe (the connect\n * route's `sync`) only when there is an account to ask about (AGL-4061);\n * and `taxDecided` / `canDecideTax` / `taxRefusal`, the \"Sales tax\" row (`posTaxStep`),\n * which card readiness never reads.\n * - `location`: registers the site's Location from an address, once.\n */\nexport const posTerminalConnectionTokenHandler: PluginApiHandler = async (req, res) => {\n if (req.method !== 'POST') return res.status(405).json({ error: 'Method not allowed' })\n const body = posRequestBody(req)\n const gate = await authorizePosStaff(req, String(body['hostId'] ?? ''))\n if ('error' in gate) return res.status(gate.status).json({ error: gate.error })\n const staff = gate.staff\n const action = String(body['action'] ?? 'token')\n try {\n switch (action) {\n case 'token':\n return await mintToken(staff, res)\n case 'status':\n return res.status(200).json(await terminalReadiness(staff))\n case 'location':\n return await registerLocation(staff, body, res)\n default:\n return res.status(400).json({ error: 'Unknown action' })\n }\n } catch (error) {\n console.error('[pos-terminal-connection-token]', action, error)\n return res.status(500).json({ error: 'Card readers could not be reached. Try again.' })\n }\n}\n\nasync function hostName(hostId: string): Promise<string> {\n const host = await firebaseAdmin.app().firestore().collection('hosts').doc(hostId).get()\n return String(host.get('name') ?? host.get('title') ?? '').trim().slice(0, 100) || 'Store'\n}\n\n/** Readiness as the native POS app's readers panel draws it. */\nexport async function terminalReadiness(staff: PosStaff) {\n const available = posTerminalAvailable()\n // Any recorded Location counts: one made on the platform before the store\n // moved to its own Stripe account is made again there at the next token.\n const [standing, locationId, tax] = await Promise.all([\n posMerchantStanding(staff.hostId, staff.org),\n posTerminalRecordedLocationId(staff.hostId),\n posTaxStep(staff),\n ])\n return {\n available,\n testMode: posStripeTestMode(),\n merchantReady: Boolean(standing.merchant),\n merchantAccount: standing.hasAccount,\n locationReady: Boolean(locationId),\n taxDecided: tax.taxDecided,\n canDecideTax: tax.canDecideTax,\n taxRefusal: tax.taxRefusal,\n }\n}\n\ntype Res = Parameters<PluginApiHandler>[1]\n\nasync function mintToken(staff: PosStaff, res: Res) {\n if (!posTerminalAvailable()) {\n return res\n .status(409)\n .json({ error: 'Card readers are not available yet.', code: 'terminal-unavailable' })\n }\n // A reader that connects for a store that cannot take card payments would\n // only fail at the first sale, in front of a customer.\n const merchant = await posMerchantAccount(staff.hostId, staff.org)\n if (!merchant) {\n return res.status(409).json({\n error: 'Finish setting up payments in the console before taking cards.',\n code: 'merchant-not-ready',\n })\n }\n let token = await createPosTerminalConnectionToken(staff.hostId)\n if (!token && !(await posTerminalLocationId(staff.hostId))) {\n // A Location made on the platform before the store moved to its own\n // Stripe account is made again there, at the same address, so the app\n // keeps working without asking the store for it again.\n const moved = await ensurePosTerminalLocation({\n hostId: staff.hostId,\n orgId: staff.orgId,\n displayName: await hostName(staff.hostId),\n })\n if (moved.ok) token = await createPosTerminalConnectionToken(staff.hostId)\n }\n if (!token) {\n const locationId = await posTerminalLocationId(staff.hostId)\n return locationId\n ? res.status(502).json({ error: 'Stripe did not answer. Try again.' })\n : res.status(409).json({\n error: 'Add the store address card readers are used at.',\n code: 'location-required',\n })\n }\n return res.status(200).json({\n secret: token.secret,\n locationId: token.locationId,\n // Identifies the merchant only. It is never the SDK's `onBehalfOf`: a\n // destination store's card-present charges settle on Aglyn's platform\n // account (AGL-3607), and a direct store's token above was minted ON its\n // own account, so the SDK already works there.\n merchantAccountId: merchant.accountId,\n merchantDisplayName: await hostName(staff.hostId),\n testMode: posStripeTestMode(),\n })\n}\n\n/** An address from the native POS app, bounded the way `pos-readers` bounds it. */\nexport function readPosTerminalAddress(raw: unknown): PosTerminalAddress | undefined {\n if (!raw || typeof raw !== 'object') return undefined\n const address = raw as Record<string, unknown>\n const text = (value: unknown, max: number) => String(value ?? '').trim().slice(0, max)\n return {\n line1: text(address['line1'], 200),\n ...(text(address['line2'], 200) ? { line2: text(address['line2'], 200) } : {}),\n city: text(address['city'], 100),\n ...(text(address['state'], 100) ? { state: text(address['state'], 100) } : {}),\n postalCode: text(address['postalCode'], 20),\n country: text(address['country'], 2).toUpperCase(),\n }\n}\n\nasync function registerLocation(staff: PosStaff, body: Record<string, unknown>, res: Res) {\n if (!posTerminalAvailable()) {\n return res\n .status(409)\n .json({ error: 'Card readers are not available yet.', code: 'terminal-unavailable' })\n }\n const address = readPosTerminalAddress(body['address'])\n const location = await ensurePosTerminalLocation({\n hostId: staff.hostId,\n orgId: staff.orgId,\n displayName: await hostName(staff.hostId),\n ...(address ? { address } : {}),\n })\n if ('error' in location) return res.status(location.status).json({ error: location.error })\n return res.status(200).json({ locationId: location.locationId })\n}\n"],"names":["firebaseAdmin","authorizePosStaff","posRequestBody","posStripeTestMode","posTerminalAvailable","createPosTerminalConnectionToken","ensurePosTerminalLocation","posMerchantAccount","posMerchantStanding","posTerminalLocationId","posTerminalRecordedLocationId","posTaxStep","posTerminalConnectionTokenHandler","req","res","body","method","status","json","error","gate","String","staff","action","mintToken","terminalReadiness","registerLocation","console","hostName","hostId","host","app","firestore","collection","doc","get","trim","slice","available","standing","locationId","tax","Promise","all","org","testMode","merchantReady","Boolean","merchant","merchantAccount","hasAccount","locationReady","taxDecided","canDecideTax","taxRefusal","code","token","moved","orgId","displayName","ok","secret","merchantAccountId","accountId","merchantDisplayName","readPosTerminalAddress","raw","undefined","address","text","value","max","line1","line2","city","state","postalCode","country","toUpperCase","location"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SAASA,aAAa,QAAQ,2BAA0B;AAExD,SAASC,iBAAiB,EAAEC,cAAc,QAAuB,gBAAY;AAC7E,SAASC,iBAAiB,EAAEC,oBAAoB,QAAQ,kBAAc;AACtE,SACEC,gCAAgC,EAChCC,yBAAyB,EACzBC,kBAAkB,EAClBC,mBAAmB,EACnBC,qBAAqB,EACrBC,6BAA6B,QAExB,oBAAgB;AACvB,SAASC,UAAU,QAAQ,oBAAgB;AAE3C;;;;;;;;;;;;;;;;;;;;;;;CAuBC,GACD,OAAO,MAAMC,oCAAsD,OAAOC,KAAKC;QAG5BC,cAG3BA;IALtB,IAAIF,IAAIG,MAAM,KAAK,QAAQ,OAAOF,IAAIG,MAAM,CAAC,KAAKC,IAAI,CAAC;QAAEC,OAAO;IAAqB;I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AO;YAAuCoC,MAAM;QAAuB;IACvF;IACA,MAAMa,UAAUH,uBAAuBlD,IAAI,CAAC,UAAU;IACtD,MAAMgE,WAAW,MAAMzE,0BAA0B;QAC/CuB,QAAQP,MAAMO,MAAM;QACpB6B,OAAOpC,MAAMoC,KAAK;QAClBC,aAAa,MAAM/B,SAASN,MAAMO,MAAM;OACpCuC,UAAU;QAAEA;IAAQ,IAAI,CAAC;IAE/B,IAAI,WAAWW,UAAU,OAAOjE,IAAIG,MAAM,CAAC8D,SAAS9D,MAAM,EAAEC,IAAI,CAAC;QAAEC,OAAO4D,SAAS5D,KAAK;IAAC;IACzF,OAAOL,IAAIG,MAAM,CAAC,KAAKC,IAAI,CAAC;QAAEsB,YAAYuC,SAASvC,UAAU;IAAC;AAChE"}
@@ -48,6 +48,15 @@ export interface PosMerchant {
48
48
  * `sellerPaymentAccount`'s business, not this file's.
49
49
  */
50
50
  export declare function posMerchantAccount(hostId: string, org: Record<string, any> | null): Promise<PosMerchant | null>;
51
+ /**
52
+ * {@link posMerchantAccount}'s answer, and whether the store has a Stripe
53
+ * account at all, ready or not (AGL-4061): an app that finds the store not
54
+ * ready asks Stripe again only when there is an account to ask about.
55
+ */
56
+ export declare function posMerchantStanding(hostId: string, org: Record<string, any> | null): Promise<{
57
+ merchant: PosMerchant | null;
58
+ hasAccount: boolean;
59
+ }>;
51
60
  /**
52
61
  * The account the site's Terminal objects belong on NOW: the store's own for
53
62
  * a direct-charge store, null (the platform) for every other. Not gated on
@@ -87,8 +87,24 @@ import { applyPosPayment, readPosSale } from "./pos-sale.js";
87
87
  * store's account through; where the account is recorded is
88
88
  * `sellerPaymentAccount`'s business, not this file's.
89
89
  */ export async function posMerchantAccount(hostId, org) {
90
+ return (await posMerchantStanding(hostId, org)).merchant;
91
+ }
92
+ /**
93
+ * {@link posMerchantAccount}'s answer, and whether the store has a Stripe
94
+ * account at all, ready or not (AGL-4061): an app that finds the store not
95
+ * ready asks Stripe again only when there is an account to ask about.
96
+ */ export async function posMerchantStanding(hostId, org) {
90
97
  const seller = await sellerPaymentAccount(hostId, org);
91
- if (!seller) return null;
98
+ if (!seller) return {
99
+ merchant: null,
100
+ hasAccount: false
101
+ };
102
+ return {
103
+ merchant: readyMerchant(hostId, seller),
104
+ hasAccount: true
105
+ };
106
+ }
107
+ function readyMerchant(hostId, seller) {
92
108
  if (!merchantAccountIsReady({
93
109
  accountId: seller.accountId,
94
110
  chargesEnabled: seller.chargesEnabled,