feat: 记录并展示订单退款流水号与失败原因
- 订单 payment 新增 refundNo/refundedAt,线上退款成功回填微信退款单号 - 人工核销退款支持录入退款流水号 - pay/status 返回退款概要(单号、流水号、状态、金额、最近失败原因) - retryRefund 补充微信支付未配置检查
This commit is contained in:
+36
-4
@@ -322,13 +322,18 @@ class HandlerPayment {
|
||||
);
|
||||
|
||||
if (status === "success") {
|
||||
const recordSet = { "payment.status": "refunded", "meta.updatetime": nowMs() };
|
||||
if (refundId) {
|
||||
recordSet["payment.refundNo"] = refundId;
|
||||
recordSet["payment.refundedAt"] = setFields.successTime || new Date();
|
||||
}
|
||||
await DBModel.PaymentRecord.updateOne(
|
||||
{ _id: refundRecord.paymentId },
|
||||
{ $set: { status: "refunded", "meta.updatetime": nowMs() } }
|
||||
);
|
||||
await DBModel.EscortRecord.updateOne(
|
||||
{ _id: refundRecord.recordId },
|
||||
{ $set: { "payment.status": "refunded", "meta.updatetime": nowMs() } }
|
||||
{ $set: recordSet }
|
||||
);
|
||||
} else {
|
||||
await DBModel.PaymentRecord.updateOne(
|
||||
@@ -732,6 +737,9 @@ class HandlerPayment {
|
||||
}
|
||||
|
||||
const fresh = await DBModel.EscortRecord.findById(id) || record;
|
||||
const refundRecord = await DBModel.RefundRecord.findOne({ recordId: record._id })
|
||||
.sort({ createdAt: -1 });
|
||||
const refundNo = fresh.payment?.refundNo || refundRecord?.refundId || "";
|
||||
return ResponseUtil.success(ctx, {
|
||||
paymentStatus: fresh.payment?.status || "unpaid",
|
||||
orderStatus: fresh.status,
|
||||
@@ -747,6 +755,22 @@ class HandlerPayment {
|
||||
timeExpireAt: payment.timeExpireAt || null,
|
||||
}
|
||||
: null,
|
||||
refund: (refundRecord || refundNo)
|
||||
? {
|
||||
outRefundNo: refundRecord?.outRefundNo || "",
|
||||
refundId: refundNo,
|
||||
status: refundRecord?.status || (fresh.payment?.status === "refunded" ? "success" : ""),
|
||||
refundAmount: refundRecord?.refundAmount || 0,
|
||||
successTime: refundRecord?.successTime || fresh.payment?.refundedAt || null,
|
||||
lastError: refundRecord?.lastError?.message
|
||||
? {
|
||||
code: refundRecord.lastError.code || "",
|
||||
message: refundRecord.lastError.message,
|
||||
at: refundRecord.lastError.at || null,
|
||||
}
|
||||
: null,
|
||||
}
|
||||
: null,
|
||||
}, "查询成功");
|
||||
} catch (err) {
|
||||
return ResponseUtil.internalError(ctx, err);
|
||||
@@ -960,11 +984,15 @@ class HandlerPayment {
|
||||
return ResponseUtil.badRequest(ctx, "存在处理中的线上退款,请先查询退款结果");
|
||||
}
|
||||
|
||||
const updatedRecord = await DBModel.EscortRecord.updateRecord(id, {
|
||||
const refundNo = String(ctx.request.body?.refundNo || "").trim().slice(0, 64);
|
||||
const setFields = {
|
||||
"payment.status": "refunded",
|
||||
});
|
||||
"payment.refundedAt": new Date(),
|
||||
};
|
||||
if (refundNo) setFields["payment.refundNo"] = refundNo;
|
||||
const updatedRecord = await DBModel.EscortRecord.updateRecord(id, setFields);
|
||||
if (!updatedRecord) return ResponseUtil.error(ctx, "陪诊记录不存在", null, 404);
|
||||
logger.info(`[payment] 人工核销退款完成 recordId=${id} method=${record.payment?.method || ""}`);
|
||||
logger.info(`[payment] 人工核销退款完成 recordId=${id} method=${record.payment?.method || ""} refundNo=${refundNo}`);
|
||||
return ResponseUtil.success(ctx, { record: updatedRecord }, "已标记为退款完成");
|
||||
} catch (err) {
|
||||
logger.error(`[payment] 人工核销退款失败 recordId=${ctx.params.id}: ${err?.message || err}`);
|
||||
@@ -990,6 +1018,10 @@ class HandlerPayment {
|
||||
if (existing?.status === "success") {
|
||||
return ResponseUtil.badRequest(ctx, "该订单退款已成功,无需再次退款");
|
||||
}
|
||||
if (!wxpay.isConfigured()) {
|
||||
logger.warn(`[payment] 微信支付未配置,拒绝再次退款 recordId=${id}:${wxpay.getConfigDiagnostics().join(";")}`);
|
||||
return ResponseUtil.error(ctx, "支付功能暂未开放,请人工核销退款", null, 503);
|
||||
}
|
||||
|
||||
const refund = await HandlerPayment.createRefundForRecord(record, { retry: true });
|
||||
if (refund?.skipped) {
|
||||
|
||||
@@ -144,6 +144,17 @@ const EscortRecordSchema = mongoose.Schema(
|
||||
default: "unpaid",
|
||||
comment: "支付状态:未支付、部分支付、已支付、退款中、已退款",
|
||||
},
|
||||
// 退款流水号:线上退款回填微信退款单号,人工核销由管理员录入
|
||||
refundNo: {
|
||||
type: String,
|
||||
default: "",
|
||||
comment: "退款流水号",
|
||||
},
|
||||
refundedAt: {
|
||||
type: Date,
|
||||
default: null,
|
||||
comment: "退款完成时间",
|
||||
},
|
||||
},
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user