小程序调用微信支付前还需要一系列准备工作
- 申请资质认证填写公司资料,等待审核通过拿到AppID和AppSecret
- 申请开通微信支付,等待审核通过
- 编写小程序端代码
// An highlighted block
wxPay: function (e) {
var that = this;
wx.request({
url: app.data.address,
header: {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": `Bearer ` + wx.getStorageSync("token"),
},
data: {
},
method: 'POST',
dataType: 'json',
responseType: 'text',
success: function (ress) {
if (ress.data.code == 200) {
wx.requestPayment({
timeStamp: ress.data.data.timeStamp,
nonceStr: ress.data.data.nonceStr,
package: ress.data.data.package,
signType: 'MD5',
paySign: ress.data.data.paySign,
success(res) {
wx.request({
url: app.data.address,
header: {
"Content-Type": "application/json;charset=UTF-8",
"Authorization": `Bearer ` + wx.getStorageSync("token"),
},
data: {
},
method: 'POST',
dataType: 'json',
success: function (ress) {
if (ress.data.code == 200) {
wx.showToast({
icon: 'success',
title: "支付成功",
duration: 1500,
})
}
}
});
},
fail(res) {
wx.showToast({
icon: 'none',
title: '支付失败'
})
}
});
}
}
});
},