小程序 调用微信支付

首先调用小程序wx.login登录接口  获取用户code

将code作为参数  传给后端  调用后端接口wechat/pay/prepay

获取后端  这五个返回值

nonceStr 
package2
paySign 
signType
timeStamp

wx.login({
      success: function (res) {
        var userId = wx.getStorageSync('userId');
        var passw = userId + orderId;
        console.log(passw)
        var password = util.sha1(passw);
        //console.log(res) //获取用户的code  微信返回的值 
        wx.request({
          url: url + 'wechat/pay/prepay',
          method: "POST",
          header: {
            'content-type': 'application/json',
          },
          data: {
            code: res.code,  //微信返的code
            userId: userId,  //登录注册时后端返给我的
            orderId: orderId, //用户提交时后端返给我的工单号
            sign: password,  //后端要求的serId + orderId加密
          },
          success: function (res) {
            // if (res.data.resultCode == "000000"){
              nonceStr = res.data.data.nonceStr;
              package2 = res.data.data.package;
              paySign = res.data.data.paySign;
              signType = res.data.data.signType;
              timeStamp = res.data.data.timeStamp;
              that.pay2()
            // }
             console.log(res.data.data.nonceStr)
              console.log(res.data.data.package)
              console.log(res.data.data.paySign)
              console.log(res.data.data.signType)
              console.log(res.data.data.timeStamp)
          },
          fail(err) {
            console.log(err)
          }
        })
      },
      fail(errs) {
        console.log(errs)
      }
    })

成功返回后,用户点击支付,调用微信支付接口wx.requestPayment:

后端设置好金额,用户调用即可支付

pay2() {
    wx.requestPayment(
      {
        timeStamp: timeStamp,  //时间戳
        nonceStr: nonceStr,  //随机字符串
        package: package2, //统一下单接口返回的 prepay_id 参数值
        signType: signType, //签名类型
        paySign: paySign, //签名
        success(res) {
          console.log(res)
          wx.navigateTo({
              url: '../successful/successful'
            })
        },
        fail(res) {
          console.log(res)
        }
      })
  },

最后我们的项目在调用微信wx.requestPayment 支付接口时,在success成功函数里请求了我方后端接口,获取状态码,判断是否支付成功。因为用户支付给微信,成功之后,只有用户能够看到,我方后端是获取不到是否支付成功的,请求这个接口就是这个目的。在微信请求支付成功之后,请求后端获取返回值。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值