diff --git a/billCodePay.html b/billCodePay.html new file mode 100644 index 0000000..202147a --- /dev/null +++ b/billCodePay.html @@ -0,0 +1,98 @@ + + + + + + + + + + + + + + + + + + + + + + + 订单支付 + + + + + + +
+
车场名称:详情+
+
停车时长:
+
进场时间:
+
停车总费用:
+
已支付金额:
+
优惠金额:
+
订单编号:
+
+
+
+ 选择支付方式 出场时间: +
+
+ +
+ + 支付宝支付 + +
+
+ +
+ + 微信支付 + +
+
+
+ +
+ + + + + diff --git a/config.js b/config.js index 6b33064..b3fa78a 100644 --- a/config.js +++ b/config.js @@ -5,10 +5,15 @@ */ window.webAppH5 = {}; +//微信appID +var appWxID = "wx1e9001e5940605b1";//wx945eccc8163fd75b + /*服务器地址*/ -var webAppRoot = "http://39.98.54.240:8090"; //"http://39.98.54.240:8090"; -//var webApp -var webAppPayResult = "http://test.pay.renniting.cn/codepay/page/wxpayResult.html"; +//var webAppRoot = "http://39.98.54.240:8090"; +var webAppRoot = "http://pay.service.renniting.cn/v1"; + +//微信支付结果回调地址 +var webAppPayResult = "http://wxgzh.renniting.cn/wechatwuxi/DEV/Threewf/codepay/page/wxpayResult.html"; /*公共参数*/ webAppH5.comParams = { @@ -20,7 +25,7 @@ webAppH5.comParams = { token: '666' } -//小票扫码支付参数 +//小票扫码支付参数codeType[0静态码、1小票二维码、2动态二维码] webAppH5.dynCode = {//卫岗固定参数 direction: 1, codeType: 1, @@ -33,6 +38,9 @@ webAppH5.comServer = "/keyTopQrCodeInOutPark/noCarQRcodeInOutPark"; //缴费-微信 webAppH5.wxPayServer = "/weixinpay/publicUnifiedOrder"; +//微信获取openID +webAppH5.wxGetOpenIdServer = "/weixinPublicPay/getOpenIdByCode"; + //缴费-支付宝 webAppH5.aliPayServer = "/alipay/aliH5Pay"; @@ -71,7 +79,7 @@ webAppH5.aliPayServer = "/alipay/aliH5Pay"; //小票二维码出场 //http://39.98.54.240/codepay/index.html?appid=0&lotId=1&payConfigId=2&receiptNo=3 -//http://localhost:1260/codepay/index.html?appid=0&lotId=1&payConfigId=2&receiptNo=3 +//http://localhost:1260/codepay/index.html?appid=0&lotId=3504&payConfigId=2&receiptNo=3& //首页 @@ -89,5 +97,22 @@ webAppH5.aliPayServer = "/alipay/aliH5Pay"; //http://localhost:1260/pay.html?orderId=101526715300086198272&parkName=%E9%9D%99%E9%9B%85%E5%9C%B0%E4%B8%8A&parkCode=B1504020C7&inparktime=2018-12-24%2010%3A58%3A24&outtime=2018-12-24%2011%3A05%3A10&staytime=420&due=70&paid=0&orderTotalFee=70&orderFee=7&discountFee=63&plateno=%E8%92%99DTK366&outPayOrderNo=B1504020C720181224110510813DTK366-172192102&appOrderTimeout=%E8%AF%B7%E5%9C%A8%E6%94%AF%E4%BB%98%E5%AE%8C%E6%88%9010%E5%88%86%E9%92%9F%E5%86%85%E5%87%BA%E5%9C%BA%EF%BC%8C%E5%A6%82%E8%B6%85%E6%97%B6%E6%9C%AA%E5%87%BA%E5%9C%BA%EF%BC%8C%E4%BC%9A%E7%BB%A7%E7%BB%AD%E8%AE%A1%E8%B4%B9%E3%80%82&discountDesc=1%E6%8A%98%E4%BC%98%E6%83%A0&queryOrderInfo=%7B%22app_id%22%3A%221%22%2C%22carNumber%22%3A%22%E8%92%99DTK366%22%2C%22channelId%22%3A%22172192102%22%2C%22codeType%22%3A0%2C%22deviceInfo%22%3A%223%22%2C%22direction%22%3A1%2C%22parkingId%22%3A%22B1504020C7%22%2C%22plNo%22%3A%22B1504020C7%22%2C%22salt%22%3A%222%22%2C%22sign%22%3A%224%22%2C%22sign_type%22%3A%22md5%22%2C%22terminalSource%22%3A%227%22%2C%22token%22%3A%22666%22%7D&needPay=true +/*正式环境前缀*/ +//http://wxgzh.renniting.cn/wechatwuxi/DEV/Threewf/codepay + +/* +lotId -->parkingId +receiptNo --> carNumber 车牌号 +direction 固定 1 +codeType 固定 1 +terminalSource 固定 7 +channelId 固定 小票二维码 +*/ +//http://localhost:1260/dynCodePay.html?appid=0&lotId=3504&payConfigId=2&receiptNo=%E8%92%99D9203C +//http://wxgzh.renniting.cn/wechatwuxi/DEV/Threewf/codepay/index.html?appid=0&lotId=3504&payConfigId=2&receiptNo=%E8%92%99D9203C + +//http://wxgzh.renniting.cn/wechatwuxi/DEV/Threewf/codepay/index.html?appid=0&lotId=3504&payConfigId=2&receiptNo=蒙D871R2 +/*类型1:codeType2动态二维码正式环境地址前缀*/ +//http://wxgzh.renniting.cn/wechatwuxi/DEV/Threewf/codepay/index.html?codeType=2&isNeedPay=1&orderId=001&parkName=万达停车场&parkCode=a001&inparktime=2018-12-26 00:00:00&outtime=2018-12-26 10:00:00&staytime=360&orderFee=100&plateno=蒙DY2408 diff --git a/doc.txt b/doc.txt index c69cef9..b04a465 100644 --- a/doc.txt +++ b/doc.txt @@ -1,19 +1,3 @@ -http://132.232.101.107/webApp/paywxali/index.html - -http://132.232.101.107/webApp/paywxali/demo.html -жϵǰֻ΢Ż֧ - - - - -΢ź֧ӿڷܽ -https://blog.csdn.net/yufeng005/article/details/54583467 - - - -ajax֧ -https://www.cnblogs.com/junjieok/p/4538580.html - @@ -51,3 +35,38 @@ http://39.98.54.240/codepay/index.html?plNo=P11011700C&parkingId=1&channelId=172 //Էҳ http://39.98.54.240/codepay/index.html?plNo=P11011700C&parkingId=1&channelId=172198243&direction=1&codeType=0&expireDate=expireDate&extendData=extendData + + +˵ +̨֧formֱ֧򵥡 +΢֧鷳1Ҫ֧Ŀ¼2ȡcode3ȡopenid4ȡ5֧ JSAPI֧ + +pay.htmlУ鶩ڷ + +//СƱ̶ +[billCodePay.htmlɨά룬ֽ] +Ӧϵ +lotId -->parkingId ͣ +receiptNo --> carNumber ƺ +direction ̶ 1 +codeType ̶ 1 +terminalSource ̶ 7 +channelId ̶ СƱά + + +//̬ά[dynCodePay.html] +String qrcodeType;//2-̬ά +private Boolean isNeedPay=true; +private String orderId;// +private String parkName;//ͣ +private String parkCode;//ͣ +private String inparktime;//ʱ ʽyyyy-MM-dd HH:mm:ss +private String outtime;//ʱ ʽyyyy-MM-dd HH:mm:ss +private String staytime;//ͣʱ1 +private String orderFee;//γʵӦȥۿۣŻȯ֮Ӧ +private String plateno;// ƺ + +?codeType=2&isNeedPay=1&orderId=001&parkName=ͣ&parkCode=a001&inparktime=2018-12-26 00:00:00&outtime=2018-12-26 10:00:00&staytime=1&orderFee=100&plateno=DY2408 + + + diff --git a/dynCodePay.html b/dynCodePay.html index d8b17ee..2bf8d43 100644 --- a/dynCodePay.html +++ b/dynCodePay.html @@ -13,12 +13,11 @@ - - - - - - + + + + + + + +
订单编号:
@@ -81,18 +80,8 @@
确定支付
-
+ - diff --git a/index.html b/index.html index 0f1f5c3..c5519a8 100644 --- a/index.html +++ b/index.html @@ -66,16 +66,22 @@ } //type[fail、sucesss] function getResponse(params) { + //debugger; var direction = params.direction;//direction=0 [通行方向 入:0,1:出 必填] var queryParams = parseParams(params); - //首先判断是否小票二维码标识位lotId - if (typeof(params.lotId)!="undefined") { - window.location.href = "dynCodePay.html?" + queryParams; + //小票二维码特殊处理[不带codeType] + if (typeof(params.lotId) != "undefined") { + window.location.href = "billCodePay.html?" + queryParams; } else { - if (direction == 0) {//入口 - window.location.href = "page/in.html?" + queryParams; - } else {//出口 - window.location.href = "page/out.html?" + queryParams; + //跳转标识codeType[0静态码、1小票二维码、2动态二维码] + if (params.codeType == 2) { + window.location.href = "dynCodePay.html?" + queryParams; + } else { + if (direction == 0) {//入口 + window.location.href = "page/in.html?" + queryParams; + } else {//出口 + window.location.href = "page/out.html?" + queryParams; + } } } } diff --git a/js/billCodePay.js b/js/billCodePay.js new file mode 100644 index 0000000..e92340c --- /dev/null +++ b/js/billCodePay.js @@ -0,0 +1,448 @@ +/* +@songcxa +@2018-12-14 +@小票动态扫码移动支付 +*/ +var webAppParams = null; +var $btnObj = null; +var $btnLoad = null; +var webAppCode = null;//如果是微信,先获取微信code +window.onload = function () { + //var root = document.getElementById("root"); + $btnObj = document.getElementById("btnPayOK"); + $btnLoad = document.getElementById("loading"); + $linkPayDetail = getObjectByID("linkPayDetail"); + $linkPayDetail.onclick = webAppClass.linkPayDetailClick; + var webRoot = window.location.href; + //当前客户端浏览器模式[微信、支付宝、其他] + webAppClass.clientBrowser(); + //绑定事件 + var ali = document.getElementById("rowAliPay"); + var wx = document.getElementById("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) { + //设置UI参数 + //webAppClass.setUI(webAppParams); + webAppClass.init(tmpURLparams); + } else { + alertMsg("没有接收到url参数信息"); + } + + //获取微信code + //webAppClass.getCode(); + var state = clientBrowserEx(); + if (state == "wxPay") { + webAppCode = webAppClass.getCode(); + } +} +/**/ +var webAppClass = { + //appid={0}&lotId={1}&payConfigId={2}&receiptNo= + init: function (params) { + var urlParams = params; + var newParams = urlParams || {}; + $btnObj.style.display = "none"; + $btnLoad.style.display = "block"; + newParams.terminalSource = "7";//1:任你听 3:微信公共号 4:云平台 7:H5 + //var tmpParams = Object.assign(params, window.webAppH5.comParams, window.webAppH5.appOut); + newParams.parkingId = params.lotId; + newParams.carNumber = params.receiptNo; + var tmpParams = Object.assign(newParams, window.webAppH5.comParams, window.webAppH5.dynCode); + this.initRequest(webAppRoot + window.webAppH5.comServer, tmpParams, "init"); + }, + initRequest: function (url, params, init) { + var btnObj = $btnObj; + postRequest(url, params, function (res) { + btnObj.style.display = "block"; + $btnLoad.style.display = "none"; + if (res.code == 0) {//进场 + //alertMsg("出场成功"); + if (res.data) { + var tmpObj = res.data; + //判断是否需要支付 + if (tmpObj.needPay) { + //var queryParams = parseParams(tmpObj); + //window.location.href = "../pay.html?" + queryParams; + webAppParams = tmpObj; + webAppClass.setUI(tmpObj); + } else { + var tipStr = "无需缴费,欢迎下次光临"; + alertMsg(tipStr); + } + } else { + alertMsg("没有找到订单"); + } + } else {//其他情况如【该卡号场内已存在】 + console.log(res.message); alertMsg(res.message); + if ((init) != "init") { alertMsg(res.message); } + } + }, function (err) { + console.log("网络服务超时..." + url); + alertMsg("网络服务超时"); + btnObj.style.display = "block"; + $btnLoad.style.display = "none"; + }); + }, + //OK + okClick: function (ev) { + document.getElementById("payTip").innerHTML = ""; + if (webAppParams == null) { + alertMsg("暂无订单信息");return; + } + //先校验停车费用后缴费提交 + webAppClass.checkParkCost(); + }, + //校验该订单当前支付时的费用(排除扫描订单后不支付时长) + checkParkCost: function () { + $btnObj.style.display = "none"; + $btnLoad.style.display = "block"; + if (typeof (webAppParams.queryOrderInfo) == "undefined") {//没有queryOrderInfo返回第一页 + window.history.back(-1); + } + var tmpParams = JSON.parse(webAppParams.queryOrderInfo); + var url = webAppRoot + window.webAppH5.comServer; + postRequest(url, tmpParams, function (res) { + //btnObj.style.display = "block"; //$btnLoad.style.display = "none"; + //$btnObj.style.display = "block"; + if (res.code == 0) {//进场 + var tmpObj = res.data; + if (tmpObj) { + webAppClass.setUI(tmpObj); + webAppParams = tmpObj;//重新赋值 + webAppClass.okPay(tmpObj);//唤醒支付 + } else { + alertMsg("没有找到相应订单"); + } + } else {//其他情况如【该卡号场内已存在】 + $btnLoad.style.display = "none"; + $btnObj.style.display = "block"; + console.log(res.message); alertMsg(res.message); + } + }, function (err) { + console.log("网络服务超时..." + url); + alertMsg("网络服务超时"); + $btnLoad.style.display = "none"; + $btnObj.style.display = "block"; + //$btnLoad.style.display = "none"; + }); + }, + + //确定支付 + okPay: function (params) { + var orderID = params.orderId;//订单号 + //var moneyID = document.getElementById("sumMoney").innerHTML = webAppParams.orderId;//订单号 + var root = webAppRoot; + //第一首先判断当前哪种浏览器方式[微信内置、支付宝内置、第三方浏览器] + var clientType = clientBrowserEx(); + switch (clientType) { + case "wxPay"://微信[内置浏览器] + webAppClass.payAjaxJDK(orderID); + console.log("wxPay");//微信支付 + break; + case "aliPay"://支付宝 + console.log("aliPay"); + //订单号 + var obj = {}; + //this.payAjax(url, { orderId: "11111", price: 0.01 }); + 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); + break; + default://第三方浏览器other[] + //默认支付宝 + if (document.getElementById("aliPay").checked) { + console.log("aliPay"); + //订单号 + var obj = {}; + //this.payAjax(url, { orderId: "11111", price: 0.01 }); + 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); + } else {//微信页面支付 + var url = " https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096"; + //window.location.href = url; + var wxPay = webAppH5.wxPayServer; + var wxParams = 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); + console.log("wxPay"); + } + break; + } + }, + 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 { + //alert(code); + 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 () { + if (/MicroMessenger/.test(window.navigator.userAgent)) { + console.log("微信客户端"); + this.switchShow("wxPay"); + } else if (/AlipayClient/.test(window.navigator.userAgent)) { + console.log("支付宝客户端"); + this.switchShow("aliPay"); + } else { + console.log("其他浏览器"); + this.switchShow("other"); + } + }, + //根据浏览器显示内容[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) { + var btnOBj = $btnObj; + //0:页面初始化获取code[webAppCode] + var codeParams = { code: webAppCode, appId: appWxID }; + var openIdUrl = webAppRoot + window.webAppH5.wxGetOpenIdServer; + //alert(openIdUrl); + //1:有code获取OpenId + postRequest(openIdUrl, codeParams, function (res) { + //alert(JSON.stringify(res)); + if (res.code == 0) { + //alert(res.data); + webAppClass.getPayParams(res.data, orderID); + } else { + alertMsg(res.message); + } + }, function (err) { + console.log("网络地址出错..." + openIdUrl); + }); + //2:获取支付参数 + //function getPayParams(openId) { + + //} + }, + 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); + 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); + 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); + $btnLoad.style.display = "none"; + $btnObj.style.display = "block"; + }); + }, + //调用微信内置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) { + var btnOBj = $btnObj; + postRequest(url, params, function (res) { + btnOBj.style.display = "block"; + $btnLoad.style.display = "none"; + if (res.code == 0) {//进场 + document.write(res.data);//打开支付表单 + } else {//其他情况如【该卡号场内已存在】 + console.log(res.message); + alertMsg(res.message); + } + }, function (err) { + console.log("网络地址出错...");alertMsg("网络地址出错..."); + $btnLoad.style.display = "none"; + btnOBj.style.display = "block"; + }); + }, + + //设置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 = "195px"; + } else { + $linkPayDetail.innerHTML = "详情+"; + $payDetail.style.height = "55px"; + } + } +} \ No newline at end of file diff --git a/js/common.js b/js/common.js index a4d6eb9..3dd8d7e 100644 --- a/js/common.js +++ b/js/common.js @@ -215,4 +215,24 @@ function checkCharAndNumber(ev) { //this.value = this.value.toUpperCase(); var tmpValue = this.value.replace(/[^\d|chun]/g, ''); this.value = this.value.toUpperCase(); -} \ No newline at end of file +} + +/*检测当前app浏览器*/ +function clientBrowserEx() { + var state = "other";//default + if (/MicroMessenger/.test(window.navigator.userAgent)) { + console.log("微信客户端"); + //this.switchShow("wxPay"); + state = "wxPay"; + } else if (/AlipayClient/.test(window.navigator.userAgent)) { + console.log("支付宝客户端"); + //this.switchShow("aliPay"); + state = "aliPay"; + } else { + console.log("其他浏览器"); + state = "other"; + } + return state; +} + + diff --git a/js/dynCodePay.js b/js/dynCodePay.js index 333eccd..25f143d 100644 --- a/js/dynCodePay.js +++ b/js/dynCodePay.js @@ -1,161 +1,104 @@ /* @songcxa -@2018-12-14 +@2018-12-26 @小票动态扫码移动支付 +@动态二维码url包括订单、金额、进出场时间、停车场编号、车牌号等 */ -var webAppParams = null; +var webAppParams = null;//URL参数 var $btnObj = null; var $btnLoad = null; -//var $btnLoad = document.getElementById("loading"); +var webAppCode = null;//如果是微信,先获取微信code window.onload = function () { - //var root = document.getElementById("root"); - $btnObj = document.getElementById("btnPayOK"); - $btnLoad = document.getElementById("loading"); + $btnObj = getObjectByID("btnPayOK"); + $btnLoad = getObjectByID("loading"); $linkPayDetail = getObjectByID("linkPayDetail"); $linkPayDetail.onclick = webAppClass.linkPayDetailClick; var webRoot = window.location.href; //当前客户端浏览器模式[微信、支付宝、其他] webAppClass.clientBrowser(); //绑定事件 - var ali = document.getElementById("rowAliPay"); - var wx = document.getElementById("rowWxPay"); + var ali = getObjectByID("rowAliPay"); + var wx = getObjectByID("rowWxPay"); ali.onclick = webAppClass.aliClick; wx.onclick = webAppClass.wxClick; $btnObj.onclick = webAppClass.okClick; - document.getElementById("payTip").innerHTML = ""; + //document.getElementById("payTip").innerHTML = ""; //参数 var tmpURLparams = getQueryString(window.location); if (tmpURLparams != null) { - //设置UI参数 - //webAppClass.setUI(webAppParams); - webAppClass.init(tmpURLparams); + 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 = { - //appid={0}&lotId={1}&payConfigId={2}&receiptNo= - init: function (params) { - var urlParams = params; - var newParams = urlParams || {}; - $btnObj.style.display = "none"; - $btnLoad.style.display = "block"; - newParams.terminalSource = "7";//1:任你听 3:微信公共号 4:云平台 7:H5 - //var tmpParams = Object.assign(params, window.webAppH5.comParams, window.webAppH5.appOut); - newParams.parkingId = params.lotId; - newParams.carNumber = params.receiptNo; - var tmpParams = Object.assign(newParams, window.webAppH5.comParams, window.webAppH5.dynCode); - this.initRequest(webAppRoot + window.webAppH5.comServer, tmpParams, "init"); - }, - initRequest: function (url, params, init) { - var btnObj = $btnObj; - postRequest(url, params, function (res) { - btnObj.style.display = "block"; - $btnLoad.style.display = "none"; - if (res.code == 0) {//进场 - //alertMsg("出场成功"); - if (res.data) { - var tmpObj = res.data; - //判断是否需要支付 - if (tmpObj.needPay) { - //var queryParams = parseParams(tmpObj); - //window.location.href = "../pay.html?" + queryParams; - webAppParams = tmpObj; - webAppClass.setUI(tmpObj); - } else { - var tipStr = "无需缴费,欢迎下次光临"; - alertMsg(tipStr); - } - } else { - alertMsg("没有找到订单"); - } - } else {//其他情况如【该卡号场内已存在】 - console.log(res.message); alertMsg(res.message); - if ((init) != "init") { alertMsg(res.message); } - } - }, function (err) { - console.log("网络服务超时..." + url); - alertMsg("网络服务超时"); - btnObj.style.display = "block"; - $btnLoad.style.display = "none"; - }); - }, //OK okClick: function (ev) { - document.getElementById("payTip").innerHTML = ""; + //document.getElementById("payTip").innerHTML = ""; if (webAppParams == null) { alertMsg("暂无订单信息");return; } - //先校验停车费用后缴费提交 - webAppClass.checkParkCost(); - }, - //校验该订单当前支付时的费用(排除扫描订单后不支付时长) - checkParkCost: function () { - $btnObj.style.display = "none"; - $btnLoad.style.display = "block"; - if (typeof (webAppParams.queryOrderInfo) == "undefined") {//没有queryOrderInfo返回第一页 - window.history.back(-1); - } - var tmpParams = JSON.parse(webAppParams.queryOrderInfo); - var url = webAppRoot + window.webAppH5.comServer; - postRequest(url, tmpParams, function (res) { - //btnObj.style.display = "block"; //$btnLoad.style.display = "none"; - //$btnObj.style.display = "block"; - if (res.code == 0) {//进场 - var tmpObj = res.data; - if (tmpObj) { - webAppClass.setUI(tmpObj); - webAppParams = tmpObj;//重新赋值 - webAppClass.okPay(tmpObj);//唤醒支付 - } else { - alertMsg("没有找到相应订单"); - } - } else {//其他情况如【该卡号场内已存在】 - $btnLoad.style.display = "none"; - $btnObj.style.display = "block"; - console.log(res.message); alertMsg(res.message); - } - }, function (err) { - console.log("网络服务超时..." + url); - alertMsg("网络服务超时"); - $btnLoad.style.display = "none"; - $btnObj.style.display = "block"; - //$btnLoad.style.display = "none"; - }); + //支付方式判断 + webAppClass.okPay(webAppParams); }, //确定支付 okPay: function (params) { var orderID = params.orderId;//订单号 - //var moneyID = document.getElementById("sumMoney").innerHTML = webAppParams.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; - //默认支付宝 - if (document.getElementById("aliPay").checked) { - console.log("aliPay"); - //订单号 - var obj = {}; - //this.payAjax(url, { orderId: "11111", price: 0.01 }); - 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); - } else {//微信 - var url = " https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096"; - //window.location.href = url; - var wxPay = webAppH5.wxPayServer; - var wxParams = 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); - console.log("wxPay"); + 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"); @@ -170,15 +113,17 @@ var webAppClass = { }, //浏览器 clientBrowser: function () { - if (/MicroMessenger/.test(window.navigator.userAgent)) { - console.log("微信客户端"); - this.switchShow("wxPay"); - } else if (/AlipayClient/.test(window.navigator.userAgent)) { - console.log("支付宝客户端"); - this.switchShow("aliPay"); - } else { - console.log("其他浏览器"); - this.switchShow("other"); + 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] @@ -197,7 +142,10 @@ var webAppClass = { aliObj.style.display = "none"; document.getElementById("wxPay").checked = true; break; - default://其他浏览器 + default://其他浏览器[浏览器暂时屏蔽微信] + lineObj.style.display = "none"; + wxObj.style.display = "none"; + document.getElementById("aliPay").checked = true; break; } }, @@ -232,41 +180,129 @@ var webAppClass = { }); }, + 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) { - var btnOBj = $btnObj; + $btnObj.style.display = "none"; $btnLoad.style.display = "block"; postRequest(url, params, function (res) { - btnOBj.style.display = "block"; - $btnLoad.style.display = "none"; - if (res.code == 0) {//进场 - document.write(res.data);//打开支付表单 - } else {//其他情况如【该卡号场内已存在】 - console.log(res.message); - alertMsg(res.message); + $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("网络地址出错..."); - $btnLoad.style.display = "none"; - btnOBj.style.display = "block"; + $btnObj.style.display = "block"; $btnLoad.style.display = "none"; }); }, - //设置UI + //设置UI参数[订单、金额、停车场名称及编号] setUI: function (params) { var $carNum = getObjectByID("carNum"),//车牌号 $orderNum = getObjectByID("orderNum"),//订单号 $sumMoney = getObjectByID("sumMoney"),//实际应付金额 $outtime = getObjectByID("outtime"),//实际出场时间 - $payTip = getObjectByID("payTip"),//支付提示 + //$payTip = getObjectByID("payTip"),//支付提示 $parkName = getObjectByID("parkName"),//车场名称 $inparktime = getObjectByID("inparktime"),//进场时间 - $staytime = getObjectByID("staytime"),//停车时长 - $due = getObjectByID("due"),//停车总费用 + $staytime = getObjectByID("staytime");//停车时长 - $paid = getObjectByID("paid"),//已支付金额 - $discountDesc = getObjectByID("discountDesc"),//8折优惠折扣费用 - $discountFee = getObjectByID("discountFee");//优惠金额 + //$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); @@ -275,16 +311,16 @@ var webAppClass = { $sumMoney.innerHTML = price;///params.orderFee//缴费金额 $outtime.innerHTML = params.outtime;//实际出场时间 //.appOrderTimeout - $payTip.innerHTML = "注意: " + params.appOrderTimeout;//请在支付完成10分钟内出场,如超时未出场,会继续计费。 + //$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; + //$due.innerHTML = keepTwoDecimalFull((params.due / 100));//停车总费用 + //$paid.innerHTML = params.paid; + //$discountFee.innerHTML = keepTwoDecimalFull((params.discountFee / 100));//优惠金额 + //$discountDesc.innerHTML = params.discountDesc; } else { console.log("没有接受到参数"); } @@ -296,7 +332,7 @@ var webAppClass = { var $linkPayDetail = getObjectByID("linkPayDetail"); if ($linkPayDetail.innerHTML == "详情+") { $linkPayDetail.innerHTML = "详情-"; - $payDetail.style.height = "195px"; + $payDetail.style.height = "110px"; } else { $linkPayDetail.innerHTML = "详情+"; $payDetail.style.height = "55px"; diff --git a/js/pay.js b/js/pay.js index 7442fc4..3d27928 100644 --- a/js/pay.js +++ b/js/pay.js @@ -6,6 +6,7 @@ var webAppParams = null; var $btnObj = null; var $btnLoad = null; +var webAppCode = null;//如果是微信,先获取微信code //var $btnLoad = document.getElementById("loading"); window.onload = function () { //var root = document.getElementById("root"); @@ -14,7 +15,7 @@ window.onload = function () { $linkPayDetail = getObjectByID("linkPayDetail"); $linkPayDetail.onclick = webAppClass.linkPayDetailClick; var webRoot = window.location.href; - //当前客户端浏览器模式[微信、支付宝、其他] + //设置当前客户端浏览器模式[微信、支付宝、其他] webAppClass.clientBrowser(); //绑定事件 var ali = document.getElementById("rowAliPay"); @@ -31,6 +32,13 @@ window.onload = function () { } else { alertMsg("暂无订单信息"); } + + //获取微信code + //webAppClass.getCode(); + var state = clientBrowserEx(); + if (state == "wxPay") { + webAppCode = webAppClass.getCode(); + } } /**/ var webAppClass = { @@ -83,32 +91,86 @@ var webAppClass = { var orderID = params.orderId;//订单号 //var moneyID = document.getElementById("sumMoney").innerHTML = webAppParams.orderId;//订单号 var root = webAppRoot; - //默认支付宝 - if (document.getElementById("aliPay").checked) { - console.log("aliPay"); - //订单号 - var obj = {}; - //this.payAjax(url, { orderId: "11111", price: 0.01 }); - 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); - } else {//微信 - var url = " https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096"; - //window.location.href = url; - var wxPay = webAppH5.wxPayServer; - var wxParams = 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); - console.log("wxPay"); + //第一首先判断当前哪种浏览器方式[微信内置、支付宝内置、第三方浏览器] + var clientType = clientBrowserEx(); + //alert(clientType); + switch (clientType) { + case "wxPay"://微信[内置浏览器] + //var wxPay = webAppH5.wxPayServer; + //var wxParams = webAppH5.comParams; + //wxParams.orderId = orderID; + //wxParams.payType = 4;// 1:支付宝 2:微信 3:银联 10:H5 4微信公众号 + //wxParams.terminalSource = 3;//请求端来源 1: 任你停 2:pda 3:微信公共号 7:H5扫码 10页面支付 + webAppClass.payAjaxJDK(orderID); + console.log("wxPay");//微信支付 + break; + case "aliPay"://支付宝 + console.log("aliPay"); + //订单号 + var obj = {}; + //this.payAjax(url, { orderId: "11111", price: 0.01 }); + 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); + break; + default://第三方浏览器other + //默认支付宝 + if (document.getElementById("aliPay").checked) { + console.log("aliPay"); + //订单号 + var obj = {}; + //this.payAjax(url, { orderId: "11111", price: 0.01 }); + 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); + } else {//微信页面支付 + var url = " https://wx.tenpay.com/cgi-bin/mmpayweb-bin/checkmweb?prepay_id=wx20161110163838f231619da20804912345&package=1037687096"; + //window.location.href = url; + var wxPay = webAppH5.wxPayServer; + var wxParams = 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); + console.log("wxPay"); + } + break; } }, + 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' + //window.event.returnValue = false; + //window.open('https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appID + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'); + //alert("22"); + } else { + //getOpenId(code); //把code传给后台获取用户信息 + //alert(code); + //webAppCode = code; + 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"); @@ -150,12 +212,15 @@ var webAppClass = { aliObj.style.display = "none"; document.getElementById("wxPay").checked = true; break; - default://其他浏览器 + default://其他浏览器[浏览器暂时屏蔽微信] + lineObj.style.display = "none"; + wxObj.style.display = "none"; + document.getElementById("aliPay").checked = true; break; } }, - //异步请求-微信 + //异步请求-微信H5页面 payAjax: function (url, params) { var btnOBj = $btnObj; postRequest(url, params, function (res) { @@ -182,9 +247,112 @@ var webAppClass = { }, function (err) { console.log("网络地址出错..."); alertMsg("网络地址出错..."); + $btnLoad.style.display = "none"; btnOBj.style.display = "block"; }); }, + //异步请求-微信JDK调用 + payAjaxJDK: function (orderID) { + var btnOBj = $btnObj; + //0:页面初始化获取code[webAppCode] + var codeParams = { code: webAppCode, appId: appWxID }; + var openIdUrl = webAppRoot + window.webAppH5.wxGetOpenIdServer; + //alert(openIdUrl); + //1:有code获取OpenId + postRequest(openIdUrl, codeParams, function (res) { + //alert(JSON.stringify(res)); + if (res.code == 0) { + //alert(res.data); + webAppClass.getPayParams(res.data, orderID); + } else { + alertMsg(res.message); + } + }, function (err) { + console.log("网络地址出错..." + openIdUrl); + }); + //2:获取支付参数 + //function getPayParams(openId) { + + //} + }, + 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); + 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); + console.log(JSON.stringify(tmpObj)); + //alert(JSON.stringify(tmpObj)); + } else { + alertMsg("没有找到返回值"); + } + } else { + $btnLoad.style.display = "none"; + $btnObj.style.display = "block"; + console.log(res.message); alertMsg(res.message); + } + }, function (err) { + console.log("网络地址出错..."); + alertMsg("网络地址出错..." + payParamsUrl); + $btnLoad.style.display = "none"; + $btnObj.style.display = "block"; + }); + }, + + //调用微信内置WeixinJSBridge对象发起支付 + weixinJSBridge: function (params) { + function onBridgeReady() { + WeixinJSBridge.invoke( + 'getBrandWCPayRequest', params, + //'getBrandWCPayRequest', { + // "appId": appWxID, //公众号名称,由商户传入 + // "timeStamp": "1395712654", //时间戳,自1970年以来的秒数 + // "nonceStr": "e61463f8efa94090b1f366cccfbbb444", //随机串 + // "package": "prepay_id=u802345jgfjsdfgsdg888", + // "signType": "MD5", //微信签名方式: + // "paySign": "70EA570631E4BB79628FBCA90534C63FF7FADD89" //微信签名 + //}, + 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) { diff --git a/page/alipayResult.html b/page/alipayResult.html index c8b2363..9af4f8c 100644 --- a/page/alipayResult.html +++ b/page/alipayResult.html @@ -51,6 +51,8 @@ console.log(JSON.stringify(tmpParams)); if (tmpParams.trade_no != "") { getResponse("success"); + } else { + getResponse("fail"); } //alertMsg("支付返回参数:" + JSON.stringify(tmpParams)); } else { diff --git a/page/wxpayResult.html b/page/wxpayResult.html index c8b2363..90201f3 100644 --- a/page/wxpayResult.html +++ b/page/wxpayResult.html @@ -49,8 +49,10 @@ var tmpParams = getQueryString(window.location); if (tmpParams != null) { console.log(JSON.stringify(tmpParams)); - if (tmpParams.trade_no != "") { + if (tmpParams.trade_no == "success") { getResponse("success"); + } else { + getResponse("fail"); } //alertMsg("支付返回参数:" + JSON.stringify(tmpParams)); } else { diff --git a/pay.html b/pay.html index 92f3215..3964641 100644 --- a/pay.html +++ b/pay.html @@ -16,7 +16,8 @@ - + +