payOrder.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 { from_id: _id, status: is_pay } = data;
  40. if (pay_for === 'lessonStudent') {
  41. // 因为上课产生的支付,去找lessonStudent,修改指定学生的支付状态
  42. console.log(is_pay, _id);
  43. const r = await this.lessonStudentModel.updateOne({ _id }, { is_pay });
  44. console.log(r);
  45. } else if (pay_for === 'tempLessonApply') {
  46. // 私教课临时上课,需要到临时申请那找到相关数据,再修改lessonStudent
  47. const tempApply = await this.tempLessonApplyModel.findById(_id);
  48. if (!tempApply) return;
  49. const { lesson_id, student_id } = tempApply;
  50. await this.lessonStudentModel.updateOne({ lesson_id, student_id }, { is_pay });
  51. }
  52. }
  53. /**
  54. * 重新支付
  55. * @param {String} body 参数体
  56. * @property {String} id 数据id
  57. */
  58. async toRePay({ id }) {
  59. const data = await this.model.findById(id);
  60. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到支付的信息');
  61. // 找到这个订单,然后获取到订单号
  62. const { order_no } = data;
  63. // 利用订单号去查询微信那边,该订单是否还存在
  64. const wxOrder = await this.payService.search(order_no);
  65. let wxSign;
  66. if (_.get(wxOrder, 'trade_state') === 'NOTPAY') {
  67. // 未支付就用原数据,去支付
  68. wxSign = await this.payService.create(data);
  69. } else if (_.get(wxOrder, 'trade_state') === 'SUCCESS') {
  70. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单已支付,无需重复支付');
  71. } else if (_.get(wxOrder, 'trade_state') === 'USERPAYING') {
  72. throw new BusinessError(ErrorCode.SERVICE_FAULT, '订单支付中');
  73. } else {
  74. // 其他情况就当订单被关闭了.直接新订单
  75. const order_no = this.getOrderNo();
  76. data.order_no = order_no;
  77. await data.save();
  78. wxSign = await this.payService.create(data);
  79. }
  80. return { data, wxSign };
  81. }
  82. /**
  83. * 申请退款
  84. * @param {Object} body 参数体
  85. * @property {String} id 支付单的数据id
  86. */
  87. async toRefund({ id }) {
  88. // 查询支付
  89. const data = await this.model.findById(id);
  90. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
  91. const wxOrderReq = await this.payService.search(data.order_no);
  92. if (wxOrderReq) {
  93. if (!wxOrderReq) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未查询到微信支付订单');
  94. const wxRefundReq = await this.payService.refund(data.order_no, '退赛');
  95. if (wxRefundReq) {
  96. if (wxRefundReq.status !== 'REFUND') throw new BusinessError(ErrorCode.SERVICE_FAULT, '退款失败');
  97. // 退款成功
  98. data.status = '-3';
  99. await data.save();
  100. await this.syncData(data);
  101. return 'ok';
  102. }
  103. }
  104. throw new BusinessError(ErrorCode.SERVICE_FAULT, '查询微信支付订单失败');
  105. }
  106. /**
  107. * 关闭订单
  108. * @param {String} id 支付id
  109. * @return {Object} 关闭订单结果
  110. */
  111. async closeOrder(id) {
  112. const data = await this.model.findById(id);
  113. if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要退款的支付数据');
  114. const res = await this.payService.close(data.order_no);
  115. return res;
  116. }
  117. getOrderNo() {
  118. return `ONCAPP${moment().format('YYYYMMDDHHmmss')}-${_.random(1, 999999)}`;
  119. }
  120. }
  121. module.exports = PayOrderService;