Reference
代码示例
PHP
$body = json_encode([
'merchant_order_no' => 'ORDER202608200001',
'amount' => 289900,
'currency' => 'CNY',
'description' => 'Order payment',
], JSON_UNESCAPED_SLASHES);
$timestamp = (string) time();
$nonce = bin2hex(random_bytes(12));
$canonical = $timestamp . "\n" . $nonce . "\n" . $body;
$signature = hash_hmac('sha256', $canonical, $apiSecret);Node.js
const rawBody = JSON.stringify(payload);
const timestamp = Math.floor(Date.now() / 1000).toString();
const nonce = crypto.randomBytes(12).toString('hex');
const canonical = `${timestamp}\n${nonce}\n${rawBody}`;
const signature = crypto
.createHmac('sha256', apiSecret)
.update(canonical)
.digest('hex');Python
raw_body = json.dumps(payload, separators=(",", ":"))
timestamp = str(int(time.time()))
nonce = secrets.token_hex(12)
canonical = f"{timestamp}\n{nonce}\n{raw_body}".encode()
signature = hmac.new(
api_secret.encode(), canonical, hashlib.sha256
).hexdigest()这些示例只展示签名核心。生产环境还应设置合理的连接超时、记录请求 ID,并避免把 API Secret 或完整签名写入公开日志。