26
26
import java .security .KeyStore ;
27
27
import java .security .PrivateKey ;
28
28
import java .security .cert .X509Certificate ;
29
+ import java .util .Base64 ;
29
30
import java .util .Collections ;
30
31
31
32
/**
@@ -101,15 +102,28 @@ public class WxPayConfig {
101
102
*/
102
103
private String signType ;
103
104
private SSLContext sslContext ;
105
+ /**
106
+ * p12证书base64编码
107
+ */
108
+ private String keyString ;
104
109
/**
105
110
* p12证书文件的绝对路径或者以classpath:开头的类路径.
106
111
*/
107
112
private String keyPath ;
108
113
114
+ /**
115
+ * apiclient_key.pem证书base64编码
116
+ */
117
+ private String privateKeyString ;
109
118
/**
110
119
* apiclient_key.pem证书文件的绝对路径或者以classpath:开头的类路径.
111
120
*/
112
121
private String privateKeyPath ;
122
+
123
+ /**
124
+ * apiclient_cert.pem证书base64编码
125
+ */
126
+ private String privateCertString ;
113
127
/**
114
128
* apiclient_cert.pem证书文件的绝对路径或者以classpath:开头的类路径.
115
129
*/
@@ -222,7 +236,7 @@ public SSLContext initSSLContext() throws WxPayException {
222
236
throw new WxPayException ("请确保商户号mchId已设置" );
223
237
}
224
238
225
- InputStream inputStream = this .loadConfigInputStream (this .getKeyPath (), this .keyContent , "p12证书" );
239
+ InputStream inputStream = this .loadConfigInputStream (this .keyString , this . getKeyPath (), this .keyContent , "p12证书" );
226
240
227
241
try {
228
242
KeyStore keystore = KeyStore .getInstance ("PKCS12" );
@@ -245,16 +259,18 @@ public SSLContext initSSLContext() throws WxPayException {
245
259
* @author doger.wang
246
260
**/
247
261
public CloseableHttpClient initApiV3HttpClient () throws WxPayException {
262
+ val privateKeyString = this .getPrivateKeyString ();
248
263
val privateKeyPath = this .getPrivateKeyPath ();
264
+ val privateCertString = this .getPrivateCertString ();
249
265
val privateCertPath = this .getPrivateCertPath ();
250
266
val serialNo = this .getCertSerialNo ();
251
267
val apiV3Key = this .getApiV3Key ();
252
268
if (StringUtils .isBlank (apiV3Key )) {
253
269
throw new WxPayException ("请确保apiV3Key值已设置" );
254
270
}
255
271
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" );
258
274
try {
259
275
PrivateKey merchantPrivateKey = PemUtils .loadPrivateKey (keyInputStream );
260
276
X509Certificate certificate = PemUtils .loadCertificate (certInputStream );
@@ -298,10 +314,13 @@ private WxPayHttpProxy getWxPayHttpProxy() {
298
314
return null ;
299
315
}
300
316
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 {
302
318
InputStream inputStream ;
303
319
if (configContent != null ) {
304
320
inputStream = new ByteArrayInputStream (configContent );
321
+ } else if (StringUtils .isNotEmpty (configString )) {
322
+ configContent = Base64 .getDecoder ().decode (configString );
323
+ inputStream = new ByteArrayInputStream (configContent );
305
324
} else {
306
325
if (StringUtils .isBlank (configPath )) {
307
326
throw new WxPayException ("请确保证书文件地址【" + fileName + "】或者内容已配置" );
0 commit comments