🎨 #2467 【微信支付】微信支付V3接口请求增加代理设置参数的支持

This commit is contained in:
wuchubuzai2018
2021-12-28 17:59:38 +08:00
committed by GitHub
parent 739c222586
commit 7101c22899
4 changed files with 165 additions and 19 deletions

View File

@@ -1,6 +1,7 @@
package com.github.binarywang.wxpay.config;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.util.HttpProxyUtils;
import com.github.binarywang.wxpay.util.ResourcesUtils;
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
import com.github.binarywang.wxpay.v3.auth.*;
@@ -260,17 +261,19 @@ public class WxPayConfig {
if(StringUtils.isBlank(serialNo)){
this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
}
//构造Http Proxy正向代理
WxPayHttpProxy wxPayHttpProxy = getWxPayHttpProxy();
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime());
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(),wxPayHttpProxy);
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
.withWechatpay(Collections.singletonList(certificate))
.withValidator(new WxPayValidator(verifier));
//初始化V3接口正向代理设置
initHttpProxy(wxPayV3HttpClientBuilder);
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,wxPayHttpProxy);
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
@@ -285,23 +288,14 @@ public class WxPayConfig {
}
/**
* 配置 http 正向代理
* 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
* @param httpClientBuilder http构造参数
* 初始化一个WxPayHttpProxy对象
* @return 返回封装的WxPayHttpProxy对象。如未指定代理主机和端口则默认返回null
*/
private void initHttpProxy(HttpClientBuilder httpClientBuilder) {
private WxPayHttpProxy getWxPayHttpProxy() {
if (StringUtils.isNotBlank(this.getHttpProxyHost()) && this.getHttpProxyPort() > 0) {
if (StringUtils.isEmpty(this.getHttpProxyUsername())) {
this.setHttpProxyUsername("whatever");
}
// 使用代理服务器 需要用户认证的代理服务器
CredentialsProvider provider = new BasicCredentialsProvider();
provider.setCredentials(new AuthScope(this.getHttpProxyHost(), this.getHttpProxyPort()),
new UsernamePasswordCredentials(this.getHttpProxyUsername(), this.getHttpProxyPassword()));
httpClientBuilder.setDefaultCredentialsProvider(provider);
httpClientBuilder.setProxy(new HttpHost(this.getHttpProxyHost(), this.getHttpProxyPort()));
return new WxPayHttpProxy(getHttpProxyHost(), getHttpProxyPort(), getHttpProxyUsername(), getHttpProxyPassword());
}
return null;
}
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {

View File

@@ -0,0 +1,75 @@
package com.github.binarywang.wxpay.config;
import java.io.Serializable;
/**
* 微信支付 HTTP Proxy 正向代理配置
*
* @author Long Yu
* @date 2021-12-28 15:49:03
*/
public class WxPayHttpProxy implements Serializable {
/**
* 代理主机
*/
private String httpProxyHost;
/**
* 代理端口
*/
private Integer httpProxyPort;
/**
* 代理用户名称
*/
private String httpProxyUsername;
/**
* 代理密码
*/
private String httpProxyPassword;
public WxPayHttpProxy() {
}
public WxPayHttpProxy(String httpProxyHost, Integer httpProxyPort, String httpProxyUsername, String httpProxyPassword) {
this.httpProxyHost = httpProxyHost;
this.httpProxyPort = httpProxyPort;
this.httpProxyUsername = httpProxyUsername;
this.httpProxyPassword = httpProxyPassword;
}
public String getHttpProxyHost() {
return httpProxyHost;
}
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
public Integer getHttpProxyPort() {
return httpProxyPort;
}
public void setHttpProxyPort(Integer httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
public String getHttpProxyUsername() {
return httpProxyUsername;
}
public void setHttpProxyUsername(String httpProxyUsername) {
this.httpProxyUsername = httpProxyUsername;
}
public String getHttpProxyPassword() {
return httpProxyPassword;
}
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
}