packServer/backend/app/lib/wxpay-xml.js
liufumingren 8949b7198c feat(pay): 微信 Native 扫码支付(下单出码 + 回调入账 + 扫码轮询)
- 新增 service/wxpay.js:统一下单 / 查询订单 / 关闭订单 / APIv2 验签,二维码在服务端本地生成 SVG
- 新增 pay/wechat.js 与 XML 编解码,公开回调 POST /api/pay/wechat/notify
- 订单:同主体 + 同套餐复用未超时待支付单;2 小时超时关单;回调 / 查单 / 手工确认统一走 payNotify
- 迁移 subject_orders 增加 code_url / prepay_id 等字段,配置项 WXPAY_*
- 前端续期弹窗展示二维码并按 2s 轮询支付结果
- 文档与测试同步更新

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-07 16:23:03 +08:00

90 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 微信支付 APIv2 的**扁平 XML** 编解码(无第三方依赖)
*
* v2 的请求/响应都只有一层 map(`<xml><a>1</a><b>2</b></xml>`),值可能包 CDATA,
* 所以不需要通用 XML 解析器:按「叶子节点」正则提取即可,不会把根节点当成字段。
*/
/**
* 生成 `<xml><k><![CDATA[v]]></k></xml>`
* @param {object} params 字段表(undefined / null 跳过)
* @return {string} XML
*/
function buildXml(params = {}) {
const items = Object.keys(params)
.filter(key => params[key] !== undefined && params[key] !== null)
.map(key => `<${key}><![CDATA[${String(params[key])}]]></${key}>`);
return `<xml>${items.join('')}</xml>`;
}
/**
* 解析扁平 XML(值可以是 CDATA 或普通文本)
* @param {string|Buffer} xml XML 源码
* @return {object} 字段表;解析不出任何字段时返回 {}
*/
function parseXml(xml) {
const out = {};
const text = String(xml || '');
// 非 CDATA 分支用 [^<]* 排除标签,避免把 <xml> 根节点当成一个字段(否则内层字段会被跳过)
const pattern = /<([A-Za-z_][\w.-]*)>\s*(?:<!\[CDATA\[([\s\S]*?)\]\]>|([^<]*))\s*<\/\1>/g;
let matched;
while ((matched = pattern.exec(text))) {
const key = matched[1];
if (key === 'xml') continue;
const value = matched[2] !== undefined ? matched[2] : matched[3];
out[key] = String(value).trim();
}
return out;
}
/**
* 微信回调/返回里的时间(`yyyyMMddHHmmss`,北京时间)→ Date
* @param {string} value 时间字符串
* @return {Date|null} 解析失败返回 null
*/
function parseWxTime(value) {
const matched = /^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/.exec(String(value || '').trim());
if (!matched) return null;
const [ , y, m, d, hh, mm, ss ] = matched;
// 微信时间固定是 +08:00
return new Date(Date.UTC(Number(y), Number(m) - 1, Number(d), Number(hh) - 8, Number(mm), Number(ss)));
}
/**
* Date → 微信时间字符串(`yyyyMMddHHmmss`,北京时间)
* @param {Date|string|number} date 时间
* @return {string|null} 无效时间返回 null
*/
function formatWxTime(date) {
const ms = date instanceof Date ? date.getTime() : new Date(date).getTime();
if (!Number.isFinite(ms)) return null;
const utc8 = new Date(ms + 8 * 3600 * 1000);
const pad = num => String(num).padStart(2, '0');
return `${utc8.getUTCFullYear()}${pad(utc8.getUTCMonth() + 1)}${pad(utc8.getUTCDate())}`
+ `${pad(utc8.getUTCHours())}${pad(utc8.getUTCMinutes())}${pad(utc8.getUTCSeconds())}`;
}
/**
* 按 UTF-8 字节数截断(微信 body 限 128 字节,中文一个字 3 字节)
* @param {string} value 原文
* @param {number} maxBytes 上限字节数
* @return {string} 截断后的字符串
*/
function truncateUtf8(value, maxBytes = 128) {
const text = String(value || '');
if (Buffer.byteLength(text, 'utf8') <= maxBytes) return text;
let out = '';
let used = 0;
for (const char of text) {
const size = Buffer.byteLength(char, 'utf8');
if (used + size > maxBytes) break;
out += char;
used += size;
}
return out;
}
module.exports = { buildXml, parseXml, parseWxTime, formatWxTime, truncateUtf8 };