小程序功能(六)支付

##支付
//appid+secret
  wx.login({
          success: function(res) {
            var code = res.code;
            wx.request({
              url:
                "https://api.weixin.qq.com/sns/jscode2session?appid=appid&secret=secret&js_code=" +code +"&grant_type=authorization_code",
              method: "POST",
              success(res) {
                console.log(res);
                payMP({
                  order_id: that.options.id,//订单id
                  openid: res.data.openid
                }).then(res => {
                  console.log(res, "微信支付");
                  wx.requestPayment({
                    timeStamp: res.data.timeStamp,
                    nonceStr: res.data.nonceStr,
                    package: res.data.package,
                    signType: res.data.signType,
                    paySign: res.data.paySign,
                    success(res) {
                      if (res.errMsg == "requestPayment:ok") {
                        paySuccess({
                          token: wx.getStorageSync("token").token,
                          main_id: that.options.id
                        }).then(res => {
                          console.log(res, "chenggong");
                          if (res.data == 1) {
                            Toast("支付成功");
                            setTimeout(() => {
                              wx.switchTab({
                                url: "/pages/order/index/main"
                              });
                            }, 2000);
                          } else {
                            Toast("支付失败,请重新支付");
                          }
                        });
                      }
                    },
                    fail(res) {
                      Toast("支付失败,请重新支付");
                    }
                  });
                });
              }
            });
          }
        });

你可能感兴趣的:(小程序)