|
@@ -0,0 +1,689 @@
|
|
1
|
+package com.huiju.estateagents.common.wxpay;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+import java.util.HashMap;
|
|
5
|
+import java.util.Map;
|
|
6
|
+import com.huiju.estateagents.common.wxpay.WXPayConstants.SignType;
|
|
7
|
+
|
|
8
|
+public class WXPay {
|
|
9
|
+
|
|
10
|
+ private WXPayConfig config;
|
|
11
|
+ private SignType signType;
|
|
12
|
+ private boolean autoReport;
|
|
13
|
+ private boolean useSandbox;
|
|
14
|
+ private String notifyUrl;
|
|
15
|
+ private WXPayRequest wxPayRequest;
|
|
16
|
+
|
|
17
|
+ public WXPay(final WXPayConfig config) throws Exception {
|
|
18
|
+ this(config, null, true, false);
|
|
19
|
+ }
|
|
20
|
+
|
|
21
|
+ public WXPay(final WXPayConfig config, final boolean autoReport) throws Exception {
|
|
22
|
+ this(config, null, autoReport, false);
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+ public WXPay(final WXPayConfig config, final boolean autoReport, final boolean useSandbox) throws Exception{
|
|
27
|
+ this(config, null, autoReport, useSandbox);
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ public WXPay(final WXPayConfig config, final String notifyUrl) throws Exception {
|
|
31
|
+ this(config, notifyUrl, true, false);
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport) throws Exception {
|
|
35
|
+ this(config, notifyUrl, autoReport, false);
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {
|
|
39
|
+ this.config = config;
|
|
40
|
+ this.notifyUrl = notifyUrl;
|
|
41
|
+ this.autoReport = autoReport;
|
|
42
|
+ this.useSandbox = useSandbox;
|
|
43
|
+ //if (useSandbox) {
|
|
44
|
+ this.signType = SignType.MD5;
|
|
45
|
+ //}
|
|
46
|
+ //else {
|
|
47
|
+ // this.signType = SignType.HMACSHA256;
|
|
48
|
+ //}
|
|
49
|
+ this.wxPayRequest = new WXPayRequest(config);
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ private void checkWXPayConfig() throws Exception {
|
|
53
|
+ if (this.config == null) {
|
|
54
|
+ throw new Exception("config is null");
|
|
55
|
+ }
|
|
56
|
+ if (this.config.getAppID() == null || this.config.getAppID().trim().length() == 0) {
|
|
57
|
+ throw new Exception("appid in config is empty");
|
|
58
|
+ }
|
|
59
|
+ if (this.config.getMchID() == null || this.config.getMchID().trim().length() == 0) {
|
|
60
|
+ throw new Exception("appid in config is empty");
|
|
61
|
+ }
|
|
62
|
+ if (this.config.getCertStream() == null) {
|
|
63
|
+ throw new Exception("cert stream in config is empty");
|
|
64
|
+ }
|
|
65
|
+ if (this.config.getWXPayDomain() == null){
|
|
66
|
+ throw new Exception("config.getWXPayDomain() is null");
|
|
67
|
+ }
|
|
68
|
+
|
|
69
|
+ if (this.config.getHttpConnectTimeoutMs() < 10) {
|
|
70
|
+ throw new Exception("http connect timeout is too small");
|
|
71
|
+ }
|
|
72
|
+ if (this.config.getHttpReadTimeoutMs() < 10) {
|
|
73
|
+ throw new Exception("http read timeout is too small");
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ }
|
|
77
|
+
|
|
78
|
+ /**
|
|
79
|
+ * 向 Map 中添加 appid、mch_id、nonce_str、sign_type、sign <br>
|
|
80
|
+ * 该函数适用于商户适用于统一下单等接口,不适用于红包、代金券接口
|
|
81
|
+ *
|
|
82
|
+ * @param reqData
|
|
83
|
+ * @return
|
|
84
|
+ * @throws Exception
|
|
85
|
+ */
|
|
86
|
+ public Map<String, String> fillRequestData(Map<String, String> reqData) throws Exception {
|
|
87
|
+ reqData.put("appid", config.getAppID());
|
|
88
|
+ reqData.put("mch_id", config.getMchID());
|
|
89
|
+ reqData.put("nonce_str", WXPayUtil.generateNonceStr());
|
|
90
|
+ if (SignType.MD5.equals(this.signType)) {
|
|
91
|
+ reqData.put("sign_type", WXPayConstants.MD5);
|
|
92
|
+ }
|
|
93
|
+ else if (SignType.HMACSHA256.equals(this.signType)) {
|
|
94
|
+ reqData.put("sign_type", WXPayConstants.HMACSHA256);
|
|
95
|
+ }
|
|
96
|
+ reqData.put("sign", WXPayUtil.generateSignature(reqData, config.getKey(), this.signType));
|
|
97
|
+ return reqData;
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ /**
|
|
101
|
+ * 判断xml数据的sign是否有效,必须包含sign字段,否则返回false。
|
|
102
|
+ *
|
|
103
|
+ * @param reqData 向wxpay post的请求数据
|
|
104
|
+ * @return 签名是否有效
|
|
105
|
+ * @throws Exception
|
|
106
|
+ */
|
|
107
|
+ public boolean isResponseSignatureValid(Map<String, String> reqData) throws Exception {
|
|
108
|
+ // 返回数据的签名方式和请求中给定的签名方式是一致的
|
|
109
|
+ return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), this.signType);
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ /**
|
|
113
|
+ * 判断支付结果通知中的sign是否有效
|
|
114
|
+ *
|
|
115
|
+ * @param reqData 向wxpay post的请求数据
|
|
116
|
+ * @return 签名是否有效
|
|
117
|
+ * @throws Exception
|
|
118
|
+ */
|
|
119
|
+ public boolean isPayResultNotifySignatureValid(Map<String, String> reqData) throws Exception {
|
|
120
|
+ String signTypeInData = reqData.get(WXPayConstants.FIELD_SIGN_TYPE);
|
|
121
|
+ SignType signType;
|
|
122
|
+ if (signTypeInData == null) {
|
|
123
|
+ signType = SignType.MD5;
|
|
124
|
+ }
|
|
125
|
+ else {
|
|
126
|
+ signTypeInData = signTypeInData.trim();
|
|
127
|
+ if (signTypeInData.length() == 0) {
|
|
128
|
+ signType = SignType.MD5;
|
|
129
|
+ }
|
|
130
|
+ else if (WXPayConstants.MD5.equals(signTypeInData)) {
|
|
131
|
+ signType = SignType.MD5;
|
|
132
|
+ }
|
|
133
|
+ else if (WXPayConstants.HMACSHA256.equals(signTypeInData)) {
|
|
134
|
+ signType = SignType.HMACSHA256;
|
|
135
|
+ }
|
|
136
|
+ else {
|
|
137
|
+ throw new Exception(String.format("Unsupported sign_type: %s", signTypeInData));
|
|
138
|
+ }
|
|
139
|
+ }
|
|
140
|
+ return WXPayUtil.isSignatureValid(reqData, this.config.getKey(), signType);
|
|
141
|
+ }
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+ /**
|
|
145
|
+ * 不需要证书的请求
|
|
146
|
+ * @param urlSuffix String
|
|
147
|
+ * @param reqData 向wxpay post的请求数据
|
|
148
|
+ * @param connectTimeoutMs 超时时间,单位是毫秒
|
|
149
|
+ * @param readTimeoutMs 超时时间,单位是毫秒
|
|
150
|
+ * @return API返回数据
|
|
151
|
+ * @throws Exception
|
|
152
|
+ */
|
|
153
|
+ public String requestWithoutCert(String urlSuffix, Map<String, String> reqData,
|
|
154
|
+ int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
155
|
+ String msgUUID = reqData.get("nonce_str");
|
|
156
|
+ String reqBody = WXPayUtil.mapToXml(reqData);
|
|
157
|
+
|
|
158
|
+ String resp = this.wxPayRequest.requestWithoutCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, autoReport);
|
|
159
|
+ return resp;
|
|
160
|
+ }
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+ /**
|
|
164
|
+ * 需要证书的请求
|
|
165
|
+ * @param urlSuffix String
|
|
166
|
+ * @param reqData 向wxpay post的请求数据 Map
|
|
167
|
+ * @param connectTimeoutMs 超时时间,单位是毫秒
|
|
168
|
+ * @param readTimeoutMs 超时时间,单位是毫秒
|
|
169
|
+ * @return API返回数据
|
|
170
|
+ * @throws Exception
|
|
171
|
+ */
|
|
172
|
+ public String requestWithCert(String urlSuffix, Map<String, String> reqData,
|
|
173
|
+ int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
174
|
+ String msgUUID= reqData.get("nonce_str");
|
|
175
|
+ String reqBody = WXPayUtil.mapToXml(reqData);
|
|
176
|
+
|
|
177
|
+ String resp = this.wxPayRequest.requestWithCert(urlSuffix, msgUUID, reqBody, connectTimeoutMs, readTimeoutMs, this.autoReport);
|
|
178
|
+ return resp;
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ /**
|
|
182
|
+ * 处理 HTTPS API返回数据,转换成Map对象。return_code为SUCCESS时,验证签名。
|
|
183
|
+ * @param xmlStr API返回的XML格式数据
|
|
184
|
+ * @return Map类型数据
|
|
185
|
+ * @throws Exception
|
|
186
|
+ */
|
|
187
|
+ public Map<String, String> processResponseXml(String xmlStr) throws Exception {
|
|
188
|
+ String RETURN_CODE = "return_code";
|
|
189
|
+ String return_code;
|
|
190
|
+ Map<String, String> respData = WXPayUtil.xmlToMap(xmlStr);
|
|
191
|
+ if (respData.containsKey(RETURN_CODE)) {
|
|
192
|
+ return_code = respData.get(RETURN_CODE);
|
|
193
|
+ }
|
|
194
|
+ else {
|
|
195
|
+ throw new Exception(String.format("No `return_code` in XML: %s", xmlStr));
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ if (return_code.equals(WXPayConstants.FAIL)) {
|
|
199
|
+ return respData;
|
|
200
|
+ }
|
|
201
|
+ else if (return_code.equals(WXPayConstants.SUCCESS)) {
|
|
202
|
+ if (this.isResponseSignatureValid(respData)) {
|
|
203
|
+ return respData;
|
|
204
|
+ }
|
|
205
|
+ else {
|
|
206
|
+ throw new Exception(String.format("Invalid sign value in XML: %s", xmlStr));
|
|
207
|
+ }
|
|
208
|
+ }
|
|
209
|
+ else {
|
|
210
|
+ throw new Exception(String.format("return_code value %s is invalid in XML: %s", return_code, xmlStr));
|
|
211
|
+ }
|
|
212
|
+ }
|
|
213
|
+
|
|
214
|
+ /**
|
|
215
|
+ * 作用:提交刷卡支付<br>
|
|
216
|
+ * 场景:刷卡支付
|
|
217
|
+ * @param reqData 向wxpay post的请求数据
|
|
218
|
+ * @return API返回数据
|
|
219
|
+ * @throws Exception
|
|
220
|
+ */
|
|
221
|
+ public Map<String, String> microPay(Map<String, String> reqData) throws Exception {
|
|
222
|
+ return this.microPay(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
223
|
+ }
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+ /**
|
|
227
|
+ * 作用:提交刷卡支付<br>
|
|
228
|
+ * 场景:刷卡支付
|
|
229
|
+ * @param reqData 向wxpay post的请求数据
|
|
230
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
231
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
232
|
+ * @return API返回数据
|
|
233
|
+ * @throws Exception
|
|
234
|
+ */
|
|
235
|
+ public Map<String, String> microPay(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
236
|
+ String url;
|
|
237
|
+ if (this.useSandbox) {
|
|
238
|
+ url = WXPayConstants.SANDBOX_MICROPAY_URL_SUFFIX;
|
|
239
|
+ }
|
|
240
|
+ else {
|
|
241
|
+ url = WXPayConstants.MICROPAY_URL_SUFFIX;
|
|
242
|
+ }
|
|
243
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
244
|
+ return this.processResponseXml(respXml);
|
|
245
|
+ }
|
|
246
|
+
|
|
247
|
+ /**
|
|
248
|
+ * 提交刷卡支付,针对软POS,尽可能做成功
|
|
249
|
+ * 内置重试机制,最多60s
|
|
250
|
+ * @param reqData
|
|
251
|
+ * @return
|
|
252
|
+ * @throws Exception
|
|
253
|
+ */
|
|
254
|
+ public Map<String, String> microPayWithPos(Map<String, String> reqData) throws Exception {
|
|
255
|
+ return this.microPayWithPos(reqData, this.config.getHttpConnectTimeoutMs());
|
|
256
|
+ }
|
|
257
|
+
|
|
258
|
+ /**
|
|
259
|
+ * 提交刷卡支付,针对软POS,尽可能做成功
|
|
260
|
+ * 内置重试机制,最多60s
|
|
261
|
+ * @param reqData
|
|
262
|
+ * @param connectTimeoutMs
|
|
263
|
+ * @return
|
|
264
|
+ * @throws Exception
|
|
265
|
+ */
|
|
266
|
+ public Map<String, String> microPayWithPos(Map<String, String> reqData, int connectTimeoutMs) throws Exception {
|
|
267
|
+ int remainingTimeMs = 60*1000;
|
|
268
|
+ long startTimestampMs = 0;
|
|
269
|
+ Map<String, String> lastResult = null;
|
|
270
|
+ Exception lastException = null;
|
|
271
|
+
|
|
272
|
+ while (true) {
|
|
273
|
+ startTimestampMs = WXPayUtil.getCurrentTimestampMs();
|
|
274
|
+ int readTimeoutMs = remainingTimeMs - connectTimeoutMs;
|
|
275
|
+ if (readTimeoutMs > 1000) {
|
|
276
|
+ try {
|
|
277
|
+ lastResult = this.microPay(reqData, connectTimeoutMs, readTimeoutMs);
|
|
278
|
+ String returnCode = lastResult.get("return_code");
|
|
279
|
+ if (returnCode.equals("SUCCESS")) {
|
|
280
|
+ String resultCode = lastResult.get("result_code");
|
|
281
|
+ String errCode = lastResult.get("err_code");
|
|
282
|
+ if (resultCode.equals("SUCCESS")) {
|
|
283
|
+ break;
|
|
284
|
+ }
|
|
285
|
+ else {
|
|
286
|
+ // 看错误码,若支付结果未知,则重试提交刷卡支付
|
|
287
|
+ if (errCode.equals("SYSTEMERROR") || errCode.equals("BANKERROR") || errCode.equals("USERPAYING")) {
|
|
288
|
+ remainingTimeMs = remainingTimeMs - (int)(WXPayUtil.getCurrentTimestampMs() - startTimestampMs);
|
|
289
|
+ if (remainingTimeMs <= 100) {
|
|
290
|
+ break;
|
|
291
|
+ }
|
|
292
|
+ else {
|
|
293
|
+ WXPayUtil.getLogger().info("microPayWithPos: try micropay again");
|
|
294
|
+ if (remainingTimeMs > 5*1000) {
|
|
295
|
+ Thread.sleep(5*1000);
|
|
296
|
+ }
|
|
297
|
+ else {
|
|
298
|
+ Thread.sleep(1*1000);
|
|
299
|
+ }
|
|
300
|
+ continue;
|
|
301
|
+ }
|
|
302
|
+ }
|
|
303
|
+ else {
|
|
304
|
+ break;
|
|
305
|
+ }
|
|
306
|
+ }
|
|
307
|
+ }
|
|
308
|
+ else {
|
|
309
|
+ break;
|
|
310
|
+ }
|
|
311
|
+ }
|
|
312
|
+ catch (Exception ex) {
|
|
313
|
+ lastResult = null;
|
|
314
|
+ lastException = ex;
|
|
315
|
+ }
|
|
316
|
+ }
|
|
317
|
+ else {
|
|
318
|
+ break;
|
|
319
|
+ }
|
|
320
|
+ }
|
|
321
|
+
|
|
322
|
+ if (lastResult == null) {
|
|
323
|
+ throw lastException;
|
|
324
|
+ }
|
|
325
|
+ else {
|
|
326
|
+ return lastResult;
|
|
327
|
+ }
|
|
328
|
+ }
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+ /**
|
|
333
|
+ * 作用:统一下单<br>
|
|
334
|
+ * 场景:公共号支付、扫码支付、APP支付
|
|
335
|
+ * @param reqData 向wxpay post的请求数据
|
|
336
|
+ * @return API返回数据
|
|
337
|
+ * @throws Exception
|
|
338
|
+ */
|
|
339
|
+ public Map<String, String> unifiedOrder(Map<String, String> reqData) throws Exception {
|
|
340
|
+ return this.unifiedOrder(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
341
|
+ }
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+ /**
|
|
345
|
+ * 作用:统一下单<br>
|
|
346
|
+ * 场景:公共号支付、扫码支付、APP支付
|
|
347
|
+ * @param reqData 向wxpay post的请求数据
|
|
348
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
349
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
350
|
+ * @return API返回数据
|
|
351
|
+ * @throws Exception
|
|
352
|
+ */
|
|
353
|
+ public Map<String, String> unifiedOrder(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
354
|
+ String url;
|
|
355
|
+ if (this.useSandbox) {
|
|
356
|
+ url = WXPayConstants.SANDBOX_UNIFIEDORDER_URL_SUFFIX;
|
|
357
|
+ }
|
|
358
|
+ else {
|
|
359
|
+ url = WXPayConstants.UNIFIEDORDER_URL_SUFFIX;
|
|
360
|
+ }
|
|
361
|
+ if(this.notifyUrl != null) {
|
|
362
|
+ reqData.put("notify_url", this.notifyUrl);
|
|
363
|
+ }
|
|
364
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
365
|
+ return this.processResponseXml(respXml);
|
|
366
|
+ }
|
|
367
|
+
|
|
368
|
+
|
|
369
|
+ /**
|
|
370
|
+ * 作用:查询订单<br>
|
|
371
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
372
|
+ * @param reqData 向wxpay post的请求数据
|
|
373
|
+ * @return API返回数据
|
|
374
|
+ * @throws Exception
|
|
375
|
+ */
|
|
376
|
+ public Map<String, String> orderQuery(Map<String, String> reqData) throws Exception {
|
|
377
|
+ return this.orderQuery(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
378
|
+ }
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+ /**
|
|
382
|
+ * 作用:查询订单<br>
|
|
383
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
384
|
+ * @param reqData 向wxpay post的请求数据 int
|
|
385
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
386
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
387
|
+ * @return API返回数据
|
|
388
|
+ * @throws Exception
|
|
389
|
+ */
|
|
390
|
+ public Map<String, String> orderQuery(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
391
|
+ String url;
|
|
392
|
+ if (this.useSandbox) {
|
|
393
|
+ url = WXPayConstants.SANDBOX_ORDERQUERY_URL_SUFFIX;
|
|
394
|
+ }
|
|
395
|
+ else {
|
|
396
|
+ url = WXPayConstants.ORDERQUERY_URL_SUFFIX;
|
|
397
|
+ }
|
|
398
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
399
|
+ return this.processResponseXml(respXml);
|
|
400
|
+ }
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+ /**
|
|
404
|
+ * 作用:撤销订单<br>
|
|
405
|
+ * 场景:刷卡支付
|
|
406
|
+ * @param reqData 向wxpay post的请求数据
|
|
407
|
+ * @return API返回数据
|
|
408
|
+ * @throws Exception
|
|
409
|
+ */
|
|
410
|
+ public Map<String, String> reverse(Map<String, String> reqData) throws Exception {
|
|
411
|
+ return this.reverse(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
412
|
+ }
|
|
413
|
+
|
|
414
|
+
|
|
415
|
+ /**
|
|
416
|
+ * 作用:撤销订单<br>
|
|
417
|
+ * 场景:刷卡支付<br>
|
|
418
|
+ * 其他:需要证书
|
|
419
|
+ * @param reqData 向wxpay post的请求数据
|
|
420
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
421
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
422
|
+ * @return API返回数据
|
|
423
|
+ * @throws Exception
|
|
424
|
+ */
|
|
425
|
+ public Map<String, String> reverse(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
426
|
+ String url;
|
|
427
|
+ if (this.useSandbox) {
|
|
428
|
+ url = WXPayConstants.SANDBOX_REVERSE_URL_SUFFIX;
|
|
429
|
+ }
|
|
430
|
+ else {
|
|
431
|
+ url = WXPayConstants.REVERSE_URL_SUFFIX;
|
|
432
|
+ }
|
|
433
|
+ String respXml = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
434
|
+ return this.processResponseXml(respXml);
|
|
435
|
+ }
|
|
436
|
+
|
|
437
|
+
|
|
438
|
+ /**
|
|
439
|
+ * 作用:关闭订单<br>
|
|
440
|
+ * 场景:公共号支付、扫码支付、APP支付
|
|
441
|
+ * @param reqData 向wxpay post的请求数据
|
|
442
|
+ * @return API返回数据
|
|
443
|
+ * @throws Exception
|
|
444
|
+ */
|
|
445
|
+ public Map<String, String> closeOrder(Map<String, String> reqData) throws Exception {
|
|
446
|
+ return this.closeOrder(reqData, config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
447
|
+ }
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+ /**
|
|
451
|
+ * 作用:关闭订单<br>
|
|
452
|
+ * 场景:公共号支付、扫码支付、APP支付
|
|
453
|
+ * @param reqData 向wxpay post的请求数据
|
|
454
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
455
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
456
|
+ * @return API返回数据
|
|
457
|
+ * @throws Exception
|
|
458
|
+ */
|
|
459
|
+ public Map<String, String> closeOrder(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
460
|
+ String url;
|
|
461
|
+ if (this.useSandbox) {
|
|
462
|
+ url = WXPayConstants.SANDBOX_CLOSEORDER_URL_SUFFIX;
|
|
463
|
+ }
|
|
464
|
+ else {
|
|
465
|
+ url = WXPayConstants.CLOSEORDER_URL_SUFFIX;
|
|
466
|
+ }
|
|
467
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
468
|
+ return this.processResponseXml(respXml);
|
|
469
|
+ }
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+ /**
|
|
473
|
+ * 作用:申请退款<br>
|
|
474
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
475
|
+ * @param reqData 向wxpay post的请求数据
|
|
476
|
+ * @return API返回数据
|
|
477
|
+ * @throws Exception
|
|
478
|
+ */
|
|
479
|
+ public Map<String, String> refund(Map<String, String> reqData) throws Exception {
|
|
480
|
+ return this.refund(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
481
|
+ }
|
|
482
|
+
|
|
483
|
+
|
|
484
|
+ /**
|
|
485
|
+ * 作用:申请退款<br>
|
|
486
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付<br>
|
|
487
|
+ * 其他:需要证书
|
|
488
|
+ * @param reqData 向wxpay post的请求数据
|
|
489
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
490
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
491
|
+ * @return API返回数据
|
|
492
|
+ * @throws Exception
|
|
493
|
+ */
|
|
494
|
+ public Map<String, String> refund(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
495
|
+ String url;
|
|
496
|
+ if (this.useSandbox) {
|
|
497
|
+ url = WXPayConstants.SANDBOX_REFUND_URL_SUFFIX;
|
|
498
|
+ }
|
|
499
|
+ else {
|
|
500
|
+ url = WXPayConstants.REFUND_URL_SUFFIX;
|
|
501
|
+ }
|
|
502
|
+ String respXml = this.requestWithCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
503
|
+ return this.processResponseXml(respXml);
|
|
504
|
+ }
|
|
505
|
+
|
|
506
|
+
|
|
507
|
+ /**
|
|
508
|
+ * 作用:退款查询<br>
|
|
509
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
510
|
+ * @param reqData 向wxpay post的请求数据
|
|
511
|
+ * @return API返回数据
|
|
512
|
+ * @throws Exception
|
|
513
|
+ */
|
|
514
|
+ public Map<String, String> refundQuery(Map<String, String> reqData) throws Exception {
|
|
515
|
+ return this.refundQuery(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
516
|
+ }
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+ /**
|
|
520
|
+ * 作用:退款查询<br>
|
|
521
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
522
|
+ * @param reqData 向wxpay post的请求数据
|
|
523
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
524
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
525
|
+ * @return API返回数据
|
|
526
|
+ * @throws Exception
|
|
527
|
+ */
|
|
528
|
+ public Map<String, String> refundQuery(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
529
|
+ String url;
|
|
530
|
+ if (this.useSandbox) {
|
|
531
|
+ url = WXPayConstants.SANDBOX_REFUNDQUERY_URL_SUFFIX;
|
|
532
|
+ }
|
|
533
|
+ else {
|
|
534
|
+ url = WXPayConstants.REFUNDQUERY_URL_SUFFIX;
|
|
535
|
+ }
|
|
536
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
537
|
+ return this.processResponseXml(respXml);
|
|
538
|
+ }
|
|
539
|
+
|
|
540
|
+
|
|
541
|
+ /**
|
|
542
|
+ * 作用:对账单下载(成功时返回对账单数据,失败时返回XML格式数据)<br>
|
|
543
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
544
|
+ * @param reqData 向wxpay post的请求数据
|
|
545
|
+ * @return API返回数据
|
|
546
|
+ * @throws Exception
|
|
547
|
+ */
|
|
548
|
+ public Map<String, String> downloadBill(Map<String, String> reqData) throws Exception {
|
|
549
|
+ return this.downloadBill(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
550
|
+ }
|
|
551
|
+
|
|
552
|
+
|
|
553
|
+ /**
|
|
554
|
+ * 作用:对账单下载<br>
|
|
555
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付<br>
|
|
556
|
+ * 其他:无论是否成功都返回Map。若成功,返回的Map中含有return_code、return_msg、data,
|
|
557
|
+ * 其中return_code为`SUCCESS`,data为对账单数据。
|
|
558
|
+ * @param reqData 向wxpay post的请求数据
|
|
559
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
560
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
561
|
+ * @return 经过封装的API返回数据
|
|
562
|
+ * @throws Exception
|
|
563
|
+ */
|
|
564
|
+ public Map<String, String> downloadBill(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
565
|
+ String url;
|
|
566
|
+ if (this.useSandbox) {
|
|
567
|
+ url = WXPayConstants.SANDBOX_DOWNLOADBILL_URL_SUFFIX;
|
|
568
|
+ }
|
|
569
|
+ else {
|
|
570
|
+ url = WXPayConstants.DOWNLOADBILL_URL_SUFFIX;
|
|
571
|
+ }
|
|
572
|
+ String respStr = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs).trim();
|
|
573
|
+ Map<String, String> ret;
|
|
574
|
+ // 出现错误,返回XML数据
|
|
575
|
+ if (respStr.indexOf("<") == 0) {
|
|
576
|
+ ret = WXPayUtil.xmlToMap(respStr);
|
|
577
|
+ }
|
|
578
|
+ else {
|
|
579
|
+ // 正常返回csv数据
|
|
580
|
+ ret = new HashMap<String, String>();
|
|
581
|
+ ret.put("return_code", WXPayConstants.SUCCESS);
|
|
582
|
+ ret.put("return_msg", "ok");
|
|
583
|
+ ret.put("data", respStr);
|
|
584
|
+ }
|
|
585
|
+ return ret;
|
|
586
|
+ }
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+ /**
|
|
590
|
+ * 作用:交易保障<br>
|
|
591
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
592
|
+ * @param reqData 向wxpay post的请求数据
|
|
593
|
+ * @return API返回数据
|
|
594
|
+ * @throws Exception
|
|
595
|
+ */
|
|
596
|
+ public Map<String, String> report(Map<String, String> reqData) throws Exception {
|
|
597
|
+ return this.report(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
598
|
+ }
|
|
599
|
+
|
|
600
|
+
|
|
601
|
+ /**
|
|
602
|
+ * 作用:交易保障<br>
|
|
603
|
+ * 场景:刷卡支付、公共号支付、扫码支付、APP支付
|
|
604
|
+ * @param reqData 向wxpay post的请求数据
|
|
605
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
606
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
607
|
+ * @return API返回数据
|
|
608
|
+ * @throws Exception
|
|
609
|
+ */
|
|
610
|
+ public Map<String, String> report(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
611
|
+ String url;
|
|
612
|
+ if (this.useSandbox) {
|
|
613
|
+ url = WXPayConstants.SANDBOX_REPORT_URL_SUFFIX;
|
|
614
|
+ }
|
|
615
|
+ else {
|
|
616
|
+ url = WXPayConstants.REPORT_URL_SUFFIX;
|
|
617
|
+ }
|
|
618
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
619
|
+ return WXPayUtil.xmlToMap(respXml);
|
|
620
|
+ }
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+ /**
|
|
624
|
+ * 作用:转换短链接<br>
|
|
625
|
+ * 场景:刷卡支付、扫码支付
|
|
626
|
+ * @param reqData 向wxpay post的请求数据
|
|
627
|
+ * @return API返回数据
|
|
628
|
+ * @throws Exception
|
|
629
|
+ */
|
|
630
|
+ public Map<String, String> shortUrl(Map<String, String> reqData) throws Exception {
|
|
631
|
+ return this.shortUrl(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
632
|
+ }
|
|
633
|
+
|
|
634
|
+
|
|
635
|
+ /**
|
|
636
|
+ * 作用:转换短链接<br>
|
|
637
|
+ * 场景:刷卡支付、扫码支付
|
|
638
|
+ * @param reqData 向wxpay post的请求数据
|
|
639
|
+ * @return API返回数据
|
|
640
|
+ * @throws Exception
|
|
641
|
+ */
|
|
642
|
+ public Map<String, String> shortUrl(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
643
|
+ String url;
|
|
644
|
+ if (this.useSandbox) {
|
|
645
|
+ url = WXPayConstants.SANDBOX_SHORTURL_URL_SUFFIX;
|
|
646
|
+ }
|
|
647
|
+ else {
|
|
648
|
+ url = WXPayConstants.SHORTURL_URL_SUFFIX;
|
|
649
|
+ }
|
|
650
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
651
|
+ return this.processResponseXml(respXml);
|
|
652
|
+ }
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+ /**
|
|
656
|
+ * 作用:授权码查询OPENID接口<br>
|
|
657
|
+ * 场景:刷卡支付
|
|
658
|
+ * @param reqData 向wxpay post的请求数据
|
|
659
|
+ * @return API返回数据
|
|
660
|
+ * @throws Exception
|
|
661
|
+ */
|
|
662
|
+ public Map<String, String> authCodeToOpenid(Map<String, String> reqData) throws Exception {
|
|
663
|
+ return this.authCodeToOpenid(reqData, this.config.getHttpConnectTimeoutMs(), this.config.getHttpReadTimeoutMs());
|
|
664
|
+ }
|
|
665
|
+
|
|
666
|
+
|
|
667
|
+ /**
|
|
668
|
+ * 作用:授权码查询OPENID接口<br>
|
|
669
|
+ * 场景:刷卡支付
|
|
670
|
+ * @param reqData 向wxpay post的请求数据
|
|
671
|
+ * @param connectTimeoutMs 连接超时时间,单位是毫秒
|
|
672
|
+ * @param readTimeoutMs 读超时时间,单位是毫秒
|
|
673
|
+ * @return API返回数据
|
|
674
|
+ * @throws Exception
|
|
675
|
+ */
|
|
676
|
+ public Map<String, String> authCodeToOpenid(Map<String, String> reqData, int connectTimeoutMs, int readTimeoutMs) throws Exception {
|
|
677
|
+ String url;
|
|
678
|
+ if (this.useSandbox) {
|
|
679
|
+ url = WXPayConstants.SANDBOX_AUTHCODETOOPENID_URL_SUFFIX;
|
|
680
|
+ }
|
|
681
|
+ else {
|
|
682
|
+ url = WXPayConstants.AUTHCODETOOPENID_URL_SUFFIX;
|
|
683
|
+ }
|
|
684
|
+ String respXml = this.requestWithoutCert(url, this.fillRequestData(reqData), connectTimeoutMs, readTimeoutMs);
|
|
685
|
+ return this.processResponseXml(respXml);
|
|
686
|
+ }
|
|
687
|
+
|
|
688
|
+
|
|
689
|
+} // end class
|