lrf 2 years ago
parent
commit
7026e10335

+ 3 - 0
app/controller/business/config/.payOrder.js

@@ -53,4 +53,7 @@ module.exports = {
   payCallBack: {
   payCallBack: {
     requestBody: ['result'],
     requestBody: ['result'],
   },
   },
+  toCharge: {
+    requestBody: ['!school_id', '!student_id', '!money', '!openid'],
+  },
 };
 };

+ 1 - 1
app/model/business/bill.js

@@ -16,7 +16,7 @@ const bill = {
   from_id: { type: String, required: false, zh: '原因id', refPath: 'pay_for' }, //
   from_id: { type: String, required: false, zh: '原因id', refPath: 'pay_for' }, //
   type: { type: String, required: false, zh: '来源' }, // 1:充值;2退款至余额;-1:直接支付;-2余额支付
   type: { type: String, required: false, zh: '来源' }, // 1:充值;2退款至余额;-1:直接支付;-2余额支付
   pay_id: { type: String, required: false, zh: '支付id', ref: 'PayOrder' }, //
   pay_id: { type: String, required: false, zh: '支付id', ref: 'PayOrder' }, //
-  is_pay: { type: String, required: false, default: '0', zh: '是否支付' }, //  0:未支付;1:支付成功;-1:支付失败;-3:已退款
+  is_pay: { type: String, required: false, default: '0', zh: '是否支付' }, //  0:未支付;1:支付成功;-1:支付失败;-3:已退款;-4:充值,已收款但是未到用户的账上
   time: { type: String, required: false, zh: '时间' }, //
   time: { type: String, required: false, zh: '时间' }, //
 };
 };
 const schema = new Schema(bill, { toJSON: { getters: true, virtuals: true } });
 const schema = new Schema(bill, { toJSON: { getters: true, virtuals: true } });

+ 33 - 1
app/service/business/payOrder.js

@@ -23,6 +23,25 @@ class PayOrderService extends CrudService {
     this.billModel = this.ctx.model.Business.Bill;
     this.billModel = this.ctx.model.Business.Bill;
     this.tran = new Transaction();
     this.tran = new Transaction();
   }
   }
+
+  async toCharge(body = {}) {
+    body.payer_role = 'Student';
+    body.pay_for = 'Charge';
+    body.time = moment().format('YYYY-MM-DD HH:mm:ss');
+    body.order_no = this.getOrderNo();
+    body.desc = '充值';
+    try {
+      const pay_id = this.tran.insert('PayOrder', body);
+      const wxSign = await this.payService.create({ ...body, notice_url: this.app.config.payReturn });
+      await this.tran.run();
+      return { pay_id, wxSign };
+    } catch (error) {
+      await this.tran.rollback();
+    } finally {
+      this.tran.clean();
+    }
+  }
+
   /**
   /**
    * 处理支付回调
    * 处理支付回调
    * @param {Object} body 参数体
    * @param {Object} body 参数体
@@ -52,12 +71,25 @@ class PayOrderService extends CrudService {
       // 生成账单
       // 生成账单
       const billData = _.pick(payOrder, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
       const billData = _.pick(payOrder, [ 'school_id', 'payer_id', 'payer_role', 'pay_for', 'from_id' ]);
       // 充值为1,直接支付为-1
       // 充值为1,直接支付为-1
-      if (billData.pay_for && _.lowerCase(billData.pay_for) === 'charge') billData.type = '1';
+      if (billData.pay_for && _.lowerFirst(billData.pay_for) === 'charge') billData.type = '1';
       else billData.type = '-1';
       else billData.type = '-1';
       billData.pay_id = payOrder._id;
       billData.pay_id = payOrder._id;
       billData.is_pay = update.status;
       billData.is_pay = update.status;
       billData.time = moment().format('YYYY-MM-DD HH:mm:ss');
       billData.time = moment().format('YYYY-MM-DD HH:mm:ss');
+      // 如果是充值,则需要将充值的钱加上
+      if (billData.type === '1') {
+        const { money: cm, payer_id, school_id } = payOrder;
+        const rss = await this.rssModel.findOne({ school_id, student_id: payer_id });
+        if (!rss) billData.is_pay = '-4';
+        else {
+          const newMoney = this.ctx.plus(cm, rss.money);
+          this.tran.update('RelationStudentSchool', rss._id, { money: newMoney });
+        }
+      }
+
       this.tran.insert('Bill', billData);
       this.tran.insert('Bill', billData);
+
+
       await this.tran.run();
       await this.tran.run();
     } catch (error) {
     } catch (error) {
       await this.tran.rollback();
       await this.tran.rollback();

+ 1 - 0
app/z_router/business/payOrder.js

@@ -7,6 +7,7 @@ const rkey = 'payOrder';
 const ckey = 'business.payOrder';
 const ckey = 'business.payOrder';
 const keyZh = '支付订单';
 const keyZh = '支付订单';
 const routes = [
 const routes = [
+  { method: 'post', path: `${rkey}/toCharge`, controller: `${ckey}.toCharge`, name: `${ckey}toCharge`, zh: `${keyZh}-充值` },
   { method: 'post', path: `${rkey}/payCallBack`, controller: `${ckey}.payCallBack`, name: `${ckey}payCallBack`, zh: `${keyZh}-正常支付回调` },
   { method: 'post', path: `${rkey}/payCallBack`, controller: `${ckey}.payCallBack`, name: `${ckey}payCallBack`, zh: `${keyZh}-正常支付回调` },
   { method: 'post', path: `${rkey}/nativeReturn`, controller: `${ckey}.nativeReturn`, name: `${ckey}nativeReturn`, zh: `${keyZh}-二维码支付回调` },
   { method: 'post', path: `${rkey}/nativeReturn`, controller: `${ckey}.nativeReturn`, name: `${ckey}nativeReturn`, zh: `${keyZh}-二维码支付回调` },
   { method: 'post', path: `${rkey}/toPayNative`, controller: `${ckey}.toPayNative`, name: `${ckey}toPayNative`, zh: `${keyZh}-二维码支付` },
   { method: 'post', path: `${rkey}/toPayNative`, controller: `${ckey}.toPayNative`, name: `${ckey}toPayNative`, zh: `${keyZh}-二维码支付` },