Loading...
Loading...
Compare original and translation side by side
// app/api/webhooks/commet/route.ts
import { Webhooks } from "@commet/next";
export const POST = Webhooks({
webhookSecret: process.env.COMMET_WEBHOOK_SECRET!,
onSubscriptionActivated: async (payload) => {
await sendWelcomeEmail(payload.data.externalId);
},
onSubscriptionCanceled: async (payload) => {
await sendCancellationEmail(payload.data.externalId);
},
});npm install @commet/next// app/api/webhooks/commet/route.ts
import { Webhooks } from "@commet/next";
export const POST = Webhooks({
webhookSecret: process.env.COMMET_WEBHOOK_SECRET!,
onSubscriptionActivated: async (payload) => {
await sendWelcomeEmail(payload.data.externalId);
},
onSubscriptionCanceled: async (payload) => {
await sendCancellationEmail(payload.data.externalId);
},
});npm install @commet/nextimport { Commet } from "@commet/node";
const commet = new Commet({ apiKey: process.env.COMMET_API_KEY! });
// Check subscription status -- do this, not webhook state sync
const { data: sub } = await commet.subscriptions.get("user_123");
if (sub?.status === "active" || sub?.status === "trialing") {
// grant access
}
// Check feature access
const { data } = await commet.features.check({
code: "advanced_analytics",
customerId: "user_123",
});import { Commet } from "@commet/node";
const commet = new Commet({ apiKey: process.env.COMMET_API_KEY! });
// 检查订阅状态 -- 请使用该方式,不要同步webhook状态
const { data: sub } = await commet.subscriptions.get("user_123");
if (sub?.status === "active" || sub?.status === "trialing") {
// 授予访问权限
}
// 检查功能访问权限
const { data } = await commet.features.check({
code: "advanced_analytics",
customerId: "user_123",
});| Event | When Fired | Common Use |
|---|---|---|
| New subscription created, before payment | Logging, analytics |
| Payment successful, subscription active | Welcome email, provision resources |
| Subscription canceled | Cancellation email, schedule cleanup |
| Subscription details changed | Sync external systems |
| Plan upgrade/downgrade | Notify of plan change, adjust resources |
| Recurring payment processed | Receipt email, update accounting |
| Recurring charge failed | Alert customer, dunning flow |
| New invoice generated | Custom invoice handling |
| 事件 | 触发时机 | 常见用途 |
|---|---|---|
| 新订阅已创建,尚未完成支付 | 日志记录、数据分析 |
| 支付成功,订阅生效 | 发送欢迎邮件、资源调配 |
| 订阅已取消 | 发送取消通知邮件、安排资源清理 |
| 订阅详情变更 | 同步外部系统 |
| 套餐升级/降级 | 通知套餐变更、调整资源 |
| 定期支付处理完成 | 发送收据邮件、更新财务账目 |
| 定期扣费失败 | 提醒客户、催缴流程 |
| 新发票已生成 | 自定义发票处理 |
{
"event": "subscription.activated",
"timestamp": "2026-03-25T14:30:00.000Z",
"organizationId": "org_abc123",
"data": { }
}{
"event": "subscription.activated",
"timestamp": "2026-03-25T14:30:00.000Z",
"organizationId": "org_abc123",
"data": { }
}| Header | Description |
|---|---|
| HMAC-SHA256 hex signature of the raw body |
| The event type |
| ISO 8601 datetime when the event was emitted |
| 请求头 | 描述 |
|---|---|
| 原始请求体的HMAC-SHA256十六进制签名 |
| 事件类型 |
| 事件触发的ISO 8601格式时间 |