实现微信小程序支付

1、在小程序中获取用户的登录信息,成功后可以获取到用户的code值

2、把code值传给服务端,服务端请求微信获取用户openid接口,成功后可以获取用户的openid值

3、服务器上面请求微信的统一下单接口,下单成功后可以获取prepay_id值

4、在微信小程序中支付订单,最终实现微信的支付功能

 //获取code
  getCode() {
    let that = this
    wx.login({
      success: function(res) {
        if (res.code) {
          that.wxPay(res.code)
        } else {
          console.error('获取用户登录态失败!' + res.errMsg)
        }
      }
    })
  },
//支付
  wxPay(code) {
    let that = this;
    let data = {
      js_code: code,
      //数据
    }
    xhr.getBuyService(data).then(res => {
      console.log(res)
      let data = res.Data
      wx.requestPayment({
        timeStamp: data.timeStamp,
        nonceStr: data.nonceStr,
        package: data.package,
        signType: data.signType,
        paySign: data.paySign,
        success: function(res) {
         console.log(res)
        },
        fail: function(err) {
          console.log(err)
        }
      })
    })
  },

  

原文地址:https://www.cnblogs.com/YAN-HUA/p/10557605.html