微信小程序支付功能的对接

  // 继续支付
  continuePay() {
    const that = this
    wx.removeStorageSync('orderId')// 清除 orderId  
    wx.setStorageSync('orderId', this.data.orderDetail.orderNo) //保存orderId
    console.log(that.data.orderDetail.orderNo)
    preOrderPay({ // 这个是后台提供的接口
      orderNo: that.data.orderDetail.orderNo,
      openId: wx.getStorageSync('openid'),
      userId: '', // 支付宝才使用
      payType: '1' // 0支付宝 1微信
    }).then(res => {
      console.log('返回的支付信息', res)
      that.setData({
        wechatPay: res.data
      })
      this.weChatPay()
    }).catch(error => {
      wx.showToast({
        icon: 'none',
        title: error.msg
      })
    })
  },

  // 调用微信支付
  weChatPay () {
	const that = this
	console.log(that.data.wechatPay)
    // 调用微信支付api(重要)
    wx.requestPayment({
      timeStamp: that.data.wechatPay.timeStamp,
      nonceStr: that.data.wechatPay.nonceStr,
      package: that.data.wechatPay.package,
      signType: that.data.wechatPay.signType,
      paySign: that.data.wechatPay.paySign,
      success: function(res) {
        console.log('支付成功')
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
        });
        wx.navigateBack({
          delta: 2,
        })
      },
      fail: function(error) {
		console.log(error)
        wx.showToast({
          icon: 'error',
          title: '支付失败'
        })
      }
    })
  },

猜你喜欢

转载自blog.csdn.net/weixin_43550562/article/details/128901224