92 lines
3.1 KiB
JavaScript
92 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
import mongoose from "mongoose";
|
|
|
|
/**
|
|
* 退款流水
|
|
*
|
|
* 本期一笔支付只发起一次全额退款:outRefundNo = 订单_id + "-R1"。
|
|
* 重试必须沿用同一 outRefundNo(微信对同号退款幂等返回原退款单)。
|
|
*/
|
|
const RefundRecordSchema = mongoose.Schema(
|
|
{
|
|
paymentId: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
comment: "关联 payment_record._id",
|
|
},
|
|
recordId: {
|
|
type: mongoose.Schema.Types.ObjectId,
|
|
comment: "冗余关联业务单据ID",
|
|
},
|
|
outTradeNo: { type: String, default: "", comment: "原商户订单号(冗余)" },
|
|
|
|
outRefundNo: { type: String, required: true, comment: "商户退款单号" },
|
|
refundId: { type: String, comment: "微信退款单号(稀疏唯一,受理成功后回填)" },
|
|
refundAmount: { type: Number, default: 0, comment: "退款金额(分)" },
|
|
totalAmount: { type: Number, default: 0, comment: "原订单金额(分)" },
|
|
reason: { type: String, default: "", comment: "退款原因" },
|
|
|
|
status: {
|
|
type: String,
|
|
enum: ["processing", "success", "abnormal", "closed"],
|
|
default: "processing",
|
|
comment: "退款状态",
|
|
},
|
|
userReceivedAccount: { type: String, default: "", comment: "退款入账账户" },
|
|
successTime: { type: Date, default: null, comment: "退款成功时间" },
|
|
lastQueriedAt: { type: Date, default: null, comment: "最近一次主动查询退款时间" },
|
|
createdAt: { type: Date, default: Date.now, comment: "退款流水创建时间" },
|
|
|
|
lastError: {
|
|
stage: { type: String, default: "" },
|
|
code: { type: String, default: "" },
|
|
message: { type: String, default: "" },
|
|
requestId: { type: String, default: "" },
|
|
httpStatus: { type: Number, default: 0 },
|
|
at: { type: Date, default: null },
|
|
},
|
|
errorCount: { type: Number, default: 0, comment: "累计失败次数" },
|
|
lastAttemptAt: { type: Date, default: null, comment: "最近一次调用时间" },
|
|
|
|
meta: {
|
|
createtime: { type: Date, default: Date.now },
|
|
updatetime: { type: Date, default: Date.now },
|
|
},
|
|
},
|
|
{
|
|
minimize: false,
|
|
strict: false,
|
|
collection: "refund_record",
|
|
timestamps: false,
|
|
}
|
|
);
|
|
|
|
RefundRecordSchema.statics.markError = async function (id, detail = {}) {
|
|
return await this.updateOne(
|
|
{ _id: id },
|
|
{
|
|
$set: {
|
|
lastError: {
|
|
stage: String(detail.stage || ""),
|
|
code: String(detail.code || "UNKNOWN"),
|
|
message: String(detail.message || "").slice(0, 500),
|
|
requestId: String(detail.requestId || ""),
|
|
httpStatus: Number(detail.httpStatus) || 0,
|
|
at: detail.at || new Date(),
|
|
},
|
|
lastAttemptAt: new Date(),
|
|
"meta.updatetime": Date.now(),
|
|
},
|
|
$inc: { errorCount: 1 },
|
|
}
|
|
);
|
|
};
|
|
|
|
RefundRecordSchema.index({ outRefundNo: 1 }, { unique: true });
|
|
RefundRecordSchema.index({ refundId: 1 }, { unique: true, sparse: true });
|
|
RefundRecordSchema.index({ recordId: 1 });
|
|
RefundRecordSchema.index({ paymentId: 1 });
|
|
RefundRecordSchema.index({ status: 1, createdAt: 1 });
|
|
|
|
export { RefundRecordSchema };
|