fix(payment): 收口取消退款规则并保护退款状态
- 未确认订单取消直接生效:未收款删除,已收款进入退款 - 待取消期间到账只记账不退款,确认取消后再退 - 退款缺少支付流水时记录错误,关单/查单兜底补触发退款 - 已收款订单禁止改支付状态与金额,管理端 Agent 取消同步关单并退款 - 新增人工退款核销接口,切换支付方式不再自动退款
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
||||
STATUS_TRANSITIONS,
|
||||
CANCEL_STATUS_VALUES,
|
||||
} from "../../../../../utils/record_state.js";
|
||||
import { HandlerPayment } from "../../../../../handler/payment.js";
|
||||
import logger from "../../../../../utils/logger.js";
|
||||
|
||||
const escortRecordSetTool = tool(
|
||||
async ({ orderId, status, cancelStatus, notes, inquiry, payment }) => {
|
||||
@@ -37,6 +39,7 @@ const escortRecordSetTool = tool(
|
||||
// 状态迁移必须与接口层同一套规则:否则 Agent 能把已完成订单改回进行中,
|
||||
// 绕过状态机并造成费用/服务记录不一致
|
||||
const update = {};
|
||||
let effectivePay = record.payment || {};
|
||||
// 兼容旧数据:取消曾存放在 status 中
|
||||
const currentCancel = record.status === "cancelled"
|
||||
? "cancelled"
|
||||
@@ -50,9 +53,16 @@ const escortRecordSetTool = tool(
|
||||
error: `Illegal cancel status transition: ${currentCancel} -> ${cancelStatus}`,
|
||||
};
|
||||
}
|
||||
if (cancelStatus === "cancelled" && record.status === "completed") {
|
||||
return { success: false, error: "已完成订单不可取消,请走线下售后处理" };
|
||||
}
|
||||
update.cancelStatus = cancelStatus;
|
||||
if (cancelStatus === "requested") update.cancelRequestedAt = new Date();
|
||||
if (cancelStatus === "cancelled") update.cancelledAt = new Date();
|
||||
// 与接口层一致:先关闭在途预付单,否则取消后旧 prepay 仍可完成支付
|
||||
await HandlerPayment.closeOpenPayRecords(orderId);
|
||||
const fresh = await DBModel.EscortRecord.findById(orderId);
|
||||
effectivePay = fresh?.payment || effectivePay;
|
||||
}
|
||||
|
||||
if (status !== undefined && status !== record.status) {
|
||||
@@ -102,7 +112,28 @@ const escortRecordSetTool = tool(
|
||||
}
|
||||
}
|
||||
|
||||
// 已收款订单不能反向改支付状态/实收金额:否则可把退款中订单改回未支付并再次收款
|
||||
const currentPayStatus = effectivePay.status || "unpaid";
|
||||
if (payment !== undefined) {
|
||||
if (["partial", "paid", "refunding", "refunded"].includes(currentPayStatus)) {
|
||||
// 取消已收款订单时允许 paid → refunding,其余反向改动一律拒绝
|
||||
const enterRefundOnCancel = update.cancelStatus === "cancelled"
|
||||
&& currentPayStatus === "paid"
|
||||
&& payment.status === "refunding";
|
||||
if (payment.status !== undefined && payment.status !== currentPayStatus && !enterRefundOnCancel) {
|
||||
return { success: false, error: "已收款订单不可修改支付状态,如需退款请走取消流程" };
|
||||
}
|
||||
if (payment.paidFee !== undefined
|
||||
&& Number(payment.paidFee) !== (Number(effectivePay.paidFee) || 0)) {
|
||||
return { success: false, error: "已收款订单不可修改实收金额" };
|
||||
}
|
||||
if (payment.totalFee !== undefined
|
||||
&& Number(payment.totalFee) !== (Number(effectivePay.totalFee) || 0)) {
|
||||
return { success: false, error: "已收款订单不可修改金额,如需调整请先退款" };
|
||||
}
|
||||
} else if (payment.status !== undefined && !["unpaid", "paid"].includes(payment.status)) {
|
||||
return { success: false, error: "未收款订单只能标记为 unpaid 或 paid" };
|
||||
}
|
||||
if (payment.totalFee !== undefined) {
|
||||
update["payment.totalFee"] = payment.totalFee;
|
||||
}
|
||||
@@ -114,6 +145,14 @@ const escortRecordSetTool = tool(
|
||||
}
|
||||
}
|
||||
|
||||
// 取消已收款订单:与接口层一致,先进入退款中,落库后再发起原路退款
|
||||
if (update.cancelStatus === "cancelled" && ["partial", "paid"].includes(currentPayStatus)) {
|
||||
if (payment?.status !== undefined && payment.status !== "refunding") {
|
||||
return { success: false, error: "取消已收款订单时支付状态必须进入 refunding" };
|
||||
}
|
||||
update["payment.status"] = "refunding";
|
||||
}
|
||||
|
||||
update["meta.updatetime"] = Date.now();
|
||||
|
||||
// 以读取时的状态作为更新条件,避免并发下跳过中间状态;同时排除已软删除的订单
|
||||
@@ -138,6 +177,17 @@ const escortRecordSetTool = tool(
|
||||
};
|
||||
}
|
||||
|
||||
// 已取消且进入退款中的线上订单:落库后触发原路退款(失败只记日志,由管理端查询兜底)
|
||||
if (updatedRecord.cancelStatus === "cancelled"
|
||||
&& updatedRecord.payment?.status === "refunding"
|
||||
&& updatedRecord.payment?.method === "online_wechat") {
|
||||
HandlerPayment.createRefundForRecord(updatedRecord, {
|
||||
reason: updatedRecord.cancelReason || "平台取消",
|
||||
}).catch((err) => {
|
||||
logger.error(`[escort-agent] 取消订单自动退款失败 recordId=${orderId}: ${err?.message || err}`);
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: updatedRecord,
|
||||
|
||||
Reference in New Issue
Block a user