vue中调起支付

最近项目中涉及到支付,h5中的微信支付,支付宝支付,微信内部的支付;

1.h5中的支付宝支付

h5中的支付宝支付后端会返回支付宝支付url或者form代码段,一般情况会返回form代码段,支付宝第三方支付会返回url,打开支付宝url,或者提供容器,打开form,跳转支付页面;

// 订单支付支付宝
let href = window.location.host;
href =window.location.protocol +"//" + href +"/landingPage/getGiftV3?outTradeNo=" +this.outTradeNo +"&channelCode=" + window.localStorage.getItem("channelCode");
//接口参数
let parmas = {
        domain: href,
        outTradeNo: this.outTradeNo,
     };
//调用接口
this.$http.post(viporderali, parmas)
.then((res) => {
       if (res.code == 200) {

            //后端会返回支付宝支付url或者form代码段
            //一般情况会返回form代码段,支付宝第三方支付会返回url
            if(res.data.indexOf("<form") == -1){

              //后端提供支付宝支付url
                location.replace(res.data);

             }else{

             //后端返回form代码段
             //提供容器,跳转到支付页面
             const div = document.createElement("div");
             div.innerHTML = res.data;
             document.body.appendChild(div);
             document.forms[0].submit();
             }
           }
        }).catch((err) => {
             console.log(err);
        });

2.h5中的微信支付

h5中的微信支付后端会返回微信支付的url,获取当前url地址作为回调地址,拼在返回的微信支付url后面,通过encodeURIComponent函数编码,访问编码后的url,可以跳转微信支付;

 // 订单支付微信h5
let href = window.location.host;
href = window.location.protocol + "//" + href;
//接口参数
let parmas = {
       domain: href,
       outTradeNo: this.outTradeNo,
     };
//调用接口
this.$http.post(viporderh5, parmas)
 .then((res) => {
          if (res.code == 200) {
                //获取当前url
                let url =href +"/landingPage/getGiftV3?outTradeNo=" +this.outTradeNo + "&channelCode=" + window.localStorage.getItem("channelCode");
                 //url编码
                 url = encodeURIComponent(url);
                 //将编码后的url拼在返回的url后面
                 let redirect_url = res.data + "&redirect_url=" + url;
                 //跳转微信支付
                 location.replace(redirect_url);
               }
        }).catch((err) => {
              console.log(err);
        });

3.微信内部支付

在微信浏览器中打开的页面调起微信支付

1.先要获取appid

2.编码当前url

3.在https://open.weixin.qq.com/connect/oauth2/authorize?response_type=code&scope=snsapi_base&state=STATE#wechat_redirect中拼接appid和当前herf


//调用获取appid的接口
this.$http.get(appid)
     .then((res) => {
         if (res.code == 200) {

             //拿到appid
             this.appid = res.data;
             //获取当前url
             let href = window.location.href;
             //编码
             href = encodeURIComponent(href);
             //在当前链接中拼接
             //appid=${this.appid}&redirect_uri=${href}
             location.replace(
                `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${this.appid}&redirect_uri=${href}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`
              );
            }
          })
          .catch((err) => {
            console.log(err);
          });
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值