Blame view

js/dynCodePay.js 18 KB
903c4448   songchongxian   微信支付
1
2
  /*
  @songcxa
a7b83e0d   songchongxian   动态二维码
3
  @2018-12-26
903c4448   songchongxian   微信支付
4
  @小票动态扫码移动支付
a7b83e0d   songchongxian   动态二维码
5
  @动态二维码url包括订单、金额、进出场时间、停车场编号、车牌号等
903c4448   songchongxian   微信支付
6
  */
a7b83e0d   songchongxian   动态二维码
7
  var webAppParams = null;//URL参数
903c4448   songchongxian   微信支付
8
9
  var $btnObj = null;
  var $btnLoad = null;
a7b83e0d   songchongxian   动态二维码
10
  var webAppCode = null;//如果是微信,先获取微信code
72b1b692   Andy   add 兑换券
11
  var flag = 0;
903c4448   songchongxian   微信支付
12
  window.onload = function () {
a7b83e0d   songchongxian   动态二维码
13
14
      $btnObj = getObjectByID("btnPayOK");
      $btnLoad = getObjectByID("loading");
72b1b692   Andy   add 兑换券
15
16
      //兑换按钮
      $discountBtn = document.getElementById("discountBtn");
903c4448   songchongxian   微信支付
17
18
19
20
21
22
      $linkPayDetail = getObjectByID("linkPayDetail");
      $linkPayDetail.onclick = webAppClass.linkPayDetailClick;
      var webRoot = window.location.href;
      //当前客户端浏览器模式[微信、支付宝、其他]
      webAppClass.clientBrowser();
      //绑定事件
a7b83e0d   songchongxian   动态二维码
23
24
      var ali = getObjectByID("rowAliPay");
      var wx = getObjectByID("rowWxPay");
903c4448   songchongxian   微信支付
25
26
27
      ali.onclick = webAppClass.aliClick;
      wx.onclick = webAppClass.wxClick;
      $btnObj.onclick = webAppClass.okClick;
a7b83e0d   songchongxian   动态二维码
28
      //document.getElementById("payTip").innerHTML = "";
903c4448   songchongxian   微信支付
29
30
31
      //参数
      var tmpURLparams = getQueryString(window.location);
      if (tmpURLparams != null) {
a7b83e0d   songchongxian   动态二维码
32
33
34
          webAppParams = tmpURLparams;
          webAppClass.setUI(webAppParams);
          //webAppClass.init(tmpURLparams);  //设置UI参数
903c4448   songchongxian   微信支付
35
36
37
      } else {
          alertMsg("没有接收到url参数信息");
      }
a7b83e0d   songchongxian   动态二维码
38
39
40
41
42
43
44
  
      //获取微信code
      //webAppClass.getCode();
      var state = clientBrowserEx();
      if (state == "wxPay") {
          webAppCode = webAppClass.getCode();
      }
72b1b692   Andy   add 兑换券
45
46
      //兑换按钮事件
      $discountBtn.onclick = webAppClass.discountClick;
903c4448   songchongxian   微信支付
47
48
49
  }
  /**/
  var webAppClass = {
72b1b692   Andy   add 兑换券
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
      //优惠券兑换
      codediscountClick:function(tmpObj){
          discountVal = document.getElementById("discountBox").value;
          // console.log(discountVal)
          // console.log(discountVal.length)
          if(discountVal.length > 0){
              $btnObj.style.display = "none";
              $btnLoad.style.display = "block";
              $discountBtn.disabled = true
              console.log(webAppParams.orderId)
              var url = webAppRoot + window.webAppH5.discountServer;
              var discountParams = {
                  app_id:'1',
                  salt:'1',
                  deviceInfo:'1',
                  sign_type:'1',
                  sign:'1',
                  token:'1',
                  terminalSource:7,
                  orderId:webAppParams.orderId,
                  cardNo:discountVal,
                  codeType:1
              }
              postRequest(url, discountParams, function (res) {
                  console.log(res)
                  if(res.code==0){
  
                      document.getElementById("sumMoney").innerHTML = (res.data.orderFee/100)
                      document.getElementById("discountTip").innerHTML = res.data.couponDescribe
                      document.getElementById("discountFee").innerHTML = (res.data.discountFee/100)
                      document.getElementById("discountDesc").style.display="none"
                      $btnLoad.style.display = "none";
                      $btnObj.style.display = "block";
                      flag = 1
                      webAppClass.okPay(tmpObj);//唤醒支付
                  }else{
                      $discountBtn.disabled = false
                      document.getElementById("discountTip").innerHTML = res.message
                  }
              })
  
          }else{
              return
          }
      },
      discountClick: function(){
          discountVal = document.getElementById("discountBox").value;
          // console.log(discountVal)
          // console.log(discountVal.length)
          if(discountVal.length > 0){
              $btnObj.style.display = "none";
              $btnLoad.style.display = "block";
              $discountBtn.disabled = true
              console.log(webAppParams.orderId)
              var url = webAppRoot + window.webAppH5.discountServer;
              var discountParams = {
                  app_id:'1',
                  salt:'1',
                  deviceInfo:'1',
                  sign_type:'1',
                  sign:'1',
                  token:'1',
                  terminalSource:7,
                  orderId:webAppParams.orderId,
                  cardNo:discountVal,
                  codeType:1
              }
              postRequest(url, discountParams, function (res) {
                  console.log(res)
                  if(res.code==0){
  
                      document.getElementById("sumMoney").innerHTML = (res.data.orderFee/100)
                      document.getElementById("discountTip").innerHTML = res.data.couponDescribe
                      document.getElementById("discountFee").innerHTML = (res.data.discountFee/100)
                      document.getElementById("discountDesc").style.display="none"
                      $btnLoad.style.display = "none";
                      $btnObj.style.display = "block";
                      document.getElementById("discountBox").setAttribute('readonly','readonly');
                      flag = 1
                  }else{
                      $discountBtn.disabled = false
                      document.getElementById("discountTip").innerHTML = res.message
                  }
              })
  
          }else{
              return
          }
      },
903c4448   songchongxian   微信支付
139
140
      //OK
      okClick: function (ev) {
a7b83e0d   songchongxian   动态二维码
141
          //document.getElementById("payTip").innerHTML = "";
903c4448   songchongxian   微信支付
142
143
144
          if (webAppParams == null) {
              alertMsg("暂无订单信息");return;
          }
a7b83e0d   songchongxian   动态二维码
145
146
          //支付方式判断
          webAppClass.okPay(webAppParams);
903c4448   songchongxian   微信支付
147
148
149
150
      },
  
      //确定支付
      okPay: function (params) {
72b1b692   Andy   add 兑换券
151
152
153
          if(flag == 1){
              webAppClass.codediscountClick(tmpObj);
          }
903c4448   songchongxian   微信支付
154
          var orderID = params.orderId;//订单号
a7b83e0d   songchongxian   动态二维码
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
          //第一首先判断当前哪种浏览器方式[微信内置、支付宝内置、第三方浏览器]
          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) {
903c4448   songchongxian   微信支付
171
          var root = webAppRoot;
a7b83e0d   songchongxian   动态二维码
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
          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);
f6c06807   songchongxian   。。。。。。。。。
188
              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'
a7b83e0d   songchongxian   动态二维码
189
190
191
192
193
194
195
196
          } 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
903c4448   songchongxian   微信支付
197
198
          }
      },
903c4448   songchongxian   微信支付
199
200
201
202
203
204
205
206
207
208
209
210
211
212
      //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 () {
a7b83e0d   songchongxian   动态二维码
213
214
215
216
217
218
219
220
221
222
223
          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;
903c4448   songchongxian   微信支付
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
          }
      },
      //根据浏览器显示内容[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;
a7b83e0d   songchongxian   动态二维码
242
243
244
245
              default://其他浏览器[浏览器暂时屏蔽微信]
                  lineObj.style.display = "none";
                  wxObj.style.display = "none";
                  document.getElementById("aliPay").checked = true;
903c4448   songchongxian   微信支付
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
                  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";
          });
      },
  
a7b83e0d   songchongxian   动态二维码
280
281
282
283
      payAjaxJDK: function (orderID) {
          //0:页面初始化获取code[webAppCode]
          var codeParams = { code: webAppCode, appId: appWxID };
          var openIdUrl = webAppRoot + window.webAppH5.wxGetOpenIdServer;
544f25d2   songchongxian   扫码支付。。。
284
285
286
287
288
          //debugger;
          if (webAppCode == null || webAppCode == "") {
              alertMsg(appState.codeNullTip);
              return;
          }
a7b83e0d   songchongxian   动态二维码
289
290
291
292
293
294
295
          $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);
544f25d2   songchongxian   扫码支付。。。
296
297
298
299
300
301
              }
              else if (res.code == 40163) {//code been used, hints
                  alertMsg(appState.codeNullTip);
                  console.log(res.message);
              }
              else {
a7b83e0d   songchongxian   动态二维码
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
                  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();
          }
      },
903c4448   songchongxian   微信支付
376
377
      //异步请求-支付宝
      payAliAjax: function (url, params) {
a7b83e0d   songchongxian   动态二维码
378
          $btnObj.style.display = "none"; $btnLoad.style.display = "block";
903c4448   songchongxian   微信支付
379
          postRequest(url, params, function (res) {
a7b83e0d   songchongxian   动态二维码
380
381
382
383
384
385
386
387
388
389
              $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("没有找到次订单信息");
903c4448   songchongxian   微信支付
390
391
392
              }
          }, function (err) {
              console.log("网络地址出错...");alertMsg("网络地址出错...");
a7b83e0d   songchongxian   动态二维码
393
              $btnObj.style.display = "block"; $btnLoad.style.display = "none";
903c4448   songchongxian   微信支付
394
395
396
          });
      },
  
a7b83e0d   songchongxian   动态二维码
397
      //设置UI参数[订单、金额、停车场名称及编号]
903c4448   songchongxian   微信支付
398
399
400
401
402
      setUI: function (params) {
          var $carNum = getObjectByID("carNum"),//车牌号
              $orderNum = getObjectByID("orderNum"),//订单号
              $sumMoney = getObjectByID("sumMoney"),//实际应付金额
              $outtime = getObjectByID("outtime"),//实际出场时间
a7b83e0d   songchongxian   动态二维码
403
              //$payTip = getObjectByID("payTip"),//支付提示
903c4448   songchongxian   微信支付
404
405
406
  
              $parkName = getObjectByID("parkName"),//车场名称
              $inparktime = getObjectByID("inparktime"),//进场时间
a7b83e0d   songchongxian   动态二维码
407
              $staytime = getObjectByID("staytime");//停车时长
903c4448   songchongxian   微信支付
408
  
a7b83e0d   songchongxian   动态二维码
409
410
411
412
              //$due = getObjectByID("due"),//停车总费用
              //$paid = getObjectByID("paid"),//已支付金额
              //$discountDesc = getObjectByID("discountDesc"),//8折优惠折扣费用
              //$discountFee = getObjectByID("discountFee");//优惠金额
903c4448   songchongxian   微信支付
413
414
415
416
417
418
419
420
  
          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
a7b83e0d   songchongxian   动态二维码
421
              //$payTip.innerHTML = "注意: " + params.appOrderTimeout;//请在支付完成10分钟内出场,如超时未出场,会继续计费。
903c4448   songchongxian   微信支付
422
423
424
425
426
  
              $parkName.innerHTML = params.parkName;
              $inparktime.innerHTML = params.inparktime;
              $staytime.innerHTML = formatSeconds(params.staytime);
              
a7b83e0d   songchongxian   动态二维码
427
428
429
430
              //$due.innerHTML = keepTwoDecimalFull((params.due / 100));//停车总费用
              //$paid.innerHTML = params.paid;
              //$discountFee.innerHTML = keepTwoDecimalFull((params.discountFee / 100));//优惠金额
              //$discountDesc.innerHTML = params.discountDesc;
903c4448   songchongxian   微信支付
431
432
433
434
435
436
437
438
439
440
441
          } else {
              console.log("没有接受到参数");
          }
      },
  
      //查看详情
      linkPayDetailClick: function (ev) {
          var $payDetail = getObjectByID("payDetail");
          var $linkPayDetail = getObjectByID("linkPayDetail");
          if ($linkPayDetail.innerHTML == "详情+") {
              $linkPayDetail.innerHTML = "详情-";
a7b83e0d   songchongxian   动态二维码
442
              $payDetail.style.height = "110px";
903c4448   songchongxian   微信支付
443
444
445
446
447
448
          } else {
              $linkPayDetail.innerHTML = "详情+";
              $payDetail.style.height = "55px";
          }
      }
  }