- queryPayment 高频轮询加短 TTL 缓存 + 同单号并发合并, 避免把支付页轮询放大成微信侧流量;失败不缓存 - closed 也允许回写流水:关单与支付成功存在竞态, 以微信侧明确终结的交易状态收敛本地流水
181 lines
5.3 KiB
JavaScript
181 lines
5.3 KiB
JavaScript
import { test } from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import crypto from "node:crypto";
|
|
|
|
import {
|
|
yuanToFen,
|
|
fenToYuan,
|
|
formatTimeExpire,
|
|
buildRequestSignature,
|
|
buildAuthHeader,
|
|
verifyMessageSignature,
|
|
decryptAesGcm,
|
|
} from "../utils/wechatpay.js";
|
|
import { createOrderQueryCache } from "../handler/payment.js";
|
|
|
|
test("元转分按小数点拆位,不经过浮点", () => {
|
|
assert.equal(yuanToFen("500.00"), 50000);
|
|
assert.equal(yuanToFen("500.5"), 50050);
|
|
assert.equal(yuanToFen(500), 50000);
|
|
assert.equal(yuanToFen("0.01"), 1);
|
|
assert.equal(yuanToFen("0.00"), 0);
|
|
// 超过两位小数、非数字、负数一律非法
|
|
assert.equal(yuanToFen("500.001"), null);
|
|
assert.equal(yuanToFen("abc"), null);
|
|
assert.equal(yuanToFen("-1"), null);
|
|
assert.equal(yuanToFen(""), null);
|
|
});
|
|
|
|
test("分转元固定两位小数", () => {
|
|
assert.equal(fenToYuan(50000), "500.00");
|
|
assert.equal(fenToYuan(1), "0.01");
|
|
assert.equal(fenToYuan(1.5), "");
|
|
});
|
|
|
|
test("time_expire 输出 RFC3339 北京时间", () => {
|
|
assert.equal(formatTimeExpire(new Date("2026-11-06T02:00:00.000Z")), "2026-11-06T10:00:00+08:00");
|
|
assert.equal(formatTimeExpire("invalid"), "");
|
|
});
|
|
|
|
test("请求签名可被同名公钥验证", () => {
|
|
const { privateKey, publicKey } = crypto.generateKeyPairSync("rsa", { modulusLength: 2048 });
|
|
const privatePem = privateKey.export({ type: "pkcs8", format: "pem" });
|
|
const publicPem = publicKey.export({ type: "spki", format: "pem" });
|
|
|
|
const method = "POST";
|
|
const urlPath = "/v3/pay/transactions/jsapi";
|
|
const timestamp = "1700000000";
|
|
const nonce = "abc123";
|
|
const body = '{"appid":"wx-test"}';
|
|
|
|
const signature = buildRequestSignature({ method, urlPath, timestamp, nonce, body, privateKey: privatePem });
|
|
const message = `${method}\n${urlPath}\n${timestamp}\n${nonce}\n${body}\n`;
|
|
assert.equal(verifyMessageSignature({ message, signature, publicKeyPem: publicPem }), true);
|
|
|
|
// 报文被篡改后验签失败
|
|
const tampered = `${method}\n${urlPath}\n${timestamp}\n${nonce}\n${body.replace("wx-test", "wx-evil")}\n`;
|
|
assert.equal(verifyMessageSignature({ message: tampered, signature, publicKeyPem: publicPem }), false);
|
|
});
|
|
|
|
test("Authorization 头使用 WECHATPAY2-SHA256-RSA2048 格式", () => {
|
|
const header = buildAuthHeader({
|
|
mchid: "1900000109",
|
|
serialNo: "SERIAL",
|
|
signature: "SIG",
|
|
timestamp: "1700000000",
|
|
nonce: "abc123",
|
|
});
|
|
assert.equal(
|
|
header,
|
|
'WECHATPAY2-SHA256-RSA2048 mchid="1900000109",nonce_str="abc123",signature="SIG",timestamp="1700000000",serial_no="SERIAL"'
|
|
);
|
|
});
|
|
|
|
test("AES-256-GCM 解密与 authTag 校验", () => {
|
|
const key = crypto.randomBytes(32);
|
|
const nonce = "abcdefghijkl";
|
|
const associatedData = "transaction";
|
|
const plaintext = JSON.stringify({ out_trade_no: "order-1", amount: { total: 1 } });
|
|
|
|
const cipher = crypto.createCipheriv("aes-256-gcm", key, Buffer.from(nonce, "utf-8"));
|
|
cipher.setAAD(Buffer.from(associatedData, "utf-8"));
|
|
const encrypted = Buffer.concat([cipher.update(plaintext, "utf-8"), cipher.final()]);
|
|
const authTag = cipher.getAuthTag();
|
|
const ciphertext = Buffer.concat([encrypted, authTag]).toString("base64");
|
|
|
|
const decrypted = decryptAesGcm({
|
|
ciphertext,
|
|
nonce,
|
|
associatedData,
|
|
apiV3Key: key,
|
|
});
|
|
assert.equal(decrypted, plaintext);
|
|
|
|
// 篡改密文 / 错误 authTag 必须抛错
|
|
const broken = Buffer.from(ciphertext, "base64");
|
|
broken[broken.length - 1] ^= 0xff;
|
|
assert.throws(() => decryptAesGcm({
|
|
ciphertext: broken.toString("base64"),
|
|
nonce,
|
|
associatedData,
|
|
apiV3Key: key,
|
|
}));
|
|
});
|
|
|
|
test("轮询查单在 TTL 内复用结果,过期后重新查询", async () => {
|
|
let calls = 0;
|
|
let current = 1000;
|
|
const query = createOrderQueryCache({
|
|
ttl: 3000,
|
|
now: () => current,
|
|
query: async (outTradeNo) => {
|
|
calls += 1;
|
|
return { outTradeNo, trade_state: "NOTPAY" };
|
|
},
|
|
});
|
|
|
|
const first = await query("T1");
|
|
current = 1500;
|
|
const second = await query("T1");
|
|
|
|
assert.equal(first, second);
|
|
assert.equal(calls, 1);
|
|
|
|
current = 4001;
|
|
await query("T1");
|
|
assert.equal(calls, 2);
|
|
});
|
|
|
|
test("同一订单的并发轮询共享一次上游查单", async () => {
|
|
let calls = 0;
|
|
let release;
|
|
const query = createOrderQueryCache({
|
|
query: () => {
|
|
calls += 1;
|
|
return new Promise((resolve) => {
|
|
release = () => resolve({ trade_state: "NOTPAY" });
|
|
});
|
|
},
|
|
});
|
|
|
|
const first = query("T2");
|
|
const second = query("T2");
|
|
|
|
await new Promise((resolve) => setImmediate(resolve));
|
|
assert.equal(calls, 1);
|
|
|
|
release();
|
|
const [a, b] = await Promise.all([first, second]);
|
|
assert.equal(a, b);
|
|
});
|
|
|
|
test("查单失败不写缓存,下一次仍会重试", async () => {
|
|
let calls = 0;
|
|
const query = createOrderQueryCache({
|
|
query: async () => {
|
|
calls += 1;
|
|
if (calls === 1) throw new Error("network down");
|
|
return { trade_state: "NOTPAY" };
|
|
},
|
|
});
|
|
|
|
await assert.rejects(query("T3"), /network down/);
|
|
await query("T3");
|
|
assert.equal(calls, 2);
|
|
});
|
|
|
|
test("force 可绕过轮询缓存", async () => {
|
|
let calls = 0;
|
|
const query = createOrderQueryCache({
|
|
ttl: 60_000,
|
|
query: async () => {
|
|
calls += 1;
|
|
return { trade_state: "NOTPAY" };
|
|
},
|
|
});
|
|
|
|
await query("T4");
|
|
await query("T4", { force: true });
|
|
assert.equal(calls, 2);
|
|
});
|