UFO支付 商户接口文档
按本文档即可完成下单、查单与回调接入。所有接口均使用 JSON 请求与应答,并通过 HMAC-SHA256 签名保证安全。
概述
- 基址:
https://api.alienpay.cc - 请求方式:
POST,Content-Type: application/json,字符编码 UTF-8。 - 商户号
merchant_no形如U60000001(必须带 U 前缀,不带前缀按参数错误处理),开通商户账户后由客服提供,密钥可在商户后台查看。 - 产品号
pay_type是您开通的收款产品编号(如1001),以商户后台显示为准。 - 请求与回调中的所有字段在 JSON 里都是字符串(包括
timestamp、amount)。应答顶层code是整数。
签名规则
- 取 body 中除
sign外所有值非空的字段,字段全部在同一层,没有嵌套。 - 按字段名 ASCII 升序排序,拼成
k1=v1&k2=v2&...,值使用 UTF-8 原值直接拼接,不做 URL 转义(值里含&或=也原样拼)。 - 以商户密钥为 key 做 HMAC-SHA256,结果转小写十六进制,放入
sign。 - 值为空串或缺失的字段不参与签名;值为
"0"要参与。 - 服务器应答与回调使用相同规则签名。应答的签名放在顶层
sign,对data内的字段签名。 - 比较签名时请使用恒定时间比较函数。
timestamp为 Unix 秒,与服务器相差超过 300 秒会被拒绝(1008);nonce为随机串,每次请求不同。
amount=100.00&attach=uid=42&client_ip=203.0.113.8&merchant_no=U60000001&nonce=a1b2c3d4e5f60718¬ify_url=https://shop.example.com/notify&out_trade_no=T20261205001&pay_type=1001&return_url=https://shop.example.com/done&subject=Test Order×tamp=1796457600,签名为 e42bb49d6dc5450b2a24e2579c5741341e74d4963c148710b13db5d2d5dff6af。完整向量见签名测试向量。下单
POST /v1/orders
| 字段 | 必填 | 说明 |
|---|---|---|
| merchant_no | 是 | 商户号,必须带 U 前缀(如 U60000001),不带前缀按参数错误处理 |
| pay_type | 是 | 产品号,如 "1001" |
| out_trade_no | 是 | 商户订单号,同商户内唯一,最长 28 |
| amount | 是 | 金额,单位元,两位小数字符串,如 "100.00" |
| notify_url | 是 | 回调地址,http/https,最长 120,主机不得为回环、内网、链路本地地址 |
| return_url | 否 | 付款完成后客人返回的地址,规则同 notify_url |
| client_ip | 建议 | 下单客人的 IP,强烈建议传 |
| subject | 否 | 订单标题,最长 60 |
| attach | 否 | 附加数据,最长 60,回调与查单时原样返回 |
| timestamp | 是 | Unix 秒 |
| nonce | 是 | 随机串 |
| sign | 是 | 签名 |
请求示例(向量 1,可直接拿去核对签名):
{
"merchant_no": "U60000001",
"pay_type": "1001",
"out_trade_no": "T20261205001",
"amount": "100.00",
"notify_url": "https://shop.example.com/notify",
"return_url": "https://shop.example.com/done",
"client_ip": "203.0.113.8",
"subject": "Test Order",
"attach": "uid=42",
"timestamp": "1796457600",
"nonce": "a1b2c3d4e5f60718",
"sign": "e42bb49d6dc5450b2a24e2579c5741341e74d4963c148710b13db5d2d5dff6af"
}
成功应答(sign 对 data 内字段签名,即向量 4):
{
"code": 0,
"msg": "ok",
"data": {
"trade_no": "UQ7XK2M9A3B5C8D4E",
"out_trade_no": "T20261205001",
"amount": "100.00",
"pay_url": "https://pay.alienpay.cc/c/UQ7XK2M9A3B5C8D4E",
"status": "pending"
},
"sign": "5d351cb29ae5459d34dbec89d2a5dbfb49665b446366aacc427f9553227e90a3"
}
引导客人打开 pay_url 完成付款。status 初始为 pending。
- 金额格式必须匹配
^[0-9]+\.[0-9]{2}$且大于 0:"100"、"100.0"、"+1.00"、"1e2"、负数、"0.00"都会返回 1002。 - 幂等:同商户同
out_trade_no且金额、pay_type相同,返回原订单(同一个trade_no与pay_url,code 为 0);参数不同返回 1004。
查单
POST /v1/orders/query
| 字段 | 必填 | 说明 |
|---|---|---|
| merchant_no | 是 | 商户号 |
| trade_no | 二选一 | 平台订单号。两者同时传以 trade_no 为准 |
| out_trade_no | 二选一 | 商户订单号 |
| timestamp / nonce / sign | 是 | 同下单 |
{
"merchant_no": "U60000001",
"out_trade_no": "T20261205001",
"timestamp": "1796457660",
"nonce": "9f8e7d6c5b4a3921",
"sign": "bb683d6a6ca73374dcc722bcda94c37c0e365cf63a41378e7b2aced355953b59"
}
应答 data 字段:
| 字段 | 说明 |
|---|---|
| trade_no | 平台订单号 |
| out_trade_no | 商户订单号 |
| amount | 订单金额(元) |
| paid_amount | 实付金额(元)。paid 时为实付数,其余状态为空 |
| status | 订单状态,见订单状态 |
| paid_at | 支付时间,Unix 秒;未付为空 |
| attach | 下单时的附加数据,原样返回 |
{
"code": 0,
"msg": "ok",
"data": {
"trade_no": "UQ7XK2M9A3B5C8D4E",
"out_trade_no": "T20261205001",
"amount": "100.00",
"paid_amount": "100.00",
"status": "paid",
"paid_at": "1796457700",
"attach": "uid=42"
},
"sign": "2214878369d5a4fbf107d2fc09fb515e9792ab8524a4d2a3ce93f705946c0ab8"
}
回调通知
订单支付成功后,我们向下单时的 notify_url 发起 POST(JSON):
| 字段 | 说明 |
|---|---|
| merchant_no | 商户号 |
| trade_no / out_trade_no | 平台订单号 / 商户订单号 |
| amount / paid_amount | 订单金额 / 实付金额(元) |
| status | 固定为 "paid" |
| paid_at | 支付时间,Unix 秒 |
| attach | 附加数据,原样回传 |
| timestamp / nonce / sign | 通知生成时刻 / 随机串 / 签名 |
示例(向量 5):
{
"merchant_no": "U60000001",
"trade_no": "UQ7XK2M9A3B5C8D4E",
"out_trade_no": "T20261205001",
"amount": "100.00",
"paid_amount": "100.00",
"status": "paid",
"paid_at": "1796457700",
"attach": "uid=42",
"timestamp": "1796457701",
"nonce": "c0ffee1234567890",
"sign": "921ccc1426b76a2a210a8bedefb38d59d909fba8d0bb3a4d4503d60034a7851f"
}
商户处理流程:
- 按签名规则验签,只验签名,不要做 300 秒时效校验(重发内容不变,timestamp 是通知生成时刻)。
- 核对
out_trade_no属于自己,并且本地订单尚未入账。 - 入账金额以
paid_amount为准(可能与amount不等,例如实付不同或补单)。只验签不够。 - 处理成功后,原样返回纯文本
OK(忽略大小写和首尾空格)。返回success等其他内容按失败处理并重发。 - 通知可能重复送达:已处理过的交易号(
trade_no)再次收到时,直接原样回OK,不要重复入账。
订单状态
| status | 含义 |
|---|---|
| pending | 待支付 |
| paid | 已支付。含补单成功;之后发生退款的仍报 paid,退款不走本接口 |
| closed | 已关闭、失败或超时 |
错误码
出错时 code 非 0,应答只有 code 和 msg,不带 data 与 sign:
{
"code": 1001,
"msg": "验签未通过"
}
| code | msg | 处理建议 |
|---|---|---|
| 1001 | 验签未通过 | 核对密钥、待签字符串(排序、空值、原值不转义) |
| 1002 | 参数缺失或格式不对 | 检查必填项、金额格式、回调地址 |
| 1003 | 商户不存在或已停用 | 核对商户号,联系客服 |
| 1004 | 订单号重复 | 同订单号参数不一致;换新订单号 |
| 1005 | 查无此单 | 核对订单号 |
| 1006 | 支付方式不可用 | 核对产品号,或稍后重试 |
| 1007 | 金额不在允许范围 | 调整金额 |
| 1008 | 请求已过期 | 校准服务器时间;nonce 不得重复使用 |
| 1099 | 服务暂不可用,请稍后重试 | 稍后重试;下单可用同一订单号重试,不会重复建单 |
补充规则
- 防重放:同一商户的
nonce在 600 秒内不得重复使用,重复请求返回 1008。 - 金额:以元为单位的两位小数字符串,不接受超过两位小数;请用定点小数类型处理,不要用浮点数。
- 编号:商户号为 "U" 加数字;平台订单号以 U 开头。
- 回调重发:未收到
OK会按固定节奏重发,内容(含 timestamp)不变。 - 入账:以回调或查单的
paid_amount为准入账,并校验订单属于自己、本地状态未入账。 - 地址限制:
notify_url与return_url只允许 http/https,最长 120,主机不得是回环、内网或链路本地地址。 - 应答签名:成功应答带顶层
sign,建议同样验签后再使用pay_url。
签名测试向量
以下向量由 tools/gen_vectors.py 实际计算得出,您的实现应得到完全相同的结果。可直接运行 python tools/gen_vectors.py 复现。
测试密钥:test_secret_0123456789abcdef(仅用于本文档向量,切勿用于真实商户)。
向量 1:下单请求
POST /v1/orders
待签字符串:
amount=100.00&attach=uid=42&client_ip=203.0.113.8&merchant_no=U60000001&nonce=a1b2c3d4e5f60718¬ify_url=https://shop.example.com/notify&out_trade_no=T20261205001&pay_type=1001&return_url=https://shop.example.com/done&subject=Test Order×tamp=1796457600签名:
e42bb49d6dc5450b2a24e2579c5741341e74d4963c148710b13db5d2d5dff6af完整 JSON:
{
"merchant_no": "U60000001",
"pay_type": "1001",
"out_trade_no": "T20261205001",
"amount": "100.00",
"notify_url": "https://shop.example.com/notify",
"return_url": "https://shop.example.com/done",
"client_ip": "203.0.113.8",
"subject": "Test Order",
"attach": "uid=42",
"timestamp": "1796457600",
"nonce": "a1b2c3d4e5f60718",
"sign": "e42bb49d6dc5450b2a24e2579c5741341e74d4963c148710b13db5d2d5dff6af"
}向量 2:下单请求(空值与 "0")
空串字段不参与签名,"0" 参与
待签字符串:
amount=0.01&attach=0&client_ip=203.0.113.8&merchant_no=U60000001&nonce=0f0e0d0c0b0a0908¬ify_url=https://shop.example.com/notify&out_trade_no=T20261205002&pay_type=1001×tamp=1796457600签名:
11f1788644daa20393f213bc509ae67c4bacd7a2fc143eceb16f2a9bbfa7608a完整 JSON:
{
"merchant_no": "U60000001",
"pay_type": "1001",
"out_trade_no": "T20261205002",
"amount": "0.01",
"notify_url": "https://shop.example.com/notify",
"return_url": "",
"client_ip": "203.0.113.8",
"subject": "",
"attach": "0",
"timestamp": "1796457600",
"nonce": "0f0e0d0c0b0a0908",
"sign": "11f1788644daa20393f213bc509ae67c4bacd7a2fc143eceb16f2a9bbfa7608a"
}向量 3:查单请求
POST /v1/orders/query
待签字符串:
merchant_no=U60000001&nonce=9f8e7d6c5b4a3921&out_trade_no=T20261205001×tamp=1796457660签名:
bb683d6a6ca73374dcc722bcda94c37c0e365cf63a41378e7b2aced355953b59完整 JSON:
{
"merchant_no": "U60000001",
"out_trade_no": "T20261205001",
"timestamp": "1796457660",
"nonce": "9f8e7d6c5b4a3921",
"sign": "bb683d6a6ca73374dcc722bcda94c37c0e365cf63a41378e7b2aced355953b59"
}向量 4:下单应答(对 data 签名)
应答顶层 sign 对 data 内字段签名
待签字符串:
amount=100.00&out_trade_no=T20261205001&pay_url=https://pay.alienpay.cc/c/UQ7XK2M9A3B5C8D4E&status=pending&trade_no=UQ7XK2M9A3B5C8D4E签名:
5d351cb29ae5459d34dbec89d2a5dbfb49665b446366aacc427f9553227e90a3完整 JSON:
{
"trade_no": "UQ7XK2M9A3B5C8D4E",
"out_trade_no": "T20261205001",
"amount": "100.00",
"pay_url": "https://pay.alienpay.cc/c/UQ7XK2M9A3B5C8D4E",
"status": "pending",
"sign": "5d351cb29ae5459d34dbec89d2a5dbfb49665b446366aacc427f9553227e90a3"
}向量 5:支付成功回调
我们 POST 到商户 notify_url
待签字符串:
amount=100.00&attach=uid=42&merchant_no=U60000001&nonce=c0ffee1234567890&out_trade_no=T20261205001&paid_amount=100.00&paid_at=1796457700&status=paid×tamp=1796457701&trade_no=UQ7XK2M9A3B5C8D4E签名:
921ccc1426b76a2a210a8bedefb38d59d909fba8d0bb3a4d4503d60034a7851f完整 JSON:
{
"merchant_no": "U60000001",
"trade_no": "UQ7XK2M9A3B5C8D4E",
"out_trade_no": "T20261205001",
"amount": "100.00",
"paid_amount": "100.00",
"status": "paid",
"paid_at": "1796457700",
"attach": "uid=42",
"timestamp": "1796457701",
"nonce": "c0ffee1234567890",
"sign": "921ccc1426b76a2a210a8bedefb38d59d909fba8d0bb3a4d4503d60034a7851f"
}代码示例
五种语言的最小示例,包含签名、下单、验回调。每个文件直接运行(main 或命令行)会打印向量 1 的签名,应为 e42bb49d6dc5450b2a24e2579c5741341e74d4963c148710b13db5d2d5dff6af。示例文件也可在 examples/ 目录下载。
<?php
const UFO_BASE = 'https://api.alienpay.cc';
function ufo_sign(array $p, string $secret): string {
unset($p['sign']);
$p = array_filter($p, fn($v) => $v !== '' && $v !== null);
ksort($p, SORT_STRING);
$pairs = [];
foreach ($p as $k => $v) { $pairs[] = $k . '=' . $v; }
return hash_hmac('sha256', implode('&', $pairs), $secret);
}
function ufo_post(string $path, array $p, string $secret) {
$p['timestamp'] = (string)time();
$p['nonce'] = bin2hex(random_bytes(8));
$p['sign'] = ufo_sign($p, $secret);
$ch = curl_init(UFO_BASE . $path);
curl_setopt_array($ch, [CURLOPT_POST => true, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode($p, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)]);
$r = curl_exec($ch);
curl_close($ch);
return json_decode($r, true);
}
function ufo_create_order(string $secret) {
return ufo_post('/v1/orders', ['merchant_no' => 'U60000001', 'pay_type' => '1001',
'out_trade_no' => 'T20261205001', 'amount' => '100.00',
'notify_url' => 'https://shop.example.com/notify', 'client_ip' => '203.0.113.8'], $secret);
// 成功时 data.pay_url 即付款链接
}
// 回调:$body = json_decode(file_get_contents('php://input'), true);通过后 echo 'OK';
function ufo_verify_callback(array $body, string $secret): bool {
return hash_equals(ufo_sign($body, $secret), (string)($body['sign'] ?? ''));
}
if (PHP_SAPI === 'cli' && realpath($_SERVER['SCRIPT_FILENAME']) === __FILE__) { // 自测:应输出向量 1 签名
echo ufo_sign(['merchant_no' => 'U60000001', 'pay_type' => '1001', 'out_trade_no' => 'T20261205001',
'amount' => '100.00', 'notify_url' => 'https://shop.example.com/notify',
'return_url' => 'https://shop.example.com/done', 'client_ip' => '203.0.113.8',
'subject' => 'Test Order', 'attach' => 'uid=42', 'timestamp' => '1796457600',
'nonce' => 'a1b2c3d4e5f60718'], 'test_secret_0123456789abcdef'), "\n";
}
import java.net.URI;
import java.net.http.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.*;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class Ufo {
static final String BASE = "https://api.alienpay.cc";
static String sign(Map<String, String> p, String secret) throws Exception {
StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> e : new TreeMap<>(p).entrySet()) { // TreeMap 按字典序
if (e.getKey().equals("sign") || e.getValue() == null || e.getValue().isEmpty()) continue;
if (sb.length() > 0) sb.append('&');
sb.append(e.getKey()).append('=').append(e.getValue());
}
Mac m = Mac.getInstance("HmacSHA256");
m.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
return HexFormat.of().formatHex(m.doFinal(sb.toString().getBytes(StandardCharsets.UTF_8)));
}
static String json(Map<String, String> p) { // 全是字符串字段,手写即可;项目里可换 Jackson
StringJoiner j = new StringJoiner(",", "{", "}");
for (Map.Entry<String, String> e : p.entrySet())
j.add("\"" + e.getKey() + "\":\"" + e.getValue().replace("\\", "\\\\").replace("\"", "\\\"") + "\"");
return j.toString();
}
static String post(String path, Map<String, String> params, String secret) throws Exception {
Map<String, String> p = new HashMap<>(params);
byte[] n = new byte[8];
new SecureRandom().nextBytes(n);
p.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000));
p.put("nonce", HexFormat.of().formatHex(n));
p.put("sign", sign(p, secret));
HttpRequest r = HttpRequest.newBuilder(URI.create(BASE + path))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json(p))).build();
return HttpClient.newHttpClient().send(r, HttpResponse.BodyHandlers.ofString()).body();
}
static String createOrder(String secret) throws Exception {
Map<String, String> p = new HashMap<>();
p.put("merchant_no", "U60000001"); p.put("pay_type", "1001");
p.put("out_trade_no", "T20261205001"); p.put("amount", "100.00");
p.put("notify_url", "https://shop.example.com/notify"); p.put("client_ip", "203.0.113.8");
return post("/v1/orders", p, secret); // 返回 JSON 文本,用你的 JSON 库取 data.pay_url
}
// body 为回调 JSON 解析出的 Map;通过后回复纯文本 OK
static boolean verifyCallback(Map<String, String> body, String secret) throws Exception {
return MessageDigest.isEqual(sign(body, secret).getBytes(StandardCharsets.UTF_8),
String.valueOf(body.get("sign")).getBytes(StandardCharsets.UTF_8));
}
public static void main(String[] a) throws Exception { // 自测:应输出文档里的向量 1 签名
Map<String, String> p = new HashMap<>();
p.put("merchant_no", "U60000001"); p.put("pay_type", "1001"); p.put("out_trade_no", "T20261205001");
p.put("amount", "100.00"); p.put("notify_url", "https://shop.example.com/notify");
p.put("return_url", "https://shop.example.com/done"); p.put("client_ip", "203.0.113.8");
p.put("subject", "Test Order"); p.put("attach", "uid=42"); p.put("timestamp", "1796457600");
p.put("nonce", "a1b2c3d4e5f60718");
System.out.println(sign(p, "test_secret_0123456789abcdef"));
}
}
import hashlib, hmac, json, secrets, time, urllib.request
BASE = "https://api.alienpay.cc"
def sign(params, secret):
s = "&".join(f"{k}={params[k]}" for k in sorted(params) if k != "sign" and params[k] != "")
return hmac.new(secret.encode(), s.encode(), hashlib.sha256).hexdigest()
def post(path, params, secret):
params = dict(params, timestamp=str(int(time.time())), nonce=secrets.token_hex(8))
params["sign"] = sign(params, secret)
req = urllib.request.Request(BASE + path, json.dumps(params).encode(),
{"Content-Type": "application/json"})
return json.load(urllib.request.urlopen(req, timeout=15))
def create_order(secret):
return post("/v1/orders", {
"merchant_no": "U60000001", "pay_type": "1001", "out_trade_no": "T20261205001",
"amount": "100.00", "notify_url": "https://shop.example.com/notify",
"client_ip": "203.0.113.8"}, secret) # 成功时 data.pay_url 即付款链接
def verify_callback(body, secret):
"""body 为解析后的 dict;通过后须再核对订单归属、按 paid_amount 入账、已处理过的 trade_no 直接回 OK、不重复入账。"""
return hmac.compare_digest(sign(body, secret), str(body.get("sign", "")))
# 验证通过后向 UFOPay 回复纯文本 OK
if __name__ == "__main__": # 自测:应输出文档里的向量 1 签名
print(sign({"merchant_no": "U60000001", "pay_type": "1001", "out_trade_no": "T20261205001",
"amount": "100.00", "notify_url": "https://shop.example.com/notify",
"return_url": "https://shop.example.com/done", "client_ip": "203.0.113.8",
"subject": "Test Order", "attach": "uid=42", "timestamp": "1796457600",
"nonce": "a1b2c3d4e5f60718"}, "test_secret_0123456789abcdef"))
const crypto = require('crypto');
const BASE = 'https://api.alienpay.cc';
function sign(params, secret) {
const s = Object.keys(params).filter(k => k !== 'sign' && params[k] !== '').sort()
.map(k => `${k}=${params[k]}`).join('&');
return crypto.createHmac('sha256', secret).update(s, 'utf8').digest('hex');
}
async function post(path, params, secret) { // Node 18+
const p = { ...params, timestamp: String(Math.floor(Date.now() / 1000)),
nonce: crypto.randomBytes(8).toString('hex') };
p.sign = sign(p, secret);
const r = await fetch(BASE + path, { method: 'POST',
headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(p) });
return r.json();
}
function createOrder(secret) {
return post('/v1/orders', { merchant_no: 'U60000001', pay_type: '1001',
out_trade_no: 'T20261205001', amount: '100.00',
notify_url: 'https://shop.example.com/notify', client_ip: '203.0.113.8' }, secret);
// 成功时 data.pay_url 即付款链接
}
function verifyCallback(body, secret) { // body 为解析后的对象;通过后回复纯文本 OK
const a = Buffer.from(sign(body, secret)), b = Buffer.from(String(body.sign || ''));
return a.length === b.length && crypto.timingSafeEqual(a, b);
}
module.exports = { sign, createOrder, verifyCallback };
if (require.main === module) { // 自测:应输出文档里的向量 1 签名
console.log(sign({ merchant_no: 'U60000001', pay_type: '1001', out_trade_no: 'T20261205001',
amount: '100.00', notify_url: 'https://shop.example.com/notify',
return_url: 'https://shop.example.com/done', client_ip: '203.0.113.8',
subject: 'Test Order', attach: 'uid=42', timestamp: '1796457600',
nonce: 'a1b2c3d4e5f60718' }, 'test_secret_0123456789abcdef'));
}
package main
import (
"bytes"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"sort"
"strconv"
"strings"
"time"
)
const base = "https://api.alienpay.cc"
func sign(p map[string]string, secret string) string {
keys := []string{}
for k, v := range p {
if k != "sign" && v != "" {
keys = append(keys, k)
}
}
sort.Strings(keys)
parts := make([]string, len(keys))
for i, k := range keys {
parts[i] = k + "=" + p[k]
}
m := hmac.New(sha256.New, []byte(secret))
m.Write([]byte(strings.Join(parts, "&")))
return hex.EncodeToString(m.Sum(nil))
}
func post(path string, p map[string]string, secret string) (map[string]any, error) {
n := make([]byte, 8)
rand.Read(n)
p["timestamp"] = strconv.FormatInt(time.Now().Unix(), 10)
p["nonce"] = hex.EncodeToString(n)
p["sign"] = sign(p, secret)
b, _ := json.Marshal(p)
r, err := http.Post(base+path, "application/json", bytes.NewReader(b))
if err != nil {
return nil, err
}
defer r.Body.Close()
var out map[string]any
return out, json.NewDecoder(r.Body).Decode(&out)
}
func createOrder(secret string) (map[string]any, error) {
return post("/v1/orders", map[string]string{"merchant_no": "U60000001", "pay_type": "1001",
"out_trade_no": "T20261205001", "amount": "100.00",
"notify_url": "https://shop.example.com/notify", "client_ip": "203.0.113.8"}, secret)
// 成功时 data.pay_url 即付款链接
}
// body 为回调 JSON 解码成的 map[string]string;通过后回复纯文本 OK
func verifyCallback(body map[string]string, secret string) bool {
return hmac.Equal([]byte(sign(body, secret)), []byte(body["sign"]))
}
func main() { // 自测:应输出文档里的向量 1 签名
fmt.Println(sign(map[string]string{"merchant_no": "U60000001", "pay_type": "1001",
"out_trade_no": "T20261205001", "amount": "100.00", "notify_url": "https://shop.example.com/notify",
"return_url": "https://shop.example.com/done", "client_ip": "203.0.113.8", "subject": "Test Order",
"attach": "uid=42", "timestamp": "1796457600", "nonce": "a1b2c3d4e5f60718"},
"test_secret_0123456789abcdef"))
}
联调说明
- 从客服处拿到商户号与密钥,在商户后台确认产品号。密钥只放服务端,不要写进网页或 App。
- 先验算法:把上面的测试密钥
test_secret_0123456789abcdef与向量 1 的参数代入您的签名函数,结果必须等于e42bb49d6dc5450b2a24e2579c5741341e74d4963c148710b13db5d2d5dff6af;再核对向量 2(空值与 "0")和向量 5(回调)。 - 再换真密钥:用自己的商户号和密钥调用下单,应答
code为 0 即签名与参数通过。出现 1001 先检查待签字符串;1008 先校准服务器时间。 - 走通支付:小额下单,打开
pay_url完成付款,确认您的notify_url收到回调、验签通过并返回OK。 - 查单对照:付款后调用查单,核对
status、paid_amount与回调一致。 - 幂等与重发:故意不返回
OK观察重发,确认已处理过的trade_no再次到达时只回OK、不重复入账。 - 回调地址必须公网可达;本机开发请使用内网穿透工具提供公网地址。