vue pc端 支付宝微信支付,实例详解

因为业务需要,公司需要在web端网页生成二维码,通过手机扫码实现支付宝,微信支付的功能。实例如下:

1、安装二维码生成插件,我这里用的是qrcodejs2插件,具体安装方法看我上一篇文章,生成二维码

2、直接上代码,这里的支付宝支付是直接生成新的html页面,支付成功后自动跳转回我们的网站,微信支付是生成二维码,沦陷后获取是否支付成功信息


    //前往支付
    submitPay() {
      if (this.payActive == 0) {
        //支付宝支付
        serveApi.payZFB(obj).then(res => {
          const div = document.createElement('div')
          div.innerHTML = res
          document.body.appendChild(div)
          document.forms[0].acceptCharset = 'UTF-8' //保持与支付宝默认编码格式一致,如果不一致将会出现:调试错误,请回到请求来源地,重新发起请求,错误代码 invalid-signature 错误原因: 验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配
          document.forms[0].submit()
        })
      } else if (this.payActive == 1) {
        //微信支付
        serveApi.payWX(obj).then(res => {
          if (res) {
            this.dialogWXpay = true
            this.drawCode(res.codeUrl)
            // 定时查询是否支付成功
            this.timer = setInterval(() => {
              this.queryIsPay()
            }, 3000)
          }
        })
      }
    },
    // 绘制二维码
    drawCode(value) {
      this.$nextTick(() => {
        let qrcode = new QRcode('qrcode', {
          width: 250,
          height: 250,
          text: value, // 二维码地址
          colorDark: '#000',
          colorLight: '#fff'
        })
      })
    },
    // 微信支付查询是否支付成功
    queryIsPay() {
      serveApi
        .confirmOrder(
          {
            id: sessionStorage.ZDDorderId,
            payment: this.payActive == 0 ? 2 : 1 //支付方式 1 微信 2 支付宝,
          },
          {
            ingoreMessage: true
          }
        )
        .then(res => {
          if (res && res.code == 0) {
            clearInterval(this.timer)
            this.timer = null
            this.$message.success('支付成功,2秒后将自动跳转到订单管理页面')
            setTimeout(() => {
              this.$router.push({
                path: '/user/myorder'
              })
            }, 2000)
          } else {
            return false
          }
        })
        .catch(err => {
          return false
        })
    },
    // 关闭弹窗清除轮询
    closeWXpay() {
      clearInterval(this.timer)
      this.timer = null
    }

如此,大功告成。

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值