payOrder.js 10 KB

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