Skip to content

Commit 1fc0da0

Browse files
🆕 #2586 【微信支付】支付证书支持base64编码配置
1 parent 220e38d commit 1fc0da0

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

+23-4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.security.KeyStore;
2727
import java.security.PrivateKey;
2828
import java.security.cert.X509Certificate;
29+
import java.util.Base64;
2930
import java.util.Collections;
3031

3132
/**
@@ -101,15 +102,28 @@ public class WxPayConfig {
101102
*/
102103
private String signType;
103104
private SSLContext sslContext;
105+
/**
106+
* p12证书base64编码
107+
*/
108+
private String keyString;
104109
/**
105110
* p12证书文件的绝对路径或者以classpath:开头的类路径.
106111
*/
107112
private String keyPath;
108113

114+
/**
115+
* apiclient_key.pem证书base64编码
116+
*/
117+
private String privateKeyString;
109118
/**
110119
* apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径.
111120
*/
112121
private String privateKeyPath;
122+
123+
/**
124+
* apiclient_cert.pem证书base64编码
125+
*/
126+
private String privateCertString;
113127
/**
114128
* apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径.
115129
*/
@@ -222,7 +236,7 @@ public SSLContext initSSLContext() throws WxPayException {
222236
throw new WxPayException("请确保商户号mchId已设置");
223237
}
224238

225-
InputStream inputStream = this.loadConfigInputStream(this.getKeyPath(), this.keyContent, "p12证书");
239+
InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(), this.keyContent, "p12证书");
226240

227241
try {
228242
KeyStore keystore = KeyStore.getInstance("PKCS12");
@@ -245,16 +259,18 @@ public SSLContext initSSLContext() throws WxPayException {
245259
* @author doger.wang
246260
**/
247261
public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
262+
val privateKeyString = this.getPrivateKeyString();
248263
val privateKeyPath = this.getPrivateKeyPath();
264+
val privateCertString = this.getPrivateCertString();
249265
val privateCertPath = this.getPrivateCertPath();
250266
val serialNo = this.getCertSerialNo();
251267
val apiV3Key = this.getApiV3Key();
252268
if (StringUtils.isBlank(apiV3Key)) {
253269
throw new WxPayException("请确保apiV3Key值已设置");
254270
}
255271

256-
InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath, this.privateKeyContent, "privateKeyPath");
257-
InputStream certInputStream = this.loadConfigInputStream(privateCertPath, this.privateCertContent, "privateCertPath");
272+
InputStream keyInputStream = this.loadConfigInputStream(privateKeyString, privateKeyPath, this.privateKeyContent, "privateKeyPath");
273+
InputStream certInputStream = this.loadConfigInputStream(privateCertString, privateCertPath, this.privateCertContent, "privateCertPath");
258274
try {
259275
PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream);
260276
X509Certificate certificate = PemUtils.loadCertificate(certInputStream);
@@ -298,10 +314,13 @@ private WxPayHttpProxy getWxPayHttpProxy() {
298314
return null;
299315
}
300316

301-
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
317+
private InputStream loadConfigInputStream(String configString, String configPath, byte[] configContent, String fileName) throws WxPayException {
302318
InputStream inputStream;
303319
if (configContent != null) {
304320
inputStream = new ByteArrayInputStream(configContent);
321+
} else if(StringUtils.isNotEmpty(configString)) {
322+
configContent = Base64.getDecoder().decode(configString);
323+
inputStream = new ByteArrayInputStream(configContent);
305324
} else {
306325
if (StringUtils.isBlank(configPath)) {
307326
throw new WxPayException("请确保证书文件地址【" + fileName + "】或者内容已配置");

weixin-java-pay/src/test/java/com/github/binarywang/wxpay/config/WxPayConfigTest.java

+7
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ public void testInitSSLContext() throws Exception {
3737
public void testHashCode() {
3838
payConfig.hashCode();
3939
}
40+
41+
@Test
42+
public void testInitSSLContext_base64() throws Exception {
43+
payConfig.setMchId("123");
44+
payConfig.setKeyString("MIIKmgIBAzCCCmQGCS...");
45+
payConfig.initSSLContext();
46+
}
4047
}

0 commit comments

Comments
 (0)