后台支付开发

来新公司的第二个星期啦,好多支付都要对接,这是我的第一个支付,也算是最简单的支付

图示

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
 public String easyPay() throws IOException {

//第三方支付的请求接口
String url = "https://g99.xxxx.com/gateway.php";
//第三方给的秘钥
String secret = "9295d6608c9bf123199c120b68f0d76c907e54";
//第三方给的商户号
String orgId = "ep2018051212693357";
//订单号
String tradeNo = String.valueOf(new Date().getTime());
//后台异步异步回调url
String notifyUrl = "http://110.110.110.38:1999/notify/test.html"

System.out.println("这次的订单号是="+tradeNo);

Map<String, String> map = new TreeMap<>();
map.put("key", orgId);
map.put("memo", "cunkuan"); //备注
map.put("money", "1"); //订单金额,单位为分
map.put("notify", notifyUrl);
map.put("platform", "alipay"); //支付方式
map.put("redirect", "https://www.demo.com/redirect"); //支付成功之后,第三方跳转的页面
map.put("timestamp", tradeNo); //支付时间
map.put("title", "cunkuan"); //商品名字
map.put("trade_no", tradeNo); //订单号

//拼接str
Iterator<String> iterator = map.keySet().iterator();
String str = "";
while (iterator.hasNext()) {
String key = iterator.next();
Object value = map.get(key);
System.out.println(key+"="+value);
if ( value != null) {
str += key + "=" + value+"&";
}else{
System.out.println("null");
}
}

//加入秘钥
str += "secret="+secret;

//签名MD5 32 小写字母
String md5Str = getMD5(str);

//发送请求
String userToken = send(url, md5Str, map);
System.out.println(userToken);

//接收参数,获取 返回跳转url
net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(userToken);

//保存数据库表

//返回给前端
HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cache-Control", "no-cache"); // 取消浏览器缓存
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
out.print("<script type=text/javascript>window.location.href='"+jsonObject.getString("page_url")+"';</script>");
out.flush();
out.close();

}

public String send(String url,String md5Str,Map<String,String> map) {
HttpClient client = new HttpClient();
PostMethod myPost = new PostMethod(url);
String responseString = null;
try {
String tradeNo = String.valueOf(new Date().getTime());
myPost.addRequestHeader("Accept", "application/json");
myPost.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");

myPost.setParameter("key", map.get("key"));
myPost.setParameter("method", map.get("method"));
myPost.setParameter("trade_no", map.get("trade_no"));
myPost.setParameter("title", map.get("title"));
myPost.setParameter("memo", map.get("memo"));
myPost.setParameter("money", map.get("money")); //订单金额,单位为分
myPost.setParameter("platform", map.get("platform")); //
myPost.setParameter("mobile", map.get("mobile")); //
myPost.setParameter("timestamp", map.get("timestamp"));
myPost.setParameter("notify", map.get("notify"));
myPost.setParameter("redirect", map.get("redirect"));
myPost.setParameter("sign", md5Str);
int statusCode = client.executeMethod(myPost);
if (statusCode == HttpStatus.SC_OK) {
responseString = myPost.getResponseBodyAsString();
if (responseString == null || responseString.equals("")) {
System.out.println(GamePlatform.SUNBET.name() + " API 接口出现问题!");
return null;
}
} else {
System.out.println("Response Code: " + statusCode+" 返回信息:"+myPost.getResponseBodyAsString());
return null;
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Response 消息: " + e.toString());
return null;
} finally {
myPost.releaseConnection();

}
return responseString;
}


public static String getMD5(String str) {
try {
// 生成一个MD5加密计算摘要
MessageDigest md = MessageDigest.getInstance("MD5");
// 计算md5函数
md.update(str.getBytes());
String md5=new BigInteger(1, md.digest()).toString(16);
return md5.length()==32?md5:fillMD5("0"+md5);
} catch (Exception e) {
throw new RuntimeException("MD5加密错误:"+e.getMessage(),e);
}
}

public static String fillMD5(String md5){
return md5.length()==32?md5:fillMD5("0"+md5);
}


public void easyPayNotify() throws Exception {

platFormType = OPPlatform.easyp.getCode();

//返回SUCCESS给第三方
final String RESULT_SUCCESS = "SUCCESS";

HttpServletResponse response = ServletActionContext.getResponse();
response.setHeader("Cache-Control", "no-cache"); // 取消浏览器缓存
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();

String trade_no = null;
String returnmsg = null;

try {
String key = getRequest().getParameter("key");
String sn = getRequest().getParameter("sn");
trade_no = getRequest().getParameter("trade_no");
String platform = getRequest().getParameter("platform");
String title = getRequest().getParameter("title");
String memo = getRequest().getParameter("memo");
String money = new BigDecimal(getRequest().getParameter("money")).divide(new BigDecimal("100")).toString();
String sysid = getRequest().getParameter("sysid");
String finish = getRequest().getParameter("finish");
String sign = getRequest().getParameter("sign");
String time = getRequest().getParameter("time");

out = this.getResponse().getWriter();
log.info("************提交IP是:" + getIp());
log.info("easyPay 支付宝扫码支付(" + platFormType + ")支付回调--》" +
"商户号="+key+
"第三方流水号="+sn+
"商户订单="+trade_no+
"商品名称="+title+
"商品备注="+memo+
"金额="+money+
"支付宝交易号="+sysid+
"支付完成时间="+finish+
"签名="+sign+
"数据返回时间="+time);

String onlinePaymentParams = AxisUtil.getObjectOne(AxisUtil.getClient(
AxisUtil.PUBLICWEBSERVICEURL + "UserWebService", false), AxisUtil.NAMESPACE,
"queryOnlinePaymentParams", new Object[]{key, platFormType}, String.class);
Map<String, String> paramMap = JsonUtil.transferJsonToMap(onlinePaymentParams);
String secret = paramMap.get("secret");

Map<String, String> map = new TreeMap<>();
map.put("finish", finish);
map.put("key", key);
map.put("memo", memo);
map.put("money", money); //
map.put("platform", platform);
map.put("sn", sn);
map.put("sysid", sysid); //
map.put("time",time);
map.put("title", title);
map.put("trade_no", trade_no);

//拼接str
Iterator<String> iterator = map.keySet().iterator();
String str = "";
while (iterator.hasNext()) {
String key2 = iterator.next();
Object value = map.get(key2);
if ( value != null) {
str += key2 + "=" + value+"&";
}else{
System.out.println("null");
}
}

//加入私钥
str += "secret="+secret;

//md5加密
String md5Str = getMD5(str);

//校验
if (sign.equals(md5Str)) {

//数据库操作
String userLoginName = AxisUtil.getObjectOne(AxisUtil.getClient(
AxisUtil.PUBLICWEBSERVICEURL + "UserWebService", false),
AxisUtil.NAMESPACE, "queryUnpaidorderLoginName", new Object[]{trade_no}, String.class);

returnmsg = AxisUtil.getObjectOne(AxisUtil.getClient(
AxisUtil.PUBLICWEBSERVICEURL + "UserWebService", false),
AxisUtil.NAMESPACE, "saveValidateOnlinePayOrder", new Object[]{
trade_no, userLoginName, key, platFormType, sn, money, null, null, null, null}, String.class);

if (returnmsg == null) {
log.info("easyPage 支付宝扫码支付(" + platFormType + ")支付交易成功,你支付的" + money + "元已经到账,此次交易的订单号为" + trade_no);
out.print(RESULT_SUCCESS);
out.flush();
}else{
log.info("easyPage 支付宝扫码支付(" + platFormType + ")支付交易失败returnmsg:" + returnmsg + ", 订单号为" + trade_no);
out.print("FAIL");
}
}else{
log.info("easyPay 支付宝扫码支付(" + platFormType + ")支付回调验签失败--》" +
"商户号="+key+
"第三方流水号="+sn+
"商户订单="+trade_no+
"商品名称="+title+
"商品备注="+memo+
"金额="+money+
"支付宝交易号="+sysid+
"支付完成时间="+finish+
"签名="+sign+
"数据返回时间="+time);
out.print("FAIL");
out.flush();
}
} catch (IOException e) {
e.printStackTrace();
log.info("easyPage 支付宝扫码支付(" + platFormType + ")支付交易失败returnmsg:" + returnmsg + ", 订单号为" + trade_no);
} finally {
out.close();
}
}