lrf 2 years ago
parent
commit
265daa057e
3 changed files with 26 additions and 4 deletions
  1. 1 1
      app/model/user/userCoupon.js
  2. 24 2
      app/service/trade/pay.js
  3. 1 1
      app/service/user/userCoupon.js

+ 1 - 1
app/model/user/userCoupon.js

@@ -6,7 +6,7 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 const userCoupon = {
   customer: { type: String, required: false, zh: '顾客', ref: 'User.User' }, //
   shop: { type: String, required: false, zh: '发行店铺', ref: 'Shop.Shop' }, //
-  coupon: { type: String, required: false, zh: '优惠券', ref: 'Trade.Coupon' }, //
+  coupon: { type: String, required: false, zh: '优惠券' }, //
   status: { type: String, required: false, default: '0', zh: '是否使用' }, //
 };
 const schema = new Schema(userCoupon, { toJSON: { getters: true, virtuals: true } });

+ 24 - 2
app/service/trade/pay.js

@@ -73,6 +73,10 @@ class PayService extends CrudService {
       await this.tran.rollback();
       console.error(error);
     }
+    if (totalMoney <= 0) {
+      // 小于等于0的支付金额,不需要付款
+      return { needPay: false };
+    }
     if (type === '0') {
       res = await this.create(payData);
       res = this.preparToUniAppWxPay(res);
@@ -80,6 +84,23 @@ class PayService extends CrudService {
     return res;
   }
 
+  async withoutPay({ order_id }) {
+    try {
+      this.tran.update('Order', order_id, { status: '1' });
+      await this.tran.run();
+      // 拆订单
+      await this.ctx.service.trade.orderDetail.create({ order_id }, this.tran);
+      await this.tran.run();
+    } catch (error) {
+      console.error(error);
+      await this.tran.rollback();
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '支付回调:修改失败');
+    } finally {
+      // 清空事务
+      this.tran.clean();
+    }
+  }
+
   /**
    * 支付订单回调函数
    * @param {Object} body 请求地址参数
@@ -112,8 +133,6 @@ class PayService extends CrudService {
       await this.tran.run();
       // 拆订单
       await this.ctx.service.trade.orderDetail.create({ order_id: orderData._id }, this.tran);
-      // 加积分
-      await this.ctx.service.user.point.afterPayOrder({ order_id: orderData._id, openid }, this.tran);
       await this.tran.run();
     } catch (error) {
       console.error(error);
@@ -190,7 +209,10 @@ class PayService extends CrudService {
         if (!detail) continue;
         totalDiscount = this.ctx.plus(totalDiscount, Object.values(detail).reduce((p, n) => this.ctx.plus(p, n), 0));
       }
+      console.log(total, totalDiscount);
       total = this.ctx.minus(total, totalDiscount);
+      console.log(total);
+      if (total <= 0) total = 0;
     }
     return total;
   }

+ 1 - 1
app/service/user/userCoupon.js

@@ -42,7 +42,7 @@ class UserCouponService extends CrudService {
     if (!res.result) return res;
     const customer = _.get(this.ctx, 'user._id');
     if (!customer) throw new BusinessError(ErrorCode.NOT_LOGIN, '未找到用户信息');
-    const data = { coupon: coupon_id, customer, shop: _.get(coupon, 'shop') };
+    const data = { coupon, customer, shop: _.get(coupon, 'shop') };
     try {
       // 领券=>减券库存
       this.tran.insert('UserCoupon', data);