Pro & Payments
Webhooks
买家门户的 Creem webhook handler。
门户的 Creem webhook handler 在 app/api/payments/creem/webhook/route.ts。它负责:
- 用
CREEM_WEBHOOK_SECRET验证 HMAC-SHA256 签名。 - 读 event 类型(
checkout.completed、subscription.paid等)。 - 按
metadata.kind分发:subscription→ 更新用户的subscription和credit_ledger行。one_time(credit 包)→ 更新用户的credits和credit_ledger行。pro→ 把pro_license行标active,邀请用户为 GitHub collaborator。
签名验证
Creem 用 CREEM_WEBHOOK_SECRET 对原始 body 做 HMAC-SHA256 签名。签名在 creem-signature header。handler 用 timing-safe 比较防时序攻击。
幂等
handler 在 event id 和 provider_payment_id 上幂等。重放同一个 webhook 是安全的。
在 Creem 后台设置 webhook
- 登录 Creem。
- 创建一个 AI Cost Gate Pro 的 product(或先用占位
prod_pro_ai_cost_gate)。 - 在 "Webhooks" 加一个端点指向
https://aicostgate.com/api/payments/creem/webhook。 - 把签名 secret 复制到
.env.local的CREEM_WEBHOOK_SECRET。
本地测试
可以用 curl 重放一个签名后的 webhook:
BODY='{"eventType":"checkout.completed","id":"evt_test","object":{...}}'
SIG=$(echo -n "$BODY" | openssl dgst -sha256 -hmac "$CREEM_WEBHOOK_SECRET" -hex | awk '{print $2}')
curl -X POST https://localhost:6080/api/payments/creem/webhook \
-H "Content-Type: application/json" \
-H "creem-signature: $SIG" \
--data "$BODY"