XXYZAPI 文档
v1.0.0下载 OpenAPI

扣款申请

玩家确认购买后,XXYZ 向渠道方发起扣款。渠道须在扣款入账后返回成功,XXYZ 收到成功后发放商品。

POST{租户配置的扣款地址}

渠道方提供完整扣款地址。XXYZ 向该地址发送 POST 请求,使用配置中的路径和查询参数。请求和响应均为 UTF-8 JSON。

请求

请求头包含 appId、timestamp、nonce、sign。正文全部字段必填。

字段类型说明
orderIdstring平台订单号。渠道以 appId 与 orderId 作为扣款幂等键。
customIdstring该租户下的会员自定义 ID。
productIdstring所购商品标识。
amountFeninteger扣款金额,单位分。
expiresAtinteger首次扣款截止时间,Unix 毫秒。已处理订单重放原结果。

验签

使用租户的 app_secret 原文作为 HMAC-SHA256 密钥,按下列六行计算 sign。行间使用 LF,末尾无换行。path 为实际请求路径及查询字符串,保留百分号编码。没有查询字符串时只签路径。bodySha256 为原始请求体字节的 SHA-256 小写十六进制值。

校验请求时间与当前时间的偏差不超过 5 分钟。验签后按租户保存已使用 nonce 至整个验签窗口结束。

签名原文
POST
{path}
appId={appId}
bodySha256={bodySha256}
timestamp={timestamp}
nonce={nonce}
Node.js 验签示例
import { createHash, createHmac, timingSafeEqual } from 'node:crypto'

function verifyCharge({ path, rawBody, appId, timestamp, nonce, sign, appSecret }) {
  if (!/^[1-9][0-9]{9}$/.test(appId) || !/^[1-9][0-9]{12}$/.test(timestamp) ||
      !/^[0-9a-f]{32}$/.test(nonce) || !/^[0-9a-f]{64}$/.test(sign) ||
      !appSecret || Math.abs(Date.now() - Number(timestamp)) > 300000) return false
  const hash = createHash('sha256').update(rawBody).digest('hex')
  const text = ['POST', path, 'appId=' + appId, 'bodySha256=' + hash,
    'timestamp=' + timestamp, 'nonce=' + nonce].join('\n')
  const expected = createHmac('sha256', appSecret).update(text, 'utf8').digest()
  return timingSafeEqual(expected, Buffer.from(sign, 'hex'))
}

以下示例的扣款路径为 /v1/payment/charge,使用占位密钥 0123456789abcdef0123456789abcdef。

签名请求示例
POST /v1/payment/charge
Content-Type: application/json
appId: 1234567890
timestamp: 1794283200000
nonce: 00112233445566778899aabbccddeeff
sign: d523e06143970ce47effeebc1ebb126ecec90acb31c4876d15588b7c780310aa

{"orderId":"6628d6b4-6cde-4c69-bde7-e0bd0f19d367","customId":"user_10001","productId":"1001","amountFen":600,"expiresAt":1794284100000}

响应

响应字段为 code、message、data、requestId、timestamp。渠道生成 requestId,timestamp 为 Unix 毫秒。成功时 data.orderId 须回传原订单号,失败时 data 为 null。业务码见 错误码。

成功响应
{
  "code": 0,
  "message": "成功",
  "data": {
    "orderId": "6628d6b4-6cde-4c69-bde7-e0bd0f19d367"
  },
  "requestId": "channel_req_001",
  "timestamp": 1794283200050
}
余额不足响应
{
  "code": 1014,
  "message": "余额不足,购买失败",
  "data": null,
  "requestId": "channel_req_002",
  "timestamp": 1794283200050
}

响应签名

使用 HTTP 接入时,每次响应均须在 sign 响应头返回签名。使用 HTTPS 时可选。签名使用 app_secret 作为 HMAC-SHA256 密钥,取小写十六进制结果。行间使用 LF,末尾无换行。

使用当次请求的路径及查询字符串、appId、orderId、timestamp 和 nonce。bodySha256 对实际发送的响应体原始字节计算。重试时响应重新签名,订单结果保持一致。

响应签名原文
POST
{path}
appId={appId}
orderId={orderId}
bodySha256={bodySha256}
timestamp={timestamp}
nonce={nonce}
Node.js 响应签名示例
import { createHash, createHmac } from 'node:crypto'

function signChargeResponse({ path, rawBody, appId, orderId, timestamp, nonce, appSecret }) {
  const hash = createHash('sha256').update(rawBody).digest('hex')
  const text = ['POST', path, 'appId=' + appId, 'orderId=' + orderId,
    'bodySha256=' + hash, 'timestamp=' + timestamp, 'nonce=' + nonce].join('\n')
  return createHmac('sha256', appSecret).update(text, 'utf8').digest('hex')
}

重复请求

在同一事务中记录订单结果和余额变动,并以 appId + orderId 唯一约束防止重复扣款。相同订单重复送达时,校验订单金额、会员和商品与原记录一致,再返回原结果。

仅在确认未扣款且拒绝订单时返回 1014 或 1015。结果不确定时返回 1006,XXYZ 将沿用原订单号重试,重试会生成新的时间戳和 nonce。

首次处理若晚于 expiresAt,拒绝扣款并返回 1015。已处理订单即使过期,也须返回原结果。