秋季计费
秋季计费的更佳认证插件
Autumn 是用于运行 SaaS 定价计划的开源基础设施。它位于您的应用程序和 Stripe 之间,作为客户订阅状态、用量计量和功能权限的数据库。
该插件由 Autumn 团队维护。如有错误、问题或功能请求, 请访问 Autumn GitHub 仓库。
在 Autumn 的 Discord 上寻求帮助
我们在线随时为您解答任何问题。
功能
- 一函数处理所有结账、订阅和支付流程
- 无需 Webhook:直接查询 Autumn 获取所需数据
- 管理您应用的免费和付费计划
- 用于用量计费和定期限制的使用跟踪
- 通过 Autumn 仪表盘自定义计划和价格变更
设置 Autumn 账户
首先,在 Autumn 的仪表盘中创建您的定价计划,定义每个计划和产品的访问权限及计费方式。此示例中,我们处理 AI 聊天机器人免费和专业版计划,每月包含一定数量的 消息。
将 Autumn 插件添加到您的 auth 配置中
import { autumn } from "autumn-js/better-auth";
export const auth = betterAuth({
// ...
plugins: [autumn()],
});import { autumn } from "autumn-js/better-auth";
import { organization } from "better-auth/plugins";
export const auth = betterAuth({
// ...
plugins: [organization(), autumn({ customerScope: "organization" })],
});import { autumn } from "autumn-js/better-auth";
import { organization } from "better-auth/plugins";
export const auth = betterAuth({
// ...
plugins: [
organization(),
autumn({ customerScope: "user_and_organization" })
],
});import { autumn } from "autumn-js/better-auth";
import { organization } from "better-auth/plugins";
export const auth = betterAuth({
// ...
plugins: [
organization(),
autumn({
identify: async ({ session, organization }) => {
return {
customerId: "your_customer_id",
customerData: {
name: "客户名称",
email: "customer@gmail.com",
},
};
},
}),
],
});当客户注册时,Autumn 会自动创建客户帐户,并分配您创建的任何默认计划(例如您的免费计划)。您可以选择成为客户的对象:个人用户、组织、两者皆可,或像工作区这样的自定义对象。
添加 <AutumnProvider />
在客户端,用 AutumnProvider 组件包裹您的应用, 并传入您在 better-auth 的 authClient 中定义的 baseUrl。
import { AutumnProvider } from "autumn-js/react";
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
return (
<html>
<body>
{/* 或者 vite 可用 meta.env.BETTER_AUTH_URL */}
<AutumnProvider betterAuthUrl={process.env.NEXT_PUBLIC_BETTER_AUTH_URL}>
{children}
</AutumnProvider>
</body>
</html>
);
}使用方法
处理支付
当客户想购买专业版时,调用 attach 函数重定向客户到 Stripe 结账页面。
如果客户已有支付方式,AttachDialog 会打开,方便客户确认订阅或购买,并处理支付。
请确保您已在 Autumn 仪表盘 中粘贴了您的 Stripe 测试密钥。
import { useCustomer, AttachDialog } from "autumn-js/react";
export default function PurchaseButton() {
const { attach } = useCustomer();
return (
<button
onClick={async () => {
await attach({
productId: "pro",
dialog: AttachDialog,
});
}}
>
升级到专业版
</button>
);
}AttachDialog 组件可以直接从 autumn-js/react 库中使用(如上例所示),也可以下载作为 shadcn/ui 组件 进行自定义。
集成定价逻辑
通过以下函数集成您的客户端和服务器端定价层逻辑:
check用于检测客户是否被允许发送消息。track向 Autumn 记录使用事件(通常在服务器端执行)customer在 UI 中显示相关计费数据(订阅、功能余额)
服务器端,您可以通过 auth 对象访问 Autumn 的函数。
import { useCustomer } from "autumn-js/react";
export default function SendChatMessage() {
const { customer, allowed, refetch } = useCustomer();
return (
<>
<button
onClick={async () => {
if (allowed({ featureId: "messages" })) {
//... 服务器端发送聊天消息,然后
await refetch(); // 重新获取客户使用数据
alert(
"剩余消息数: " + customer?.features.messages?.balance
);
} else {
alert("消息已用完");
}
}}
>
发送消息
</button>
</>
);
}import { auth } from "@/lib/auth";
// 在服务器端检查客户是否能发送消息
const { allowed } = await auth.api.check({
headers: await headers(), // 传递请求头
body: {
featureId: "messages",
},
});
// 服务器端发送消息函数
// 然后记录用量
await auth.api.track({
headers: await headers(),
body: {
featureId: "messages",
value: 2,
},
});其他功能
openBillingPortal()
打开计费门户,客户可更新支付方式或取消计划。
import { useCustomer } from "autumn-js/react";
export default function BillingSettings() {
const { openBillingPortal } = useCustomer();
return (
<button
onClick={async () => {
await openBillingPortal({
returnUrl: "/settings/billing",
});
}}
>
管理计费
</button>
);
}cancel()
取消产品或订阅。
import { useCustomer } from "autumn-js/react";
export default function CancelSubscription() {
const { cancel } = useCustomer();
return (
<button
onClick={async () => {
await cancel({ productId: "pro" });
}}
>
取消订阅
</button>
);
}获取发票历史记录
向 useCustomer 传入 expand 参数以获取更多信息。您可以展开 invoices、trials_used、payment_method 或 rewards。
import { useCustomer } from "autumn-js/react";
export default function CustomerProfile() {
const { customer } = useCustomer({ expand: ["invoices"] });
return (
<div>
<h2>客户档案</h2>
<p>姓名: {customer?.name}</p>
<p>邮箱: {customer?.email}</p>
<p>余额: {customer?.features.chat_messages?.balance}</p>
</div>
);
}