vue 调用微信支付方法

pay(){
let data ={
  order_id:this.order_id,
  wechatpay_type:this.wechatpay_type,
  merchant_id:localStorage.merchant_id,
  authentication_token:this.token,
  client_token:this.client_token
}

this.$fetch(this.Wxpay, data)
.then(res=>{
  if(res.code==200){
    if (res.data.wechatpay_type == "wxpay") {
    // 公众号支付
    this.callWxPay(res.data.pay_param);
  } else if (res.data.wechatpay_type == "wxpay_h5_wap") {
    this.callWxPayH5(res.data.pay_param.mweb_url);
  } else if (res.data.wechatpay_type == "wxpay_app") {
    this.callWxPayAPP(res.data.pay_param);
  } else {
    Toast('无效的支付类型');
  }
  }else{
    Toast(res.error)
  }
  },err=>{
    alert(JSON.stringify(err))
  })
},
 
jsApiCall(params) {
  let that = this
  WeixinJSBridge.invoke(
    'getBrandWCPayRequest', {
      'appId': params.appId,
      'timeStamp':params.timeStamp,
      'nonceStr': params.nonceStr,
      'package': params.package,
      'signType': params.signType,
      'paySign': params.paySign
    },
    function (res) {
      if (res.err_msg === 'get_brand_wcpay_request:ok') {
        Toast('微信支付成功')
        that.$router.replace({name:'fullOrder',query:{id:'2'}})
      } else if (res.err_msg === 'get_brand_wcpay_request:cancel') {
        Toast('用户取消支付')
        that.$router.replace({name:'fullOrder',query:{id:'1'}})
      } else if (res.err_msg === 'get_brand_wcpay_request:fail') {
        Toast('网络异常,请重试')
      }
    }
  );
},
 
callWxPay(params) {
  if (typeof WeixinJSBridge == "undefined"){
  if( document.addEventListener ){
    document.addEventListener('WeixinJSBridgeReady', this.jsApiCall(params), false);
  }else if (document.attachEvent){
    document.attachEvent('WeixinJSBridgeReady', this.jsApiCall(params));
    document.attachEvent('onWeixinJSBridgeReady', this.jsApiCall(params));
    }
  }else{
    this.jsApiCall(params);
  }
},
 
callWxPayH5(mweb_url) {
  location.href = mweb_url;
},
 
callWxPayAPP(params) {
  let dsBridge = require("dsbridge");
  dsBridge.call("requestWeChatPay", params, function (data) {
    alert(data);
  })
},
 
wechatpaytype(){
  if(this.isWeiXin()){
    this.wechatpay_type='wxpay'
  }else{
    this.wechatpay_type='wxpay_h5_wap'
  }
},
 
isWeiXin(){    //判断是否微信平台
  var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/MicroMessenger/i) == 'micromessenger'){
    return true;
  } else {
    return false;
  }
}

转载于:https://www.cnblogs.com/ruthless/p/9009235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值