系列文章
1、使用wechatpay-apache-httpclient接入微信ApiV3详细步骤
在上一篇文章中给大家讲解了后端如何生成预支付订单,现在就给大家讲解下小程序如何根据后端生成的预支付订单调起微信支付。
注意,小程序使用uniapp开发,因此本文展示的开发代码均是uniapp代码格式
this.$post({
url: 'vip/vipSubmit',
data,
header: {
'content-type': 'application/json',
'X-token': uni.getStorageSync('token'),
},
}).then(res => {
console.log(res)
// return;
if (res.code == '00000') {
uni.requestPayment({
provider: this.payTypeList[this.payType].provider,
timeStamp: res.data.currentTimeLong,
nonceStr: res.data.nonceStr,
package: res.data.packageExt,
signType: res.data.signType,
paySign: res.data.paySign,
success: res => {
console.log(res);
this.$util.redirectTo('pages/userInfo/memberOrder', { }, '', 'redirectTo');
},
fail: res => {
console.log(res);
this.flag = false;
if (res.errMsg == 'requestPayment:fail cancel') {
this.$util.showToast({ title: '您已取消支付' });
} else {
uni.showModal({ content: '支付失败,失败原因: ' + res.errMsg, showCancel: false });
}
this.$util.redirectTo('pages/userInfo/memberOrder', { }, '', 'redirectTo');
}
});
} else {
this.$util.showToast({ title: res.data.msg });
this.flag = false;
}
});
上面代码注意:
1、vip/vipSubmit为后端生成预支付订单号的接口
2、由于本文采用的是JSAPI支付,因此类型不要传错