fix(payment): 增加支付配置诊断并拦截取消中的订单
- isConfigured 拆出 getConfigDiagnostics,日志列出缺失项且不输出密钥 - 证书路径支持 WXPAY_PRIVATE_KEY_PATH / WXPAY_PUBLIC_KEY_PATH 覆盖 - 已申请取消或已取消的订单不再回刷 paid,也不允许发起支付 - 发起支付与查询退款未配置时输出诊断日志
This commit is contained in:
+8
-2
@@ -182,7 +182,7 @@ class HandlerPayment {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付成功收敛:流水 → pay_success,订单快照 → paid
|
* 支付成功收敛:流水 → pay_success,订单快照 → paid
|
||||||
* 订单已取消时不刷 paid,返回 cancelled 由调用方发起退款。
|
* 订单已取消(cancelStatus 非 none)时不刷 paid,返回 cancelled 由调用方发起退款。
|
||||||
* @returns {{ payment: Object, record: Object|null, cancelled: boolean }}
|
* @returns {{ payment: Object, record: Object|null, cancelled: boolean }}
|
||||||
*/
|
*/
|
||||||
static async applyPaymentSuccess(paymentRecord, transaction = {}) {
|
static async applyPaymentSuccess(paymentRecord, transaction = {}) {
|
||||||
@@ -209,7 +209,8 @@ class HandlerPayment {
|
|||||||
if (!record || record.deletedAt) {
|
if (!record || record.deletedAt) {
|
||||||
return { payment, record: null, cancelled: true };
|
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 };
|
return { payment, record, cancelled: true };
|
||||||
}
|
}
|
||||||
// 已进入退款阶段的流水不再回刷 paid,避免快照倒退
|
// 已进入退款阶段的流水不再回刷 paid,避免快照倒退
|
||||||
@@ -463,6 +464,9 @@ class HandlerPayment {
|
|||||||
if (payChannel !== "online_wechat") {
|
if (payChannel !== "online_wechat") {
|
||||||
return ResponseUtil.badRequest(ctx, "请先切换为微信支付");
|
return ResponseUtil.badRequest(ctx, "请先切换为微信支付");
|
||||||
}
|
}
|
||||||
|
if ((record.cancelStatus || "none") !== "none" || record.status === "cancelled") {
|
||||||
|
return ResponseUtil.badRequest(ctx, "订单已取消或正在取消,无法支付");
|
||||||
|
}
|
||||||
if (!PAYABLE_ORDER_STATUS.includes(record.status)) {
|
if (!PAYABLE_ORDER_STATUS.includes(record.status)) {
|
||||||
return ResponseUtil.badRequest(ctx, "当前订单状态不支持支付");
|
return ResponseUtil.badRequest(ctx, "当前订单状态不支持支付");
|
||||||
}
|
}
|
||||||
@@ -470,6 +474,7 @@ class HandlerPayment {
|
|||||||
return ResponseUtil.badRequest(ctx, "订单已支付,请勿重复支付");
|
return ResponseUtil.badRequest(ctx, "订单已支付,请勿重复支付");
|
||||||
}
|
}
|
||||||
if (!wxpay.isConfigured()) {
|
if (!wxpay.isConfigured()) {
|
||||||
|
logger.warn(`[payment] 微信支付未配置,拒绝发起支付 recordId=${id}:${wxpay.getConfigDiagnostics().join(";")}`);
|
||||||
return ResponseUtil.error(ctx, "支付功能暂未开放,请选择线下支付", null, 503);
|
return ResponseUtil.error(ctx, "支付功能暂未开放,请选择线下支付", null, 503);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -879,6 +884,7 @@ class HandlerPayment {
|
|||||||
.sort({ createdAt: -1 });
|
.sort({ createdAt: -1 });
|
||||||
if (!refundRecord) return ResponseUtil.error(ctx, "无退款记录", null, 404);
|
if (!refundRecord) return ResponseUtil.error(ctx, "无退款记录", null, 404);
|
||||||
if (!wxpay.isConfigured()) {
|
if (!wxpay.isConfigured()) {
|
||||||
|
logger.warn(`[payment] 微信支付未配置,拒绝查询退款 recordId=${id}:${wxpay.getConfigDiagnostics().join(";")}`);
|
||||||
return ResponseUtil.error(ctx, "支付功能暂未开放,请选择线下支付", null, 503);
|
return ResponseUtil.error(ctx, "支付功能暂未开放,请选择线下支付", null, 503);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+59
-19
@@ -54,21 +54,31 @@ function resolvePath(value) {
|
|||||||
return path.isAbsolute(value) ? value : path.resolve(PROJECT_ROOT, 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 cachedPrivateKey = null;
|
||||||
let cachedPublicKey = null;
|
let cachedPublicKey = null;
|
||||||
|
|
||||||
function loadPrivateKey() {
|
function loadPrivateKey() {
|
||||||
const cfg = payConfig();
|
const privatePath = privateKeyPathValue();
|
||||||
if (!cachedPrivateKey && cfg.privateKeyPath) {
|
if (!cachedPrivateKey && privatePath) {
|
||||||
cachedPrivateKey = readFileSync(resolvePath(cfg.privateKeyPath), "utf-8");
|
cachedPrivateKey = readFileSync(resolvePath(privatePath), "utf-8");
|
||||||
}
|
}
|
||||||
return cachedPrivateKey;
|
return cachedPrivateKey;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadPublicKey() {
|
function loadPublicKey() {
|
||||||
const cfg = payConfig();
|
const publicPath = publicKeyPathValue();
|
||||||
if (!cachedPublicKey && cfg.publicKeyPath) {
|
if (!cachedPublicKey && publicPath) {
|
||||||
cachedPublicKey = readFileSync(resolvePath(cfg.publicKeyPath), "utf-8");
|
cachedPublicKey = readFileSync(resolvePath(publicPath), "utf-8");
|
||||||
}
|
}
|
||||||
return cachedPublicKey;
|
return cachedPublicKey;
|
||||||
}
|
}
|
||||||
@@ -86,24 +96,53 @@ function apiV3Key() {
|
|||||||
return key;
|
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 密钥)
|
* 配置是否完整(商户参数 + 两把证书文件 + APIv3 密钥)
|
||||||
* 缺配置时支付接口降级为「未开放」,不影响下单、取消等其它链路。
|
* 缺配置时支付接口降级为「未开放」,不影响下单、取消等其它链路。
|
||||||
*/
|
*/
|
||||||
function isConfigured() {
|
function isConfigured() {
|
||||||
const cfg = payConfig();
|
return getConfigDiagnostics().length === 0;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function requireFullConfig() {
|
function requireFullConfig() {
|
||||||
@@ -513,6 +552,7 @@ function verifyNotify({ headers = {}, rawBody = "" }) {
|
|||||||
export {
|
export {
|
||||||
WechatPayError,
|
WechatPayError,
|
||||||
isConfigured,
|
isConfigured,
|
||||||
|
getConfigDiagnostics,
|
||||||
yuanToFen,
|
yuanToFen,
|
||||||
fenToYuan,
|
fenToYuan,
|
||||||
formatTimeExpire,
|
formatTimeExpire,
|
||||||
|
|||||||
Reference in New Issue
Block a user