payOrder.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. //
  8. class PayOrderService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'payorder');
  11. this.model = this.ctx.model.Business.PayOrder;
  12. this.payService = this.ctx.service.wxpay;
  13. this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
  14. this.tempLessonApplyModel = this.ctx.model.Apply.TempLessonApply;
  15. }
  16. async beforeCreate(data) {
  17. if (!_.get(data, 'order_no')) data.order_no = this.getOrderNo();
  18. return data;
  19. }
  20. async afterCreate(body, data) {
  21. await this.syncData(data);
  22. const wxSign = await this.payService.create(data);
  23. return { data, wxSign };
  24. }
  25. async afterUpdate(filter, body, data) {
  26. await this.syncData(data);
  27. return data;
  28. }
  29. /**
  30. * 支付同步
  31. * @param {Object} data 支付单数据
  32. */
  33. async syncData(data) {
  34. const pay_for = _.get(data, 'pay_for');
  35. if (!pay_for) {
  36. // 不知道该去同步哪个表的支付状态,不处理
  37. return;
  38. }
  39. const { form_id: _id, status: is_pay } = data;
  40. if (pay_for === 'lessonStudent') {
  41. // 因为上课产生的支付,去找lessonStudent,修改指定学生的支付状态
  42. await this.lessonStudentModel.updateOne({ _id }, { is_pay });
  43. } else if (pay_for === 'tempLessonApply') {
  44. // 私教课临时上课,需要到临时申请那找到相关数据,再修改lessonStudent
  45. const tempApply = await this.tempLessonApplyModel.findById(_id);
  46. if (!tempApply) return;
  47. const { lesson_id, student_id } = tempApply;
  48. await this.lessonStudentModel.updateOne({ lesson_id, student_id }, { is_pay });
  49. }
  50. }
  51. /**
  52. * 重新支付
  53. * @param {String} body 参数体
  54. * @property {String} id 数据id
  55. */
  56. async toRePay({ id }) {
  57. const data = await this.model.findById(id);
  58. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付的信息');
  59. // 找到这个订单,然后获取到订单号
  60. const { order_no } = data;
  61. // 利用订单号去查询微信那边,该订单是否还存在
  62. const wxOrder = await this.payService.search(order_no);
  63. let wxSign;
  64. if (_.get(wxOrder, 'trade_state') === 'NOTPAY') {
  65. // 未支付就用原数据,去支付
  66. wxSign = await this.payService.create(data);
  67. } else if (_.get(wxOrder, 'trade_state') === 'SUCCESS') {
  68. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单已支付,无需重复支付');
  69. } else if (_.get(wxOrder, 'trade_state') === 'USERPAYING') {
  70. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单支付中');
  71. } else {
  72. // 其他情况就当订单被关闭了.直接新订单
  73. const order_no = this.getOrderNo();
  74. data.order_no = order_no;
  75. await data.save();
  76. wxSign = await this.payService.create(data);
  77. }
  78. return { data, wxSign };
  79. }
  80. /**
  81. * 申请退款
  82. * @param {Object} body 参数体
  83. * @property {String} id 支付单的数据id
  84. */
  85. async toRefund({ id }) {
  86. // 查询支付
  87. const data = await this.model.findById(id);
  88. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
  89. const wxOrderReq = await this.payService.search(data.order_no);
  90. if (wxOrderReq) {
  91. if (!wxOrderReq) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未查询到微信支付订单');
  92. const wxRefundReq = await this.payService.refund(data.order_no, '退赛');
  93. if (wxRefundReq) {
  94. if (wxRefundReq.status !== 'REFUND') throw new BusinessError(ErrorCode.SERVICE_FAULT, '退款失败');
  95. // 退款成功
  96. data.status = '-3';
  97. await data.save();
  98. await this.syncData(data);
  99. return 'ok';
  100. }
  101. }
  102. throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询微信支付订单失败');
  103. }
  104. /**
  105. * 关闭订单
  106. * @param {String} id 支付id
  107. * @return {Object} 关闭订单结果
  108. */
  109. async closeOrder(id) {
  110. const data = await this.model.findById(id);
  111. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
  112. const res = await this.payService.close(data.order_no);
  113. return res;
  114. }
  115. getOrderNo() {
  116. return `ONCAPP${moment().format('YYYYMMDDHHmmss')}-${_.random(1, 999999)}`;
  117. }
  118. }
  119. module.exports = PayOrderService;