@@ -115,8 +115,6 @@ public class WxPayConfig {
115
115
* apiV3 证书序列号值
116
116
*/
117
117
private String certSerialNo ;
118
-
119
-
120
118
/**
121
119
* 微信支付分serviceId
122
120
*/
@@ -128,8 +126,10 @@ public class WxPayConfig {
128
126
private String payScoreNotifyUrl ;
129
127
130
128
private CloseableHttpClient apiV3HttpClient ;
131
-
132
-
129
+ /**
130
+ * 私钥信息
131
+ */
132
+ private PrivateKey privateKey ;
133
133
/**
134
134
* p12证书文件内容的字节数组.
135
135
*/
@@ -197,44 +197,7 @@ public SSLContext initSSLContext() throws WxPayException {
197
197
if (StringUtils .isBlank (this .getKeyPath ())) {
198
198
throw new WxPayException ("请确保证书文件地址keyPath已配置" );
199
199
}
200
-
201
- final String prefix = "classpath:" ;
202
- String fileHasProblemMsg = String .format (PROBLEM_MSG , this .getKeyPath ());
203
- String fileNotFoundMsg = String .format (NOT_FOUND_MSG , this .getKeyPath ());
204
- if (this .getKeyPath ().startsWith (prefix )) {
205
- String path = RegExUtils .removeFirst (this .getKeyPath (), prefix );
206
- if (!path .startsWith ("/" )) {
207
- path = "/" + path ;
208
- }
209
- try {
210
- inputStream = ResourcesUtil .getResourceAsStream (path );
211
- if (inputStream == null ) {
212
- throw new WxPayException (fileNotFoundMsg );
213
- }
214
- } catch (Exception e ) {
215
- throw new WxPayException (fileNotFoundMsg , e );
216
- }
217
- } else if (this .getKeyPath ().startsWith ("http://" ) || this .getKeyPath ().startsWith ("https://" )) {
218
- try {
219
- inputStream = new URL (this .keyPath ).openStream ();
220
- if (inputStream == null ) {
221
- throw new WxPayException (fileNotFoundMsg );
222
- }
223
- } catch (IOException e ) {
224
- throw new WxPayException (fileNotFoundMsg , e );
225
- }
226
- } else {
227
- try {
228
- File file = new File (this .getKeyPath ());
229
- if (!file .exists ()) {
230
- throw new WxPayException (fileNotFoundMsg );
231
- }
232
-
233
- inputStream = new FileInputStream (file );
234
- } catch (IOException e ) {
235
- throw new WxPayException (fileHasProblemMsg , e );
236
- }
237
- }
200
+ inputStream = this .loadConfigInputStream (this .getKeyPath ());
238
201
}
239
202
240
203
try {
@@ -275,39 +238,8 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
275
238
throw new WxPayException ("请确保apiV3Key值已设置" );
276
239
}
277
240
278
- InputStream keyInputStream = null ;
279
- InputStream certInputStream = null ;
280
- final String prefix = "classpath:" ;
281
- if (privateKeyPath .startsWith (prefix )) {
282
- String keypath = RegExUtils .removeFirst (privateKeyPath , prefix );
283
- if (!keypath .startsWith ("/" )) {
284
- keypath = "/" + keypath ;
285
- }
286
- try {
287
- keyInputStream = ResourcesUtil .getResourceAsStream (keypath );
288
- if (keyInputStream == null ) {
289
- throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateKeyPath ()));
290
- }
291
- } catch (Exception e ) {
292
- throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateKeyPath ()), e );
293
- }
294
- }
295
-
296
- if (privateCertPath .startsWith (prefix )) {
297
- String certpath = RegExUtils .removeFirst (privateCertPath , prefix );
298
- if (!certpath .startsWith ("/" )) {
299
- certpath = "/" + certpath ;
300
- }
301
- try {
302
- certInputStream = ResourcesUtil .getResourceAsStream (certpath );
303
- if (certInputStream == null ) {
304
- throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateCertPath ()));
305
- }
306
- } catch (Exception e ) {
307
- throw new WxPayException (String .format (NOT_FOUND_MSG , this .getPrivateCertPath ()), e );
308
- }
309
- }
310
-
241
+ InputStream keyInputStream = this .loadConfigInputStream (privateKeyPath );
242
+ InputStream certInputStream = this .loadConfigInputStream (privateCertPath );
311
243
try {
312
244
PrivateKey merchantPrivateKey = PemUtils .loadPrivateKey (keyInputStream );
313
245
@@ -323,10 +255,59 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
323
255
.build ();
324
256
this .apiV3HttpClient = httpClient ;
325
257
this .verifier =verifier ;
258
+ this .privateKey = merchantPrivateKey ;
326
259
327
260
return httpClient ;
328
261
} catch (Exception e ) {
329
262
throw new WxPayException ("v3请求构造异常!" , e );
330
263
}
331
264
}
265
+
266
+ /**
267
+ * 从配置路径 加载配置 信息(支持 classpath、本地路径、网络url)
268
+ * @param configPath 配置路径
269
+ * @return
270
+ * @throws WxPayException
271
+ */
272
+ private InputStream loadConfigInputStream (String configPath ) throws WxPayException {
273
+ InputStream inputStream ;
274
+ final String prefix = "classpath:" ;
275
+ String fileHasProblemMsg = String .format (PROBLEM_MSG , configPath );
276
+ String fileNotFoundMsg = String .format (NOT_FOUND_MSG , configPath );
277
+ if (configPath .startsWith (prefix )) {
278
+ String path = RegExUtils .removeFirst (configPath , prefix );
279
+ if (!path .startsWith ("/" )) {
280
+ path = "/" + path ;
281
+ }
282
+ try {
283
+ inputStream = ResourcesUtil .getResourceAsStream (path );
284
+ if (inputStream == null ) {
285
+ throw new WxPayException (fileNotFoundMsg );
286
+ }
287
+ } catch (Exception e ) {
288
+ throw new WxPayException (fileNotFoundMsg , e );
289
+ }
290
+ } else if (configPath .startsWith ("http://" ) || configPath .startsWith ("https://" )) {
291
+ try {
292
+ inputStream = new URL (configPath ).openStream ();
293
+ if (inputStream == null ) {
294
+ throw new WxPayException (fileNotFoundMsg );
295
+ }
296
+ } catch (IOException e ) {
297
+ throw new WxPayException (fileNotFoundMsg , e );
298
+ }
299
+ } else {
300
+ try {
301
+ File file = new File (configPath );
302
+ if (!file .exists ()) {
303
+ throw new WxPayException (fileNotFoundMsg );
304
+ }
305
+
306
+ inputStream = new FileInputStream (file );
307
+ } catch (IOException e ) {
308
+ throw new WxPayException (fileHasProblemMsg , e );
309
+ }
310
+ }
311
+ return inputStream ;
312
+ }
332
313
}
0 commit comments