Skip to content

Commit dae8e40

Browse files
committed
重构已有支付代码
1 parent c538f69 commit dae8e40

File tree

2 files changed

+51
-132
lines changed

2 files changed

+51
-132
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,25 @@ public interface WxMpPayService {
3030
* @param tradeType 交易类型 JSAPI,NATIVE,APP,WAP
3131
* @param ip 发起支付的客户端IP
3232
* @param notifyUrl 通知地址
33+
* @throws WxErrorException
3334
* @deprecated Use me.chanjar.weixin.mp.api.WxMpService.getPrepayId(Map<String, String>) instead
3435
*/
3536
@Deprecated
36-
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt, String body, String tradeType, String ip, String notifyUrl);
37+
WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo, double amt,
38+
String body, String tradeType, String ip, String notifyUrl)
39+
throws WxErrorException;
3740

3841
/**
3942
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
4043
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
4144
*
4245
* @param parameters All required/optional parameters for weixin payment
46+
* @throws WxErrorException
4347
* @deprecated use me.chanjar.weixin.mp.api.WxMpPayService.unifiedOrder(WxUnifiedOrderRequest) instead
4448
*/
4549
@Deprecated
46-
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters);
50+
WxMpPrepayIdResult getPrepayId(Map<String, String> parameters)
51+
throws WxErrorException;
4752

4853
/**
4954
* 统一下单(详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
@@ -98,9 +103,11 @@ WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
98103
/**
99104
* 该接口提供所有微信支付订单的查询,当支付通知处理异常戒丢失的情冴,商户可以通过该接口查询订单支付状态。
100105
* 详见http://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
106+
* @throws WxErrorException
101107
*
102108
*/
103-
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo);
109+
WxMpPayResult getJSSDKPayResult(String transactionId, String outTradeNo)
110+
throws WxErrorException;
104111

105112
/**
106113
* 读取支付结果通知

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 41 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package me.chanjar.weixin.mp.api.impl;
22

3-
import java.io.IOException;
43
import java.lang.reflect.Field;
54
import java.util.HashMap;
65
import java.util.List;
@@ -10,16 +9,9 @@
109
import java.util.TreeMap;
1110

1211
import org.apache.commons.codec.digest.DigestUtils;
13-
import org.apache.http.Consts;
14-
import org.apache.http.HttpHost;
15-
import org.apache.http.client.config.RequestConfig;
16-
import org.apache.http.client.methods.CloseableHttpResponse;
17-
import org.apache.http.client.methods.HttpPost;
18-
import org.apache.http.entity.StringEntity;
1912
import org.joor.Reflect;
2013
import org.slf4j.Logger;
2114
import org.slf4j.LoggerFactory;
22-
import org.slf4j.helpers.MessageFormatter;
2315

2416
import com.google.common.collect.Lists;
2517
import com.google.common.collect.Maps;
@@ -29,9 +21,9 @@
2921
import me.chanjar.weixin.common.annotation.Required;
3022
import me.chanjar.weixin.common.bean.result.WxError;
3123
import me.chanjar.weixin.common.exception.WxErrorException;
32-
import me.chanjar.weixin.common.util.http.Utf8ResponseHandler;
3324
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
3425
import me.chanjar.weixin.mp.api.WxMpPayService;
26+
import me.chanjar.weixin.mp.api.WxMpService;
3527
import me.chanjar.weixin.mp.bean.pay.WxMpPayCallback;
3628
import me.chanjar.weixin.mp.bean.pay.WxMpPayRefundResult;
3729
import me.chanjar.weixin.mp.bean.pay.WxMpPayResult;
@@ -54,19 +46,17 @@ public class WxMpPayServiceImpl implements WxMpPayService {
5446
private final String[] REQUIRED_ORDER_PARAMETERS = new String[] { "appid",
5547
"mch_id", "body", "out_trade_no", "total_fee", "spbill_create_ip",
5648
"notify_url", "trade_type" };
57-
private HttpHost httpProxy;
58-
private WxMpServiceImpl wxMpService;
49+
private WxMpService wxMpService;
5950

60-
public WxMpPayServiceImpl(WxMpServiceImpl wxMpService) {
51+
public WxMpPayServiceImpl(WxMpService wxMpService) {
6152
this.wxMpService = wxMpService;
62-
this.httpProxy = wxMpService.getHttpProxy();
6353
}
6454

6555
@Override
6656
@Deprecated
6757
public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo,
6858
double amt, String body, String tradeType, String ip,
69-
String callbackUrl) {
59+
String callbackUrl) throws WxErrorException {
7060
Map<String, String> packageParams = new HashMap<>();
7161
packageParams.put("appid",
7262
this.wxMpService.getWxMpConfigStorage().getAppId());
@@ -85,7 +75,8 @@ public WxMpPrepayIdResult getPrepayId(String openId, String outTradeNo,
8575

8676
@Override
8777
@Deprecated
88-
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
78+
public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters)
79+
throws WxErrorException {
8980
final SortedMap<String, String> packageParams = new TreeMap<>(parameters);
9081
packageParams.put("appid",
9182
this.wxMpService.getWxMpConfigStorage().getAppId());
@@ -106,29 +97,11 @@ public WxMpPrepayIdResult getPrepayId(final Map<String, String> parameters) {
10697

10798
request.append("</xml>");
10899

109-
HttpPost httpPost = new HttpPost(
110-
"https://api.mch.weixin.qq.com/pay/unifiedorder");
111-
if (this.httpProxy != null) {
112-
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
113-
.build();
114-
httpPost.setConfig(config);
115-
}
116-
117-
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
118-
httpPost.setEntity(entity);
119-
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
120-
.execute(httpPost)) {
121-
String responseContent = Utf8ResponseHandler.INSTANCE
122-
.handleResponse(response);
123-
XStream xstream = XStreamInitializer.getInstance();
124-
xstream.alias("xml", WxMpPrepayIdResult.class);
125-
return (WxMpPrepayIdResult) xstream.fromXML(responseContent);
126-
} catch (IOException e) {
127-
throw new RuntimeException("Failed to get prepay id due to IO exception.",
128-
e);
129-
} finally {
130-
httpPost.releaseConnection();
131-
}
100+
String url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
101+
String responseContent = this.wxMpService.post(url, request.toString());
102+
XStream xstream = XStreamInitializer.getInstance();
103+
xstream.alias("xml", WxMpPrepayIdResult.class);
104+
return (WxMpPrepayIdResult) xstream.fromXML(responseContent);
132105
}
133106

134107
private void checkParameters(Map<String, String> parameters) {
@@ -238,7 +211,7 @@ public Map<String, String> getPayInfo(Map<String, String> parameters)
238211

239212
@Override
240213
public WxMpPayResult getJSSDKPayResult(String transactionId,
241-
String outTradeNo) {
214+
String outTradeNo) throws WxErrorException {
242215
String nonce_str = System.currentTimeMillis() + "";
243216

244217
SortedMap<String, String> packageParams = new TreeMap<>();
@@ -267,27 +240,11 @@ public WxMpPayResult getJSSDKPayResult(String transactionId,
267240
}
268241
request.append("</xml>");
269242

270-
HttpPost httpPost = new HttpPost(
271-
"https://api.mch.weixin.qq.com/pay/orderquery");
272-
if (this.httpProxy != null) {
273-
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
274-
.build();
275-
httpPost.setConfig(config);
276-
}
277-
278-
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
279-
httpPost.setEntity(entity);
280-
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
281-
.execute(httpPost)) {
282-
String responseContent = Utf8ResponseHandler.INSTANCE
283-
.handleResponse(response);
284-
XStream xstream = XStreamInitializer.getInstance();
285-
xstream.alias("xml", WxMpPayResult.class);
286-
return (WxMpPayResult) xstream.fromXML(responseContent);
287-
} catch (IOException e) {
288-
throw new RuntimeException("Failed to query order due to IO exception.",
289-
e);
290-
}
243+
String url = "https://api.mch.weixin.qq.com/pay/orderquery";
244+
String responseContent = this.wxMpService.post(url, request.toString());
245+
XStream xstream = XStreamInitializer.getInstance();
246+
xstream.alias("xml", WxMpPayResult.class);
247+
return (WxMpPayResult) xstream.fromXML(responseContent);
291248
}
292249

293250
@Override
@@ -325,49 +282,26 @@ public WxMpPayRefundResult refundPay(Map<String, String> parameters)
325282
}
326283
request.append("</xml>");
327284

328-
HttpPost httpPost = new HttpPost(
329-
"https://api.mch.weixin.qq.com/secapi/pay/refund");
330-
if (this.httpProxy != null) {
331-
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
332-
.build();
333-
httpPost.setConfig(config);
334-
}
335-
336-
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
337-
httpPost.setEntity(entity);
338-
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
339-
.execute(httpPost)) {
340-
String responseContent = Utf8ResponseHandler.INSTANCE
341-
.handleResponse(response);
342-
XStream xstream = XStreamInitializer.getInstance();
343-
xstream.processAnnotations(WxMpPayRefundResult.class);
344-
WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream
345-
.fromXML(responseContent);
346-
347-
if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode())
348-
|| !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) {
349-
WxError error = new WxError();
350-
error.setErrorCode(-1);
351-
error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode()
352-
+ ";return_msg:" + wxMpPayRefundResult.getReturnMsg()
353-
+ ";result_code:" + wxMpPayRefundResult.getResultCode()
354-
+ ";err_code" + wxMpPayRefundResult.getErrCode() + ";err_code_des"
355-
+ wxMpPayRefundResult.getErrCodeDes());
356-
throw new WxErrorException(error);
357-
}
285+
String url = "https://api.mch.weixin.qq.com/secapi/pay/refund";
286+
String responseContent = this.wxMpService.post(url, request.toString());
287+
XStream xstream = XStreamInitializer.getInstance();
288+
xstream.processAnnotations(WxMpPayRefundResult.class);
289+
WxMpPayRefundResult wxMpPayRefundResult = (WxMpPayRefundResult) xstream
290+
.fromXML(responseContent);
358291

359-
return wxMpPayRefundResult;
360-
} catch (IOException e) {
361-
String message = MessageFormatter
362-
.format("Exception happened when sending refund '{}'.",
363-
request.toString())
364-
.getMessage();
365-
this.log.error(message, e);
366-
throw new WxErrorException(
367-
WxError.newBuilder().setErrorMsg(message).build());
368-
} finally {
369-
httpPost.releaseConnection();
292+
if (!"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getResultCode())
293+
|| !"SUCCESS".equalsIgnoreCase(wxMpPayRefundResult.getReturnCode())) {
294+
WxError error = new WxError();
295+
error.setErrorCode(-1);
296+
error.setErrorMsg("return_code:" + wxMpPayRefundResult.getReturnCode()
297+
+ ";return_msg:" + wxMpPayRefundResult.getReturnMsg()
298+
+ ";result_code:" + wxMpPayRefundResult.getResultCode() + ";err_code"
299+
+ wxMpPayRefundResult.getErrCode() + ";err_code_des"
300+
+ wxMpPayRefundResult.getErrCodeDes());
301+
throw new WxErrorException(error);
370302
}
303+
304+
return wxMpPayRefundResult;
371305
}
372306

373307
@Override
@@ -400,34 +334,12 @@ public WxRedpackResult sendRedpack(Map<String, String> parameters)
400334

401335
request.append("</xml>");
402336

403-
HttpPost httpPost = new HttpPost(
404-
"https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack");
405-
if (this.httpProxy != null) {
406-
RequestConfig config = RequestConfig.custom().setProxy(this.httpProxy)
407-
.build();
408-
httpPost.setConfig(config);
409-
}
337+
String url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
410338

411-
StringEntity entity = new StringEntity(request.toString(), Consts.UTF_8);
412-
httpPost.setEntity(entity);
413-
try (CloseableHttpResponse response = this.wxMpService.getHttpclient()
414-
.execute(httpPost)) {
415-
String responseContent = Utf8ResponseHandler.INSTANCE
416-
.handleResponse(response);
417-
XStream xstream = XStreamInitializer.getInstance();
418-
xstream.processAnnotations(WxRedpackResult.class);
419-
return (WxRedpackResult) xstream.fromXML(responseContent);
420-
} catch (IOException e) {
421-
String message = MessageFormatter
422-
.format("Exception occured when sending redpack '{}'.",
423-
request.toString())
424-
.getMessage();
425-
this.log.error(message, e);
426-
throw new WxErrorException(
427-
WxError.newBuilder().setErrorMsg(message).build());
428-
} finally {
429-
httpPost.releaseConnection();
430-
}
339+
String responseContent = this.wxMpService.post(url, request.toString());
340+
XStream xstream = XStreamInitializer.getInstance();
341+
xstream.processAnnotations(WxRedpackResult.class);
342+
return (WxRedpackResult) xstream.fromXML(responseContent);
431343
}
432344

433345
@Override
@@ -544,7 +456,7 @@ public WxUnifiedOrderResult unifiedOrder(WxUnifiedOrderRequest request)
544456

545457
private void checkParameters(WxUnifiedOrderRequest request) {
546458

547-
List<String> nullFields = com.google.common.collect.Lists.newArrayList();
459+
List<String> nullFields = Lists.newArrayList();
548460
for (Entry<String, Reflect> entry : Reflect.on(request).fields()
549461
.entrySet()) {
550462
Reflect reflect = entry.getValue();

0 commit comments

Comments
 (0)