🎨 微信支付分相关接口代码重构规范化

This commit is contained in:
Binary Wang
2020-05-19 21:28:26 +08:00
parent 8709a9c5a7
commit 39cea92171
17 changed files with 587 additions and 546 deletions

View File

@@ -9,6 +9,7 @@ import com.github.binarywang.wxpay.v3.auth.WechatPay2Validator;
import com.github.binarywang.wxpay.v3.util.PemUtils;
import lombok.Data;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.ssl.SSLContexts;
@@ -16,6 +17,7 @@ import org.apache.http.ssl.SSLContexts;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
@@ -107,7 +109,7 @@ public class WxPayConfig {
/**
* apiV3 秘钥值.
*/
private String apiv3Key;
private String apiV3Key;
/**
* apiV3 证书序列号值
@@ -125,7 +127,7 @@ public class WxPayConfig {
*/
private String payScoreNotifyUrl;
private CloseableHttpClient apiv3HttpClient;
private CloseableHttpClient apiV3HttpClient;
/**
@@ -151,6 +153,7 @@ public class WxPayConfig {
/**
* 返回所设置的微信支付接口请求地址域名.
*
* @return 微信支付接口请求地址域名
*/
public String getPayBaseUrl() {
@@ -184,10 +187,11 @@ public class WxPayConfig {
String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!";
String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!";
if (this.getKeyPath().startsWith(prefix)) {
String path = StringUtils.removeFirst(this.getKeyPath(), prefix);
String path = RegExUtils.removeFirst(this.getKeyPath(), prefix);
if (!path.startsWith("/")) {
path = "/" + path;
}
inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(path);
if (inputStream == null) {
throw new WxPayException(fileNotFoundMsg);
@@ -230,18 +234,17 @@ public class WxPayConfig {
/**
* @Author doger.wang
* @Description 初始化api v3请求头 自动签名验签
* 初始化api v3请求头 自动签名验签
* 方法参照微信官方https://github.com/wechatpay-apiv3/wechatpay-apache-httpclient
* @Date 2020/5/14 10:10
* @Param []
*
* @return org.apache.http.impl.client.CloseableHttpClient
* @author doger.wang
**/
public CloseableHttpClient initApiV3HttpClient()throws WxPayException {
public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
String privateKeyPath = this.getPrivateKeyPath();
String privateCertPath = this.getPrivateCertPath();
String certSerialNo = this.getCertSerialNo();
String apiv3Key = this.getApiv3Key();
String apiV3Key = this.getApiV3Key();
if (StringUtils.isBlank(privateKeyPath)) {
throw new WxPayException("请确保privateKeyPath已设置");
}
@@ -251,55 +254,55 @@ public class WxPayConfig {
if (StringUtils.isBlank(certSerialNo)) {
throw new WxPayException("请确保certSerialNo证书序列号已设置");
}
if (StringUtils.isBlank(apiv3Key)) {
throw new WxPayException("请确保apiv3Key值已设置");
if (StringUtils.isBlank(apiV3Key)) {
throw new WxPayException("请确保apiV3Key值已设置");
}
InputStream keyinputStream=null;
InputStream certinputStream=null;
InputStream keyInputStream = null;
InputStream certInputStream = null;
final String prefix = "classpath:";
if (privateKeyPath.startsWith(prefix)) {
String keypath = StringUtils.removeFirst(privateKeyPath, prefix);
String keypath = RegExUtils.removeFirst(privateKeyPath, prefix);
if (!keypath.startsWith("/")) {
keypath = "/" + keypath;
}
keyinputStream = WxPayConfig.class.getResourceAsStream(keypath);
if (keyinputStream == null) {
keyInputStream = WxPayConfig.class.getResourceAsStream(keypath);
if (keyInputStream == null) {
throw new WxPayException("证书文件【" + this.getPrivateKeyPath() + "】不存在,请核实!");
}
}
if (privateCertPath.startsWith(prefix)) {
String certpath = StringUtils.removeFirst(privateCertPath, prefix);
if (!certpath.startsWith("/")) {
certpath = "/" + certpath;
}
certinputStream = WxPayConfig.class.getResourceAsStream(certpath);
if (certinputStream == null) {
throw new WxPayException("证书文件【" + this.getPrivateCertPath() + "】不存在,请核实!");
}
if (privateCertPath.startsWith(prefix)) {
String certpath = RegExUtils.removeFirst(privateCertPath, prefix);
if (!certpath.startsWith("/")) {
certpath = "/" + certpath;
}
certInputStream = WxPayConfig.class.getResourceAsStream(certpath);
if (certInputStream == null) {
throw new WxPayException("证书文件【" + this.getPrivateCertPath() + "】不存在,请核实!");
}
CloseableHttpClient httpClient = null;
try {
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create();
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyinputStream);
X509Certificate x509Certificate = PemUtils.loadCertificate(certinputStream);
ArrayList<X509Certificate> certificates = new ArrayList<>();
certificates.add(x509Certificate);
builder.withMerchant(mchId, certSerialNo, merchantPrivateKey);
builder.withWechatpay(certificates);
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
apiv3Key.getBytes("utf-8"));
builder.withValidator(new WechatPay2Validator(verifier));
httpClient = builder.build();
this.apiv3HttpClient =httpClient;
} catch (Exception e) {
throw new WxPayException("v3请求构造异常", e);
}
return httpClient;
}
CloseableHttpClient httpClient;
try {
WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create();
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
X509Certificate x509Certificate = PemUtils.loadCertificate(certInputStream);
ArrayList<X509Certificate> certificates = new ArrayList<>();
certificates.add(x509Certificate);
builder.withMerchant(mchId, certSerialNo, merchantPrivateKey);
builder.withWechatpay(certificates);
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
new WechatPay2Credentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
apiV3Key.getBytes(StandardCharsets.UTF_8));
builder.withValidator(new WechatPay2Validator(verifier));
httpClient = builder.build();
this.apiV3HttpClient = httpClient;
} catch (Exception e) {
throw new WxPayException("v3请求构造异常", e);
}
return httpClient;
}
}