ACG 文档
Pro & Payments

Webhooks

买家门户的 Creem webhook handler。

门户的 Creem webhook handler 在 app/api/payments/creem/webhook/route.ts。它负责:

  1. CREEM_WEBHOOK_SECRET 验证 HMAC-SHA256 签名。
  2. 读 event 类型(checkout.completedsubscription.paid 等)。
  3. metadata.kind 分发:
    • subscription → 更新用户的 subscriptioncredit_ledger 行。
    • one_time(credit 包)→ 更新用户的 creditscredit_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

  1. 登录 Creem。
  2. 创建一个 AI Cost Gate Pro 的 product(或先用占位 prod_pro_ai_cost_gate)。
  3. 在 "Webhooks" 加一个端点指向 https://aicostgate.com/api/payments/creem/webhook
  4. 把签名 secret 复制到 .env.localCREEM_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"

On this page