记录今天使用uni-app写的微信支付问题接口调用报错问题,刚开始是使用uni官网文档的支付API接口,发起支付时就一直提示支付基本参数requestPayment:fail parameter error: parameter.timeStamp should be String instead of Undefined;parameter.nonceStr should be String instead of Undefined等未定义报错,折磨半天也是改了数据方式才能正常支付。
一、uni官网支付API接口
地址:uni.requestPayment(OBJECT) | uni-app官网 (dcloud.net.cn)
uni.requestPayment({
"provider": "wxpay",
"orderInfo": {
"appid": "wx499********7c70e", // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
"noncestr": "c5sEwbaNPiXAF3iv", // 随机字符串
"package": "Sign=WXPay", // 固定值
"partnerid": "148*****52", // 微信支付商户号
"prepayid": "wx202254********************fbe90000", // 统一下单订单号
"timestamp": 1597935292, // 时间戳(单位:秒)
"sign": "A842B45937F6EFF60DEC7A2EAA52D5A0" // 签名,这里用的 MD5/RSA 签名
},
success(res) {},
fail(e) {}
})
就是使用官网API支付接口时订单信息中的参数值在发起支付过程中有可能会出现参数未定义(Undefined)报错问题,具体是什么原因导致自己也是还没摸索出来,小菜了!!
二、根据官网API支付接口自己更改的写法
// 开通支付按钮回调函数方法
ConfirmPay() {
//后端返回所需的支付参数API接口
setPayDo().then(res => {
if (res.code == 20000) {
var data = res.data //订单信息
uni.requestPayment({
provider: "wxpay",
appid: "wx91*******d79f", // 微信开放平台 - 应用 - AppId,注意和微信小程序、公众号 AppId 可能不一致
timeStamp: data.timeStamp, // 时间戳(单位:秒)
package: 'prepay_id=' + data.package, // 固定值
paySign: data.paySign, //签名
signType: data.signType, // 签名算法,这里用的 MD5/RSA 签名
nonceStr: data.nonceStr, // 随机字符串
success(res) {
if (res.errMsg == "requestPayment:ok") {
this.getPayVIP() //调用获取会员信息方法函数,更新会员开通状态
this.$refs.WchatPopup.close() //关闭弹窗
uni.$u.toast('支付成功')
}
},
fail(e) {
if (e.errMsg == "requestPayment:fail cancel") {
uni.$u.toast('支付已取消')
} else {
uni.$u.toast('错误:' + e.errMsg)
}
}
})
}
})
},
这样写就解决了出现支付参数未定义(Undefined)的报错问题了,具体出现报错原因也有可能是后端返回的支付参数数据不是String字符串格式的,可能需要转换成String字符串格式,还有一个问题是最容易被忽略的;其中package参数的赋值方式需要加上 “prepay_id=” + 后端返回的支付数据包串,这里有可能后端返回只返回了串,没带上 “prepay_id=” ,需自行检查后端接口返回的数据体。如果没带上 “prepay_id=” 则会弹出一个警告弹窗,调用支付JSAPI缺少参数:total_fee。