小程序調用微信支付

  onLoad: function (options) {   //獲取用戶的Code

    // 登錄
    wx.login({
      success: function (res) {
        // 發送 res.code 到後臺換取 openId, sessionKey, unionId
        var that = this;
        if (res.code) {
          console.log('獲取用戶登錄態success!' + res.code)
          app.globalData.code = res.code
        } else {
          console.log('獲取用戶登錄態失敗!' + res.errMsg)
        }
      }
    })  
  },

getOpenId:function(that, code){    //獲取OpenId
    console.log(code);
    let operFlag = "getOpenid";
    console.log(operFlag);
    wx.request({
      url: 'https://xxx/wechatserver/servlet/WechatServlet',
      data: {
        code:code,
        operFlag:operFlag
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res); 
        var openid = res.data.openid;
        console.log(openid);
        that.paypay(that, openid); //預下單並支付
      },
      fail: function (res) {
        console.log(res.data.errmsg);
        console.log(res.data.errcode);
      },
      complete:function(res){
      }
    })
  },

paypay(){

var openId = uni.getStorageSync('openId')   
                var that = this
                uni.request({    //獲取購物券
                    method: 'POST',
                    url: getApp().globalData.url + '/wx/wxPay',  //保存所選會員卡回滾
                    data:{
                        payAmount:0.01,
                        openId:openId
                    },
                    header: {
                        "Content-Type": "application/json" //如果爲空,加上頭部接收     
                    },
                    success: (res) => {
                        console.log(res,'res')
                        uni.requestPayment({
                          timeStamp: res.data.data.timeStamp,
                          nonceStr: res.data.data.nonceStr,
                          package: `prepay_id=${res.data.data.prepayId}`,
                          signType: res.data.data.signType,
                          paySign: res.data.data.sign,
                          success: function (event) {
                            // success   
                            console.log(event);
                            if(event.errMsg == "requestPayment:ok") {
                               uni.request({
                                method: 'POST',
                                url:  getApp().globalData.url + '/applet/card/applyCardOrConsumerCard',
                                data: {
                                    mechanismId:getApp().globalData.mechanismId,
                                    staffId:'',
                                    customerId:uni.getStorageSync('user').customerId,
                                    // customerId:51,
                                    storeId:getApp().globalData.storeId,
                                    cardId:that.cardId
                                },
                                header: {
                                    "Content-Type": "application/json" //如果爲空,加上頭部接收     
                                },
                                success: (res) => {
                                    console.log(res,'購買')
                                    if(res.data.result == 'success') {
                                        uni.showToast({
                                            title:'購買成功',
                                            icon:'none'
                                        })
                                        uni.navigateTo({
                                            url:"../VIPcar/VIpcarlist"
                                        })
                                    }
                                }
                               })
                            }
                            
                          },
                          fail: function (error) {
                            uni.showToast({
                                title:'支付失敗',
                                icon:'none'
                            })
                          }
                        });
                    },fail:(err) => {
                            uni.showToast({
                                title:'網絡請求超時,請重試',
                                icon:'none'
                            })
                        }
                })

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章