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
+6
View File
@@ -8,6 +8,8 @@ import { HospitalSchema } from "./schema/hospital.js"
import { EmployeeSchema } from "./schema/employee.js"
import { EmployeeReviewSchema } from "./schema/employee_review.js"
import { TrainingApplicationSchema } from "./schema/training_application.js"
import { PaymentRecordSchema } from "./schema/payment_record.js"
import { RefundRecordSchema } from "./schema/refund_record.js"
import config from '../config.js';
import logger from '../utils/logger.js';
@@ -22,6 +24,8 @@ class MongoDBSchema {
this.HealthProfile = null;
this.EmployeeReview = null;
this.TrainingApplication = null;
this.PaymentRecord = null;
this.RefundRecord = null;
}
init() {
@@ -54,6 +58,8 @@ class MongoDBSchema {
this.Employee = this.dbConnection.model('employee', EmployeeSchema)
this.EmployeeReview = this.dbConnection.model('employee_review', EmployeeReviewSchema)
this.TrainingApplication = this.dbConnection.model('training_application', TrainingApplicationSchema)
this.PaymentRecord = this.dbConnection.model('payment_record', PaymentRecordSchema)
this.RefundRecord = this.dbConnection.model('refund_record', RefundRecordSchema)
}
}
+13 -1
View File
@@ -123,7 +123,19 @@ const EscortRecordSchema = mongoose.Schema(
type: String,
enum: ["", "online_wechat", "offline_cash", "offline_transfer"],
default: "",
comment: "支付方式:线上微信、线下现金、线下转账",
comment: "实际收款方式:线上微信、线下现金、线下转账",
},
// 用户选择的支付渠道;历史订单缺失该字段时展示层按 offline 解释
payChannel: {
type: String,
enum: ["online_wechat", "offline"],
comment: "用户选择的支付渠道",
},
// 当前有效支付流水;历史流水保留在 payment_record 中审计
paymentRecordId: {
type: mongoose.Schema.Types.ObjectId,
default: null,
comment: "当前有效支付流水",
},
status: {
type: String,
+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 };
+91
View File
@@ -0,0 +1,91 @@
"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 };