|
@@ -5,6 +5,7 @@ const _ = require('lodash');
|
|
const assert = require('assert');
|
|
const assert = require('assert');
|
|
const moment = require('moment');
|
|
const moment = require('moment');
|
|
const { ObjectId } = require('mongoose').Types;
|
|
const { ObjectId } = require('mongoose').Types;
|
|
|
|
+const Transaction = require('mongoose-transactions');
|
|
|
|
|
|
class PayOrderService extends CrudService {
|
|
class PayOrderService extends CrudService {
|
|
constructor(ctx) {
|
|
constructor(ctx) {
|
|
@@ -19,82 +20,51 @@ class PayOrderService extends CrudService {
|
|
this.coachModel = this.ctx.model.User.Coach;
|
|
this.coachModel = this.ctx.model.User.Coach;
|
|
this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
|
|
this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
|
|
this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
|
|
this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
|
|
-
|
|
|
|
this.billModel = this.ctx.model.Business.Bill;
|
|
this.billModel = this.ctx.model.Business.Bill;
|
|
|
|
+ this.tran = new Transaction();
|
|
}
|
|
}
|
|
-
|
|
+
|
|
- async beforeCreate(data) {
|
|
+ * 处理支付回调
|
|
- if (!_.get(data, 'order_no')) data.order_no = this.getOrderNo();
|
|
+ * @param {Object} body 参数体
|
|
- const { data: nd, next } = await this.checkSurplus(data);
|
|
+ */
|
|
- if (!next) throw new BusinessError(0, '扣除余额成功', nd);
|
|
+ async payCallBack({ result }) {
|
|
- data = nd;
|
|
+ const { out_trade_no, payer } = result;
|
|
- return data;
|
|
+
|
|
- }
|
|
+ if (!out_trade_no) {
|
|
-
|
|
+ console.error('没有支付订单号');
|
|
- async checkSurplus(data) {
|
|
+ return;
|
|
- const pay_for = _.get(data, 'pay_for');
|
|
+ }
|
|
-
|
|
+ const openid = _.get(payer, 'openid');
|
|
- if (pay_for && pay_for === 'Bill') return { data, next: true };
|
|
+ const query = { order_no: out_trade_no, openid };
|
|
- const payer_id = _.get(data, 'payer_id');
|
|
+ let payOrder = await this.model.findOne(query);
|
|
- const payer_role = _.get(data, 'payer_role');
|
|
+
|
|
- const school_id = _.get(data, 'school_id');
|
|
+ if (!payOrder) {
|
|
- let billData;
|
|
+ console.error('没有找到付款单');
|
|
- let relation;
|
|
+ return;
|
|
-
|
|
+ }
|
|
- if (!(payer_id && payer_role)) return { data, next: true };
|
|
+ payOrder = JSON.parse(JSON.stringify(payOrder));
|
|
-
|
|
+ const config = _.get(payOrder, 'config', {});
|
|
- 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 });
|
|
+ config.pay = result;
|
|
-
|
|
+ const update = { config, status: _.get(result, 'trade_state') === 'SUCCESS' ? '1' : '-1' };
|
|
- if (!relation) return { data, next: true };
|
|
+ try {
|
|
-
|
|
+ this.tran.update('PayOrder', payOrder._id, update);
|
|
- const { money: surplus } = relation;
|
|
+
|
|
- const { money } = data;
|
|
+ const billData = _.pick(payOrder, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
-
|
|
+
|
|
- if (surplus && surplus <= 0) return { data, next: true };
|
|
+ if (billData.pay_for && _.lowerCase(billData.pay_for) === 'charge') billData.type = '1';
|
|
-
|
|
+ else billData.type = '-1';
|
|
- if (this.ctx.minus(surplus, money) >= 0) {
|
|
+ billData.pay_id = payOrder._id;
|
|
- try {
|
|
+ billData.is_pay = update.status;
|
|
-
|
|
+ billData.time = moment().format('YYYY-MM-DD HH:mm:ss');
|
|
- const bill = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
+ this.tran.insert('Bill', billData);
|
|
-
|
|
+ await this.tran.run();
|
|
- bill.money = money;
|
|
+ } catch (error) {
|
|
- bill.type = '-2';
|
|
+ await this.tran.rollback();
|
|
- bill.is_pay = '1';
|
|
+ console.log(error);
|
|
- billData = await this.billModel.create({ ...bill, time: moment().format('YYYY-MM-DD HH:mm:ss') });
|
|
+ } finally {
|
|
-
|
|
+ this.tran.clean();
|
|
- relation.money = surplus - money;
|
|
|
|
- await relation.save();
|
|
|
|
-
|
|
|
|
- return { data: billData, next: false };
|
|
|
|
- } catch (error) {
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- if (billData) {
|
|
|
|
- await this.billModel.deleteOne({ _id: billData._id });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- }
|
|
|
|
- } else if (surplus !== 0) {
|
|
|
|
-
|
|
|
|
- const needPay = this.ctx.minus(money, surplus);
|
|
|
|
- const bill = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
|
|
|
|
- bill.money = surplus;
|
|
|
|
- bill.type = '-2';
|
|
|
|
- if (!data.config) data.config = {};
|
|
|
|
-
|
|
|
|
- data.config.useSurplus = true;
|
|
|
|
- data.config.bill = bill;
|
|
|
|
- data.money = needPay;
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
- return { data, next: true };
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- async afterCreate(body, data) {
|
|
|
|
-
|
|
|
|
- const wxSign = await this.payService.create(data);
|
|
|
|
- return { data, wxSign };
|
|
|
|
}
|
|
}
|
|
|
|
|
|
async afterUpdate(filter, body, data) {
|
|
async afterUpdate(filter, body, data) {
|
|
@@ -157,7 +127,6 @@ class PayOrderService extends CrudService {
|
|
wxBill = await this.billModel.create(obj);
|
|
wxBill = await this.billModel.create(obj);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
|
|
if (_.get(config, 'useSurplus')) {
|
|
if (_.get(config, 'useSurplus')) {
|
|
const { bill } = config;
|
|
const { bill } = config;
|
|
@@ -178,12 +147,6 @@ class PayOrderService extends CrudService {
|
|
await relation.save();
|
|
await relation.save();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -208,13 +171,14 @@ class PayOrderService extends CrudService {
|
|
} else if (_.get(wxOrder, 'trade_state') === 'USERPAYING') {
|
|
} else if (_.get(wxOrder, 'trade_state') === 'USERPAYING') {
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单支付中');
|
|
throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单支付中');
|
|
} else {
|
|
} else {
|
|
-
|
|
+ await this.payService.close(order_no);
|
|
- const order_no = this.getOrderNo();
|
|
+
|
|
- data.order_no = order_no;
|
|
+ const newNo = this.getOrderNo();
|
|
|
|
+ data.order_no = newNo;
|
|
await data.save();
|
|
await data.save();
|
|
wxSign = await this.payService.create(data);
|
|
wxSign = await this.payService.create(data);
|
|
}
|
|
}
|
|
- return { data, wxSign };
|
|
+ return { pay_id: id, wxSign };
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|