Skip to content

Commit d88a8d3

Browse files
committed
抽取获取代理对象的方法到接口中,方便微信支付调用,并优化部分代码
1 parent 1ceb4bf commit d88a8d3

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.chanjar.weixin.common.util.http.RequestExecutor;
66
import me.chanjar.weixin.mp.bean.*;
77
import me.chanjar.weixin.mp.bean.result.*;
8+
import org.apache.http.HttpHost;
89

910
/**
1011
* 微信API的Service
@@ -228,6 +229,11 @@ public interface WxMpService {
228229
*/
229230
<T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException;
230231

232+
/**
233+
* 获取代理对象
234+
*/
235+
HttpHost getHttpProxy();
236+
231237
/**
232238
* 注入 {@link WxMpConfigStorage} 的实现
233239
*/

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import org.apache.commons.codec.digest.DigestUtils;
1515
import org.apache.commons.lang3.ArrayUtils;
1616
import org.apache.commons.lang3.StringUtils;
17+
import org.apache.http.Consts;
18+
import org.apache.http.client.config.RequestConfig;
1719
import org.apache.http.client.methods.CloseableHttpResponse;
1820
import org.apache.http.client.methods.HttpPost;
1921
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
@@ -27,6 +29,7 @@
2729
import javax.net.ssl.SSLContext;
2830
import java.io.File;
2931
import java.io.FileInputStream;
32+
import java.io.IOException;
3033
import java.security.KeyStore;
3134
import java.util.*;
3235

@@ -199,7 +202,7 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
199202

200203
String url = PAY_BASE_URL + "/pay/orderquery";
201204

202-
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
205+
String responseContent = this.executeRequest(url, xstream.toXML(request));
203206
WxPayOrderQueryResult result = (WxPayOrderQueryResult) xstream.fromXML(responseContent);
204207
result.composeCoupons(responseContent);
205208
if ("FAIL".equals(result.getResultCode())) {
@@ -233,7 +236,7 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxErrorExcepti
233236

234237
String url = PAY_BASE_URL + "/pay/closeorder";
235238

236-
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
239+
String responseContent = this.executeRequest(url, xstream.toXML(request));
237240
WxPayOrderCloseResult result = (WxPayOrderCloseResult) xstream.fromXML(responseContent);
238241
if ("FAIL".equals(result.getResultCode())) {
239242
throw new WxErrorException(WxError.newBuilder()
@@ -263,7 +266,7 @@ public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request)
263266

264267
String url = PAY_BASE_URL + "/pay/unifiedorder";
265268

266-
String responseContent = this.wxMpService.post(url, xstream.toXML(request));
269+
String responseContent = this.executeRequest(url, xstream.toXML(request));
267270
WxPayUnifiedOrderResult result = (WxPayUnifiedOrderResult) xstream
268271
.fromXML(responseContent);
269272
if ("FAIL".equals(result.getResultCode())) {
@@ -377,7 +380,27 @@ public WxEntPayQueryResult queryEntPay(String partnerTradeNo, File keyFile) thro
377380
return result;
378381
}
379382

383+
private String executeRequest( String url, String requestStr) throws WxErrorException {
384+
HttpPost httpPost = new HttpPost(url);
385+
if (this.wxMpService.getHttpProxy() != null) {
386+
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
387+
}
388+
389+
try (CloseableHttpClient httpclient = HttpClients.custom().build()) {
390+
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
391+
392+
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
393+
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
394+
}
395+
} catch (IOException e) {
396+
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);
397+
}finally {
398+
httpPost.releaseConnection();
399+
}
400+
}
401+
380402
private String executeRequestWithKeyFile( String url, File keyFile, String requestStr, String mchId) throws WxErrorException {
403+
381404
try (FileInputStream inputStream = new FileInputStream(keyFile)) {
382405
KeyStore keyStore = KeyStore.getInstance("PKCS12");
383406
keyStore.load(inputStream, mchId.toCharArray());
@@ -386,13 +409,18 @@ private String executeRequestWithKeyFile( String url, File keyFile, String reque
386409
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
387410
new DefaultHostnameVerifier());
388411

412+
HttpPost httpPost = new HttpPost(url);
413+
if (this.wxMpService.getHttpProxy() != null) {
414+
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
415+
}
416+
389417
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
390-
HttpPost httpPost = new HttpPost(url);
391418
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
392-
393419
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
394-
return EntityUtils.toString(response.getEntity());
420+
return EntityUtils.toString(response.getEntity(), Consts.UTF_8);
395421
}
422+
}finally {
423+
httpPost.releaseConnection();
396424
}
397425
} catch (Exception e) {
398426
throw new WxErrorException(WxError.newBuilder().setErrorMsg(e.getMessage()).build(), e);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ protected synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor,
425425
}
426426
}
427427

428+
@Override
428429
public HttpHost getHttpProxy() {
429430
return this.httpProxy;
430431
}

0 commit comments

Comments
 (0)