微信小程序调起微信支付接口,开发代码必填的5个参数

今天想看了微信小程序调起微信支付的代码
首先我们看一下官方的具体文档
在这里插入图片描述
这5个参数是必须要填的5个参数,当然作为前端我们需要做的就是将参数放入到指定的接口,然后将数据放入到页面上,今天学习到的就是微信调起微信支付接口
1用当前手机号码+地址发起请求生成订单+用openid+ordercode换取调取微信支付的必要参数5个然后调起微信支付

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nickName:"",
    phone:"",
    area:"",
    addressId:"",
    express:"12.0",
    inwins:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title:"加载中",
      mask:true
    });
    var that=this;
    var app=getApp();
    var infourl=app.globalData.infourl;
    var openId=app.globalData.openId;
    if(openId==""){
      wx.reLaunch({
        url: '../home/home'
      })
    }else{
    wx.request({
      url:infourl+"/orderInterface/queryOrderAddress",
      data:{
        openId:openId
      },
      method:"get",
      success:function(res){
        wx.hideLoading();
        console.log(res);
        var datas=res.data;
        var code=datas.code;
        var content=datas.content;
        var area=content.province+content.city+content.district+content.address;
        var addressId=content.id;
        if(code==200){
          that.setData({
            nickName:content.nickName,
            phone:content.phone,
            area:area,
            addressId:addressId
          });
          
        }else{
          var msg=datas.msg;
          wx.showToast({
            title: msg,
            icon: 'none',
            mask: true
          })
        }
      },
      fail:function(res){
        wx.hideLoading();
        wx.showToast({
          title: '数据连接失败!',
          icon: 'none',
          mask: true
        })
      }
    });
    }
  },
  suborder: function(){//提交订单
    this.setData({
      inwins:true
    });
    wx.showLoading({
      title:"加载中",
      mask:true
    });
    var that=this;
    var app=getApp();
    var infourl=app.globalData.infourl;
    var openId=app.globalData.openId;
    var addressId=this.data.addressId;
    var express=this.data.express;
    wx.request({
      url:infourl+"/orderInterface/submitOrder",
      data:{
        openId:openId,
        addressId:addressId,
        freight:express
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      },
      method:"post",
      success:function(res){
        console.log(res);
        var datas=res.data;
        var code=datas.code;
        var orderCode=datas.content;
        app.globalData.orderCode=orderCode;
        if(code==200){

          wx.request({//订单支付
            url:infourl+"/orderInterface/submitOrderPay",
            data:{
              openId:openId,
              orderCode:orderCode
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded' 
            },
            method:"post",
            success:function(ress){
              wx.hideLoading();
              console.log(ress);
              var datass=ress.data;
              var codes=datass.code;
              var contents=datass.content;
              console.log(contents);
              console.log(contents.timeStamp);
              console.log(contents.nonceStr);
              console.log(contents.timeStamp);
              console.log(contents.timeStamp);
              if(codes==200){
                
                wx.requestPayment({
                  'timeStamp': contents.timeStamp,
                  'nonceStr': contents.nonceStr,
                  'package': contents.package,
                  'signType': contents.signType,
                  'paySign': contents.paySign,
                  'success': function (e) {
                    console.log(e.errMsg);
                    // wx.showToast({
                    //   title: '支付成功',
                    //   icon: 'none',
                    //   mask: true
                    // })
                    wx.reLaunch({
                      url: '../success/success'
                    })
                    // wx.showModal({
                    //   title:"支付确认",
                    //   cancelText:"重新支付",
                    //   cancelColor:"#2AA515",
                    //   confirmText:"支付完成",
                    //   confirmColor:"#000",
                    //   content:"1.如您已完成支付,请点击[支付完成]\r\n2.支付遇到问题,请重新支付",
                    //   success(res){
                    //     if(res.confirm){
                    //       wx.reLaunch({
                    //         url: '../success/success'
                    //       })
                    //     }else if(res.cancel){
                    //       that.suborder();
                    //     }
                    //   }
                    // })
                    
                    // setTimeout(function () {
                    //   wx.reLaunch({
                    //     url: '../../success/success',
                    //   })
                    // }, 500);
                  },
                  'fail': function (e) {
                    console.log(e.errMsg);
                    if (e.errMsg == 'requestPayment:fail cancel') {
                      // wx.showToast({
                      //   title: '已取消支付',
                      //   icon: 'none',
                      //   mask: true
                      // });
                      that.setData({
                        inwins:false
                      });

                    } else {
                      wx.showToast({
                        title: '支付失败',
                        icon: 'none',
                        mask: true
                      })
                    }
                  }
                })
                
              }else{
                var msgs=datass.msg;
                wx.showToast({
                  title: msgs,
                  icon: 'none',
                  mask: true
                })
              }
            },
            fail:function(ress){
              wx.hideLoading();
              wx.showToast({
                title: '数据连接失败!',
                icon: 'none',
                mask: true
              })
            }
          });        
          
        }else{
          wx.hideLoading();
          var msg=datas.msg;
          wx.showToast({
            title: msg,
            icon: 'none',
            mask: true
          })
        }
      },
      fail:function(res){
        wx.hideLoading();
        wx.showToast({
          title: '数据连接失败!',
          icon: 'none',
          mask: true
        })
      }
    });    
  },
  payclose: function(){
    this.setData({
      inwins:true
    });
  },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值