IT评测·应用市场-qidao123.com技术社区

标题: uniapp实现微信支付和app支付,兼容小程序和app [打印本页]

作者: 曂沅仴駦    时间: 2025-1-14 22:27
标题: uniapp实现微信支付和app支付,兼容小程序和app


1,微信支付

各平台支持的支付情况阐明

1.1,从后端获取支付签名数据

  1. 从后端获取支付所需
  2. this.$api.pay.pullUpPayment(payParam).then(res => {
  3.      if (res.code == 200) {
  4.         this.pullUpPayInfo = res.result
  5.      }
  6. })
复制代码
1.2,调用uni.requestPayment(options)调起微信支付

  1. let that = this;
  2. // 在小程序中,以下是支付所需的主要参数,其他参数参考官方文档
  3. // #ifdef MP-WEIXIN
  4. uni.requestPayment({
  5.     provider: 'wxpay',
  6.         timeStamp: this.pullUpPayInfo.timeStamp,
  7.         nonceStr: this.pullUpPayInfo.nonceStr,
  8.         package: this.pullUpPayInfo.package,
  9.         signType: this.pullUpPayInfo.signType,
  10.         paySign: this.pullUpPayInfo.paySign,
  11.         success: function (res) {
  12.                 console.log('success:' + JSON.stringify(res));
  13.         },
  14.         fail: function (err) {
  15.                 console.log('fail:' + JSON.stringify(err));
  16.         }
  17. });
  18. // #endif
  19. // 在app中,需要在自己的小程序中实现个支付页面,然后app中微信支付的时候,打开自己小程序的支付页面完成支付
  20. // #ifdef APP-PLUS
  21. let minProgram = {
  22.         path: `pagesC/pages/shops/orderSubmit/pay?data=${that.pullUpPayInfo.payId}&payType=1&orderIds=${JSON.stringify(that.pullUpPayInfo.orderIdList)}`, //  可带参数
  23.         type: that.miniProgramType || 0, //可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。
  24.         id: 'gh_aa7750a7c7677' //小程序的原始id       
  25. }
  26. // sweixin需要在页面onLoad中处理
  27. this.sweixin ? this.sweixin.launchMiniProgram({...minProgram},
  28.     res => {
  29.     },
  30.     err => {
  31.     if (err.code == -8) {
  32.        uni.showToast({
  33.           title: "未安装微信客户端",
  34.           duration: 2000
  35.        });
  36.        return
  37.     } else {
  38.        // 跳到小程序的首页
  39.        uni.switchTab({
  40.            url: '/pagesD/pages/index/index/index'
  41.        });
  42.     }
  43. }) : plus.nativeUI.alert('当前环境不支持微信操作!');
  44. // #endif
复制代码
  1. onLoad(options){
  2.     // #ifdef APP-PLUS  
  3.     plus.share.getServices((s) => {
  4.       let shares = {};
  5.       for (let i = 0; i < s.length; i++) {
  6.         let t = s[i];
  7.         shares[t.id] = t;
  8.       }
  9.       let sweixin = shares['weixin'];
  10.       this.sweixin = sweixin
  11.       console.log(this.sweixin)
  12.     }, function (e) {
  13.       console.log("获取分享服务列表失败:" + e.message);
  14.     });
  15.     //#endif
  16. }
复制代码

2,支付宝支付

2.1,从后端获取支付签名数据

  1. 从后端获取支付所需
  2. this.$api.pay.pullUpPayment(payParam).then(res => {
  3.      if (res.code == 200) {
  4.         this.pullUpPayInfo = res.result
  5.      }
  6. })
复制代码
2.2,调用uni.requestPayment(options)调起微信支付

orderInfo 注意事项

  1. // pullUpPayInfo 为从后端获取的支付签名参数
  2. uni.requestPayment({
  3.         provider: 'alipay',
  4.         orderInfo: this.pullUpPayInfo, //微信、支付宝订单数据
  5.         success: function (log) {
  6.                 console.log('支付成功')
  7.         },
  8.         fail: function (err) {
  9.                 console.log('我今天支付失败了')
  10.         }
  11. });
复制代码
以下为通过支付宝交易号拉支付宝支付
  1. // 通过支付宝交易号拉支付宝支付
  2. uni.requestPayment({
  3.         provider: 'alipay',
  4.         orderInfo: `trade_no=${payInfo.tradeNo}`,
  5.         success: function (log) {
  6.                 console.log('支付成功')
  7.         },
  8.         fail: function (err) {
  9.                 console.log('我今天支付失败了')
  10.         }
  11. });
复制代码


App平台支付流程

流程:支付平台功能申请 -> manifest.json 里设置支付参数 -> uni-app 里调用 API 进行支付
manifest.json里设置相关参数



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4