跳至主要內容
StagePay

開發者

只要幾個步驟,就能完成金流串接

透過付款頁面 URL 或伺服器對伺服器的付款建立 API 發起付款,以附簽章的回呼接收結果,並使用 REST API 查詢結算。

伺服器對伺服器建立付款bash
curl -X POST https://api.stagepay.io/api/v1/payments \
  -H "Authorization: Bearer $STAGEPAY_KEY_ID:$STAGEPAY_KEY_SECRET" \
  -H "Idempotency-Key: 3f6c1b2e-8a4d-4c1e-9b7a-2d5e0f1a6c90" \
  -H "Content-Type: application/json" \
  -d '{"productId":"gem_100","currency":"KRW","userId":"user_42","pg":"KCP","method":"card"}'

串接步驟

  1. 建立應用程式與 API 金鑰

    在後台註冊應用程式,並核發 API 金鑰與 Webhook 密鑰(whsec_…)。

  2. 建立付款

    重新導向至付款頁面,或呼叫 POST /api/v1/payments 取得 tid 與 checkoutUrl。

  3. 處理回呼

    驗證 payment.succeeded / payment.refunded 事件的簽章,並以 2xx 回應。

  4. 查詢結算

    透過 GET /api/v1/settlements 與各批次的明細列(lines)取得結算明細(支援 CSV)。

範例

請將預留位置(<…>)替換為您在後台確認的實際值。完整欄位請參閱文件。

付款頁面 URLurl
https://pay.stagepay.io/request?appId=<APP_ID>&pg=KCP&method=card&productId=gem_100&currency=KRW&userId=user_42
伺服器對伺服器建立付款bash
curl -X POST https://api.stagepay.io/api/v1/payments \
  -H "Authorization: Bearer $STAGEPAY_KEY_ID:$STAGEPAY_KEY_SECRET" \
  -H "Idempotency-Key: 3f6c1b2e-8a4d-4c1e-9b7a-2d5e0f1a6c90" \
  -H "Content-Type: application/json" \
  -d '{"productId":"gem_100","currency":"KRW","userId":"user_42","pg":"KCP","method":"card"}'
201 Created 回應json
{
  "tid": "KCP-20261128031500-a1b2c3-0f1e2d3c",
  "checkoutUrl": "https://pay.stagepay.io/checkout?tid=KCP-…&t=<hmac>",
  "expiresAt": "2026-11-28T03:45:00.000Z",
  "amount": "33000",
  "amountMinor": "33000",
  "currency": "KRW",
  "status": "pending"
}
商戶回呼標頭http
POST https://merchant.example/stagepay/callback
Content-Type: application/json
X-StagePay-Event: payment.succeeded
X-StagePay-Delivery: 7c1e9a42-3b6d-4f0e-9a51-8e2f4c6d5d0b
X-StagePay-Timestamp: 1795835700
X-StagePay-Signature: v1=<hex(hmac_sha256(webhookSecret, timestamp + "." + rawBody))>
回呼簽章驗證(Node.js)node
import crypto from "node:crypto"

export function verifyStagePaySignature(rawBody, timestamp, signature, secret) {
  if (!/^\d+$/.test(timestamp ?? "")) return false
  if (Math.abs(Date.now() / 1000 - Number(timestamp)) > 300) return false
  const expected =
    "v1=" + crypto.createHmac("sha256", secret).update(`${timestamp}.`).update(rawBody).digest("hex")
  const a = Buffer.from(expected)
  const b = Buffer.from(signature ?? "")
  return a.length === b.length && crypto.timingSafeEqual(a, b)
}

回呼重試排程

若未收到 2xx 回應或 10 秒內沒有回應,將依下列間隔重新傳送。8 次全部失敗後,可在管理後台手動重新傳送。

  1. #1

    0s

  2. #2

    +1m

  3. #3

    +5m

  4. #4

    +15m

  5. #5

    +1h

  6. #6

    +3h

  7. #7

    +6h

  8. #8

    +12h

  9. #9

    +24h

REST API v1

  1. 驗證

    以 Authorization: Bearer <keyId>:<secret> 標頭進行驗證。每組金鑰都可設定權限範圍與 IP 允許清單。

  2. 冪等性

    透過 Idempotency-Key 標頭安全處理建立付款的重試。以相同金鑰傳送不同內容時會回傳 409。

  3. 請求簽章

    可選擇為請求附上 X-StagePay-Signature 簽章,採用與回呼相同的 HMAC 規格。

  4. OpenAPI 3.1

    可從 /api/v1/openapi.json 下載完整規格,用於瀏覽文件或產生用戶端。

常見問題

API 如何進行驗證?

REST API v1 以 Authorization: Bearer <keyId>:<secret> 標頭進行驗證。您也可以選擇使用 X-StagePay-Signature 為請求簽章,並為每組金鑰套用權限範圍與 IP 允許清單。

如何驗證回呼簽章?

確認 X-StagePay-Signature 的 v1 值等於 hex(HMAC-SHA256(webhookSecret, timestamp + "." + rawBody))、時間戳記與目前時間相差在 300 秒以內,且 X-StagePay-Delivery 尚未被處理過。

有測試環境嗎?

每個應用程式都可以另外設定測試用的回呼 URL。詳細的測試方式請參閱開發者文件中的付款測試頁面。

從一次串接開始

依照快速入門文件建立應用程式、建立付款,並驗證回呼簽章。