fix(payment): 增加支付配置诊断并拦截取消中的订单
- isConfigured 拆出 getConfigDiagnostics,日志列出缺失项且不输出密钥 - 证书路径支持 WXPAY_PRIVATE_KEY_PATH / WXPAY_PUBLIC_KEY_PATH 覆盖 - 已申请取消或已取消的订单不再回刷 paid,也不允许发起支付 - 发起支付与查询退款未配置时输出诊断日志
This commit is contained in:
+59
-19
@@ -54,21 +54,31 @@ function resolvePath(value) {
|
||||
return path.isAbsolute(value) ? value : path.resolve(PROJECT_ROOT, value);
|
||||
}
|
||||
|
||||
// 证书路径允许用环境变量覆盖:仓库里的 conf.json 面向部署机(Linux 路径),
|
||||
// 本地开发可用 .env 指向本机证书,避免为了本地调试改回仓库配置。
|
||||
function privateKeyPathValue() {
|
||||
return process.env.WXPAY_PRIVATE_KEY_PATH || payConfig().privateKeyPath || "";
|
||||
}
|
||||
|
||||
function publicKeyPathValue() {
|
||||
return process.env.WXPAY_PUBLIC_KEY_PATH || payConfig().publicKeyPath || "";
|
||||
}
|
||||
|
||||
let cachedPrivateKey = null;
|
||||
let cachedPublicKey = null;
|
||||
|
||||
function loadPrivateKey() {
|
||||
const cfg = payConfig();
|
||||
if (!cachedPrivateKey && cfg.privateKeyPath) {
|
||||
cachedPrivateKey = readFileSync(resolvePath(cfg.privateKeyPath), "utf-8");
|
||||
const privatePath = privateKeyPathValue();
|
||||
if (!cachedPrivateKey && privatePath) {
|
||||
cachedPrivateKey = readFileSync(resolvePath(privatePath), "utf-8");
|
||||
}
|
||||
return cachedPrivateKey;
|
||||
}
|
||||
|
||||
function loadPublicKey() {
|
||||
const cfg = payConfig();
|
||||
if (!cachedPublicKey && cfg.publicKeyPath) {
|
||||
cachedPublicKey = readFileSync(resolvePath(cfg.publicKeyPath), "utf-8");
|
||||
const publicPath = publicKeyPathValue();
|
||||
if (!cachedPublicKey && publicPath) {
|
||||
cachedPublicKey = readFileSync(resolvePath(publicPath), "utf-8");
|
||||
}
|
||||
return cachedPublicKey;
|
||||
}
|
||||
@@ -86,24 +96,53 @@ function apiV3Key() {
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置诊断:只返回缺失项名称,不输出密钥内容,便于线上日志定位
|
||||
* @returns {string[]} 缺失/不可用项
|
||||
*/
|
||||
function getConfigDiagnostics() {
|
||||
const cfg = payConfig();
|
||||
const issues = [];
|
||||
if (!cfg.mchid) issues.push("mchid 未配置");
|
||||
if (!cfg.appid) issues.push("appid 未配置");
|
||||
if (!cfg.merchantSerial) issues.push("merchantSerial 未配置");
|
||||
if (!cfg.publicKeyId) issues.push("publicKeyId 未配置");
|
||||
|
||||
const privatePath = resolvePath(privateKeyPathValue());
|
||||
if (!privatePath) {
|
||||
issues.push("privateKeyPath 未配置");
|
||||
} else {
|
||||
try {
|
||||
readFileSync(privatePath, "utf-8");
|
||||
} catch (err) {
|
||||
issues.push(`商户私钥不可读:${privatePath}`);
|
||||
}
|
||||
}
|
||||
|
||||
const publicPath = resolvePath(publicKeyPathValue());
|
||||
if (!publicPath) {
|
||||
issues.push("publicKeyPath 未配置");
|
||||
} else {
|
||||
try {
|
||||
readFileSync(publicPath, "utf-8");
|
||||
} catch (err) {
|
||||
issues.push(`微信支付公钥不可读:${publicPath}`);
|
||||
}
|
||||
}
|
||||
|
||||
const envKey = process.env[cfg.apiv3KeyEnv || "WXPAY_APIV3_KEY"] || "";
|
||||
if (envKey.length !== 32) {
|
||||
issues.push(`环境变量 ${cfg.apiv3KeyEnv || "WXPAY_APIV3_KEY"} 缺失或长度不为 32`);
|
||||
}
|
||||
return issues;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置是否完整(商户参数 + 两把证书文件 + APIv3 密钥)
|
||||
* 缺配置时支付接口降级为「未开放」,不影响下单、取消等其它链路。
|
||||
*/
|
||||
function isConfigured() {
|
||||
const cfg = payConfig();
|
||||
if (!cfg.mchid || !cfg.appid || !cfg.merchantSerial || !cfg.publicKeyId) return false;
|
||||
const privatePath = resolvePath(cfg.privateKeyPath);
|
||||
const publicPath = resolvePath(cfg.publicKeyPath);
|
||||
if (!privatePath || !publicPath) return false;
|
||||
try {
|
||||
readFileSync(privatePath, "utf-8");
|
||||
readFileSync(publicPath, "utf-8");
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
const envKey = process.env[cfg.apiv3KeyEnv || "WXPAY_APIV3_KEY"] || "";
|
||||
return envKey.length === 32;
|
||||
return getConfigDiagnostics().length === 0;
|
||||
}
|
||||
|
||||
function requireFullConfig() {
|
||||
@@ -513,6 +552,7 @@ function verifyNotify({ headers = {}, rawBody = "" }) {
|
||||
export {
|
||||
WechatPayError,
|
||||
isConfigured,
|
||||
getConfigDiagnostics,
|
||||
yuanToFen,
|
||||
fenToYuan,
|
||||
formatTimeExpire,
|
||||
|
||||
Reference in New Issue
Block a user