certificate file bug fixed

This commit is contained in:
jinyu 2017-07-11 17:13:07 +08:00
parent c439e38915
commit bee75e55cf
3 changed files with 330 additions and 340 deletions

View File

@ -3,6 +3,7 @@ package com.foxinmy.weixin4j.api;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
@ -16,6 +17,7 @@ import com.foxinmy.weixin4j.sign.WeixinSignature;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 商户支付
@ -31,8 +33,7 @@ public class MchApi extends BaseApi {
private final static ResourceBundle WEIXIN_BUNDLE;
static {
WEIXIN_BUNDLE = ResourceBundle
.getBundle("com/foxinmy/weixin4j/payment/weixin");
WEIXIN_BUNDLE = ResourceBundle.getBundle("com/foxinmy/weixin4j/payment/weixin");
}
protected final WeixinPayAccount weixinAccount;
@ -41,8 +42,7 @@ public class MchApi extends BaseApi {
public MchApi(WeixinPayAccount weixinAccount) {
this.weixinAccount = weixinAccount;
this.weixinSignature = new WeixinPaymentSignature(
weixinAccount.getPaySignKey());
this.weixinSignature = new WeixinPaymentSignature(weixinAccount.getPaySignKey());
}
@Override
@ -91,22 +91,23 @@ public class MchApi extends BaseApi {
*
* @return
*/
protected WeixinRequestExecutor getWeixinSSLExecutor()
throws WeixinException {
protected WeixinRequestExecutor getWeixinSSLExecutor() throws WeixinException {
if (weixinSSLExecutor == null) {
try {
File certificate = new File(weixinAccount.getCertificateFile());
InputStream is = null;
File certificate = new File(
Weixin4jConfigUtil.replaceClassPathValue(weixinAccount.getCertificateFile()));
if (!certificate.exists() || !certificate.isFile()) {
throw new WeixinException("Invalid certificate file : "
+ certificate.toString());
is = Weixin4jConfigUtil.CLASSLOADER.getResourceAsStream(weixinAccount.getCertificateFile());
} else {
is = new FileInputStream(certificate);
}
this.weixinSSLExecutor = weixinExecutor
.createSSLRequestExecutor(
weixinAccount.getCertificateKey(),
new FileInputStream(certificate));
if (is == null) {
throw new WeixinException("Invalid certificate file : " + certificate.toString());
}
this.weixinSSLExecutor = weixinExecutor.createSSLRequestExecutor(weixinAccount.getCertificateKey(), is);
} catch (IOException e) {
throw new WeixinException(
"IO Error on createSSLRequestExecutor", e);
throw new WeixinException("IO Error on createSSLRequestExecutor", e);
}
}
return this.weixinSSLExecutor;

View File

@ -3,7 +3,6 @@ package com.foxinmy.weixin4j.model;
import com.alibaba.fastjson.annotation.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 微信支付账户
@ -79,10 +78,8 @@ public class WeixinPayAccount extends WeixinAccount {
* @param certificateFile
* 商户证书文件(默认加载classpath:ca.p12)
*/
public WeixinPayAccount(String id, String paySignKey, String mchId,
String certificateKey, String certificateFile) {
this(id, null, paySignKey, mchId, certificateKey, certificateFile,
null, null, null, null);
public WeixinPayAccount(String id, String paySignKey, String mchId, String certificateKey, String certificateFile) {
this(id, null, paySignKey, mchId, certificateKey, certificateFile, null, null, null, null);
}
/**
@ -110,16 +107,12 @@ public class WeixinPayAccount extends WeixinAccount {
* 微信支付分配的子商户号(非必填)
*/
@JSONCreator
public WeixinPayAccount(@JSONField(name = "id") String id,
@JSONField(name = "secret") String secret,
@JSONField(name = "paySignKey") String paySignKey,
@JSONField(name = "mchId") String mchId,
public WeixinPayAccount(@JSONField(name = "id") String id, @JSONField(name = "secret") String secret,
@JSONField(name = "paySignKey") String paySignKey, @JSONField(name = "mchId") String mchId,
@JSONField(name = "certificateKey") String certificateKey,
@JSONField(name = "certificateFile") String certificateFile,
@JSONField(name = "deviceInfo") String deviceInfo,
@JSONField(name = "partnerId") String partnerId,
@JSONField(name = "subId") String subId,
@JSONField(name = "subMchId") String subMchId) {
@JSONField(name = "deviceInfo") String deviceInfo, @JSONField(name = "partnerId") String partnerId,
@JSONField(name = "subId") String subId, @JSONField(name = "subMchId") String subMchId) {
super(id, secret);
this.paySignKey = paySignKey;
this.mchId = mchId;
@ -164,7 +157,7 @@ public class WeixinPayAccount extends WeixinAccount {
}
public String getCertificateFile() {
return Weixin4jConfigUtil.replaceClassPathValue(certificateFile);
return certificateFile;
}
public void setCertificateFile(String certificateFile) {
@ -189,10 +182,8 @@ public class WeixinPayAccount extends WeixinAccount {
@Override
public String toString() {
return "WeixinPayAccount [" + super.toString() + ", paySignKey="
+ paySignKey + ", mchId=" + mchId + ", certificateKey="
+ certificateKey + ",certificateFile =" + certificateFile
+ ", deviceInfo=" + deviceInfo + ", partnerId=" + partnerId
+ ", subId=" + subId + ", subMchId=" + subMchId + "]";
return "WeixinPayAccount [" + super.toString() + ", paySignKey=" + paySignKey + ", mchId=" + mchId
+ ", certificateKey=" + certificateKey + ",certificateFile =" + certificateFile + ", deviceInfo="
+ deviceInfo + ", partnerId=" + partnerId + ", subId=" + subId + ", subMchId=" + subMchId + "]";
}
}

View File

@ -18,10 +18,11 @@ import com.foxinmy.weixin4j.model.WeixinAccount;
public class Weixin4jConfigUtil {
private final static String CLASSPATH_PREFIX = "classpath:";
private final static String CLASSPATH_VALUE;
public final static ClassLoader CLASSLOADER;
private static ResourceBundle weixinBundle;
static {
CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader()
.getResource("").getPath();
CLASSLOADER = Thread.currentThread().getContextClassLoader();
CLASSPATH_VALUE = CLASSLOADER.getResource("").getPath();
try {
weixinBundle = ResourceBundle.getBundle(Consts.WEIXIN4J);
} catch (MissingResourceException e) {
@ -103,14 +104,11 @@ public class Weixin4jConfigUtil {
public static WeixinAccount getWeixinAccount() {
WeixinAccount account = null;
try {
account = JSON
.parseObject(getValue("account"), WeixinAccount.class);
account = JSON.parseObject(getValue("account"), WeixinAccount.class);
} catch (NullPointerException e) {
System.err
.println("'weixin4j.account' key not found in weixin4j.properties.");
System.err.println("'weixin4j.account' key not found in weixin4j.properties.");
} catch (MissingResourceException e) {
System.err
.println("'weixin4j.account' key not found in weixin4j.properties.");
System.err.println("'weixin4j.account' key not found in weixin4j.properties.");
}
return account;
}