1、支付流程
- 调用自己的接口可以理解为下单接口
- 成功后调用微信api wx.requestPayment
2、具体实现代码
topUp为调用下单接口的函数,成功后调用 requestPayment 微信支付的函数
topUp(e) {
const _this = this
if (this.data.checked) {
if (this.data.num >= 0) {
req.tag.wxindex({
money: this.data.num
}).then((res)=>{
_this.setData({
// orderId:orderId, //订单号
num:null //充值金额
})
_this.requestPayment(res.data); //调用微信支付api
})
} else {
console.log("小于50")
wx.showToast({
title: '单笔充值金额需要不低于50元',
icon: 'none',
})
}
} else {
this.agreement()
console.log('未勾选')
}
},
时间戳,随机字符串等参数一般由后端返回,可在第一个下单接口成功后返回,后者调用一个新的接口单独返回。
// 微信支付
requestPayment(data){
var _this = this
console.log(data,'传入的data')
wx.showLoading({
title: '支付中',
icon:'none',
duration:2000
})
wx.requestPayment({
// timeStamp:'',//时间戳
// nonceStr:'',//随机字符串
// package:'',//下单接口返回的
// signType:'MD5',//签名算法,暂支持MD5
// paySign:'', //签名
...data,
success(res){
wx.hideLoading()
_this.setData({
num:null
})
wx.showToast({
title:'支付成功',
icon:'success',
duration:2000
})
},
fail(res){
wx.showToast({
title:'支付失败,请从新支付',
icon:'none',
duration:2000
})
}
})
},