From 983e55b67250c2a64d79430441ea7c7c13dd91b7 Mon Sep 17 00:00:00 2001 From: lik Date: Thu, 5 Nov 2026 18:54:59 +0800 Subject: [PATCH] =?UTF-8?q?fix(payment):=20=E5=A2=9E=E5=8A=A0=E6=94=AF?= =?UTF-8?q?=E4=BB=98=E9=85=8D=E7=BD=AE=E8=AF=8A=E6=96=AD=E5=B9=B6=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=8F=96=E6=B6=88=E4=B8=AD=E7=9A=84=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - isConfigured 拆出 getConfigDiagnostics,日志列出缺失项且不输出密钥 - 证书路径支持 WXPAY_PRIVATE_KEY_PATH / WXPAY_PUBLIC_KEY_PATH 覆盖 - 已申请取消或已取消的订单不再回刷 paid,也不允许发起支付 - 发起支付与查询退款未配置时输出诊断日志 --- handler/payment.js | 10 ++++-- utils/wechatpay.js | 78 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 67 insertions(+), 21 deletions(-) diff --git a/handler/payment.js b/handler/payment.js index 28aa7d6..0f1e5f2 100644 --- a/handler/payment.js +++ b/handler/payment.js @@ -182,7 +182,7 @@ class HandlerPayment { /** * 支付成功收敛:流水 → pay_success,订单快照 → paid - * 订单已取消时不刷 paid,返回 cancelled 由调用方发起退款。 + * 订单已取消(cancelStatus 非 none)时不刷 paid,返回 cancelled 由调用方发起退款。 * @returns {{ payment: Object, record: Object|null, cancelled: boolean }} */ static async applyPaymentSuccess(paymentRecord, transaction = {}) { @@ -209,7 +209,8 @@ class HandlerPayment { if (!record || record.deletedAt) { return { payment, record: null, cancelled: true }; } - if (record.status === "cancelled") { + // 取消与 status 解耦:已申请取消或已取消的订单都不再回刷 paid(兼容旧数据 status=cancelled) + if ((record.cancelStatus || "none") !== "none" || record.status === "cancelled") { return { payment, record, cancelled: true }; } // 已进入退款阶段的流水不再回刷 paid,避免快照倒退 @@ -463,6 +464,9 @@ class HandlerPayment { if (payChannel !== "online_wechat") { return ResponseUtil.badRequest(ctx, "请先切换为微信支付"); } + if ((record.cancelStatus || "none") !== "none" || record.status === "cancelled") { + return ResponseUtil.badRequest(ctx, "订单已取消或正在取消,无法支付"); + } if (!PAYABLE_ORDER_STATUS.includes(record.status)) { return ResponseUtil.badRequest(ctx, "当前订单状态不支持支付"); } @@ -470,6 +474,7 @@ class HandlerPayment { return ResponseUtil.badRequest(ctx, "订单已支付,请勿重复支付"); } if (!wxpay.isConfigured()) { + logger.warn(`[payment] 微信支付未配置,拒绝发起支付 recordId=${id}:${wxpay.getConfigDiagnostics().join(";")}`); return ResponseUtil.error(ctx, "支付功能暂未开放,请选择线下支付", null, 503); } @@ -879,6 +884,7 @@ class HandlerPayment { .sort({ createdAt: -1 }); if (!refundRecord) return ResponseUtil.error(ctx, "无退款记录", null, 404); if (!wxpay.isConfigured()) { + logger.warn(`[payment] 微信支付未配置,拒绝查询退款 recordId=${id}:${wxpay.getConfigDiagnostics().join(";")}`); return ResponseUtil.error(ctx, "支付功能暂未开放,请选择线下支付", null, 503); } diff --git a/utils/wechatpay.js b/utils/wechatpay.js index 44c7e04..ce18d24 100644 --- a/utils/wechatpay.js +++ b/utils/wechatpay.js @@ -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,