Step 1
生成签名请求头
开放接口请求统一使用 x-api-key、x-merchant-uid、x-timestamp、x-nonce、x-signature 这 5 个请求头。
- x-api-key 和签名密钥都使用同一串 mch_ 开头 API Key。
- 签名 path 只写接口路径,例如 /openapi/payin/orders,不要带域名。
- 签名原文固定为 Merchant UID、timestamp、nonce、method、path、canonical query、canonical body。
Node.js
import { createHmac, randomBytes } from 'node:crypto';
function required(name) {
const value = process.env[name]?.trim();
if (!value) throw new Error(name + ' is required');
return value;
}
const uid = "880001", key = "mch_xxxxxxxxxxxxxxxxxxxx";
const timestamp = "1776193200000", nonce = "2f5c7b147c3748f0a8b3d9bb38aa91a4";
const method = 'POST', path = '/openapi/payin/orders';
const canonicalQuery = '', canonicalBody = "{\"amount\":\"100.00\",\"chainCode\":\"TRON\",\"merchantOrderNo\":\"M202604150001\",\"notifyUrl\":\"https://merchant.example.com/api/uugate/payin-notify\",\"tokenSymbol\":\"USDT\"}";
const payload = [uid, timestamp, nonce, method, path, canonicalQuery, canonicalBody].join('\n');
const signature = createHmac('sha256', key).update(payload, 'utf8').digest('hex');
console.log(signature);