feat:增加微信支付功能

This commit is contained in:
lik
2026-11-05 16:31:51 +08:00
parent 770f7ebb1d
commit 23b9de16a6
12 changed files with 2126 additions and 13 deletions
+112
View File
@@ -0,0 +1,112 @@
"use strict";
import mongoose from "mongoose";
/**
* 支付流水(支付事实源)
*
* 一次支付尝试一条文档,订单上的 payment 只是汇总快照。
* 金额全链路用「分」,与微信账单口径一致。
*/
const PaymentRecordSchema = mongoose.Schema(
{
bizType: { type: String, default: "escort", comment: "业务类型" },
recordId: {
type: mongoose.Schema.Types.ObjectId,
comment: "关联业务单据ID( escort_record._id )",
},
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "user",
comment: "付款用户",
},
// 微信侧标识(对账核心字段)
outTradeNo: { type: String, required: true, comment: "商户订单号,永不重用" },
attemptNo: { type: Number, default: 1, comment: "第几次支付尝试(从 1 起)" },
transactionId: { type: String, comment: "微信支付订单号(稀疏唯一,支付成功后回填)" },
prepayId: { type: String, default: "", comment: "预支付会话标识" },
prepayExpiresAt: { type: Date, default: null, comment: "prepay_id 过期时间(2小时)" },
timeExpireAt: { type: Date, default: null, comment: "下单传入的 time_expire(支付窗口终点)" },
appid: { type: String, default: "", comment: "下单 appid" },
mchid: { type: String, default: "", comment: "商户号" },
tradeType: { type: String, default: "JSAPI", comment: "交易类型" },
totalFee: { type: Number, default: 0, comment: "应付金额(分)" },
payerTotal: { type: Number, default: 0, comment: "用户实付(分,回调回填)" },
/**
* created → pay_success / closed
* created → closed_unconfirmed(关单失败且查单未确认)
* pay_success → refunding → refunded
*/
status: {
type: String,
enum: ["created", "pay_success", "closed", "closed_unconfirmed", "refunding", "refunded"],
default: "created",
comment: "支付流水状态",
},
closeUnconfirmed: { type: Boolean, default: false, comment: "关单结果不确定,待人工核对" },
createdAt: { type: Date, default: Date.now, comment: "流水创建时间" },
prepayedAt: { type: Date, default: null, comment: "最近一次预下单时间" },
paidAt: { type: Date, default: null, comment: "支付成功时间" },
closedAt: { type: Date, default: null, comment: "关单时间" },
bankType: { type: String, default: "", comment: "支付方式(如 OTHERS/CMB_CREDIT)" },
successTime: { type: Date, default: null, 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: "payment_record",
timestamps: false,
}
);
// 异常留档:message 截断 500,避免超长文本撑库;写库失败由调用方 logger.error 兜底
PaymentRecordSchema.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 },
}
);
};
PaymentRecordSchema.index({ outTradeNo: 1 }, { unique: true });
PaymentRecordSchema.index({ transactionId: 1 }, { unique: true, sparse: true });
PaymentRecordSchema.index({ recordId: 1 });
PaymentRecordSchema.index({ userId: 1, createdAt: -1 });
PaymentRecordSchema.index({ status: 1, createdAt: 1 });
export { PaymentRecordSchema };