@fantastic.dev/better-stripe

中文文档
0.111.0发布于 2026/11/01MITTypeScriptES Module构建来源已验证

Convex component for Stripe Accounts v2, subscriptions, metered billing and Connect marketplaces, with React workflows.

原始使用文档
文档目录10 节
  1. Package and application responsibilities
  2. Installation
  3. Quick start
  4. Step 1: Register the component
  5. Step 2: Create a BetterStripe instance
  6. Step 3: Register webhook routes
  7. Documentation
  8. Metered usage
  9. Contributing
  10. License

better-stripe

A Convex component for Stripe Accounts v2, billing, and Connect marketplaces. It provides checkout, subscriptions, webhook syncing, marketplace fees and splits, payouts, refunds, and disputes, with React hooks and headless UI components.

Version: 0.111.0 (pre-1.0). API may change between minor versions until 1.0.

Connect platforms use the default GA API version. Non-Connect merchants using customer-only Accounts v2 need preview access and an explicit preview version. Read the Accounts v2 setup guide before integrating.

Package and application responsibilities

BetterStripe owns reusable payment protocols, financial records, durable execution, and unstyled React workflows. The example app demonstrates those public APIs with Convex bindings and styled renderers. Consumers should not need to copy its payment or UI controllers to use the package.

Your application authenticates callers, authorizes the original account or payment, selects products and business policy, and decides what a paid purchase unlocks. One-time purchases use payment checkout; subscriptions use subscription checkout. Digital downloads, access grants, license keys, shipping, and fulfillment belong to the consuming application. A successful checkout page alone is not proof of payment. Use verified payment state and make fulfillment idempotent.

See the example boundary audit for concrete ownership rules and the remaining application adapters.

Installation

npm install @fantastic.dev/better-stripe

Requires Node.js 20+, Convex ^1.46.0, and React/React DOM ^18.3.1 or ^19.0.0. Stripe SDKs are included as dependencies.

Quick start

Set STRIPE_SECRET_KEY in your Convex deployment, then wire the component, client, and webhook route as follows. See configuration for all environment variables and TypeScript setup.

Step 1: Register the component

// convex/convex.config.ts
import betterStripe from "@fantastic.dev/better-stripe/convex.config";
import { defineApp } from "convex/server";
import { v } from "convex/values";

// betterStripe requires STRIPE_SECRET_KEY; declare it at the app level and pass
// it to the component by reference.
const app = defineApp({
  env: { STRIPE_SECRET_KEY: v.string() },
});
app.use(betterStripe, {
  env: { STRIPE_SECRET_KEY: app.env.STRIPE_SECRET_KEY },
});

export default app;

Step 2: Create a BetterStripe instance

// convex/stripe.ts
import { BetterStripe } from "@fantastic.dev/better-stripe";

import { components } from "./_generated/api";

export const stripe = new BetterStripe(components.betterStripe, {
  STRIPE_SECRET_KEY: process.env.STRIPE_SECRET_KEY,
  triggers: {
    checkoutSession: {
      onCompleted: async (ctx, session) => {
        // App-specific transactional logic (same DB transaction)
      },
    },
  },
});

// Export the webhook handler pair; http.ts passes their refs to registerRoutes.
// `syncWebhook` runs your sync triggers in the same transaction as the component
// upsert; `asyncWebhook` runs your async hooks after commit.
export const { syncWebhook, asyncWebhook } = stripe.webhookHandlers();

Step 3: Register webhook routes

// convex/http.ts
import { registerRoutes } from "@fantastic.dev/better-stripe";
import { httpRouter } from "convex/server";

import { components, internal } from "./_generated/api";

const http = httpRouter();

registerRoutes(http, components.betterStripe, {
  webhookPath: "/stripe/webhook",
  stripeSecretKey: process.env.STRIPE_SECRET_KEY,
  webhookSecret: process.env.STRIPE_WEBHOOK_SECRET,
  webhookSecretV2: process.env.STRIPE_WEBHOOK_SECRET_V2,
  webhooks: internal.stripe,
});

export default http;

Passing webhooks: internal.stripe runs your sync triggers in the same transaction as component writes. Async hooks run after commit. See webhooks and triggers for event destinations, signing secrets, and hook examples.

Create separate V1 snapshot and V2 thin event destinations pointing to https://<your-deployment>.convex.site/stripe/webhook. Store their signing secrets as STRIPE_WEBHOOK_SECRET and STRIPE_WEBHOOK_SECRET_V2 in your Convex deployment. Follow webhook setup to configure both destinations.

Documentation

The documentation index lists all guides. Start with:

The example app demonstrates customer, seller, affiliate, and admin flows, including several checkout integration styles.

Metered usage

Use native Stripe billing for pay as you go, included monthly units with overages, and paid prepaid credits. Developers choose units, precision, rates and policy; BetterStripe owns durable usage/delivery, allowance and credit accounting, corrections, original-payment settlement and reusable React workflows. Your app owns authentication, billable activity and authorization.

Start with setup for developers and AI agents, then the copyable three-model recipes and runnable example. Use your existing Stripe sandbox and Convex credentials. Metronome is optional, never a prerequisite.

See React views and configuration UI for forms, exact usage, allowances, prepaid balances and paginated history. The verification matrix records native paid-source proof, packed consumers and account/clock limits. Existing apps should follow opt-in adoption, model transitions and rollback.

Contributing

See the development guide for local setup and code generation, testing utilities for fixtures, and the release guide for publishing. Run pnpm run check:all before submitting changes.

License

MIT

版本与发布记录

@fantastic.dev/better-stripe 共发布 147 个版本,首次发布于 2026/09/26;过去 12 个月发布了 147 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.111.0latest2026/11/012 天前
0.110.02026/11/012 天前
0.109.12026/11/012 天前
0.109.02026/11/012 天前
0.108.02026/11/012 天前
0.107.02026/11/012 天前

其他发布通道:bootstrap → 0.4.0-bootstrap.0。

关于 @fantastic.dev/better-stripe 的常见问题

@fantastic.dev/better-stripe 是什么?

凸组件用于Stripe Accounts v2,订阅,计量计费和连接市场,与React工作流。

如何安装 @fantastic.dev/better-stripe?

在项目目录运行 npm install @fantastic.dev/better-stripe,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.3.1 || ^19.0.0)、convex(^1.46.0)、react-dom(^18.3.1 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fantastic.dev/better-stripe 支持 TypeScript 吗?

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

@fantastic.dev/better-stripe 支持 ESM 还是 CommonJS?

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

@fantastic.dev/better-stripe 需要什么版本的 Node.js?

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

@fantastic.dev/better-stripe 可以商用吗?

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

@fantastic.dev/better-stripe 还在维护吗?

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