lrf 2 years ago
parent
commit
32d810a656
3 changed files with 13 additions and 4 deletions
  1. 2 1
      app/service/group/group.js
  2. 0 3
      app/service/trade/orderDetail.js
  3. 11 0
      app/service/trade/pay.js

+ 2 - 1
app/service/group/group.js

@@ -15,6 +15,7 @@ class GroupService extends CrudService {
     this.goodsSpecModel = this.ctx.model.Shop.GoodsSpec;
     this.userModel = this.ctx.model.User.User;
     this.orderModel = this.ctx.model.Trade.Order;
+    this.joinErrorRefundCode = _.get(this.app, 'config.errcode.groupJoinRefund');
   }
 
   /**
@@ -45,7 +46,7 @@ class GroupService extends CrudService {
    */
   async join(customer, group_id, tran) {
     const result = await this.checkGroupCanJoin({ id: group_id, customer });
-    if (!result.result) throw new BusinessError(ErrorCode.DATA_INVALID, result.msg);
+    if (!result.result) throw new BusinessError(this.joinErrorRefundCode, result.msg);
     const data = await this.model.findById(group_id);
     if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到团信息');
     const { persons = [], person_limit } = data;

+ 0 - 3
app/service/trade/orderDetail.js

@@ -58,13 +58,10 @@ class OrderDetailService extends CrudService {
       // #region 团购
       const type = _.get(order, 'type');
       let group = _.get(order, 'group');
-      console.log(type);
-      console.log(group);
       if (type === '1') {
         // 说明是团购,需要找订单中有没有团的id
         obj.type = '1';
         obj.group = group;
-        console.log(group);
         if (!group) {
           // 需要创建团,这是团长支付的单子,开团;然后把id回补
           group = await this.ctx.service.group.group.create(obj, tran);

+ 11 - 0
app/service/trade/pay.js

@@ -16,6 +16,7 @@ class PayService extends CrudService {
     this.payOrderReturnUrl = this.app.config.payReturn.order;
     this.wxDomain = _.get(this.app, 'config.httpPrefix.wechat');
     this.tran = new Transaction();
+    this.joinErrorRefundCode = _.get(this.app, 'config.errcode.groupJoinRefund');
   }
 
   /**
@@ -137,6 +138,16 @@ class PayService extends CrudService {
     } catch (error) {
       console.error(error);
       await this.tran.rollback();
+      let reason = '服务处理发生错误,原路退款!';
+      if (error.errcode === this.joinErrorRefundCode) {
+        reason = '您未能成功参与拼团:拼团人数已满';
+        // 需要删除订单
+        await this.ctx.service.trade.order.cancel({ order_id: _.get(orderData, '_id') });
+      }
+      // 先退款,所有回调发生错误的单子都需要退掉
+      const str = this.ctx.service.util.trade.createNonceStr();
+      const obj = { order_no: _.get(orderData, 'pay.pay_no'), out_trade_no: `${_.get(orderData, 'pay.pay_no')}-service_error-${str}`, money: _.get(orderData, 'pay.pay_money'), reason };
+      await this.refund(obj);
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '支付回调:修改失败');
     } finally {
       // 清空事务