uniapp APP端微信支付避坑流程

上周五到这周一直研究app端微信支付,踩了好多坑,太痛了!

app端支付宝支付还好,微信支付真的踩了一堆坑。

1、首先支付功能需要到manifest.json文件中,在安卓/ios模块配置中,开启Payment(支付),根据自己的需求开启,如果是微信支付appid必须和微信开放平台一样。

2、uniapp微信支付参数传入(uniapp官网截图)

3、安卓端和IOS中appId区别

// 获取当前app在哪个端          
         const platform = uni.getSystemInfoSync().platform;
        // 安卓端是appid,IOS端是appId,一定区分好,uniapp文档并没有说明。
          const appId = platform === 'ios' ? 'appId' : 'appid';
          const orderInfo = {
            [appId]: res.result.appid,
            // appid: res.result.appid,
            noncestr: String(res.result.nonceStr),
            package: res.result.packageVal,
            partnerid: res.result.partnerId,
            prepayid: res.result.prepayId,
            timestamp: res.result.timestamp,
            sign: String(res.result.sign),
          };
          if (res.code == 200) {
            uni.requestPayment({
              provider: 'wxpay',
              orderInfo: orderInfo,
              success(res) {
                console.log('微信支付成功:', res);
              },
              fail(e) {
                console.log('微信支付失败:', e);
              },
            });
          }

4、线下测试报错。

安卓端报错包名和微信开发平台不一致或IOS报错appid商户参数不正确。

首先需要重复检查你的包名和appid是否和微信开放平台一样,如果一样,那可能是下面的问题了。

如果要测试安卓或ios手机的微信支付功能,点击运行->app云打包需要打一个自定义基座,才能测试。标准基座是测试不了微信支付的。

 打包成功之后,你的项目中unpackage->debug文件夹下面就会多了一个安卓的apk文件。

自定义基座打包完成之后,点击运行app基座。


这时你就可以勾选自定义基座,测试微信支付了。一般如果你是真机调试,这里可以直接连接上你的设备。

到这里,我这边就成功了,安卓和IOS我这边都支付成功了。感觉微信支付并不难,就是太多坑了,很多问题uniapp官网都没有提过,我也是查了很多文档才搞定的。

微信和支付宝总体代码。

//支付宝、微信支付
      async pay() {
         // 支付宝支付
        if (this.paymentList[this.paymentIndex].title == '支付宝') {
          // 获取支付参数
          const { data: res } = await serviceAdvanced()
          if (res.code == 200) {
            uni.requestPayment({
              provider: 'alipay',
              orderInfo: res.result, // 字符串
              success(res) {},
              fail(e) {
                this.$refs.uToast.show({
                  title: '支付出错,请重试',
                  type: 'warning',
                });
              },
            });
          }
        } else {
            // 微信支付
           // 获取微信需要的参数对象
          const { data: res } = await createOrder();
          console.log('后端返回信息:', res.result);
          const platform = uni.getSystemInfoSync().platform;
          const appId = platform === 'ios' ? 'appId' : 'appid';
    
          const orderInfo = {
            [appId]: res.result.appid,
            // appid: res.result.appid, // appId和appid还不能同时传,否则还会报错
            noncestr: String(res.result.nonceStr),
            package: res.result.packageVal,
            partnerid: res.result.partnerId,
            prepayid: res.result.prepayId,
            timestamp: res.result.timestamp,
            sign: String(res.result.sign),
          };
          if (res.code == 200) {
            uni.requestPayment({
              provider: 'wxpay',
              orderInfo: orderInfo,
              success(res) {
                console.log('微信支付成功:', res);
              },
              fail(e) {
                console.log('微信支付失败:', e);
              },
            });
          }
        }
      },

//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值