项目中遇到了小程序的支付,由于第一次做,说实话心里没有一点底气,总觉得支付很难,最后发现难得不是前端,而是后端,简单说下流程吧,我觉得思路很重要;
首先是请求后端给的接口,传一个钱数的金额给后端人员,后端会返回给一个支付所需要的数组,里面包含了我们唤醒支付所需要的字段,直接
wx.requestPayment就唤醒了支付。
废话少说,直接上代码
//充值
paynow:function(e){
var that=this;
wx.request({
header: { 'Authorization': 'Bearer' + wx.getStorageSync('token') },
url: app.globalData.host+"/pay/wxpay",
data: {
money: that.data.czmoney,
},
method: 'POST',
success(res) {
console.log(res);
if (res.data.status_code == 200) {
const payargs = res.data.data
wx.requestPayment({
timeStamp: payargs.timeStamp,
nonceStr: payargs.nonceStr,
package: payargs.package,
signType: payargs.signType,
paySign: payargs.paySign,
success(res2) {
wx.reLaunch({
url: '../../pages/wallet/index',
})
},
fail(res3) {
wx.showToast({
title:'支付失败!',
icon: 'none',
duration: 2000
})
}
})
}else{
wx.showToast({
title: res.data.message,
icon: 'none',
duration: 2000
})
}
}
})
},