payOrder.js 4.0 KB

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