Webhook/回呼事件與酬載
說明 StagePay 傳送到商戶伺服器的付款完成・退款回呼之事件類型、HTTP 標頭與所有本文欄位。
付款確定或退款時,StagePay(pay)會向商戶伺服器傳送 POST 請求。本文件將其稱為回呼(或 Webhook)。這與 v1 的「儲值(purchase)回呼」是相同概念,v2 另外加入了簽章・重試・事件區分。
接收 URL
| 順序 | App 設定 | 使用條件 |
|---|---|---|
| 1 | purchaseTestUrl | 測試環境,或符合測試白名單(IP・userId)的付款 |
| 2 | purchaseRealUrl | 正式環境 |
| 3 | callbackUrl | 上述值為空時的備援值 |
GamePot 串接 App(webhookType = GAMEPOT)會傳送到 GamePot 端點 → GamePot App 特殊事項。
事件
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 <App apiKey>| 標頭 | 說明 |
|---|---|
X-StagePay-Event | 事件名稱(與本文的 event 相同) |
X-StagePay-Delivery | 傳送 ID(UUID)。重試時值也相同 → 作為冪等鍵使用 |
X-StagePay-Timestamp | 簽章時間(Unix 秒)。與目前時間相差超過 300 秒即拒絕 |
X-StagePay-Signature | v1= + hex(HMAC-SHA256(webhookSecret, timestamp + "." + rawBody)) → 簽章驗證 |
X-Request-Id | 此次傳送的追蹤 ID(用於詢問・日誌追蹤) |
Authorization | Bearer <App apiKey> — v1 向下相容用,deprecated。新的串接請以簽章驗證 |
| 自訂標頭 | 會加上在 App 設定 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 保證的欄位
| 欄位 | 型別 | 說明 |
|---|---|---|
event | string | payment.succeeded 或 payment.refunded(與標頭 X-StagePay-Event 相同) |
tid | string | StagePay 交易編號 |
amount | number | 付款金額(major unit,JSON 數字)。例如:9.99、12000。顯示用 |
amountMinor | integer | 結算・驗證基準金額(minor unit)。例如:USD 999、KRW 12000 |
currency | string | 交易(商品價格)幣別。ISO 4217 或 USDT |
transactionStatus | string | 回呼當下的交易狀態:success、partial_refunded、refunded |
paidAt | string | 付款確定時間(ISO 8601,UTC) |
在退款事件中,amount・amountMinor・currency 同樣是原付款金額。驗證道具發放時,請使用 amountMinor 與 currency。
v1 相容欄位
| 欄位 | 說明 |
|---|---|
status | v1 相容狀態值。payment.succeeded → "completed",payment.refunded → 交易狀態(partial_refunded / refunded) |
orderId | 與 tid 相同的值 |
appId | App ID |
userId / serverId / playerId | 付款請求時傳入的值 |
productId / productName | 商品資訊。若商品另設有商品代碼,productId 即為該代碼 |
paymentMethod | 付款方式代碼(例如:card、TRC20) |
pgProvider | PG 代碼(例如:KCP、STRIPE、USDT) |
pgTransactionId | v1 相容欄位(與 tid 相同的值) |
pgTid | PG 交易編號(KCP tno、Stripe PaymentIntent ID、USDT 交易雜湊等) |
createdAt / completedAt | 交易建立時間 / 付款確定時間(與 paidAt 相同) |
remoteIp / userAgent | 提出付款請求之客戶的 IP / 瀏覽器 |
userdata / etc / meta | 付款請求時傳入的自由資料 |
environment | production 或 test(包含測試白名單) |
webhookType | purchase(付款完成)或 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.pgRefundId | PG 退款交易編號(PG 有提供時) |
回應規則
- 狀態碼為 2xx 即視為傳送成功。本文會被忽略(
ok等任意值皆可)。 - 2xx 以外的回應、連線失敗、10 秒逾時都會視為失敗並重試 → 重試與冪等處理。
- 簽章錯誤時請回傳
401,無法處理的暫時性錯誤請回傳5xx。若再次收到已處理過的 Delivery,必須回傳200才會停止重試。 - 若發放處理需要較長時間,請先儲存(放入佇列)並立即回應
200,再以非同步方式處理。
安全建議
- 驗證所有回呼的簽章與時間戳記
- 以
X-StagePay-Delivery與tid防止重複處理 - 確認回呼的
amountMinor・currency・productId與自己的訂單一致 - 以
transactionStatus判定交易狀態(status僅供 v1 相容) - 僅使用 HTTPS 端點
- 將收到的請求標頭・本文保存為日誌(機密值除外)