今天想看了微信小程序调起微信支付的代码
首先我们看一下官方的具体文档
这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
});
},