UnionPayUtil.java
5.33 KB
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
package com.cjs.site.util.union;
import com.cjs.site.util.lang.DateEnum;
import com.cjs.site.util.lang.DateUtil;
import com.cjs.site.util.lang.JsonUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.*;
/**
* Created by bruce on 2019-05-14 10:07
*/
public class UnionPayUtil {
public static String createQrCode(String qrCodeId) {
return UnionConstants.QRCODE_URL + qrCodeId;
}
private static String makeSign(Map<String, String> params) {
String preStr = buildSignString(params);
String text = preStr + UnionConstants.MD5_KEY;
return DigestUtils.md5Hex(getContentBytes(text)).toUpperCase();
}
private static String buildSignString(Map<String, String> params) {
if (params == null || params.size() == 0) {
return "";
}
List<String> keys = new ArrayList<String>(params.size());
for (String key : params.keySet()) {
if ("sign".equals(key))
continue;
if (StringUtils.isEmpty(params.get(key)))
continue;
keys.add(key);
}
Collections.sort(keys);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < keys.size(); i++) {
String key = keys.get(i);
String value = params.get(key);
if (i == keys.size() - 1) {
buf.append(key).append("=").append(value);
} else {
buf.append(key).append("=").append(value).append("&");
}
}
return buf.toString();
}
public static Boolean checkSign(Map<String, String> params) {
String sign = params.get("sign");
if (StringUtils.isBlank(sign)) {
return false;
}
String signV = makeSign(params);
return StringUtils.equalsIgnoreCase(sign, signV);
}
public static String getQrCodeId() {
String date = DateUtil.parseDate(new Date(), DateEnum.UNSIGNED_DATE_TIME_MILLS);
String rand = RandomStringUtils.randomNumeric(7);
return UnionConstants.MSG_ID + date + rand;
}
private static byte[] getContentBytes(String content) {
try {
return content.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("签名过程中出现错误");
}
}
public static Map<String, String> getRequestParams(HttpServletRequest request) {
Map<String, String[]> params = request.getParameterMap();
Map<String, String> params2 = new HashMap<String, String>();
for (String key : params.keySet()) {
String[] values = params.get(key);
if (values.length > 0) {
params2.put(key, values[0]);
}
}
return params2;
}
public static String sendPost(String url, Map<String, String> data) {
PrintWriter out = null;
BufferedReader in = null;
StringBuilder result = new StringBuilder();
data.put("sign", makeSign(data));
try {
URL realUrl = new URL(url);
URLConnection conn = realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
out = new PrintWriter(conn.getOutputStream());
out.write(JsonUtil.toJson(data));
out.flush();
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
} catch (Exception e) {
e.printStackTrace();
result.append(e.getMessage());
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
try {
return URLDecoder.decode(result.toString(), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "转码出错";
}
}
/* public static void main(String[] args) {
Map<String, String> params = new HashMap<String, String>();
params.put("mid", UnionConstants.MID);
params.put("tid", UnionConstants.TID);
params.put("msgType", "bills.refund");
params.put("msgSrc", "WWW.TEST.COM");
params.put("instMid", "QRPAYDEFAULT");
params.put("billNo", "3194201905231441281233453476");
params.put("billDate", "2019-05-23");
params.put("refundAmount", "1");
params.put("requestTimestamp", DateUtil.getNow());
params.put("sign", makeSign(params));
String result = sendPost("https://qr-test2.chinaums.com/netpay-route-server/api/", params);
System.out.println(JsonUtil.toJson(result));
}*/
}