uni-app uni.requestPayment 取消支付fail方法里面接口没有走
uni-app uni.requestPayment 取消支付fail方法里面接口没有走
项目信息 | 详情 |
---|---|
产品分类 | uniapp/小程序/微信 |
PC开发环境操作系统 | Windows |
PC开发环境操作系统版本号 | 19045.3086 |
HBuilderX类型 | 正式 |
HBuilderX版本号 | 3.8.4 |
第三方开发者工具版本号 | 微信开发者工具1.06 |
基础库版本号 | 2.11.1 |
项目创建方式 | HBuilderX |
操作步骤:
// #ifdef MP-WEIXIN
uni.requestPayment({
'timeStamp': "",
'nonceStr': "",
'package': "",
'signType': "",
'paySign': "",
'success': function(res) {
uni.hideLoading();
_this.isFrolleShow = true
//支付成功
uni.showToast({
title: '正在出票中...',
icon: 'none',
});
setTimeout(() => {
uni.hideToast();
//关闭提示后跳转
uni.navigateTo({
url: "../wdddxq/wdddxq?orderId=" +
order.id
})
}, 4000)
},
'fail': function(res) {
//console.warn(order)
console.error(456)
wx.request({
url: getApp().globalDataserverName +
"/weixin/order_updateOrderToPay.do",
method: "post",
data: {
orderNo: order.orderNo,
openId: getApp().globalData.openId,
platformCode: getApp().globalData.platformCode
},
success: function(res) {
console.log(123)
}
})
//支付失败
uni.showToast({
title: '下单失败!',
duration: 2000,
icon: "none"
})
},
});
// #endif
预期结果:
取消支付后
控制台打印
456
123
实际结果:
只打印了456
bug描述:
uni.requestPayment 取消支付fail方法里面接口没有走
1 回复
在使用 uni.requestPayment
进行支付时,如果用户取消了支付,fail
回调会被触发。如果你在 fail
回调中调用了某个接口,但接口没有执行,可能是以下几个原因导致的:
1. 网络问题
- 确保设备网络连接正常,能够访问服务器。
- 可以在
fail
回调中添加日志,检查是否有网络错误。
2. 接口调用问题
- 确保接口调用的代码正确,没有语法错误或逻辑错误。
- 可以在
fail
回调中添加console.log
或uni.showToast
来调试,确认代码是否执行到接口调用部分。
3. 异步问题
- 如果接口调用是异步的,确保在
fail
回调中正确处理了异步操作。例如,使用async/await
或Promise
来确保接口调用完成。
4. 接口地址或参数问题
- 检查接口地址是否正确,参数是否完整。
- 可以在
fail
回调中打印接口地址和参数,确认是否正确。
5. 服务器问题
- 确保服务器正常运行,接口能够正常响应。
- 可以在
fail
回调中捕获接口调用的错误,查看是否有服务器返回的错误信息。
6. 跨域问题
- 如果接口涉及到跨域请求,确保服务器配置了正确的跨域策略。
7. 调试工具
- 使用
uni-app
提供的调试工具,查看控制台输出,确认是否有错误信息。
示例代码
以下是一个示例代码,展示了如何在 fail
回调中调用接口并进行调试:
uni.requestPayment({
provider: 'wxpay', // 支付提供商,如微信支付
orderInfo: {}, // 支付订单信息
success(res) {
console.log('支付成功', res);
// 支付成功后的处理
},
fail(err) {
console.log('支付失败', err);
// 支付失败后的处理
if (err.errMsg === 'requestPayment:fail cancel') {
console.log('用户取消了支付');
// 调用取消支付的接口
uni.request({
url: 'https://your-api-url.com/cancel-payment',
method: 'POST',
data: {
orderId: 'your-order-id'
},
success(res) {
console.log('取消支付接口调用成功', res);
},
fail(err) {
console.log('取消支付接口调用失败', err);
}
});
}
}
});