Browse Source

update[litemall-wx]: 小商场小程序端支持微信支付。
1. 删除原来pay.js,而是直接在下单页面和订单付款页面直引入微信支付api
2. 小商城后台服务的订单预支付会话api调整

Junling Bu 7 years ago
parent
commit
8ab026529e

+ 1 - 3
litemall-wx/config/api.js

@@ -48,8 +48,6 @@ module.exports = {
     CartGoodsCount: WxApiRoot + 'cart/goodscount', // 获取购物车商品件数
     CartCheckout: WxApiRoot + 'cart/checkout', // 下单前信息确认
 
-    PayPrepayId: WxApiRoot + 'pay/prepay', //获取微信统一下单prepay_id
-
     CollectList: WxApiRoot + 'collect/list',  //收藏列表
     CollectAddOrDelete: WxApiRoot + 'collect/addordelete',  //添加或取消收藏
 
@@ -74,7 +72,7 @@ module.exports = {
     RegionList: WxApiRoot + 'region/list',  //获取区域列表
 
     OrderSubmit: WxApiRoot + 'order/submit', // 提交订单
-    OrderPay: WxApiRoot + 'order/pay', // 支付订单
+    OrderPrepay: WxApiRoot + 'order/prepay', // 订单的预支付会话
     OrderList: WxApiRoot + 'order/list',  //订单列表
     OrderDetail: WxApiRoot + 'order/detail',  //订单详情
     OrderCancel: WxApiRoot + 'order/cancel',  //取消订单

+ 49 - 29
litemall-wx/pages/payResult/payResult.js

@@ -1,6 +1,5 @@
 var util = require('../../utils/util.js');
 var api = require('../../config/api.js');
-const pay = require('../../services/pay.js');
 
 var app = getApp();
 Page({
@@ -32,36 +31,57 @@ Page({
   },
   payOrder() {
     let that = this;
-    // 目前不能支持微信支付,这里仅仅是模拟支付成功,同理,后台也仅仅是返回一个成功的消息而已
-    wx.showModal({
-      title: '目前不能微信支付',
-      content: '点击确定模拟支付成功,点击取消模拟未支付成功',
-      success: function (res) {
-        if (res.confirm) {
-          util.request(api.OrderPay, { orderId: that.data.orderId }, 'POST').then(res => {
-            if (res.errno === 0) {
-              that.setData({
-                status: true
-              });
-            }
-            else {
-              util.showErrorToast('支付失败');
-            }
-          });
-        }
-        else if (res.cancel) {
-          util.showErrorToast('支付失败');
-        }
+    // 模拟支付成功,同理,后台也仅仅是返回一个成功的消息而已
+    // wx.showModal({
+    //   title: '目前不能微信支付',
+    //   content: '点击确定模拟支付成功,点击取消模拟未支付成功',
+    //   success: function (res) {
+    //     if (res.confirm) {
+    //       util.request(api.OrderPrepay, { orderId: that.data.orderId }, 'POST').then(res => {
+    //         if (res.errno === 0) {
+    //           that.setData({
+    //             status: true
+    //           });
+    //         }
+    //         else {
+    //           util.showErrorToast('支付失败');
+    //         }
+    //       });
+    //     }
+    //     else if (res.cancel) {
+    //       util.showErrorToast('支付失败');
+    //     }
 
+    //   }
+    // });
+
+    util.request(api.OrderPrepay, {
+      orderId: that.data.orderId
+    }, 'POST').then(function (res) {
+      if (res.errno === 0) {
+        const payParam = res.data;
+        console.log("支付过程开始")
+        wx.requestPayment({
+          'timeStamp': payParam.timeStamp,
+          'nonceStr': payParam.nonceStr,
+          'package': payParam.packageValue,
+          'signType': payParam.signType,
+          'paySign': payParam.paySign,
+          'success': function (res) {
+            console.log("支付过程成功")
+            that.setData({
+              status: true
+            });
+          },
+          'fail': function (res) {
+            console.log("支付过程失败")
+            util.showErrorToast('支付失败');
+          },
+          'complete': function (res) {
+            console.log("支付过程结束")
+          }
+        });
       }
     });
-
-    // pay.payOrder(this.data.orderId).then(res => {
-    //   this.setData({
-    //     status: true
-    //   });
-    // }).catch(res => {
-    //   util.showErrorToast('支付失败');
-    // });
   }
 })

+ 53 - 32
litemall-wx/pages/shopping/checkout/checkout.js

@@ -1,6 +1,5 @@
 var util = require('../../../utils/util.js');
 var api = require('../../../config/api.js');
-const pay = require('../../../services/pay.js');
 
 var app = getApp();
 
@@ -106,47 +105,69 @@ Page({
       if (res.errno === 0) {
         const orderId = res.data.orderId;
 
-      // 目前不能支持微信支付,这里仅仅是模拟支付成功,同理,后台也仅仅是返回一个成功的消息而已
-      wx.showModal({
-        title: '目前不能微信支付',
-        content: '点击确定模拟支付成功,点击取消模拟未支付成功',
-        success: function(res) {
-          if (res.confirm) {
-            util.request(api.OrderPay, { orderId: orderId }, 'POST').then(res => {
-              if (res.errno === 0) {
+        // 模拟支付成功,同理,后台也仅仅是返回一个成功的消息而已
+        // wx.showModal({
+        //   title: '目前不能微信支付',
+        //   content: '点击确定模拟支付成功,点击取消模拟未支付成功',
+        //   success: function(res) {
+        //     if (res.confirm) {
+        //       util.request(api.OrderPrepay, { orderId: orderId }, 'POST').then(res => {
+        //         if (res.errno === 0) {
+        //           wx.redirectTo({
+        //             url: '/pages/payResult/payResult?status=1&orderId=' + orderId
+        //           });
+        //         }
+        //         else{
+        //           wx.redirectTo({
+        //             url: '/pages/payResult/payResult?status=0&orderId=' + orderId
+        //           });
+        //         }
+        //       });
+        //     }
+        //     else if (res.cancel) {
+        //       wx.redirectTo({
+        //         url: '/pages/payResult/payResult?status=0&orderId=' + orderId
+        //       });
+        //     }
+
+        //   }
+        // });
+
+        util.request(api.OrderPrepay, {
+          orderId: orderId
+        }, 'POST').then(function (res) {
+          if (res.errno === 0) {
+            const payParam = res.data;
+            console.log("支付过程开始")
+            wx.requestPayment({
+              'timeStamp': payParam.timeStamp,
+              'nonceStr': payParam.nonceStr,
+              'package': payParam.packageValue,
+              'signType': payParam.signType,
+              'paySign': payParam.paySign,
+              'success': function (res) {
+                console.log("支付过程成功")
                 wx.redirectTo({
                   url: '/pages/payResult/payResult?status=1&orderId=' + orderId
                 });
-              }
-              else{
+              },
+              'fail': function (res) {
+                console.log("支付过程失败")
                 wx.redirectTo({
                   url: '/pages/payResult/payResult?status=0&orderId=' + orderId
                 });
+              },
+              'complete': function (res) {
+                console.log("支付过程结束")
               }
             });
           }
-          else if (res.cancel) {
-            wx.redirectTo({
-              url: '/pages/payResult/payResult?status=0&orderId=' + orderId
-            });
-          }
-
-        }
-      });
+        });
 
-      //   pay.payOrder(orderId).then(res => {
-      //     wx.redirectTo({
-      //       url: '/pages/payResult/payResult?status=1&orderId=' + orderId
-      //     });
-      //   }).catch(res => {
-      //     wx.redirectTo({
-      //       url: '/pages/payResult/payResult?status=0&orderId=' + orderId
-      //     });
-      //   });
-      // } else {
-      //   wx.redirectTo({
-      //     url: '/pages/payResult/payResult?status=0&orderId=' + orderId
-      //   });
+      } else {
+        wx.redirectTo({
+          url: '/pages/payResult/payResult?status=0&orderId=' + orderId
+        });
       }
     });
   }

+ 0 - 56
litemall-wx/services/pay.js

@@ -1,56 +0,0 @@
-/**
- * 支付相关服务
- */
-
-const util = require('../utils/util.js');
-const api = require('../config/api.js');
-
-/**
- * 判断用户是否登录
- */
-function payOrder(orderId) {
-  return new Promise(function (resolve, reject) {
-    util.request(api.PayPrepayId, {
-      orderId: orderId
-    }).then((res) => {
-      console.log(res)
-      if (res.errno === 0) {
-        const payParam = res.data;
-        wx.requestPayment({
-          'timeStamp': payParam.timeStamp,
-          'nonceStr': payParam.nonceStr,
-          'package': payParam.package,
-          'signType': payParam.signType,
-          'paySign': payParam.paySign,
-          'success': function (res) {
-            resolve(res);
-          },
-          'fail': function (res) {
-            reject(res);
-          },
-          'complete': function (res) {
-            reject(res);
-          }
-        });
-      } else {
-        reject(res);
-      }
-    });
-  });
-}
-
-
-module.exports = {
-  payOrder,
-};
-
-
-
-
-
-
-
-
-
-
-