uni-app 中小程序调用微信支付踩坑

uni-app 中小程序调用微信支付踩坑

自我记录

首先使用了官网的微信APP支付这个是不对的 会报错
requestPayment:fail parameter error: parameter.timeStamp should be String instead of Undefined;parameter.nonceStr should be String instead of Undefined

这个不是小程序支付
https://uniapp.dcloud.net.cn/api/plugins/payment.html#微信app支付

正确调用微信小程序支付
https://uniapp.dcloud.net.cn/api/plugins/payment.html#微信小程序支付

// 仅作为示例,非真实参数信息。
uni.requestPayment({
    provider: 'wxpay',
	timeStamp: String(Date.now()),
	nonceStr: 'A1B2C3D4E5',
	package: 'prepay_id=wx20180101abcdefg',
	signType: 'MD5',
	paySign: '',
	success: function (res) {
		console.log('success:' + JSON.stringify(res));
	},
	fail: function (err) {
		console.log('fail:' + JSON.stringify(err));
	}
});

这里有个注意的地方 我的项目的vue3+ts 按照小程序的这个写法给我报ts警告,没办法只能使用// @ts-ignore 去解决了
坑就在这里,我一开始看到了微信app支付,ts提示也给我提示的这个,我就去用了,怎么调用都报错,后来查了一些文档用现在这个,但是会有ts警告!!!

// 发情求
const onSend = () => {
  // TODO
 uni.showToast({
    title: '调取支付中',
  })
  uni.request({
    url: `${支付地址}`,
    method: 'POST',
    success: async (res: any) => {
      uni.hideLoading()
      const payData = (await res.data.data) as PayData
      // @ts-ignore
      uni.requestPayment({
        provider: 'wxpay', 
        timeStamp: payData.timeStamp, // 时间戳(单位:秒)
        nonceStr: payData.nonceStr, // 随机字符串
        package: payData.package, // 固定值
        signType: payData.signType, // 签名算法,这里用的 MD5/RSA 签名
        paySign: payData.paySign, //签名
        success(res) {
          console.log('🚀 ~ file: index.vue:121 ~ success ~ res:', res)
        },
        fail(e) {
          console.log('🚀 ~ file: index.vue:124 ~ fail ~ e:', e)
        },
      })
    },
    fail: (err) => {
      uni.hideLoading()
      console.log(err)
    },
  })
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值