@hanzo/pay

中文文档
0.1.2发布于 2026/09/30Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 5031

Square payments for web and native behind one interface. Card, Apple Pay, Google Pay, Cash App Pay, ACH and gift cards, feature-detected — with one colour table that dresses the web iframe and the native sheet alike.

原始使用文档
文档目录6 节
  1. The shape
  2. Taking a payment
  3. What it knows that you would otherwise learn the hard way
  4. Theming
  5. Native setup
  6. License

@hanzo/pay

Square payments for web and native, behind one interface.

pnpm add @hanzo/pay

The shape

Three entries. The root is platform-agnostic and dependency-free — contracts, the colour table, the idempotency-key rule — so a server, a shared module and a test can all hold it. The adapters are explicit:

EntryNeedsGives you
@hanzo/paynothingTerminal, Method, Tender, Token, PALETTE, legible, attempt
@hanzo/pay/weba browserterminal() over the Web Payments SDK, style(), pin()
@hanzo/pay/nativereact-native-square-in-app-paymentsterminal() over the In-App Payments SDK, appearance()

Both adapters export terminal(config) and both return the same Terminal. Picking one by filename magic works right up until a bundler picks the other, and then it fails at runtime, in a payment, on someone else's device — so the host says which platform it is, in the import.

Taking a payment

import { cancelled, attempt } from '@hanzo/pay'
import { terminal } from '@hanzo/pay/web'

const pay = await terminal({ applicationId, locationId, environment })
const tender = { total: { amount: '12.00', currency: 'USD' }, country: 'US', label: 'Credit' }

const rails = await pay.offers(tender)     // only what can really pay, here, now
await pay.mount?.('card', '#card')          // absent on native: there is no element

const key = attempt()                       // hold this across retries
try {
  const token = await pay.collect('card', tender)
  // charge server-side with token.value and `key` as the idempotency key
} catch (e) {
  if (!cancelled(e)) throw e                // a dismissed sheet is not a failure
}

This package never moves money. Every rail ends at a single-use token; the charge is a server call you make with it.

What it knows that you would otherwise learn the hard way

  • offers() asks the SDK, one rail at a time. Availability depends on the browser, the device, the buyer's saved cards and whether the merchant account has the rail switched on. A picker listing a wallet the platform cannot use is a dead end with a logo on it.
  • REACH.native has no Cash App Pay and no ACH. Square's native SDK ships neither. That is reach, not policy — a host may decline a rail this table lists, and hanzoai/pay declines ACH on Square because its top-up endpoint credits on charge success while an ACH debit settles days later.
  • Nothing is awaited before tokenize(). Apple refuses a sheet not opened by the gesture that asked for it, so offers() builds every rail up front and collect() only reads a map. One stray await above that call breaks Apple Pay and nothing else.
  • Cash App Pay and ACH deliver on an event, not a return value. A caller reading only the return gets nothing from either, silently, on a payment that succeeded.
  • pin() is applied for you. input.backgroundColor is accepted and then ignored: color-scheme inherits into the cross-origin iframe and the UA paints form controls over whatever the SDK declared. One rule fixes it, and it has to be in the sheet before the card attaches — so terminal() puts it there rather than leaving a stylesheet you can forget to import. It carries the FOCUS RING too, for the mirror-image reason: a ring is drawn outside the field, and everything Square will style for you stops at the field's edge. Call pin() yourself only if you drive payments.card() directly instead of taking a Terminal.
  • attempt() fits Square's ruler. An idempotency_key over 45 characters is rejected before authorization. A 36-char UUID under a topup:<org>: wrapper is 49, which failed every card charge for one org and read as "charge failed".
  • Detail.charge holds the native sheet open. Square's native card sheet stays up after minting the token and is told afterwards whether the charge worked — so a decline retries in place instead of dumping the buyer back to an empty form. The web has no equivalent and ignores it.

Theming

One Palette dresses both platforms: style() renders it as the CSS literals the web iframe takes, appearance() as the {r,g,b,a} components the native sheet takes. Neither surface can read your CSS variables — one is cross-origin, the other is not the web — which is why the table holds literals.

import { legible, PALETTE } from '@hanzo/pay'

legible(PALETTE.dark)                        // [] — every contrast floor met
legible({ ...PALETTE.dark, field: '#fafafa' }) // names the pairs that fail

Run legible() over a brand palette before shipping it. A reviewer looking at a dark mock cannot tell 2.6:1 from 4.4:1, and the focus ring is a WCAG 2.4.11 requirement rather than a preference.

Set field and border to what your own inputs COMPOSITE to, measured in a browser. The card strip lands in a column of your fields and is read as one of them or as a foreign object; there is no third outcome. Ours shipped on --card and its hairline — real tokens, for a card — so the strip rendered a lighter ground behind a fainter edge than every field above it.

ring is the one focus indicator, and the box does not move. pin() draws it around the container as an outline, so it is the same ring your page already puts on its own controls; style() therefore paints the FOCUSED hairline the resting colour, because Square spends that value twice (the border and a ::before ring one pixel outside it) and any other value is a second ring inside the first.

Native setup

@hanzo/pay/native needs react-native-square-in-app-payments and the native build steps that plugin documents (Apple Pay entitlement and merchant id, Google Pay via Play Services). appearance() is iOS only — Android's card entry is themed through native XML styles at build time, with no runtime channel.

License

Apache-2.0

版本与发布记录

@hanzo/pay 共发布 3 个版本,首次发布于 2026/08/05;过去 12 个月发布了 3 个版本。

版本发布日期距今
0.1.2latest2026/09/301 个月前
0.1.12026/08/272 个月前
0.1.02026/08/053 个月前

关于 @hanzo/pay 的常见问题

@hanzo/pay 是什么?

Square payments for web and native behind one interface. Card, Apple Pay, Google Pay, Cash App Pay, ACH and gift cards, feature-detected — with one colour table that dresses the web iframe and the native sheet alike.

如何安装 @hanzo/pay?

在项目目录运行 npm install @hanzo/pay,或使用 pnpm add / yarn add / bun add。

@hanzo/pay 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@hanzo/pay 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@hanzo/pay 可以商用吗?

@hanzo/pay 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@hanzo/pay 的下载量有多少?

根据 npm 官方下载统计,@hanzo/pay 在 2026/10/17 – 2026/11/15 这 30 天内被下载 5,031 次,日均约 168 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@hanzo/pay 还在维护吗?

最新版本 0.1.2 发布于 2026/09/30(1 个月前);至今共发布 3 个版本,其中过去 12 个月发布了 3 个;首次发布于 2026/08/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。