前端做的事: 只要等后台看完api给你返回5个字段就行
wxml: <button bindtap="onWxPay">这不是一个按钮</button>
js:
onWxPay: function () {
let that = this
wx.request({
url: '', //从这个后端接口拿几个字段(如下)
method: 'POST',
data: {
//给后端所需字段,例如:
"appId": "",
"paymentChannel": "wechat"
},
header: {
'Content-Type': 'application/json'
},
success: res => {
//发起微信支付
wx.requestPayment({
//所需字段
appId: res.data.data.appId,
timeStamp: res.data.data.timestamp,
nonceStr: res.data.data.nonceStr,
package: res.data.data.packageStr,
signType: res.data.data.signType,
paySign: res.data.data.sign,
success: function (res) {
console.log("wx.requestPayment返回信息", res)
wx.showModal({ //可有可无
title: '支付成功',
content: '您将在“微信支付”官方号中收到支付凭证',
showCancel: false,
success: function (res) {
if (res.confirm) {
//dosth
} else if (res.cancel) {
//dosth
}
}
})
}
})
}
})
}
!如果不幸报错
调用JSAPI支付缺参数 total_fee => 你只需要仔细检查检查在检查你和后端的代码(各种格式、大小写、顺序)
还是不行的话在仔细看一下api