跳到内容
StagePayDocs v2

Webhook/回调事件与载荷

说明 StagePay 发送到商户服务器的支付完成·退款回调的事件类型、HTTP 请求头和全部正文字段。

支付确认或退款后,StagePay(pay)会向商户服务器发送 POST 请求。本文档将其称为回调(或 Webhook)。它与 v1 的“充值(purchase)回调”是同一概念,v2 新增了签名、重试和事件区分。

接收 URL

顺序应用设置使用条件
1purchaseTestUrl测试环境,或命中测试白名单(IP·userId)的支付
2purchaseRealUrl生产环境
3callbackUrl上述值为空时的备用值

GamePot 接入应用(webhookType = GAMEPOT)会发送到 GamePot 端点 → GamePot 应用特殊说明。

事件

X-StagePay-Event触发时机
payment.succeeded交易从 pending → success 迁移后立即发送(PG 服务器验证或 USDT 确认完成)
payment.refunded退款生效、迁移为 partial_refunded 或 refunded 后立即发送

失败·取消·过期不会发送回调。如有需要,请通过 REST API 查询交易状态。

请求头

POST /stagepay/callback HTTP/1.1
Host: game.example.com
Content-Type: application/json
Accept: application/json
User-Agent: StagePay/2 (+https://stagepay.io)
X-StagePay-Event: payment.succeeded
X-StagePay-Delivery: 2f1d8c7a-4b9e-4e57-9a51-0c6d3b2e8f14
X-StagePay-Timestamp: 1767225600
X-StagePay-Signature: v1=<64 位十六进制>
X-Request-Id: 9b2f0c1e-6c1a-4d3e-9f20-3b1f0a8e7d11
Authorization: Bearer <应用 apiKey>
请求头说明
X-StagePay-Event事件名称(与正文 event 相同)
X-StagePay-Delivery投递 ID(UUID)。重试时值不变 → 用作幂等键
X-StagePay-Timestamp签名时间(Unix 秒)。与当前时间相差超过 300 秒则拒绝
X-StagePay-Signaturev1= + hex(HMAC-SHA256(webhookSecret, timestamp + "." + rawBody)) → 签名验证
X-Request-Id本次发送的追踪 ID(用于咨询·日志追踪)
AuthorizationBearer <应用 apiKey> — 用于 v1 向下兼容,deprecated。新接入请使用签名进行验证
自定义请求头会附加应用设置 webhookHeaders 中登记的请求头

正文 (payment.succeeded)

{
  "event": "payment.succeeded",
  "tid": "KCP-20260101090000123456-a1b2c3-9f8e7d6c",
  "amount": 12000,
  "amountMinor": 12000,
  "currency": "KRW",
  "transactionStatus": "success",
  "paidAt": "2026-01-01T00:00:00.000Z",
  "orderId": "KCP-20260101090000123456-a1b2c3-9f8e7d6c",
  "userId": "user_42",
  "playerId": "hero42",
  "serverId": "s1",
  "paymentMethod": "card",
  "status": "completed",
  "productId": "gem_100",
  "productName": "보석 100개",
  "createdAt": "2025-12-31T23:59:40.000Z",
  "completedAt": "2026-01-01T00:00:00.000Z",
  "pgProvider": "KCP",
  "pgTransactionId": "KCP-20260101090000123456-a1b2c3-9f8e7d6c",
  "pgTid": "24123456789012",
  "remoteIp": "203.0.113.10",
  "userAgent": "Mozilla/5.0 ...",
  "etc": "",
  "meta": "",
  "userdata": "{\"orderNo\":\"A-1001\"}",
  "appId": "6650f0c2a1b2c3d4e5f60718",
  "environment": "production",
  "webhookType": "purchase",
  "originalPayload": { "...": "..." },
  "userID": "user_42",
  "orderID": "KCP-20260101090000123456-a1b2c3-9f8e7d6c",
  "playerID": "hero42",
  "serverID": "s1",
  "productID": "gem_100",
  "productNAME": "보석 100개",
  "pgTransactionID": "KCP-20260101090000123456-a1b2c3-9f8e7d6c",
  "remoteIP": "203.0.113.10",
  "appID": "6650f0c2a1b2c3d4e5f60718"
}

请使用 transactionStatus 判断交易状态

v2 接入请用 event 区分事件,用 transactionStatus 读取交易状态。status 是为兼容 v1 接收代码而保留的字段,在 payment.succeeded 中始终为 "completed"。新代码请勿依赖 status。

v2 保证的字段

字段类型说明
eventstringpayment.succeeded 或 payment.refunded(与请求头 X-StagePay-Event 相同)
tidstringStagePay 交易号
amountnumber支付金额(major unit,JSON 数字)。例如 9.99、12000。用于展示
amountMinorinteger结算·校验基准金额(minor unit)。例如 USD 999、KRW 12000
currencystring交易(商品价格)币种。ISO 4217 或 USDT
transactionStatusstring回调时的交易状态:success、partial_refunded、refunded
paidAtstring支付确认时间(ISO 8601,UTC)

在退款事件中,amount·amountMinor·currency 也是原支付金额。校验物品发放时请使用 amountMinor 和 currency。

v1 兼容字段

字段说明
statusv1 兼容状态值。payment.succeeded → "completed",payment.refunded → 交易状态(partial_refunded / refunded)
orderId与 tid 相同的值
appId应用 ID
userId / serverId / playerId发起支付请求时传入的值
productId / productName商品信息。如果商品另设了商品代码,productId 即为该代码
paymentMethod支付方式代码(例如 card、TRC20)
pgProviderPG 代码(例如 KCP、STRIPE、USDT)
pgTransactionIdv1 兼容字段(与 tid 相同的值)
pgTidPG 交易号(KCP tno、Stripe PaymentIntent ID、USDT 交易哈希等)
createdAt / completedAt交易创建时间 / 支付确认时间(与 paidAt 相同)
remoteIp / userAgent发起支付请求的客户 IP / 浏览器
userdata / etc / meta发起支付请求时传入的自定义数据
environmentproduction 或 test(含测试白名单)
webhookTypepurchase(支付完成)或 refund(退款)
originalPayload经过整理的支付方式详情(结构因 PG 而异,不含敏感值)。v1 会原样发送 PG 原始数据,而 v2 只发送整理后的值。请仅用于调试和展示
userID, orderID, playerID, serverID, productID, productNAME, pgTransactionID, remoteIP, appID大写 ID 写法的别名(兼容 v1 接收代码)。值与 camelCase 字段相同

USDT 支付的链上信息(网络、交易哈希、实际到账金额等)包含在 originalPayload 中 → USDT 支付原理。

正文 (payment.refunded)

在 payment.succeeded 的字段基础上增加退款信息。webhookType 为 refund,status 和 transactionStatus 为退款后的交易状态。

{
  "event": "payment.refunded",
  "tid": "KCP-20260101090000123456-a1b2c3-9f8e7d6c",
  "amount": 12000,
  "amountMinor": 12000,
  "currency": "KRW",
  "transactionStatus": "partial_refunded",
  "paidAt": "2026-01-01T00:00:00.000Z",
  "status": "partial_refunded",
  "webhookType": "refund",
  "refundedAmountMinor": 5000,
  "refundedAmount": 5000,
  "refund": {
    "refundId": "rf_01JH2X8Q4M3N",
    "amountMinor": 5000,
    "pgRefundId": "24123456789012-1",
    "amount": 5000
  }
}

(上例仅显示与退款相关的字段。其余字段与 payment.succeeded 相同。)

字段说明
refundedAmountMinor累计退款金额(minor)。与 amountMinor 相等即为全额退款
refundedAmount累计退款金额(major,JSON 数字)
refund.refundId本次退款的 ID
refund.amountMinor / refund.amount本次退款金额(minor / major)
refund.pgRefundIdPG 退款交易号(PG 提供时)

响应规则

  • 返回 2xx 状态码即视为投递成功。响应正文会被忽略(ok 等任意值均可)。
  • 非 2xx 响应、连接失败、10 秒超时都视为失败并重试 → 重试与幂等处理。
  • 签名错误时返回 401,遇到无法处理的临时错误时返回 5xx。再次收到已处理过的 Delivery 时,必须返回 200 才会停止重试。
  • 如果发放处理耗时较长,请先保存(放入队列)并立即返回 200,再异步处理。

安全建议

  • 验证所有回调的签名和时间戳
  • 使用 X-StagePay-Delivery 和 tid 防止重复处理
  • 确认回调中的 amountMinor·currency·productId 与自己的订单一致
  • 使用 transactionStatus 判断交易状态(status 仅用于 v1 兼容)
  • 只使用 HTTPS 端点
  • 以日志形式保存收到的请求头·正文(敏感值除外)

本页目录