diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index a9092e2fd..6756b0b4b 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -108,6 +108,16 @@ public class WxPayConfig { */ private String privateCertPath; + /** + * apiclient_key.pem证书文件内容的字节数组. + */ + private byte[] privateKeyContent; + + /** + * apiclient_cert.pem证书文件内容的字节数组. + */ + private byte[] privateCertContent; + /** * apiV3 秘钥值. */ @@ -205,15 +215,7 @@ public class WxPayConfig { throw new WxPayException("请确保商户号mchId已设置"); } - InputStream inputStream; - if (this.keyContent != null) { - inputStream = new ByteArrayInputStream(this.keyContent); - } else { - if (StringUtils.isBlank(this.getKeyPath())) { - throw new WxPayException("请确保证书文件地址keyPath已配置"); - } - inputStream = this.loadConfigInputStream(this.getKeyPath()); - } + InputStream inputStream = this.loadConfigInputStream(this.getKeyPath(), this.keyContent, "p12证书"); try { KeyStore keystore = KeyStore.getInstance("PKCS12"); @@ -240,21 +242,12 @@ public class WxPayConfig { val privateCertPath = this.getPrivateCertPath(); val serialNo = this.getCertSerialNo(); val apiV3Key = this.getApiV3Key(); - if (StringUtils.isBlank(privateKeyPath)) { - throw new WxPayException("请确保privateKeyPath已设置"); - } - if (StringUtils.isBlank(privateCertPath)) { - throw new WxPayException("请确保privateCertPath已设置"); - } -// if (StringUtils.isBlank(certSerialNo)) { -// throw new WxPayException("请确保certSerialNo证书序列号已设置"); -// } if (StringUtils.isBlank(apiV3Key)) { throw new WxPayException("请确保apiV3Key值已设置"); } - InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath); - InputStream certInputStream = this.loadConfigInputStream(privateCertPath); + InputStream keyInputStream = this.loadConfigInputStream(privateKeyPath, this.privateKeyContent, "privateKeyPath"); + InputStream certInputStream = this.loadConfigInputStream(privateCertPath, this.privateCertContent, "privateCertPath"); try { PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); X509Certificate certificate = PemUtils.loadCertificate(certInputStream); @@ -281,6 +274,22 @@ public class WxPayConfig { } } + + + private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException { + InputStream inputStream; + if (configContent != null) { + inputStream = new ByteArrayInputStream(configContent); + } else { + if (StringUtils.isBlank(configPath)) { + throw new WxPayException("请确保证书文件地址【" + fileName + "】或者内容已配置"); + } + inputStream = this.loadConfigInputStream(configPath); + } + return inputStream; + } + + /** * 从配置路径 加载配置 信息(支持 classpath、本地路径、网络url) * @param configPath 配置路径