payOrder.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. const moment = require('moment');
  7. const { ObjectId } = require('mongoose').Types;
  8. //
  9. class PayOrderService extends CrudService {
  10. constructor(ctx) {
  11. super(ctx, 'payorder');
  12. this.model = this.ctx.model.Business.PayOrder;
  13. this.payService = this.ctx.service.wxpay;
  14. this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
  15. this.lessonStudentService = this.ctx.service.business.lessonStudent;
  16. this.tempLessonApplyModel = this.ctx.model.Apply.TempLessonApply;
  17. this.userModel = this.ctx.model.User.User;
  18. this.studentModel = this.ctx.model.User.Student;
  19. this.coachModel = this.ctx.model.User.Coach;
  20. this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
  21. this.rssModel = this.ctx.model.Relation.RelationStudentSchool;
  22. this.billModel = this.ctx.model.Business.Bill;
  23. // this.costDetailModel = this.ctx.model.Business.CostDetail;
  24. // this.chargeModel = this.ctx.model.Business.Charge;
  25. }
  26. async beforeCreate(data) {
  27. if (!_.get(data, 'order_no')) data.order_no = this.getOrderNo();
  28. const { data: nd, next } = await this.checkSurplus(data);
  29. if (!next) throw new BusinessError(0, '扣除余额成功');
  30. data = nd;
  31. return data;
  32. }
  33. // 检查是否扣除余额
  34. async checkSurplus(data) {
  35. const pay_for = _.get(data, 'pay_for');
  36. // 充值不检查余额
  37. if (pay_for && pay_for === 'Bill') return { data, next: true };
  38. const payer_id = _.get(data, 'payer_id');
  39. const payer_role = _.get(data, 'payer_role');
  40. let billData;
  41. let relation;
  42. // 没有要支付的人及其角色,那就找不到余额,返回下单
  43. if (!(payer_id && payer_role)) return { data, next: true };
  44. // 有支付的人,可以查查这个人的余额
  45. if (payer_role === 'Student') relation = await this.rssModel.findOne({ student_id: payer_id });
  46. else if (payer_role === 'Coach') relation = await this.rcsModel.findOne({ coach_id: payer_id });
  47. // 没找到关系,那就直接返回正常下单缴费
  48. if (!relation) return { data, next: true };
  49. // 有关系,找余额
  50. const { money: surplus } = relation;
  51. const { money } = data;
  52. // 没有余额,或者余额没钱,返回正常下单缴费
  53. if (surplus && surplus <= 0) return { data, next: true };
  54. // 有余额,看余额够不够
  55. if (surplus >= money) {
  56. try {
  57. // 余额够, 无需生成订单,直接去使用余额缴费
  58. const bill = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
  59. // 本次扣除余额的金额就是本次需要支付的金额
  60. bill.money = money;
  61. bill.type = '-2';
  62. billData = await this.billModel.create({ ...bill, time: moment().format('YYYY-MM-DD HH:mm:ss') });
  63. // 余额需要扣除本次支付的费用
  64. relation.money = surplus - money;
  65. await relation.save();
  66. return false;
  67. } catch (error) {
  68. // 抓取数据库创建和修改的异常,
  69. // 如果 billData没有值,说明是创建消费记录时发生的错误,不需要做回滚处理;如果有值,那就删除
  70. if (billData) {
  71. await this.billModel.deleteOne({ _id: billData._id });
  72. }
  73. // 如果是余额修改错误,上面的回滚删除就足够了.因为保存新余额发生错误,余额不会保存,只要把消费记录删了,就回到原样了
  74. }
  75. } else {
  76. // 余额不够,在config里记录部分使用了余额,再进行下单,下单完后,生成余额的扣款
  77. const needPay = surplus - money;
  78. const bill = _.pick(data, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
  79. bill.money = surplus;
  80. bill.type = '-2';
  81. if (!data.config) data.config = {};
  82. // 添加余额的设置, 等支付成功后,再生成余额的消费记录
  83. data.config.useSurplus = true;
  84. data.config.bill = bill;
  85. data.money = needPay;
  86. }
  87. return { data, next: true };
  88. }
  89. async afterCreate(body, data) {
  90. await this.syncData(data);
  91. const wxSign = await this.payService.create(data);
  92. return { data, wxSign };
  93. }
  94. async afterUpdate(filter, body, data) {
  95. await this.syncData(data);
  96. return data;
  97. }
  98. /**
  99. * 支付同步
  100. * @param {Object} data 支付单数据
  101. */
  102. async syncData(data) {
  103. const pay_for = _.get(data, 'pay_for');
  104. // 不知道该去同步哪个表的支付状态,不处理
  105. if (!pay_for) return;
  106. const { from_id: _id, status: is_pay, money, _id: pay_id, config } = data;
  107. if (pay_for === 'lessonStudent') {
  108. // 因为上课产生的支付,去找lessonStudent,修改指定学生的支付状态
  109. await this.lessonStudentModel.updateOne({ _id }, { is_pay });
  110. // 检查下各种记录
  111. await this.makeRecord(data);
  112. } else if (pay_for === 'tempLessonApply') {
  113. // 私教课临时上课,需要到临时申请那找到相关数据
  114. const tempApply = await this.tempLessonApplyModel.findById(_id);
  115. if (!tempApply) return;
  116. tempApply.pay_id = pay_id;
  117. await tempApply.save();
  118. // 修改完申请,再创建 lessonStudent
  119. const { lesson_id, student_id, school_id } = tempApply;
  120. const obj = { lesson_id, student_id, school_id, is_pay, pay_id, config, money };
  121. await this.lessonStudentService.create(obj);
  122. await this.makeRecord(data);
  123. } else if (pay_for === 'Bill' && is_pay !== '0') {
  124. // 充值记录,找到充值记录,没有就生成
  125. const data = await this.billModel.findOne({ pay_id });
  126. if (data) {
  127. // 有就修改
  128. data.is_pay = is_pay;
  129. await data.save();
  130. } else {
  131. const obj = _.pick(data, [ 'school_id', 'payer_id', 'payer_role' ]);
  132. await this.billModel.create({ ...obj, pay_for: 'Bill', pay_id, is_pay, time: moment().format('YYYY-MM-DD HH:mm:ss'), type: '1' });
  133. }
  134. }
  135. }
  136. // 检查 消费记录
  137. async makeRecord(data) {
  138. const { config, _id: pay_id } = data;
  139. // 消费记录
  140. if (_.get(config, 'useSurplus')) {
  141. const { bill } = config;
  142. // 使用了余额,但是余额记录不是直接生成的,需要检查下costDetail是否为ObjectId
  143. // 如果是ObjectId,说明余额记录已经生成.无需操作
  144. if (_.isObject(bill)) {
  145. // 是数据,生成消费记录
  146. const cdd = await this.billModel.create({ ...bill, pay_id, time: moment().format('YYYY-MM-DD HH:mm:ss') });
  147. if (cdd) data.config.bill = ObjectId(cdd._id).toString();
  148. await this.model.updateOne({ _id: data._id }, data);
  149. }
  150. }
  151. }
  152. /**
  153. * 重新支付
  154. * @param {String} body 参数体
  155. * @property {String} id 数据id
  156. */
  157. async toRePay({ id }) {
  158. const data = await this.model.findById(id);
  159. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付的信息');
  160. // 找到这个订单,然后获取到订单号
  161. const { order_no } = data;
  162. // 利用订单号去查询微信那边,该订单是否还存在
  163. const wxOrder = await this.payService.search(order_no);
  164. let wxSign;
  165. if (_.get(wxOrder, 'trade_state') === 'NOTPAY') {
  166. // 未支付就用原数据,去支付
  167. wxSign = await this.payService.create(data);
  168. } else if (_.get(wxOrder, 'trade_state') === 'SUCCESS') {
  169. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单已支付,无需重复支付');
  170. } else if (_.get(wxOrder, 'trade_state') === 'USERPAYING') {
  171. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单支付中');
  172. } else {
  173. // 其他情况就当订单被关闭了.直接新订单
  174. const order_no = this.getOrderNo();
  175. data.order_no = order_no;
  176. await data.save();
  177. wxSign = await this.payService.create(data);
  178. }
  179. return { data, wxSign };
  180. }
  181. /**
  182. * 申请退款
  183. * @param {Object} body 参数体
  184. * @property {String} id 支付单的数据id
  185. */
  186. async toRefund({ id }) {
  187. // 查询支付
  188. const data = await this.model.findById(id);
  189. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
  190. const wxOrderReq = await this.payService.search(data.order_no);
  191. if (wxOrderReq) {
  192. if (!wxOrderReq) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未查询到微信支付订单');
  193. const wxRefundReq = await this.payService.refund(data.order_no, '退赛');
  194. if (wxRefundReq) {
  195. if (wxRefundReq.status !== 'REFUND') throw new BusinessError(ErrorCode.SERVICE_FAULT, '退款失败');
  196. // 退款成功
  197. data.status = '-3';
  198. await data.save();
  199. await this.syncData(data);
  200. return 'ok';
  201. }
  202. }
  203. throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询微信支付订单失败');
  204. }
  205. /**
  206. * 关闭订单
  207. * @param {String} id 支付id
  208. * @return {Object} 关闭订单结果
  209. */
  210. async closeOrder(id) {
  211. const data = await this.model.findById(id);
  212. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
  213. const res = await this.payService.close(data.order_no);
  214. return res;
  215. }
  216. getOrderNo() {
  217. return `ONCAPP${moment().format('YYYYMMDDHHmmss')}-${_.random(1, 999999)}`;
  218. }
  219. // 二维码扫码
  220. async toPayNative(body) {
  221. body = await this.beforeCreate(body);
  222. const data = await this.model.create(body);
  223. const qrcode = await this.payService.createNative({ ...body, notice_url: '/newCourt/v2/api/payOrder/nativeReturn' });
  224. return qrcode;
  225. }
  226. // 二维码扫码回调
  227. async nativeReturn(body) {
  228. const result = _.get(body, 'result');
  229. if (!result) throw new BusinessError(ErrorCode.BADPARAM, '参数错误');
  230. const { out_trade_no: order_no, trade_state, payer } = result;
  231. const obj = {};
  232. if (trade_state === 'SUCCESS') obj.status = '1';
  233. else if (trade_state === 'REFUND') obj.status = '-3';
  234. const openid = _.get(payer, 'openid');
  235. if (openid) {
  236. obj.openid = openid;
  237. // 找payer_id的信息,学员,教练
  238. const user = await this.userModel.findOne({ openid });
  239. if (user) {
  240. const { _id: user_id } = user;
  241. let roleUser = await this.studentModel.findOne({ user_id });
  242. if (!roleUser) {
  243. roleUser = await this.coachModel.findOne({ user_id });
  244. if (roleUser) {
  245. obj.payer_role = 'Coach';
  246. obj.payer_id = roleUser._id;
  247. }
  248. } else {
  249. obj.payer_role = 'Student';
  250. obj.payer_id = roleUser._id;
  251. }
  252. }
  253. }
  254. await this.model.updateOne({ order_no }, obj);
  255. const data = await this.model.findOne({ order_no });
  256. await this.syncData(data);
  257. }
  258. }
  259. module.exports = PayOrderService;