Skip to content

Commit 1bda3ee

Browse files
无处不在2015binarywang
无处不在2015
authored andcommitted
🎨 #2467 【微信支付】微信支付V3接口请求增加代理设置参数的支持
1 parent 7116fe5 commit 1bda3ee

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

Diff for: weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

+31-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
import org.apache.commons.io.IOUtils;
1010
import org.apache.commons.lang3.RegExUtils;
1111
import org.apache.commons.lang3.StringUtils;
12+
import org.apache.http.HttpHost;
13+
import org.apache.http.auth.AuthScope;
14+
import org.apache.http.auth.UsernamePasswordCredentials;
15+
import org.apache.http.client.CredentialsProvider;
16+
import org.apache.http.impl.client.BasicCredentialsProvider;
1217
import org.apache.http.impl.client.CloseableHttpClient;
18+
import org.apache.http.impl.client.HttpClientBuilder;
1319
import org.apache.http.ssl.SSLContexts;
1420

1521
import javax.net.ssl.SSLContext;
@@ -259,11 +265,15 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
259265
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
260266
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime());
261267

262-
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
268+
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
263269
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
264270
.withWechatpay(Collections.singletonList(certificate))
265-
.withValidator(new WxPayValidator(verifier))
266-
.build();
271+
.withValidator(new WxPayValidator(verifier));
272+
//初始化V3接口正向代理设置
273+
initHttpProxy(wxPayV3HttpClientBuilder);
274+
275+
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
276+
267277
this.apiV3HttpClient = httpClient;
268278
this.verifier=verifier;
269279
this.privateKey = merchantPrivateKey;
@@ -274,7 +284,25 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
274284
}
275285
}
276286

287+
/**
288+
* 配置 http 正向代理
289+
* 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
290+
* @param httpClientBuilder http构造参数
291+
*/
292+
private void initHttpProxy(HttpClientBuilder httpClientBuilder) {
293+
if (StringUtils.isNotBlank(this.getHttpProxyHost()) && this.getHttpProxyPort() > 0) {
294+
if (StringUtils.isEmpty(this.getHttpProxyUsername())) {
295+
this.setHttpProxyUsername("whatever");
296+
}
277297

298+
// 使用代理服务器 需要用户认证的代理服务器
299+
CredentialsProvider provider = new BasicCredentialsProvider();
300+
provider.setCredentials(new AuthScope(this.getHttpProxyHost(), this.getHttpProxyPort()),
301+
new UsernamePasswordCredentials(this.getHttpProxyUsername(), this.getHttpProxyPassword()));
302+
httpClientBuilder.setDefaultCredentialsProvider(provider);
303+
httpClientBuilder.setProxy(new HttpHost(this.getHttpProxyHost(), this.getHttpProxyPort()));
304+
}
305+
}
278306

279307
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
280308
InputStream inputStream;

Diff for: weixin-java-pay/src/test/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImplTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.github.binarywang.wxpay.bean.request.*;
1111
import com.github.binarywang.wxpay.bean.result.*;
1212
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
13+
import com.github.binarywang.wxpay.config.WxPayConfig;
1314
import com.github.binarywang.wxpay.constant.WxPayConstants.AccountType;
1415
import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
1516
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
@@ -775,4 +776,24 @@ public void testRefundQueryV3() throws WxPayException {
775776
System.out.println(GSON.toJson(result));
776777
}
777778

779+
/**
780+
* 测试包含正向代理的测试
781+
* @throws WxPayException
782+
*/
783+
@Test
784+
public void testQueryOrderV3WithProxy() {
785+
try {
786+
WxPayOrderQueryV3Request request = new WxPayOrderQueryV3Request();
787+
request.setOutTradeNo("n1ZvYqjAg3D3LUBa");
788+
WxPayConfig config = this.payService.getConfig();
789+
config.setPayBaseUrl("http://api.mch.weixin.qq.com");
790+
config.setHttpProxyHost("12.11.1.113");
791+
config.setHttpProxyPort(8015);
792+
WxPayOrderQueryV3Result result = this.payService.queryOrderV3(request);
793+
System.out.println(GSON.toJson(result));
794+
} catch (WxPayException e) {
795+
}
796+
797+
}
798+
778799
}

0 commit comments

Comments
 (0)