公众号 微信支付 2种方法

第一种

import router from '../router/index'
let Pay = new Object();
function onBridgeReady(jsApi) {
    let _this = this;
    console.log(jsApi,JSON.stringify(jsApi));
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest',JSON.parse(jsApi),
        function(res){
            if(res.err_msg=='get_brand_wcpay_request:ok'){
                router.push({                        
                    path: '/myOrder'
                });
            }else if(res.err_msg=='get_brand_wcpay_request:cancel'){
                //信息框
                _this.$toast('您已取消支付');
            }

        }
    );
}
//微信支付
Pay.wxPay = function(jsApi,type) {
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    }else{
        onBridgeReady(jsApi,type);
    }
}

export default Pay;

第二种

//通过config接口注入权限验证配置
		 _this.$http.post(_this.$http.wx_config,{
            headers:{
                     'Content-Type': 'application/x-www-form-urlencoded'
                 }
             }).then(res => {
                 console.log(res.data);
                 if(res.code==1){
                     wx.config({
                         // debug: true, // 开启调试模式
                         appId: res.data.appId, // 公众号的唯一标识
                         timestamp: res.data.timestamp, // 生成签名的时间戳
                         nonceStr: res.data.nonceStr, // 生成签名的随机串
                         signature: res.data.signature,// 签名
                         jsApiList: ['chooseWXPay'] // 填入需要使用的JS接口列表,这里是先声明我们要用到支付的JS接口
                     });
                 }
             })
                        
               wx.ready(function(){
                     //弹出支付窗口
                     wx.chooseWXPay({
                         timestamp: wxdata.data.str.timeStamp, // 支付签名时间戳,
                         nonceStr: wxdata.data.str.nonceStr, // 支付签名随机串,不长于 32 位
                         package: wxdata.data.str.package , // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=xxxx)
                         signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
                         paySign: wxdata.data.str.paySign, // 支付签名
                         success: function (res) {
                             // 支付成功后的回调函数
                             _this.$router.push({
                                 path: "/myOrder"
                             });
                             //成功之后返回首页
                         },cancel:function(res){
                             // alert("失败:"+res);
                             // _this.$toast(res);
                         },fail:function(res){
                             console.log("失败1111:"+JSON.stringify(res));
                         }
                     });
                 });
                        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值