微信native支付对接 支付宝当面付对接

接上两篇,微信及支付宝支付请求后需要将二维码url展示并轮询订单查询接口,所以本篇就用jsp简单实现二维码的展示并轮询

二维码展示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//导入qrcodejs
<script type="text/javascript" src="javascript/qrcode.min.js?r=<%=Math.random() %>"></script>
//创建二维码div
<div id="qrCodeImg" style="position:absolute;left:731px;top:248px;width:209px;height:208px;z-index:4;visibility:hidden"></div>
//使用qrcode对象展示二维码
var qrCode ;//二维码对象
function makeCode (id,qrUrl) {
if (qrCode){
qrCode.makeCode(qrUrl);
}else{
qrCode = new QRCode(document.getElementById(id), {
width : 207,
height : 206
});
}
qrCode.makeCode(qrUrl);
}
//将二维码清除
qrCode.clear();//置空
js异步轮询查询接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
var xhr = '';
//ajax异步访问后端
function post(data, url, type) {
//请求参数、url、创建XMLHttpRequest
var data = data;
var postUrl = url;
var type = type;//类型: 1,订购下单 2,下单查询
if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
// IE浏览器
else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
xhr.open('post', postUrl);
//设置header
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(data);
xhr.onreadystatechange = function () {
var jsonText = xhr.responseText;
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304 || xhr.status === 500)) {
if ('' !== jsonText) {
if ('1' === type) {//支付下单
orderJsonParse(jsonText)
} else if ('2' === type) {//下单查询
var jsonP = eval("(" + jsonText + ")");
var result = jsonP.result;
var isKeep = jsonP.isKeep;//继续
if ('1' == isKeep){
if (result == 0) {//
clearInterval(inquire);
hiddenQR();
window.android.payFinish('1');
}
}else {
clearInterval(inquire);
if (result == 0) {//
hiddenQR();
window.android.payFinish('1');
} else {//显示主见面
hiddenQR();
//显示主界面
nowI = 0; //interface 主界面:0 其他
nowF = lastF;
$(nowF).style.visibility = "hidden";
$('f_' + nowF).style.visibility = "visible";
window.android.toast("订购失败");
window.android.payFinish('-1');
}
}
}
}
}
}
}

//解析下单返回的json
function orderJsonParse(orderjson) {
$("wait").style.visibility = "hidden";
var data = JSON.parse(orderjson);
if (data.result === '0') {
nowI = 2;
var qrCodeImg = data.qrCodeImg;
out_trade_no = data.outTrade;
nowF = 'tip3';
$(nowF).style.visibility = "visible";
//重新生成图片
$(nowF + '_1').src = "http://111.19.132.254:8088/joymusic_cs_gwb/imgerU?pic=http://111.19.132.254:8088/joymusic_cs_gwb/images/common/order/gwb/qr_bj_"+payType+".png&type=watermark&z=" + productName + "," + priceStr + "&s=22&x=435&y=250&x2=400&y2=320&rand=<%=Math.random()%>";
$(nowF + '_1').style.visibility = "visible";
//qrCode.clear();//置空
makeCode('qrCodeImg',qrCodeImg);
$('qrCodeImg').style.visibility = "visible";//将二维码div生成显示
$('ico').style.visibility = "visible";
//将num置零
num = 0;
//定时器 定时查询
setTimeout(checkOrder(),3000);
} else {
//下单失败
window.location = 'pay.jsp?k=0&userid=' + userId + '&platform=' + platform + '&ip=' + ip + '&terminalId=' + terminalId + '&token=' + token + '&songId=' + songId;
}
}
支付jsp完整代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.joymusic.servlet.utils.wxPay.PayCommonUtil" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="com.joymusic.api.DB" %>
<%!
org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger("mydebug");

//记录订购触发
private int setPay(String uid, int platform, String ip, String songId) {
int row = 0;
String sql = "INSERT INTO `user_pay_all` (ip,uid,ctype,result,songid,area,platform,createtime) VALUES ('" + ip + "','" + uid + "','apply','0','" + songId + "','86','" + platform + "',NOW())";
row = DB.update(sql);
return row;
}

//记录发起订购
private int setInitPay(String ip, String uid, int amount, String purchase, String area, String songId, int platform, String myOrderId,String payType) {
int row = 0;
String sname = "";
String sql0 = "SELECT CONCAT(cname,'-',artist) AS songInfo FROM entity_song WHERE id= " + songId;
List<Map<String, Object>> li = DB.query(sql0, false);
if (li.size() > 0) sname = li.get(0).get("songInfo").toString();
String sql = "INSERT INTO user_pay_info(ip, uid, fee, createtime, purchase, area, sid, sname, platform, orderStat,orderId,payType) VALUE('" + ip + "', '" + uid + "', '" + amount + "', NOW(), '"
+ purchase + "', '" + area + "', '" + songId + "', '" + sname + "', '" + platform + "',0, '" + myOrderId + "', '" + payType + "')";
row = DB.update(sql);

return row;
}

//更新订购记录
private int updatePay(String uid) {
int row = 0;
String sql1 = "SELECT COUNT(1) FROM `user_pay_all` WHERE uid like '" + uid + "'";
int query = DB.queryCount(sql1, false);
if (query > 0) {
String sql2 = "UPDATE `user_pay_all` SET result='1' WHERE uid like '" + uid + "' ORDER BY createtime DESC LIMIT 1";
row = DB.update(sql2);
}
return row;
}
//更新订购发起记录
private int updateInfoPay(String uid,String myOrderId, String orderStat) {
int row = 0;
String sql2 = "UPDATE `user_pay_info` SET orderStat='"+orderStat+"' WHERE uid = '" + uid + "' AND orderId ='"+myOrderId+"'";
row = DB.update(sql2);
return row;
}

/**
* 检查是否鉴权
* @param uid
* @param platform
* @return
*/
private String checkAuth(String uid, int platform) {
String result = "1";
//先查询表中该用户是否订购
String sql = "SELECT * FROM `user_pay_val` WHERE uid = '" + uid + "'AND platform ='" + platform + "'AND NOW() > paytime AND NOW() < exptime";
List<Map<String, Object>> list = DB.query(sql, false);
int size = list.size();
//订购表里是否有记录
if (size > 0) {
String stat = list.get(0).get("stat").toString();//判断是否支付正常
if ("1".equals(stat)) result = "0";
}
return result;
}

//订购详情表
private String setPayResult(String ip, String uid, String area, String sid, int platform, String amount, String validTime,
String expireTime, String autoSub, String orderId, String payType, String myorderId, String openid, int stype) {
String result = "1";
int row = 0;
try {
String sname = "";
String sqlS = "SELECT CONCAT(cname,'-',artist) AS songInfo FROM entity_song WHERE id= " + sid;
List<Map<String, Object>> li = DB.query(sqlS, false);
if (li.size() > 0) sname = li.get(0).get("songInfo").toString();
String sql = "";
String sql0 = "SELECT COUNT(1) FROM user_pay_val WHERE uid='" + uid + "' AND platform=" + platform;
int totalRows = DB.queryCount(sql0, false);
if (totalRows > 0) {//表中有记录
if (stype == 0) { // 退订后又续包月
sql = "UPDATE user_pay_val SET paytime='" + validTime + "', sid='" + sid + "', sname='" + sname + "', stat=1, fee='"
+ amount + "', myorderId='" + myorderId + "', exptime='" + expireTime +
"', autoSub='" + autoSub + "', orderId='" + orderId + "', payType='" + payType + "', openid='" + openid + "' WHERE uid='" + uid + "' AND platform=" + platform;
}
row = DB.update(sql);
if (row > 0) result = "0";
} else {//没订购
if (stype == 0) { // 订购续包月
sql = "INSERT INTO user_pay_val(ip, uid, fee, paytime, exptime, createtime, myorderId, area, sid, sname, platform, stat,orderId,autoSub,payType,openid) VALUE('" + ip + "', '" + uid + "', '" + amount + "', '" + validTime + "', '"
+ expireTime + "', NOW(), '" + myorderId + "', '" + area + "', '" + sid + "', '" + sname + "', '" + platform + "',1, '" + orderId + "', '" + autoSub + "', '" + payType + "', '" + openid + "')";
}
row = DB.update(sql);
if (row > 0) result = "0";
}
} catch (Exception e) {
result = "-1";
}
return result;
}

/**
* 微信计算失效时间
* @param inputDate
* @param number
* @return
*/
public static String getExpireTime(String inputDate,int number) {
Calendar c = Calendar.getInstance();//获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
Date date = null;
try{
date = sdf.parse(inputDate);//初始日期
}catch(Exception e){

}
c.setTime(date);//设置日历时间
c.add(Calendar.MONTH,number);//在日历的月份上增加?个月
return sdf.format(c.getTime());
}

/**
* 支付宝计算失效时间
* @param inputDate
* @param number
* @return
*/
public static String getAliExpireTime(String inputDate,int number) {
Calendar c = Calendar.getInstance();//获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try{
date = sdf.parse(inputDate);//初始日期
}catch(Exception e){

}
c.setTime(date);//设置日历时间
c.add(Calendar.MONTH,number);//在日历的月份上增加?个月
return sdf.format(c.getTime());
}

//分转换为元
public static String cent2yuan(int orderPrice){
Double num1 = Double.valueOf(orderPrice);
Double ratio = num1 / 100;
return String.format("%.2f", ratio);
}


%>
<%
//设置字符集及编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//设置允许跨域请求
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET,POST");
response.addHeader("Access-Control-Allow-Headers", "x-requested-with,content-type");
//获取参数
String k = request.getParameter("k");//跳转标识 null:支付回调, k=1:订购触发; k=2 发起订购; k=3 异步查询接口
String userId = request.getParameter("userid");
String ip = request.getParameter("ip") == null ? "192.168.0.1" : request.getParameter("ip");
int platform = request.getParameter("platform") == null ? 1 : Integer.parseInt(request.getParameter("platform"));
String songId = request.getParameter("songId") == null ? "10000" : request.getParameter("songId");
String terminalId = request.getParameter("terminalId");//终端标识
String token = request.getParameter("token");//token标识
int monthPrice = 1;//包月
int quarterPrice = 4900;//包季节
int halfYearPrice = 9800;//包半年
int yearPrice = 18900;//包年

String WXQRcode = "weixin://wxpay/bizpayurl?pr=bOBVTSL";//微信支付二维码
String orderResult = "1";//订购结果 0 成功 1 失败
String titleName = "订购页";
String out_trade_no = "";//商户订单号
if (StringUtils.isBlank(k)){//支付回调通知
HashMap<String, String> resultMap = null;
try {
resultMap = PayCommonUtil.wxPayNotify(request, response);
} catch (Exception e) {
e.printStackTrace();
}
String result = resultMap.get("result");
if ("SUCCESS".equals(result));{//记录数据库
userId = resultMap.get("userId");
//时间
String validTime = resultMap.get("orderTime");//用户产品包生效时间
String orderFee = resultMap.get("orderFee");
String wxOderNumber = resultMap.get("wxOderNumber");
String myOrderNumber = resultMap.get("myOrderNumber");
String openid = resultMap.get("openid");
String expireTime = "";
int orderPrice = Integer.valueOf(orderFee);
if (monthPrice == orderPrice) expireTime = getExpireTime(validTime,1);
else if (quarterPrice == orderPrice) expireTime = getExpireTime(validTime,3);
else if (halfYearPrice == orderPrice) expireTime = getExpireTime(validTime,6);
else if (yearPrice == orderPrice) expireTime = getExpireTime(validTime,12);
updateInfoPay(userId,myOrderNumber,"1");
updatePay(userId);
String res = setPayResult(ip, userId, "86", songId, platform, orderFee, validTime, expireTime, "0", wxOderNumber, "2", myOrderNumber, openid, 0);
JSONObject jsonStr = new JSONObject();
jsonStr.put("result", res);
//写入流返回
out.print(jsonStr);
out.flush();
out.close();
}
return;
}else if ("0".equals(k)){
//记录发起订购数1
setPay(userId, platform, ip, songId);
}else if ("1".equals(k)){//发起订购
String payType = request.getParameter("payType");//微信:2 支付宝:1
String productId = request.getParameter("productId");//1 单包月;2 包季; 3 包半年; 4 包年
String body = "欢乐歌房-包月"; //商品名称 默认单包月
int orderPrice = monthPrice;//支付金额 默认单包月
if ("1".equals(productId)){
body = "欢乐歌房-包月";
orderPrice = monthPrice;
}else if ("2".equals(productId)){
body = "欢乐歌房-包季";
orderPrice = quarterPrice;
}else if ("3".equals(productId)){
body = "欢乐歌房-包半年";
orderPrice = halfYearPrice;
}else if ("4".equals(productId)){
body = "欢乐歌房-包年";
orderPrice = yearPrice;
}
out_trade_no = userId.substring(0,5)+"_"+ PayCommonUtil.getCurrTime() + PayCommonUtil.buildRandom(4); // 商户订单号
int res = setInitPay(ip, userId,orderPrice,body,"86",songId,platform,out_trade_no,payType);
JSONObject jsonStr = new JSONObject();
if ("2".equals(payType)) {
WXQRcode = PayCommonUtil.wxPay(ip, body, orderPrice, userId, out_trade_no);
}else if ("1".equals(payType)){
String total_amount = cent2yuan(orderPrice);
WXQRcode = com.joymusic.servlet.utils.aliPay.PayCommonUtil.aliPay(body, total_amount, userId, out_trade_no);
//logger.info("Alijsp:"+WXQRcode);
}

if (StringUtils.isNotBlank(WXQRcode)) {
jsonStr.put("result", "0");
jsonStr.put("qrCodeImg", WXQRcode);
jsonStr.put("outTrade", out_trade_no);
} else {
jsonStr.put("result", "-1");
}
//写入流返回
out.print(jsonStr);
out.flush();
out.close();
return;
}else if("2".equals(k)){//异步鉴权
String isLast = request.getParameter("isLast");//最后一次
out_trade_no = request.getParameter("outTrade");
String isKeep = "0";//继续查询 1 ,false: 0
Map orderSelectMap = null;
String payType = request.getParameter("payType");
if ("2".equals(payType)){
orderSelectMap = PayCommonUtil.selectOrder("out_trade_no", out_trade_no);
String return_code = orderSelectMap.get("return_code").toString();
String result_code = orderSelectMap.get("result_code").toString();
String trade_state = orderSelectMap.get("trade_state").toString();
if ("SUCCESS".equals(return_code) && "SUCCESS".equals(result_code)){
String myOrderNumber = orderSelectMap.get("out_trade_no").toString();
if ("SUCCESS".equals(trade_state)){//记录数据库
//时间
String validTime = orderSelectMap.get("time_end").toString();//用户产品包生效时间
String orderFee = orderSelectMap.get("cash_fee").toString();
String wxOderNumber = orderSelectMap.get("transaction_id").toString();
String openid = orderSelectMap.get("openid").toString();
String expireTime = "";
int orderPrice = Integer.valueOf(orderFee);
if (monthPrice == orderPrice) expireTime = getExpireTime(validTime,1);
else if (quarterPrice == orderPrice) expireTime = getExpireTime(validTime,3);
else if (halfYearPrice == orderPrice) expireTime = getExpireTime(validTime,6);
else if (yearPrice == orderPrice) expireTime = getExpireTime(validTime,12);
updateInfoPay(userId,myOrderNumber,"1");
updatePay(userId);
setPayResult(ip, userId, "86", songId, platform, orderFee, validTime, expireTime, "0", wxOderNumber, payType, myOrderNumber, openid, 0);
orderResult = "0";
}else if("USERPAYING".equals(trade_state) ||"NOTPAY".equals(trade_state)){//当返回未支付或者 正在支付中时
if ("1".equals(isLast)) {
updateInfoPay(userId,myOrderNumber,"3");
}else {
isKeep = "1";
}
}else if("REFUND".equals(trade_state)){
updateInfoPay(userId,myOrderNumber,"2");
}else if("CLOSED".equals(trade_state)){
updateInfoPay(userId,myOrderNumber,"4");
}else if("REVOKED".equals(trade_state)){
updateInfoPay(userId,myOrderNumber,"5");
}else if("PAYERROR".equals(trade_state)){
updateInfoPay(userId,myOrderNumber,"6");
}
}

}else{
//订单状态 :0: 用户支付中 1: 支付成功 3:未支付 4:已关闭
orderSelectMap = com.joymusic.servlet.utils.aliPay.PayCommonUtil.selectOrder(out_trade_no);
String aliReturnCode = orderSelectMap.get("returnCode").toString();
String trade_state = orderSelectMap.get("trade_status").toString();
if("1".equals(aliReturnCode)){
String myOrderNumber = orderSelectMap.get("out_trade_no").toString();
if ("TRADE_SUCCESS".equals(trade_state)){//记录数据库
//时间
String validTime = orderSelectMap.get("send_pay_date").toString();//用户产品包生效时间
String orderFee = orderSelectMap.get("amount").toString();
String aliOderNumber = orderSelectMap.get("trade_no").toString();
String openid = orderSelectMap.get("buyer_user_id").toString();
String expireTime = "";
int orderPrice =(int)(Double.valueOf(orderFee)*100);
if (monthPrice == orderPrice) expireTime = getAliExpireTime(validTime,1);
else if (quarterPrice == orderPrice) expireTime = getAliExpireTime(validTime,3);
else if (halfYearPrice == orderPrice) expireTime = getAliExpireTime(validTime,6);
else if (yearPrice == orderPrice) expireTime = getAliExpireTime(validTime,12);
updateInfoPay(userId,myOrderNumber,"1");
updatePay(userId);
setPayResult(ip, userId, "86", songId, platform, orderPrice+"", validTime, expireTime, "0", aliOderNumber, payType, myOrderNumber, openid, 0);
orderResult = "0";
}else if("WAIT_BUYER_PAY".equals(trade_state)){//正在支付中时
if ("1".equals(isLast)) {
updateInfoPay(userId,myOrderNumber,"3");
}else {
isKeep = "1";
}
}else if("TRADE_CLOSED".equals(trade_state)){//订单关闭
updateInfoPay(userId,myOrderNumber,"3");
}else if("TRADE_FINISHED".equals(trade_state)){
updateInfoPay(userId,myOrderNumber,"4");
}
}
}

JSONObject jsonStr = new JSONObject();
jsonStr.put("isKeep", isKeep);
jsonStr.put("result", orderResult);
//写入流返回
out.print(jsonStr);
out.flush();
out.close();
return;
}else if ("3".equals(k)){//关闭支付
String myOrderNumber = request.getParameter("outTrade");
updateInfoPay(userId,myOrderNumber,"3");
return;
}
%>
<!DOCTYPE html>
<html>
<head>
<title><%=titleName%>
</title>
<meta http-equiv='pragma' content='no-cache'>

<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'/>

<meta http-equiv='cache-control' content='no-store,no-cache,must-revalidate'>

<meta http-equiv='expires' content='0'>
<!--1.在ajax请求的那个服务器上输出的页面上做header的一个标签-->
<meta http-equiv="Access-Control-Allow-Origin" content="*">
<meta name='page-view-size' content='1280*720'>
<link rel="shortcut icon" href="<%=request.getContextPath() %>/images/common/favicon.ico" type="image/x-icon">
<script type="text/javascript" src="javascript/common.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/jsdata.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/direct.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/extra.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/common.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/jsdata.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/direct.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/extra.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/clock.js?r=<%=Math.random() %>"></script>
<script type="text/javascript" src="javascript/qrcode.min.js?r=<%=Math.random() %>"></script>
<script type="text/javascript">

var nowF = "ele1";
var nowI = 0; //interface 主界面:1 二维码展示页
var lastF = 'ele1'; // 上一个焦点
var songId = '<%=songId%>';
var platform = '<%=platform%>';
var userId = '<%=userId%>';
var ip = '<%=ip%>';
var terminalId = '<%=terminalId%>';
var token = '<%=token%>';
var productName = '欢乐歌房—包月';//产品名
var priceStr = '20元/月';
var productId = '1';//1:包月 2:包季 3:包半年;4:包年;
var out_trade_no = '';//商户订单号
var payType = '1';// 支付方式: 1:支付宝 2:微信
// 按键事件
document.onkeydown = keyPress;
/*function keyPress(event) {
event = event ? event : window.event;
var keyCode = event.which ? event.which : event.keyCode;
if (keyCode == 8 || keyCode == 24 ) {
clearInterval(inquire);
__return();
return false;
} else if (keyCode == 37) { // 左
move_center('moveL');
return false;
} else if (keyCode == 38) { // 上
move_center('moveU');
return false;
} else if (keyCode == 39) { // 右
move_center('moveR');
return false;
} else if (keyCode == 40) { // 下
move_center('moveD');
return false;
} else if (keyCode == 13) {//回车
move_center('ok')
} else if (keyCode == 32){//空格
cancel();
}
}*/

// 需要修改的部分
function move_center(dir) {
if (dir === 'moveL') {
if (nowI == 0) {
if (nowF == "ele2") {
$("ele2").style.visibility = "visible";
$("f_ele2").style.visibility = "hidden";
$("ele1").style.visibility = "hidden";
$("f_ele1").style.visibility = "visible";
nowF = "ele1";
}else if (nowF == "ele3") {
$("ele3").style.visibility = "visible";
$("f_ele3").style.visibility = "hidden";
$("ele2").style.visibility = "hidden";
$("f_ele2").style.visibility = "visible";
nowF = "ele2";
}else if (nowF == "ele4") {
$("ele4").style.visibility = "visible";
$("f_ele4").style.visibility = "hidden";
$("ele3").style.visibility = "hidden";
$("f_ele3").style.visibility = "visible";
nowF = "ele3";
}
}
} else if (dir === 'moveR') {
if (nowI == 0) {
if (nowF == "ele1") {
$("ele1").style.visibility = "visible";
$("f_ele1").style.visibility = "hidden";
$("ele2").style.visibility = "hidden";
$("f_ele2").style.visibility = "visible";
nowF = "ele2";
}else if (nowF == "ele2") {
$("ele2").style.visibility = "visible";
$("f_ele2").style.visibility = "hidden";
$("ele3").style.visibility = "hidden";
$("f_ele3").style.visibility = "visible";
nowF = "ele3";
}else if (nowF == "ele3") {
$("ele3").style.visibility = "visible";
$("f_ele3").style.visibility = "hidden";
$("ele4").style.visibility = "hidden";
$("f_ele4").style.visibility = "visible";
nowF = "ele4";
}
}
} else if (dir == 'moveU') {
if (nowI == 1) {
if (nowF == "ele6") {
$("ele6").style.visibility = "visible";
$("f_ele6").style.visibility = "hidden";
$("ele5").style.visibility = "hidden";
$("f_ele5").style.visibility = "visible";
nowF = "ele5";
}
}
} else if (dir == 'moveD') {
if (nowI == 1) {
if (nowF == "ele5") {
$("ele5").style.visibility = "visible";
$("f_ele5").style.visibility = "hidden";
$("ele6").style.visibility = "hidden";
$("f_ele6").style.visibility = "visible";
nowF = "ele6";
}
}
} else if (dir == 'ok') {
doclick();
}
/*if ("0c41e992b8ed" == userId){
$('test').innerHTML = "nowF: "+nowF;
}*/
}

// 需要修改的部分
function doclick() {
if (nowI === 0) {//主界面
nowI = 1;
$('f_'+nowF).style.visibility = "hidden";
$(nowF).style.visibility = "visible";
lastF = nowF;
$('tip2').style.visibility = "visible";
nowF = 'ele5';
$('f_'+nowF).style.visibility = "visible";
$('ele6').style.visibility = "visible";
} else if (nowI === 1) {
if (lastF === "ele1") {
productId = '1';
productName = '欢乐歌房—包月';
priceStr = '<%=cent2yuan(monthPrice)%>'+'元/月';
}
else if (lastF === "ele2") {
productId = '2';
productName = '欢乐歌房—包季';
priceStr = '<%=cent2yuan(quarterPrice)%>'+'元/季';
}
else if (lastF === "ele3") {
productId = '3';
productName = '欢乐歌房—包半年';
priceStr = '<%=cent2yuan(halfYearPrice)%>'+'元/半年';
}
else if (lastF === "ele4") {
productId = '4';
productName = '欢乐歌房—包年';
priceStr = '<%=cent2yuan(yearPrice)%>'+'元/年';
}

$("wait").style.visibility = "visible";
var url = 'k=1&userid='+userId+'&platform='+platform+'&ip='+ip+'&terminalId='+terminalId+'&token='+token+'&songId='+songId+'&productId='+productId;
if (nowF === "ele5"){//微信支付
payType = '2';
url = url+'&payType='+payType;
}else if (nowF === "ele6"){//支付宝支付
payType = '1';
url = url+'&payType='+payType;
}
lastF = nowF;
nowF = 'tip3';
nowI = 2;
var orderJson = post(url , 'pay.jsp', '1')
}else if (nowI === 2){//显示二维码

}
}

var qrCode ;//二维码对象
function makeCode (id,qrUrl) {
if (qrCode){
qrCode.makeCode(qrUrl);
}else{
qrCode = new QRCode(document.getElementById(id), {
width : 207,
height : 206
});
}
qrCode.makeCode(qrUrl);
}

//解析下单返回的json
function orderJsonParse(orderjson) {
$("wait").style.visibility = "hidden";
var data = JSON.parse(orderjson);
if (data.result === '0') {
nowI = 2;
var qrCodeImg = data.qrCodeImg;
out_trade_no = data.outTrade;
nowF = 'tip3';
$(nowF).style.visibility = "visible";
//重新生成图片
$(nowF + '_1').src = "http://111.19.132.254:8088/joymusic_cs_gwb/imgerU?pic=http://111.19.132.254:8088/joymusic_cs_gwb/images/common/order/gwb/qr_bj_"+payType+".png&type=watermark&z=" + productName + "," + priceStr + "&s=22&x=435&y=250&x2=400&y2=320&rand=<%=Math.random()%>";
$(nowF + '_1').style.visibility = "visible";
//qrCode.clear();//置空
makeCode('qrCodeImg',qrCodeImg);
$('qrCodeImg').style.visibility = "visible";//将二维码div生成显示
$('ico').style.visibility = "visible";
//将num置零
num = 0;
//定时器 定时查询
setTimeout(checkOrder(),3000);
} else {
//下单失败
window.location = 'pay.jsp?k=0&userid=' + userId + '&platform=' + platform + '&ip=' + ip + '&terminalId=' + terminalId + '&token=' + token + '&songId=' + songId;
}
}


var xhr = '';
//ajax异步访问后端
function post(data, url, type) {
//请求参数、url、创建XMLHttpRequest
var data = data;
var postUrl = url;
var type = type;//类型: 1,订购下单 2,下单查询
if (window.XMLHttpRequest) xhr = new XMLHttpRequest();
// IE浏览器
else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
}
}
}
xhr.open('post', postUrl);
//设置header
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(data);
xhr.onreadystatechange = function () {
var jsonText = xhr.responseText;
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304 || xhr.status === 500)) {
if ('' !== jsonText) {
if ('1' === type) {//支付下单
orderJsonParse(jsonText)
} else if ('2' === type) {//下单查询
var jsonP = eval("(" + jsonText + ")");
var result = jsonP.result;
var isKeep = jsonP.isKeep;//继续
if ('1' == isKeep){
if (result == 0) {//
clearInterval(inquire);
hiddenQR();
window.android.payFinish('1');
}
}else {
clearInterval(inquire);
if (result == 0) {//
hiddenQR();
window.android.payFinish('1');
} else {//显示主见面
hiddenQR();
//显示主界面
nowI = 0; //interface 主界面:0 其他
nowF = lastF;
$(nowF).style.visibility = "hidden";
$('f_' + nowF).style.visibility = "visible";
window.android.toast("订购失败");
window.android.payFinish('-1');
}
}
}
}
}
}
}

//销毁、取消
function cancel() {
console.info("nowF: "+nowF+",nowI: "+nowI);
if (nowI == 1) {
//显示主界面
nowI = 0; //interface 主界面:0 其他
nowF = lastF;
if ('ele5' == nowF || 'ele6' == nowF){//从二维码界面返回时 无法找到最开始的订购类型 所以直接设置为第一个
nowF = 'ele1';
}
//懒得判断 全部置为不显示
$('ele5').style.visibility = "hidden";
$('ele6').style.visibility = "hidden";
$('f_ele5').style.visibility = "hidden";
$('f_ele6').style.visibility = "hidden";
$('tip2').style.visibility = "hidden";
$(nowF).style.visibility = "hidden";
$('f_' + nowF).style.visibility = "visible";
}else if (nowI == 2) {
clearInterval(inquire);
hiddenQR();
var checkUrl = 'k=3&userid='+userId+'&platform='+platform+'&ip='+ip+'&terminalId='+terminalId+'&token='+token+'&songId='+songId+'&productId='+productId+'&outTrade='+out_trade_no;
post(checkUrl, 'pay.jsp', '3');
//显示主界面
nowI = 1; //订购方式选择
nowF = lastF;
$('tip2').style.visibility = "visible";
if ('ele5' === nowF) $('ele6').style.visibility = "visible";
else if ('ele6' === nowF) $('ele5').style.visibility = "visible";
$('f_' + nowF).style.visibility = "visible";
}else {
window.android.finish();
}
}


var inquire = null;
var num = 0;
//支付状态查询
function checkOrder() {
inquire = setInterval(function () {
var checkUrl = 'k=2&userid='+userId+'&platform='+platform+'&ip='+ip+'&terminalId='+terminalId+'&token='+token+'&songId='+songId+'&productId='+productId+'&isLast=0&outTrade='+out_trade_no+'&payType='+payType;
if (num === 100) {
clearInterval(inquire);
}else if (num === 99 ){//最后一次下单查询
checkUrl = 'k=2&userid='+userId+'&platform='+platform+'&ip='+ip+'&terminalId='+terminalId+'&token='+token+'&songId='+songId+'&productId='+productId+'&isLast=1&outTrade='+out_trade_no+'&payType='+payType;
}
post(checkUrl, 'pay.jsp', '2');
num += 1;
}, 3000);
}

//隐藏二维码
function hiddenQR() {
//超时 隐藏二维码
$('ico').style.visibility = "hidden";
$('tip3_1').style.visibility = "hidden";
$('tip3').style.visibility = "hidden";
qrCode.clear();//置空
$('qrCodeImg').style.visibility = "hidden";//将二维码div生成显示
}

//页面加载调用
function start() {
$("f_" + nowF).style.visibility = "visible";
}
</script>
</head>

<body onload="start()" bgcolor="transparent">
<!-- 订购页 主界面 以后背景优先加载 -->
<img id="tip1" src="images/common/order/gwb/bj.jpg" style="position:absolute;left:0px;top:0px;width:1280px;height:720px;z-index:-1">
<img id="ele1" src="images/common/order/gwb/1.png?r=<%=Math.random() %>" style="position:absolute;left:109px;top:219px;width:264px;height:344px;visibility:visible">
<img id="f_ele1" src="images/common/order/gwb/f_1.png?r=<%=Math.random() %>" style="position:absolute;left:109px;top:219px;width:264px;height:344px;visibility:hidden">
<img id="ele2" src="images/common/order/gwb/2.png?r=<%=Math.random() %>" style="position:absolute;left:389px;top:219px;width:264px;height:344px;visibility:visible">
<img id="f_ele2" src="images/common/order/gwb/f_2.png?r=<%=Math.random() %>" style="position:absolute;left:389px;top:219px;width:264px;height:344px;visibility:hidden">
<img id="ele3" src="images/common/order/gwb/3.png?r=<%=Math.random() %>" style="position:absolute;left:669px;top:219px;width:264px;height:344px;visibility:visible">
<img id="f_ele3" src="images/common/order/gwb/f_3.png?r=<%=Math.random() %>" style="position:absolute;left:669px;top:219px;width:264px;height:344px;visibility:hidden">
<img id="ele4" src="images/common/order/gwb/4.png?r=<%=Math.random() %>" style="position:absolute;left:949px;top:219px;width:264px;height:344px;visibility:visible">
<img id="f_ele4" src="images/common/order/gwb/f_4.png?r=<%=Math.random() %>" style="position:absolute;left:949px;top:219px;width:264px;height:344px;visibility:hidden">
<!-- 订购方式选择 以后背景优先加载 -->
<img id="tip2" src="images/common/order/gwb/order_bj.png" style="position:absolute;left:0px;top:0px;width:1280px;height:720px;z-index:1;visibility:hidden">
<img id="ele5" src="images/common/order/gwb/wx.png?r=<%=Math.random() %>" style="position:absolute;left:500px;top:340px;width:393px;height:115px;visibility:hidden;z-index:2">
<img id="f_ele5" src="images/common/order/gwb/f_wx.png?r=<%=Math.random() %>" style="position:absolute;left:500px;top:340px;width:393px;height:115px;visibility:hidden;z-index:2">
<img id="ele6" src="images/common/order/gwb/ali.png?r=<%=Math.random() %>" style="position:absolute;left:500px;top:430px;width:393px;height:115px;visibility:hidden;z-index:2">
<img id="f_ele6" src="images/common/order/gwb/f_ali.png?r=<%=Math.random() %>" style="position:absolute;left:500px;top:430px;width:393px;height:115px;visibility:hidden;z-index:2">
<!-- 订购确认页 以后背景优先加载 -->
<img id="tip3" src="images/common/order/gwb/bj.jpg" style="position:absolute;left:0px;top:0px;width:1280px;height:720px;visibility:hidden;z-index:3">
<img id="tip3_1" src="images/common/order/gwb/qr_bj.png" style="position:absolute;left:0px;top:0px;width:1280px;height:720px;visibility:hidden;z-index:3">
<%--将base64字符串转换为二维码--%>
<div id="qrCodeImg" style="position:absolute;left:731px;top:248px;width:209px;height:208px;z-index:4;visibility:hidden"></div>
<img id="ico" src="images/common/order/gwb/ico.jpg" style="position:absolute;left:808px;top:310px;width:60px;height:60px;visibility:hidden;z-index:4">
<!-- 订购等待背景 -->
<img id="wait" src="images/common/order/gwb/wait_bj.png" style="position:absolute;left:0px;top:0px;width:1280px;height:720px;z-index:3;visibility:hidden">
</body>
</html>