当当微信支付js

来自ling
跳转至: 导航搜索
<script type="text/javascript">

    var params = {"user_client":"touch","client_version":"1.0","order_type":0,"order_info":"33360946561,74.10","kmerger":"1","hasweixin":"","hasapplepay":"","shop_id":"","sid":"b2cfe3903e3fb0e3f6873401286b218f"};
    var urlParams = ''; //跳转成功页时,需要拆单的orderinfo,此参数只能从拉单接口获取,所以只能放到拉单ajax里返回

    $(document).ready(function() {
        var proc = new PayCenter(params);
         //打开全屏加载遮罩
        $("#loading").css({"display":"block"});
        var rqData = {op: 'list'};
        var count = 0;
        var counttimes=3;
        var timeout=3000;
        var is_weixin = false;
        errorOpe();
    
        function errorOpe(){
            count++;
            aj = proc.ajax_request(rqData, "", 5000);
            aj.done(function(data){
                if(data.errorCode != 0) {
                    if(data.errorCode == 2){
                        //电子书拒绝支付
                        $("#loading").hide();
                        $("body").addClass("refresh_page").html("").append(data.content);
                        location.hash='ebook_fail';
                        return;
                    }
                    if(count != counttimes){
                        //拉单失败
                        setTimeout(function(){
                            errorOpe();
                        }, timeout)
                    }else{
                        showFailTpl();
                    }
                } else {
                    //拉单成功
                    $("#loading").hide();
                    urlParams = data.urlParams;
                    // 如果在微信下访问 直接触发微信支付
                    if(is_weixin){
                        // 加载WeixinJSBridge后再展示支付方式list
                        if (typeof window.WeixinJSBridge == "undefined"){
                            $(document).on('WeixinJSBridgeReady',function(){
                                $(".order_end.pay_type").html("").append(data.content);
                                $('#weiXinPay').click();
                            });
                        }else{
                            $(".order_end.pay_type").html("").append(data.content);
                            $('#weiXinPay').click();
                        }
                    } else {
                        $(".order_end.pay_type").html("").append(data.content);
                    }
                }
            }).fail(function(){
                if(count != counttimes){
                    errorOpe();
                } else {
                    showFailTpl();
                }
            });
        }

        function showFailTpl() {
            var content=$("#fail_template").html();
            $("#loading").hide();
            $("body").addClass("refresh_page").html("").append(content);

        }

    });
    
    
    function callWeiXinPublicPay(wx_pay_bizpackage)
    {
        try {
            WeixinJSBridge.invoke('getBrandWCPayRequest', wx_pay_bizpackage, function(res) {
                WeixinJSBridge.log(res.err_msg);
                if(res.err_msg == 'get_brand_wcpay_request:ok') {
                    location.href = 'payok.php?' + urlParams;
                }
                else if(res.err_msg == 'get_brand_wcpay_request:cancel') {
                    //alert('取消支付'); 直接关闭窗口
                }
                else if(res.err_msg == 'get_brand_wcpay_request:fail') {
                    location.href = 'paycenter.php' + location.search + '&op=fail';
                }
            });
        } catch (e) {
            alert('不支持此支付方式');
        }

        return false;
    }
    window.onbeforeunload = function() {
        $("#loading").css({"display":"block"});
        $('#fullScreen_mask').show();
    }


</script>