|
@@ -106,140 +106,6 @@ class PayOrderService extends CrudService {
|
|
|
await this.syncData(data);
|
|
|
return data;
|
|
|
}
|
|
|
-
|
|
|
- * 支付同步
|
|
|
- * @param {Object} data 支付单数据
|
|
|
- */
|
|
|
- async syncData(data) {
|
|
|
- const pay_for = _.get(data, 'pay_for');
|
|
|
-
|
|
|
- if (!pay_for) return;
|
|
|
- const { from_id: _id, status: is_pay, money, _id: pay_id, config } = data;
|
|
|
- if (pay_for === 'lessonStudent') {
|
|
|
-
|
|
|
- await this.lessonStudentModel.updateOne({ _id }, { is_pay, pay_id });
|
|
|
-
|
|
|
- await this.makeRecord(data);
|
|
|
- } else if (pay_for === 'tempLessonApply') {
|
|
|
-
|
|
|
- const tempApply = await this.tempLessonApplyModel.findById(_id);
|
|
|
- if (!tempApply) return;
|
|
|
- tempApply.pay_id = pay_id;
|
|
|
- await tempApply.save();
|
|
|
-
|
|
|
- if (is_pay === '1') {
|
|
|
- const { lesson_id, student_id, school_id } = tempApply;
|
|
|
- const obj = { lesson_id, student_id, school_id, is_pay, pay_id, config, money };
|
|
|
- await this.lessonStudentService.create(obj);
|
|
|
- }
|
|
|
- await this.makeRecord(data);
|
|
|
- } else if (pay_for === 'Bill' && is_pay !== '0') {
|
|
|
-
|
|
|
- const billData = await this.billModel.findOne({ pay_id });
|
|
|
- if (billData) {
|
|
|
-
|
|
|
- billData.is_pay = is_pay;
|
|
|
- await billData.save();
|
|
|
- } else {
|
|
|
- const obj = _.pick(data, [ 'school_id', 'payer_id', 'payer_role' ]);
|
|
|
- await this.billModel.create({ ...obj, pay_for: 'Bill', pay_id, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss'), type: '1' });
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- async makeRecord(data) {
|
|
|
- const { config, _id: pay_id, status: is_pay, pay_for, from_id } = data;
|
|
|
- let billData;
|
|
|
- let wxBill;
|
|
|
-
|
|
|
- wxBill = await this.billModel.findOne({ pay_id });
|
|
|
- if (!wxBill) {
|
|
|
-
|
|
|
- const obj = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id', 'time', 'money' ]);
|
|
|
- obj.type = '-1';
|
|
|
- obj.pay_id = data._id;
|
|
|
- obj.is_pay = is_pay;
|
|
|
- wxBill = await this.billModel.create(obj);
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (_.get(config, 'useSurplus')) {
|
|
|
- const { bill } = config;
|
|
|
-
|
|
|
-
|
|
|
- if (_.isObject(bill)) {
|
|
|
-
|
|
|
- billData = await this.billModel.create({ ...bill, is_pay, pay_for, from_id, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
|
- if (billData) data.config.bill = ObjectId(billData._id).toString();
|
|
|
- await this.model.updateOne({ _id: data._id }, data);
|
|
|
-
|
|
|
- const { type, payer_role, payer_id, money, school_id } = billData;
|
|
|
- if (type === '-2') {
|
|
|
- let relation;
|
|
|
- if (payer_role === 'Student') relation = await this.rssModel.findOne({ student_id: payer_id, school_id });
|
|
|
- else if (payer_role === 'Coach') relation = await this.rcsModel.findOne({ coach_id: payer_id, school_id });
|
|
|
- relation.money = relation.money - money;
|
|
|
- await relation.save();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 重新支付; 弃用
|
|
|
- * @param {String} body 参数体
|
|
|
- * @property {String} id 数据id
|
|
|
- */
|
|
|
- async toRePay({ id }) {
|
|
|
- const data = await this.model.findById(id);
|
|
|
- if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付的信息');
|
|
|
-
|
|
|
- const { order_no } = data;
|
|
|
-
|
|
|
- const wxOrder = await this.payService.search(order_no);
|
|
|
- let wxSign;
|
|
|
- if (_.get(wxOrder, 'trade_state') === 'NOTPAY') {
|
|
|
-
|
|
|
- wxSign = await this.payService.create(data);
|
|
|
- } else if (_.get(wxOrder, 'trade_state') === 'SUCCESS') {
|
|
|
- throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单已支付,无需重复支付');
|
|
|
- } else if (_.get(wxOrder, 'trade_state') === 'USERPAYING') {
|
|
|
- throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单支付中');
|
|
|
- } else {
|
|
|
- await this.payService.close(order_no);
|
|
|
-
|
|
|
- const newNo = this.getOrderNo();
|
|
|
- data.order_no = newNo;
|
|
|
- await data.save();
|
|
|
- wxSign = await this.payService.create(data);
|
|
|
- }
|
|
|
- return { pay_id: id, wxSign };
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- * 申请退款,弃用
|
|
|
- * @param {Object} body 参数体
|
|
|
- * @property {String} id 支付单的数据id
|
|
|
- */
|
|
|
- async toRefund({ id }) {
|
|
|
-
|
|
|
- const data = await this.model.findById(id);
|
|
|
- if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
|
|
|
- const wxOrderReq = await this.payService.search(data.order_no);
|
|
|
- if (wxOrderReq) {
|
|
|
- if (!wxOrderReq) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未查询到微信支付订单');
|
|
|
- const wxRefundReq = await this.payService.refund(data.order_no, '退赛');
|
|
|
- if (wxRefundReq) {
|
|
|
- if (wxRefundReq.status !== 'REFUND') throw new BusinessError(ErrorCode.SERVICE_FAULT, '退款失败');
|
|
|
-
|
|
|
- data.status = '-3';
|
|
|
- await data.save();
|
|
|
- await this.syncData(data);
|
|
|
- return 'ok';
|
|
|
- }
|
|
|
- }
|
|
|
- throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询微信支付订单失败');
|
|
|
- }
|
|
|
|
|
|
|
|
|
* 关闭订单
|
|
@@ -258,11 +124,11 @@ class PayOrderService extends CrudService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- async toPayNative(body) {
|
|
|
- body = await this.beforeCreate(body);
|
|
|
- const data = await this.model.create(body);
|
|
|
+ async toPayNative(body, tran) {
|
|
|
+ if (!body.order_no) body.order_no = this.getOrderNo();
|
|
|
+ const pay_id = tran.insert('PayOrder', body);
|
|
|
const qrcode = await this.payService.createNative({ ...body, notice_url: '/newCourt/v2/api/payOrder/nativeReturn' });
|
|
|
- return qrcode;
|
|
|
+ return { pay_id, qrcode };
|
|
|
}
|
|
|
|
|
|
async nativeReturn(body) {
|
|
@@ -273,8 +139,47 @@ class PayOrderService extends CrudService {
|
|
|
if (trade_state === 'SUCCESS') obj.status = '1';
|
|
|
else if (trade_state === 'REFUND') obj.status = '-3';
|
|
|
const openid = _.get(payer, 'openid');
|
|
|
+
|
|
|
+ const payerInfo = await this.findPayerByOpenid(openid);
|
|
|
+ const data = await this.model.findOne({ order_no });
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ this.tran.update('PayOrder', data._id, { ...payerInfo, ...obj });
|
|
|
+ if (_.get(data, 'pay_for') === 'TempLessonApply') {
|
|
|
+
|
|
|
+
|
|
|
+ this.tran.update('TempLessonApply', data.from_id, { is_pay: obj.status });
|
|
|
+
|
|
|
+ const billData = {
|
|
|
+ school_id: _.get(data, 'school_id'),
|
|
|
+ pay_for: 'TempLessonApply',
|
|
|
+ from_id: data.from_id,
|
|
|
+ type: '-1',
|
|
|
+ is_pay: obj.status,
|
|
|
+ time: moment().format('YYYY-MM-DD HH:mm:ss'),
|
|
|
+ };
|
|
|
+ const payer_id = _.get(payerInfo, 'payer_id');
|
|
|
+ const payer_role = _.get(payerInfo, 'payer_role');
|
|
|
+ if (payer_id && payer_role) {
|
|
|
+ billData.payer_id = payer_id;
|
|
|
+ billData.payer_role = payer_role;
|
|
|
+ }
|
|
|
+ this.tran.insert('Bill', billData);
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.tran.run();
|
|
|
+ } catch (error) {
|
|
|
+ await this.tran.rollback();
|
|
|
+ console.error(error);
|
|
|
+ } finally {
|
|
|
+ this.tran.clean();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ async findPayerByOpenid(openid) {
|
|
|
+ const obj = { openid };
|
|
|
if (openid) {
|
|
|
- obj.openid = openid;
|
|
|
|
|
|
const user = await this.userModel.findOne({ openid });
|
|
|
if (user) {
|
|
@@ -292,9 +197,7 @@ class PayOrderService extends CrudService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- await this.model.updateOne({ order_no }, obj);
|
|
|
- const data = await this.model.findOne({ order_no });
|
|
|
- await this.syncData(data);
|
|
|
+ return obj;
|
|
|
}
|
|
|
}
|
|
|
|