dynCodePay.js 13.9 KB
/*
@songcxa
@2018-12-26
@小票动态扫码移动支付
@动态二维码url包括订单、金额、进出场时间、停车场编号、车牌号等
*/
var webAppParams = null;//URL参数
var $btnObj = null;
var $btnLoad = null;
var webAppCode = null;//如果是微信,先获取微信code
window.onload = function () {
    $btnObj = getObjectByID("btnPayOK");
    $btnLoad = getObjectByID("loading");
    $linkPayDetail = getObjectByID("linkPayDetail");
    $linkPayDetail.onclick = webAppClass.linkPayDetailClick;
    var webRoot = window.location.href;
    //当前客户端浏览器模式[微信、支付宝、其他]
    webAppClass.clientBrowser();
    //绑定事件
    var ali = getObjectByID("rowAliPay");
    var wx = getObjectByID("rowWxPay");
    ali.onclick = webAppClass.aliClick;
    wx.onclick = webAppClass.wxClick;
    $btnObj.onclick = webAppClass.okClick;
    //document.getElementById("payTip").innerHTML = "";
    //参数
    var tmpURLparams = getQueryString(window.location);
    if (tmpURLparams != null) {
        webAppParams = tmpURLparams;
        webAppClass.setUI(webAppParams);
        //webAppClass.init(tmpURLparams);  //设置UI参数
    } else {
        alertMsg("没有接收到url参数信息");
    }

    //获取微信code
    //webAppClass.getCode();
    var state = clientBrowserEx();
    if (state == "wxPay") {
        webAppCode = webAppClass.getCode();
    }
}
/**/
var webAppClass = {
    //OK
    okClick: function (ev) {
        //document.getElementById("payTip").innerHTML = "";
        if (webAppParams == null) {
            alertMsg("暂无订单信息");return;
        }
        //支付方式判断
        webAppClass.okPay(webAppParams);
    },

    //确定支付
    okPay: function (params) {
        var orderID = params.orderId;//订单号
        //第一首先判断当前哪种浏览器方式[微信内置、支付宝内置、第三方浏览器]
        var clientType = clientBrowserEx();
        switch (clientType) {
            case "wxPay"://微信[内置浏览器]
                 webAppClass.payAjaxJDK(orderID);console.log("wxPay");//微信支付
                break;
            case "aliPay"://支付宝
                webAppClass.payAliOption(orderID); console.log("aliPay");
                break;
            default://第三方浏览器other[暂只支持支付宝]
                webAppClass.payAliOption(orderID);
                break;
        }
    },
    //支付宝支付参数
    payAliOption: function (orderID) {
        var root = webAppRoot;
        var aliPay = webAppH5.aliPayServer;
        var aliParams = webAppH5.comParams;
        aliParams.orderId = orderID;
        aliParams.payType = 1;// 1:支付宝 2:微信 3:银联 10:H5   4微信公众号
        aliParams.terminalSource = 7;//请求端来源 1: 任你停 2:pda 3:微信公共号 7:H5扫码 10页面支付
        aliParams.paySrcType = 101;//101停车支付
        webAppClass.payAliAjax(root + aliPay, aliParams);
    },
    //获取微信code
    getCode: function () {
        var appID = appWxID;
        var code = getUrlParam('code');
        var local = window.location.href;
        //alert("local:" + local);
        if (code == null || code === '') {
            //alert(code);
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
        } else {
            return code;
        }
        function getUrlParam(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
            var r = window.location.search.substr(1).match(reg)
            if (r != null) return unescape(r[2])
            return null
        }
    },
    //Ali
    aliClick: function (ev) {
        console.log("AliClick");
        document.getElementById("aliPay").checked = true;
        ev.preventDefault();
    },
    //Wx
    wxClick: function (ev) {
        console.log("wxClick");
        document.getElementById("wxPay").checked = true;
        ev.preventDefault();
    },
    //浏览器
    clientBrowser: function () {
        var type = clientBrowserEx();
        switch (type) {
            case "aliPay":
                this.switchShow("aliPay"); console.log("支付宝客户端");
                break;
            case "wxPay":
                this.switchShow("wxPay"); console.log("微信客户端");
                break;
            default://other
                this.switchShow("other"); console.log("其他浏览器");
                break;
        }
    },
    //根据浏览器显示内容[aliPay、wxPay、other]
    switchShow: function (type) {
        var aliObj = document.getElementById("rowAliPay");
        var wxObj = document.getElementById("rowWxPay");
        var lineObj = document.getElementById("splitLine");
        switch (type) {
            case "aliPay":
                lineObj.style.display = "none";
                wxObj.style.display = "none";
                document.getElementById("aliPay").checked = true;
                break;
            case "wxPay":
                lineObj.style.display = "none";
                aliObj.style.display = "none";
                document.getElementById("wxPay").checked = true;
                break;
            default://其他浏览器[浏览器暂时屏蔽微信]
                lineObj.style.display = "none";
                wxObj.style.display = "none";
                document.getElementById("aliPay").checked = true;
                break;
        }
    },

    //异步请求-微信
    payAjax: function (url, params) {
        var btnOBj = $btnObj;
        postRequest(url, params, function (res) {
            btnOBj.style.display = "block";
            $btnLoad.style.display = "none";
            //debugger;
            if (res.code == 0) {//进场
                if (res.data) {
                    var tmpObj = res.data;
                    if (typeof (tmpObj.mwebUrl) != "undefined") {
                        window.location.href = tmpObj.mwebUrl + "&redirect_url=" + webAppPayResult;
                    } else {
                        alertMsg("没有返回支付地址mwebUrl");
                    }
                    console.log(JSON.stringify(tmpObj));
                } else {
                    alertMsg("没有找到返回值");
                }
            } else {//其他情况如【该卡号场内已存在】
                console.log(res.message);
                alertMsg(res.message);
            }
        }, function (err) {
            console.log("网络地址出错...");
            alertMsg("网络地址出错...");
            btnOBj.style.display = "block";
        });
    },

    payAjaxJDK: function (orderID) {
        //0:页面初始化获取code[webAppCode]
        var codeParams = { code: webAppCode, appId: appWxID };
        var openIdUrl = webAppRoot + window.webAppH5.wxGetOpenIdServer;
        $btnObj.style.display = "none"; $btnLoad.style.display = "block";
        //1:有code获取OpenId
        postRequest(openIdUrl, codeParams, function (res) {
            $btnObj.style.display = "block"; $btnLoad.style.display = "none";
            //alert(JSON.stringify(res));
            if (res.code == 0) {
                webAppClass.getPayParams(res.data, orderID);
            } else {
                alertMsg(res.message);
            }
        }, function (err) {
            $btnObj.style.display = "block"; $btnLoad.style.display = "none";
            console.log("网络地址出错..." + openIdUrl);
        });
    },
    getPayParams: function (openId, orderID) {
        var wxPay = window.webAppH5.wxPayServer;
        var wxParams = window.webAppH5.comParams;
        wxParams.orderId = orderID;
        wxParams.payType = 4;// 1:支付宝 2:微信 3:银联 10:H5   4微信公众号
        wxParams.terminalSource = 7;//请求端来源 1: 任你停 2:pda 3:微信公共号 7:H5扫码 10页面支付
        //webAppClass.payAjax(root + wxPay, wxParams);
        wxParams.openId = openId;
        wxParams.appId = appWxID;
        var payParamsUrl = webAppRoot + window.webAppH5.wxPayServer;
        //alert(payParamsUrl);alert(orderID);
        $btnObj.style.display = "none"; $btnLoad.style.display = "block";
        postRequest(payParamsUrl, wxParams, function (res) {
            $btnObj.style.display = "block"; $btnLoad.style.display = "none";
            //debugger;
            if (res.code == 0) {//进场
                //alertMsg("出场成功");
                if (res.data) {
                    var tmpObj = res.data;
                    webAppClass.weixinJSBridge(res.data);//唤醒wx支付
                    console.log(JSON.stringify(tmpObj));
                    //alert(JSON.stringify(tmpObj));
                } else {
                    alertMsg("没有找到返回值");
                }
            } else {
                $btnObj.style.display = "block";$btnLoad.style.display = "none";
                console.log(res.message); alertMsg(res.message);
            }
        }, function (err) {
            console.log("网络地址出错...");
            alertMsg("网络地址出错..." + payParamsUrl);
            $btnObj.style.display = "block"; $btnLoad.style.display = "none";
        });
    },
    //调用微信内置WeixinJSBridge对象发起支付
    weixinJSBridge: function (params) {
        function onBridgeReady() {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest', params,
               function (res) {
                   //alert(res.err_msg);
                   if (res.err_msg == "get_brand_wcpay_request:ok") {
                       // 使用以上方式判断前端返回,微信团队郑重提示:
                       //res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
                       window.location.href = webAppPayResult + "?trade_no=" + "success";
                   }
                   else if (res.err_msg == 'get_brand_wcpay_request:cancel') {
                       window.location.href = webAppPayResult + "?trade_no=" + "fail";
                   }
                   else {
                       alertMsg(JSON.stringify(res));
                       //alert(res.err_msg);
                   }
               });
        }
        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();
        }
    },
    //异步请求-支付宝
    payAliAjax: function (url, params) {
        $btnObj.style.display = "none"; $btnLoad.style.display = "block";
        postRequest(url, params, function (res) {
            $btnObj.style.display = "block"; $btnLoad.style.display = "none";
            if (res !=null) {
                if (res.code == 0) {//进场
                    document.write(res.data);//打开支付表单
                } else {//其他情况如【该卡号场内已存在】
                    console.log(res.message);
                    alertMsg(res.message);
                }
            } else {
                alertMsg("没有找到次订单信息");
            }
        }, function (err) {
            console.log("网络地址出错...");alertMsg("网络地址出错...");
            $btnObj.style.display = "block"; $btnLoad.style.display = "none";
        });
    },

    //设置UI参数[订单、金额、停车场名称及编号]
    setUI: function (params) {
        var $carNum = getObjectByID("carNum"),//车牌号
            $orderNum = getObjectByID("orderNum"),//订单号
            $sumMoney = getObjectByID("sumMoney"),//实际应付金额
            $outtime = getObjectByID("outtime"),//实际出场时间
            //$payTip = getObjectByID("payTip"),//支付提示

            $parkName = getObjectByID("parkName"),//车场名称
            $inparktime = getObjectByID("inparktime"),//进场时间
            $staytime = getObjectByID("staytime");//停车时长

            //$due = getObjectByID("due"),//停车总费用
            //$paid = getObjectByID("paid"),//已支付金额
            //$discountDesc = getObjectByID("discountDesc"),//8折优惠折扣费用
            //$discountFee = getObjectByID("discountFee");//优惠金额

        if (params != null && params!="") {
            var price = keepTwoDecimalFull((params.orderFee / 100));//(params.orderFee / 100);
            $carNum.innerHTML = params.plateno;//手机号
            $orderNum.innerHTML = params.orderId;//订单号
            $sumMoney.innerHTML = price;///params.orderFee//缴费金额
            $outtime.innerHTML = params.outtime;//实际出场时间
            //.appOrderTimeout
            //$payTip.innerHTML = "注意: " + params.appOrderTimeout;//请在支付完成10分钟内出场,如超时未出场,会继续计费。

            $parkName.innerHTML = params.parkName;
            $inparktime.innerHTML = params.inparktime;
            $staytime.innerHTML = formatSeconds(params.staytime);
            
            //$due.innerHTML = keepTwoDecimalFull((params.due / 100));//停车总费用
            //$paid.innerHTML = params.paid;
            //$discountFee.innerHTML = keepTwoDecimalFull((params.discountFee / 100));//优惠金额
            //$discountDesc.innerHTML = params.discountDesc;
        } else {
            console.log("没有接受到参数");
        }
    },

    //查看详情
    linkPayDetailClick: function (ev) {
        var $payDetail = getObjectByID("payDetail");
        var $linkPayDetail = getObjectByID("linkPayDetail");
        if ($linkPayDetail.innerHTML == "详情+") {
            $linkPayDetail.innerHTML = "详情-";
            $payDetail.style.height = "110px";
        } else {
            $linkPayDetail.innerHTML = "详情+";
            $payDetail.style.height = "55px";
        }
    }
}