@x-money/checkout

中文文档
0.2.0发布于 2026/10/30MITTypeScriptES Module零依赖

Pay with X checkout button for the browser

原始使用文档
文档目录11 节
  1. Quick start
  2. The button
  3. The flow on its own
  4. Errors
  5. Account exchanges
  6. Popup mode requirements
  7. What it handles
  8. Theming
  9. Documentation
  10. Support
  11. License

@x-money/checkout

The browser half of the X Money SDK: the Pay with X button, and the popup checkout flow behind it.

Zero runtime dependencies, no framework required.

npm install @x-money/checkout

Quick start

Sessions are created with your secret key, so that part stays on your server. The button calls your endpoint and takes it from there.

import { Checkout } from "@x-money/checkout";

const checkout = Checkout();

checkout
  .button({
    intent: "checkout",
    size: "lg",
    session: () =>
      fetch("/api/checkout", { method: "POST" })
        .then((r) => r.json())
        .then((s) => s.url),
    onSuccess: ({ sessionId }) => showPendingState(sessionId),
    onCancel: () => {},
    onError: (error) => handleError(error),
  })
  .mount(document.querySelector("#pay"));

Use @x-money/core on the server to create the session.

onSuccess means the customer finished checkout, not that you have been paid. Fulfill on the checkout_session_completed webhook and treat onSuccess as a cue to show a pending state.

The button

button() returns a handle with mount(target), update(props), and unmount(). Options are documented in full under Props; briefly, intent picks the copy from a closed set of sixteen, locale picks its language (the buyer's browser languages by default), and shape, size, theme, border, and width are independent appearance axes.

The button renders into a shadow root, so page CSS cannot restyle it. It sets state to "loading" while a checkout is in flight, which also blocks clicks so a second one cannot open a second popup.

unmount() stops the SDK listening but deliberately does not close a popup the customer is still paying in.

The flow on its own

If you want the payment without the button, checkout.start() is the same flow with no UI.

import { Checkout } from "@x-money/checkout";

const checkout = Checkout();

const handle = checkout.start({
  session: () => createSessionOnMyServer(),
  onSuccess: ({ sessionId }) => showPendingState(sessionId),
  onCancel: () => resetMyButton(),
  onError: (error) => handleError(error),
});

handle.cancel(); // stop observing, without closing the popup

Call it directly inside your click handler. It opens the popup synchronously before awaiting anything, which is what keeps popup blockers from killing it.

Errors

onError receives an XMoneyCheckoutError. Its code is one of no_browser (called during a server render), session_failed (your session callback rejected; the rejection is on cause), or invalid_session_url (the callback resolved to something the popup cannot navigate to). None of them mean a payment failed: a declined payment stays in the popup for the customer to retry, and an abandoned one arrives as onCancel.

The package ships a source map with the TypeScript sources embedded, so browser devtools show stack traces against the original source. If your bundler emits its own source maps, have it read the ones in your dependencies too (webpack needs source-map-loader for that).

Account exchanges

session also accepts an account exchange url, for when the customer authorizes future charges instead of paying now. onSuccess then receives { exchangeId } rather than { sessionId }, and a customer who declines closes the popup, which arrives as onCancel. Read the minted customer_id and account_ids from the account_exchange_setup_succeeded webhook.

Popup mode requirements

The popup can only report back to your page when your page is served over https and its origin (scheme, host, and port) exactly matches the origin of the session's success_url or failure_url. Otherwise the payment still completes, but the popup redirects to success_url, onSuccess never fires, and closing the popup calls onCancel. A Cross-Origin-Opener-Policy: same-origin header on your page has the same effect.

Pass debug: true while you integrate to get console warnings, prefixed [XMoney], for the cases the SDK can detect. It is off by default so the SDK never writes to your console unasked.

What it handles

  • Popup blockers. Browsers block popups opened after an await, which is exactly what creating a session on click requires. The window is opened blank and synchronously, then navigated when the URL arrives.
  • Mobile. Redirects rather than opening a popup, so the /i/money/checkout universal link can open the X app.
  • Blocked popups. Falls back to a full-page redirect.
  • Cancellation. A customer who closes the popup sends no signal, so the closed window is watched for and reported as a cancel.
  • Result validation. A result is accepted only from the checkout origin, only from the window that was opened, and only when it carries the expected marker. onSuccess fires exactly once.

Theming

theme="auto" renders dark on light surfaces and light on dark ones, following prefers-color-scheme by default. Chromium-based browsers will follow a .dark ancestor class instead where one is present; Safari and Firefox do not implement the selector that makes that work, so they stay on the OS preference.

That difference is visible rather than merely unsupported: a site in light mode by class, viewed on a machine set to dark, gets the dark button in those browsers. Pass theme="light" or theme="dark" when the tone has to be guaranteed.

Documentation

The full guide, every intent and variant, and a live playground are under Checkout in the X Money docs.

To have an AI coding agent collect a payment with this package:

npx skills add https://xmoney.mintlify.site/docs

Support

Report bugs and ask questions through the X Help Center. Security issues go through the same form; please do not post them publicly.

License

MIT. The full text ships in the package as LICENSE.

版本与发布记录

@x-money/checkout 共发布 2 个版本,首次发布于 2026/10/03;过去 12 个月发布了 2 个版本。

版本发布日期距今
0.2.0latest2026/10/304 天前
0.1.02026/10/031 个月前

关于 @x-money/checkout 的常见问题

@x-money/checkout 是什么?

支付与X结帐按钮的浏览器

如何安装 @x-money/checkout?

在项目目录运行 npm install @x-money/checkout,或使用 pnpm add / yarn add / bun add。

@x-money/checkout 支持 TypeScript 吗?

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

@x-money/checkout 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@x-money/checkout 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=24。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@x-money/checkout 可以商用吗?

@x-money/checkout 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@x-money/checkout 还在维护吗?

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