扣款申请
玩家确认购买后,放置小兵 向渠道方发起扣款。渠道须在扣款入账后返回成功,放置小兵 收到成功后发放商品。
{租户配置的完整扣款地址}在租户后台的“扣款地址”中填写渠道接收 POST 的完整 URL,包含 HTTP 或 HTTPS 协议、实际路径及所需查询参数。后台原样保存输入,放置小兵直接向该地址发送 UTF-8 JSON 请求。渠道也以 UTF-8 JSON 响应。
玉石套餐
以下为初始配置,运营后台可调整名称、价格和玉石数量。商品标识固定,实际扣款以已确认订单的 amountFen 为准;配置变更不改变已有订单。
| productId | 初始价格(元) | 初始玉石数量 |
|---|---|---|
place-soldier-jade-1 | ¥6 | 80 |
place-soldier-jade-2 | ¥30 | 500 |
place-soldier-jade-3 | ¥68 | 1200 |
place-soldier-jade-4 | ¥128 | 2500 |
place-soldier-jade-5 | ¥328 | 6500 |
place-soldier-jade-6 | ¥648 | 14000 |
请求
请求头包含 appId、timestamp、nonce、sign。正文全部字段必填。
| 字段 | 类型 | 说明 |
|---|---|---|
orderId | string | 平台订单号。渠道以 appId 与 orderId 作为扣款幂等键。 |
customId | string | 该租户下的会员自定义 ID。 |
productId | string | 所购商品标识。 |
amountFen | integer | 扣款金额,单位分。 |
expiresAt | integer | 首次扣款截止时间,Unix 毫秒。已处理订单重放原结果。 |
验签
使用租户的 app_secret 原文作为 HMAC-SHA256 密钥,按下列六行计算 sign。行间使用 LF,末尾无换行。path 取当次 HTTP 请求的原始路径及查询字符串,保留参数顺序、重复参数和百分号编码。Node.js 原生 HTTP 服务可取 req.url。没有查询字符串时只签路径。bodySha256 为原始请求体字节的 SHA-256 小写十六进制值。
校验请求时间与当前时间的偏差不超过 5 分钟。验签后按租户保存已使用 nonce 至整个验签窗口结束。
POST
{path}
appId={appId}
bodySha256={bodySha256}
timestamp={timestamp}
nonce={nonce}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'))
}示例租户登记的完整扣款地址为 https://pay.example.com/,使用占位密钥 0123456789abcdef0123456789abcdef。
POST /billing/debit?game=place-soldier&channel=web HTTP/1.1
Host: pay.example.com
Content-Type: application/json
appId: 1234567890
timestamp: 1794283200000
nonce: 00112233445566778899aabbccddeeff
sign: c74eb0652f9db7b11baa5cad5758cbd53cd89eaafcc09b4c1776045fac061645
{"orderId":"6628d6b4-6cde-4c69-bde7-e0bd0f19d367","customId":"user_10001","productId":"place-soldier-jade-1","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,末尾无换行。
响应签名的 path 与请求验签使用同一原始路径及查询字符串。其余字段取当次请求的 appId、orderId、timestamp 和 nonce。bodySha256 对实际发送的响应体原始字节计算。重试时响应重新签名,订单结果保持一致。
POST
{path}
appId={appId}
orderId={orderId}
bodySha256={bodySha256}
timestamp={timestamp}
nonce={nonce}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,放置小兵 将沿用原订单号重试,重试会生成新的时间戳和 nonce。
首次处理若晚于 expiresAt,拒绝扣款并返回 1015。已处理订单即使过期,也须返回原结果。