代付回调
代付回调采用和收款回调相同的签名头和重试机制。
平台仅在代付进入 status=confirmed 或 status=failed 终态时回调。
请求头
| 字段名称 | 字段类型 | 是否必填 | 说明 |
|---|---|---|---|
| content-type | application/json | 是 | 固定为 application/json。 |
| x-callback-timestamp | string | 是 | 回调发送时的毫秒时间戳。 |
| x-callback-nonce | string | 是 | 回调随机串。 |
| x-callback-signature | string | 是 | HMAC-SHA256(apiKey, timestamp + "." + nonce + "." + rawBody)。 |
| x-callback-signature-algorithm | string | 是 | 固定为 HMAC-SHA256。 |
| x-callback-signature-version | string | 是 | 固定为 v1。 |
接口参数
| 字段名称 | 字段类型 | 是否必填 | 说明 |
|---|---|---|---|
| merchantUid | string | 是 | Merchant UID。 |
| orderNo | string | 是 | 平台代付订单号。 |
| merchantOrderNo | string | null | 是 | 商户订单号。 |
| chainCode | string | 是 | 链编码。 |
| tokenSymbol | string | 是 | 代币符号。 |
| fromAddress | string | null | 是 | 付款地址。 |
| toAddress | string | 是 | 收款地址。 |
| txHash | string | null | 否 | 链上哈希。 |
| amount | string | 是 | 代付金额。 |
| status | string | 是 | 代付终态:confirmed 或 failed。 |
| failedStage | string | 否 | 仅 status=failed 时返回。 |
请求示例
{
"merchantUid": "880001",
"orderNo": "PO1776193200A1B2C3D4",
"merchantOrderNo": "PO202604150001",
"chainCode": "TRON",
"tokenSymbol": "USDT",
"fromAddress": "TNjBrNq2a9FK2vwu4W9C1QdLKSZ42Yx5YY",
"toAddress": "TS7b7iD8G2PaPqK1TqSmLJ9nrrYH4oKX1S",
"txHash": "b3d4fb0cb11eb86bb906d1e0ee3b84e17f40d5441d1e4c651ea46920a48a9a01",
"amount": "35.500000",
"status": "confirmed"
}接口返回
| 字段名称 | 字段类型 | 是否必填 | 说明 |
|---|---|---|---|
| HTTP Status | 200-299 | 是 | 任意 2xx 都视为成功。 |
| Response Body | string | json | 否 | 内容可自定义。 |
返回示例
HTTP/1.1 200 OK
Content-Type: text/plain
success代码示例
运行:Java 保存为 UUGateExample.java(回调为 UUGateCallback.java),使用 java 文件名;PHP 保存为 example.php,使用 php example.php;Go 保存为 example.go,使用 go run example.go;Node.js 保存为 example.mjs,使用 node example.mjs。回调原文从标准输入读取。
import { createHmac, timingSafeEqual } from 'node:crypto';
function required(name) {
const value = process.env[name]?.trim();
if (!value) throw new Error(name + ' is required');
return value;
}
const key = required('UUGATE_API_KEY');
const timestamp = required('UUGATE_CALLBACK_TIMESTAMP'), nonce = required('UUGATE_CALLBACK_NONCE');
const signature = required('UUGATE_CALLBACK_SIGNATURE');
const chunks = [];
for await (const chunk of process.stdin) chunks.push(chunk);
const rawBody = Buffer.concat(chunks);
const expected = createHmac('sha256', key).update(timestamp + '.' + nonce + '.', 'utf8').update(rawBody).digest();
const actual = Buffer.from(signature, 'hex');
if (!/^[a-fA-F0-9]{64}$/.test(signature) || actual.length !== expected.length || !timingSafeEqual(expected, actual))
throw new Error('Invalid callback signature');
console.log('VERIFIED');import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.HexFormat;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class UUGateCallback {
static String required(String name) {
String value = System.getenv(name);
if (value == null || value.isBlank()) throw new IllegalArgumentException(name + " is required");
return value.trim();
}
public static void main(String[] args) throws Exception {
String key = required("UUGATE_API_KEY");
String timestamp = required("UUGATE_CALLBACK_TIMESTAMP"), nonce = required("UUGATE_CALLBACK_NONCE");
String signature = required("UUGATE_CALLBACK_SIGNATURE");
byte[] rawBody = System.in.readAllBytes();
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
mac.update((timestamp + "." + nonce + ".").getBytes(StandardCharsets.UTF_8));
byte[] expected = mac.doFinal(rawBody);
if (!signature.matches("[a-fA-F0-9]{64}") ||
!MessageDigest.isEqual(expected, HexFormat.of().parseHex(signature)))
throw new IllegalArgumentException("Invalid callback signature");
System.out.println("VERIFIED");
}
}<?php
function required(string $name): string {
$value = trim((string) getenv($name));
if ($value === '') throw new RuntimeException($name . ' is required');
return $value;
}
$key = required('UUGATE_API_KEY');
$timestamp = required('UUGATE_CALLBACK_TIMESTAMP'); $nonce = required('UUGATE_CALLBACK_NONCE');
$signature = required('UUGATE_CALLBACK_SIGNATURE');
$rawBody = file_get_contents('php://stdin');
if ($rawBody === false) throw new RuntimeException('Cannot read raw body');
$expected = hash_hmac('sha256', $timestamp . '.' . $nonce . '.' . $rawBody, $key);
if (!preg_match('/^[a-fA-F0-9]{64}$/D', $signature) || !hash_equals($expected, strtolower($signature)))
throw new RuntimeException('Invalid callback signature');
echo 'VERIFIED', PHP_EOL;package main
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"strings"
)
func required(name string) string {
value := strings.TrimSpace(os.Getenv(name))
if value == "" { panic(name + " is required") }
return value
}
func main() {
key := required("UUGATE_API_KEY")
timestamp, nonce := required("UUGATE_CALLBACK_TIMESTAMP"), required("UUGATE_CALLBACK_NONCE")
signature := required("UUGATE_CALLBACK_SIGNATURE")
rawBody, err := io.ReadAll(os.Stdin); if err != nil { panic(err) }
mac := hmac.New(sha256.New, []byte(key))
mac.Write([]byte(timestamp + "." + nonce + ".")); mac.Write(rawBody)
actual, err := hex.DecodeString(signature)
if err != nil || !hmac.Equal(mac.Sum(nil), actual) { panic("Invalid callback signature") }
fmt.Println("VERIFIED")
}说明
- 请按 orderNo 做幂等处理;系统只自动推送 confirmed 或 failed 终态。
- 以下代码只演示回调验签:将原始请求体作为标准输入,回调头映射到 UUGATE_CALLBACK_TIMESTAMP、UUGATE_CALLBACK_NONCE、UUGATE_CALLBACK_SIGNATURE,UUGATE_API_KEY 使用创建该订单时的 API Key。验签后仍须核对订单并事务幂等持久化,完成后才返回 HTTP 2xx。