kira-sg-billing 是统一计费服务,处理美国(US,legacy)和新加坡(SG,新订阅)两个 Stripe 账户。所有订阅 / 加油包相关的 PostHog 事件由 Stripe webhook 处理器触发。
源:kira-sg-billing/src/billing/index.ts。用 posthog-node,host us.i.posthog.com,带 disableGeoip: true,distinctId = user_profiles.id。
双区域。 webhook 入口分两条:POST /billing/webhook/us(US Stripe,STRIPE_WEBHOOK_SECRET 验签)和 POST /billing/webhook/sg(SG Stripe,STRIPE_WEBHOOK_SECRET_SG 验签)。两条都会发同样的事件,属性里的 amount / currency 反映对应区域 Stripe 的实际结算。金额统一从 Stripe 的「分」转成「元」(amount / 100)。
subscription
订阅生效时触发。处理器在 checkout.session.completed(subscription)/ 订阅更新路径里调 setPlanAndCredit(),更新 plan + credit + billing_region 后发事件。
| 属性 | 类型 | 说明 |
|---|
plan | string | 订阅计划:basic | basic_year | pro | pro_year | max | max_year |
first | boolean | 是否首次订阅 |
interval | month | year | 计费周期(由 plan 经 getIntervalFromPlan() 推导) |
amount | number? | 金额(元,amount_total / 100) |
currency | string? | 货币 |
subscription_canceled
订阅被降级到 free 时触发。降级路径 downgradeToFree(customerId, reason) 把 plan 置 free + credit 置 0 后,对存在的 user_profile 发事件。
| 属性 | 类型 | 说明 |
|---|
previous_plan | string | 取消前的计划(userProfile.plan,缺省 "unknown") |
reason | string | 取消原因(见下) |
reason 来源(即 Stripe 传入的 subscription status,或固定值 "deleted"):
| 值 | 来源 |
|---|
canceled | 订阅 status = canceled |
incomplete | status = incomplete |
incomplete_expired | status = incomplete_expired |
past_due | status = past_due |
paused | status = paused |
unpaid | status = unpaid |
deleted | customer.subscription.deleted webhook(固定传 "deleted") |
reason 直接取 event.data.object.status(订阅更新降级路径)或在删除路径写死 "deleted",所以可能的取值就是 Stripe subscription status 枚举加上 deleted。
booster_purchase
购买加油包(一次性付费)成功时触发。处理器在 checkout.session.completed(payment)里给用户加 credits 后发事件。
| 属性 | 类型 | 说明 |
|---|
amount | number? | 金额(元,session.amount_total / 100) |
currency | string? | 货币(session.currency) |
credits | number | 本次新增 credits(creditsToAdd) |
booster_tier | string | 档位,由 credits 数量映射 |
booster_tier 映射(按新增 credits):
| credits | booster_tier |
|---|
| 5000 | small |
| 10000 | medium |
| 20000 | large |
| 其他 | unknown |
与前端 billing 事件的关系
前端的 billing_checkout / billing_onetime(见 前端事件)是点击意图信号,在跳转 Stripe 之前发;这里的 subscription / booster_purchase 是 Stripe 付款成功后 webhook 确认的信号。漏斗分析时两者配合:billing_checkout → subscription 之间的差就是支付流失。