lrf 2 years ago
parent
commit
0aff7e754a

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

@@ -47,4 +47,7 @@ module.exports = {
   toRePay: {
     requestBody: ['!id'],
   },
+  toPayNative: {
+    requestBody: ['!school_id', 'pay_for', 'from_id', 'money', 'time'],
+  },
 };

+ 5 - 0
app/controller/business/payOrder.js

@@ -9,5 +9,10 @@ class PayOrderController extends Controller {
     super(ctx);
     this.service = this.ctx.service.business.payOrder;
   }
+
+  async nativeReturn() {
+    const res = await this.service.nativeReturn(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
 }
 module.exports = CrudController(PayOrderController, meta);

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

@@ -14,7 +14,7 @@ const payOrder = {
   time: { type: String, required: false, zh: '时间' }, //
   order_no: { type: String, required: false, zh: '订单号' }, //
   desc: { type: String, required: false, zh: '支付说明' }, //
-  status: { type: String, required: false, default: '0', zh: '支付状态' }, // 0:未支付;1:已支付;-1:支付失败;-2:已退款
+  status: { type: String, required: false, default: '0', zh: '支付状态' }, // 0:未支付;1:已支付;-1:支付失败;-3:已退款
   config: { type: Object, zh: '设置' },
 };
 const schema = new Schema(payOrder, { toJSON: { virtuals: true } });

+ 4 - 4
app/model/relation/relationStudentCoach.js

@@ -4,11 +4,11 @@ const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
 
 // 学员与教练关系表
 const relationStudentCoach = {
-  student_id: { type: String, required: true, zh: '学生id', ref: 'Student', getProp: [ 'name' ] }, //
-  coach_id: { type: String, required: true, zh: '教练id', ref: 'Coach', getProp: [ 'icon', 'name', 'phone', 'age', 'gender', 'level', 'honor' ] }, //
-  school_id: { type: String, required: true, zh: '学校id', ref: 'School', getProp: [ 'name', 'phone' ] }, //
+  student_id: { type: String, required: true, zh: '学生id', ref: 'Student', getProp: ['name'] }, //
+  coach_id: { type: String, required: true, zh: '教练id', ref: 'Coach', getProp: ['icon', 'name', 'phone', 'age', 'gender', 'level', 'honor'] }, //
+  school_id: { type: String, required: true, zh: '学校id', ref: 'School', getProp: ['name', 'phone'] }, //
   doc: { type: Object, required: false, zh: '档案' }, //
-  config: { type: Object, required: false, zh: '设置' }, //
+  config: { type: Object, required: false, zh: '设置' }, // 打折方式 discount_type:fixed 固定;subtract 减; discount折 ;number
 };
 const schema = new Schema(relationStudentCoach, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });

+ 44 - 0
app/service/business/payOrder.js

@@ -12,6 +12,9 @@ class PayOrderService extends CrudService {
     this.payService = this.ctx.service.wxpay;
     this.lessonStudentModel = this.ctx.model.Business.LessonStudent;
     this.tempLessonApplyModel = this.ctx.model.Apply.TempLessonApply;
+    this.userModel = this.ctx.model.User.User;
+    this.studentModel = this.ctx.model.User.Student;
+    this.coachModel = this.ctx.model.User.Coach;
   }
 
   async beforeCreate(data) {
@@ -124,6 +127,47 @@ class PayOrderService extends CrudService {
   getOrderNo() {
     return `ONCAPP${moment().format('YYYYMMDDHHmmss')}-${_.random(1, 999999)}`;
   }
+
+  // 二维码扫码
+  async toPayNative(body) {
+    body = await this.beforeCreate(body);
+    const data = await this.model.create(body);
+    const qrcode = await this.payService.createNative({ ...body });
+    return qrcode;
+  }
+  // 二维码扫码回调
+  async nativeReturn(body) {
+    const result = _.get(body, 'result');
+    console.log(result);
+    if (!result) throw new BusinessError(ErrorCode.BADPARAM, '参数错误');
+    const { out_trade_no: order_no, trade_state, payer } = body;
+    const obj = {};
+    if (trade_state === 'SUCCESS') obj.status = '1';
+    else if (trade_state === 'REFUND') obj.status = '-3';
+    const openid = _.get(payer, 'openid');
+    if (openid) {
+      obj.openid = openid;
+      // 找payer_id的信息,学员,教练
+      const user = await this.userModel.findOne({ openid });
+      if (user) {
+        const { _id: user_id } = user;
+        let roleUser = await this.studentModel.findOne({ user_id });
+        if (!roleUser) {
+          roleUser = await this.coachModel.findOne({ user_id });
+          if (roleUser) {
+            obj.payer_role = 'Coach';
+            obj.payer_id = roleUser._id;
+          }
+        } else {
+          obj.payer_role = 'Student';
+          obj.payer_id = roleUser._id;
+        }
+      }
+    }
+    console.log(order_no);
+    console.log(obj);
+    await this.model.updateOne({ order_no }, obj);
+  }
 }
 
 module.exports = PayOrderService;

+ 9 - 0
app/service/wxpay.js

@@ -38,6 +38,15 @@ class WxpayService extends CrudService {
     throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信下单失败!');
   }
 
+  async createNative(data) {
+    const { money, order_no, desc, notice_url } = data;
+    const wxOrderData = { config: this.appConfig, money, order_no, desc, notice_url };
+    const url = `${this.wxDomain}/pay/payNative`;
+    const res = await this.httpUtil.cpost(url, wxOrderData);
+    if (res) return res;
+    throw new BusinessError(ErrorCode.SERVICE_FAULT, '微信二维码支付下单失败!');
+  }
+
   /**
    * 关闭订单
    * @param {String} order_no 订单号

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

@@ -7,6 +7,8 @@ const rkey = 'payOrder';
 const ckey = 'business.payOrder';
 const keyZh = '支付订单';
 const routes = [
+  { 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}/toRePay`, controller: `${ckey}.toRePay`, name: `${ckey}toRePay`, zh: `${keyZh}-重新支付订单` },
   { method: 'post', path: `${rkey}/closeOrder`, controller: `${ckey}.closeOrder`, name: `${ckey}CloseOrder`, zh: `${keyZh}-关闭订单` },
   { method: 'post', path: `${rkey}/toRefund`, controller: `${ckey}.toRefund`, name: `${ckey}ToRefund`, zh: `${keyZh}-退款` },