Quellcode durchsuchen

feat 购物流程添加支付模拟

tumobi vor 8 Jahren
Ursprung
Commit
0a00cfb331
3 geänderte Dateien mit 91 neuen und 2 gelöschten Zeilen
  1. 2 2
      src/api/controller/order.js
  2. 74 0
      src/api/controller/pay.js
  3. 15 0
      src/api/logic/pay.js

+ 2 - 2
src/api/controller/order.js

@@ -158,7 +158,7 @@ export default class extends Base {
 
         //开启事务,插入订单信息和订单商品
         let orderId = await this.model('order').add(orderInfo);
-
+        orderInfo.id = orderId;
         if (!orderId) {
             return this.fail('订单提交失败');
         }
@@ -185,7 +185,7 @@ export default class extends Base {
 
         await this.model('cart').clearBuyGoods();
 
-        return this.success('订单提交成功');
+        return this.success({orderInfo: orderInfo});
     }
 
 }

+ 74 - 0
src/api/controller/pay.js

@@ -0,0 +1,74 @@
+'use strict';
+
+import Base from './base.js';
+const rp = require("request-promise");
+const request = require("request");
+
+export default class extends Base {
+
+    // 支付类型 1 微信支付 2支付宝
+    // TODO 支付功能由于没有公司账号和微信支付账号,所以没有经过测试,如您可以提供相关账号测试,可联系 tumobi@163.com
+
+    /**
+     * 获取支付信息(订单信息和支持的支付方式信息)
+     * @return {Promise} []
+     */
+    async indexAction() {
+        //auto render template file index_index.html
+        return this.display();
+    }
+
+    /**
+     * 获取支付的请求参数
+     * @returns {Promise<PreventPromise|void|Promise>}
+     */
+    async payPrepayAction() {
+
+        const orderId = this.get('orderId');
+        const payType = this.get('payType');
+
+        const orderInfo = await this.model('order').where({id: orderId}).find();
+        if (think.isEmpty(orderInfo)) {
+            return this.fail(400, '订单已取消');
+        }
+        console.log(orderInfo)
+        if (parseInt(orderInfo.pay_status) !== 0) {
+            return this.fail(400, '订单已支付,请不要重复操作');
+        }
+
+        //微信支付统一调用接口,body参数请查看微信支付文档:https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_sl_api.php?chapter=9_1
+        let options = {
+            method: 'POST',
+            url: 'https://api.mch.weixin.qq.com/pay/unifiedorder',
+            body: {
+                appid: 'payload',
+                mch_id: '',
+                sub_appid: '',
+                sub_mch_id: '',
+                device_info: '',
+                nonce_str: think.uuid(32),
+                sign: '',
+                sign_type: 'MD5',
+                body: '',
+                out_trade_no: '',
+                total_fee: orderInfo.actual_price * 100,
+                spbill_create_ip: '',
+                notify_url: '',
+                trade_type: 'JSAPI',
+                openid: '',
+                sub_openid: '',
+            },
+        };
+        //let payParam = await rp(options);
+
+        //统一返回成功,方便测试
+        return this.success({
+            'timeStamp': getTime(),
+            'nonceStr': think.uuid(16),
+            'package': 'prepay_id=wx201410272009395522657a690389285100',
+            'signType': 'MD5',
+            'paySign': 'jdsdlsdsd',
+        });
+    }
+
+}

+ 15 - 0
src/api/logic/pay.js

@@ -0,0 +1,15 @@
+'use strict';
+/**
+ * logic
+ * @param  {} []
+ * @return {}     []
+ */
+export default class extends think.logic.base {
+  /**
+   * index action logic
+   * @return {} []
+   */
+  indexAction(){
+   
+  }
+}