Skip to content

Commit

Permalink
🆕 Wechat-Group#2586 【微信支付】支付证书支持base64编码配置
Browse files Browse the repository at this point in the history
  • Loading branch information
jianchengwang committed Apr 24, 2022
1 parent 9e0a5bc commit 21a9aa4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.Collections;

/**
Expand Down Expand Up @@ -101,15 +102,28 @@ public class WxPayConfig {
*/
private String signType;
private SSLContext sslContext;
/**
* p12证书base64编码
*/
private String keyString;
/**
* p12证书文件的绝对路径或者以classpath:开头的类路径.
*/
private String keyPath;

/**
* apiclient_key.pem证书base64编码
*/
private String privateKeyString;
/**
* apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径.
*/
private String privateKeyPath;

/**
* apiclient_cert.pem证书base64编码
*/
private String privateCertString;
/**
* apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径.
*/
Expand Down Expand Up @@ -222,7 +236,7 @@ public SSLContext initSSLContext() throws WxPayException {
throw new WxPayException("请确保商户号mchId已设置");
}

InputStream inputStream = this.loadConfigInputStream(this.getKeyPath(), this.keyContent, "p12证书");
InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(), this.keyContent, "p12证书");

try {
KeyStore keystore = KeyStore.getInstance("PKCS12");
Expand All @@ -245,16 +259,18 @@ public SSLContext initSSLContext() throws WxPayException {
* @author doger.wang
**/
public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
val privateKeyString = this.getPrivateKeyString();
val privateKeyPath = this.getPrivateKeyPath();
val privateCertString = this.getPrivateCertString();
val privateCertPath = this.getPrivateCertPath();
val serialNo = this.getCertSerialNo();
val apiV3Key = this.getApiV3Key();
if (StringUtils.isBlank(apiV3Key)) {
throw new WxPayException("请确保apiV3Key值已设置");
}

InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath, this.privateKeyContent, "privateKeyPath");
InputStream certInputStream = this.loadConfigInputStream(privateCertPath, this.privateCertContent, "privateCertPath");
InputStream keyInputStream = this.loadConfigInputStream(privateKeyString, privateKeyPath, this.privateKeyContent, "privateKeyPath");
InputStream certInputStream = this.loadConfigInputStream(privateCertString, privateCertPath, this.privateCertContent, "privateCertPath");
try {
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
X509Certificate certificate = PemUtils.loadCertificate(certInputStream);
Expand Down Expand Up @@ -298,10 +314,13 @@ private WxPayHttpProxy getWxPayHttpProxy() {
return null;
}

private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
private InputStream loadConfigInputStream(String configString, String configPath, byte[] configContent, String fileName) throws WxPayException {
InputStream inputStream;
if (configContent != null) {
inputStream = new ByteArrayInputStream(configContent);
} else if(StringUtils.isNotEmpty(configString)) {
configContent = Base64.getDecoder().decode(configString);
inputStream = new ByteArrayInputStream(configContent);
} else {
if (StringUtils.isBlank(configPath)) {
throw new WxPayException("请确保证书文件地址【" + fileName + "】或者内容已配置");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ public void testInitSSLContext() throws Exception {
public void testHashCode() {
payConfig.hashCode();
}

@Test
public void testInitSSLContext_base64() throws Exception {
payConfig.setMchId("123");
payConfig.setKeyString("MIIKmgIBAzCCCmQGCS...");
payConfig.initSSLContext();
}
}

0 comments on commit 21a9aa4

Please sign in to comment.