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.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.ResourceBundle; import java.util.ResourceBundle;
@ -16,6 +17,7 @@ import com.foxinmy.weixin4j.sign.WeixinSignature;
import com.foxinmy.weixin4j.type.IdQuery; import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.util.RandomUtil; import com.foxinmy.weixin4j.util.RandomUtil;
import com.foxinmy.weixin4j.util.StringUtil; 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; private final static ResourceBundle WEIXIN_BUNDLE;
static { static {
WEIXIN_BUNDLE = ResourceBundle WEIXIN_BUNDLE = ResourceBundle.getBundle("com/foxinmy/weixin4j/payment/weixin");
.getBundle("com/foxinmy/weixin4j/payment/weixin");
} }
protected final WeixinPayAccount weixinAccount; protected final WeixinPayAccount weixinAccount;
@ -41,8 +42,7 @@ public class MchApi extends BaseApi {
public MchApi(WeixinPayAccount weixinAccount) { public MchApi(WeixinPayAccount weixinAccount) {
this.weixinAccount = weixinAccount; this.weixinAccount = weixinAccount;
this.weixinSignature = new WeixinPaymentSignature( this.weixinSignature = new WeixinPaymentSignature(weixinAccount.getPaySignKey());
weixinAccount.getPaySignKey());
} }
@Override @Override
@ -91,22 +91,23 @@ public class MchApi extends BaseApi {
* *
* @return * @return
*/ */
protected WeixinRequestExecutor getWeixinSSLExecutor() protected WeixinRequestExecutor getWeixinSSLExecutor() throws WeixinException {
throws WeixinException {
if (weixinSSLExecutor == null) { if (weixinSSLExecutor == null) {
try { try {
File certificate = new File(weixinAccount.getCertificateFile()); InputStream is = null;
File certificate = new File(
Weixin4jConfigUtil.replaceClassPathValue(weixinAccount.getCertificateFile()));
if (!certificate.exists() || !certificate.isFile()) { if (!certificate.exists() || !certificate.isFile()) {
throw new WeixinException("Invalid certificate file : " is = Weixin4jConfigUtil.CLASSLOADER.getResourceAsStream(weixinAccount.getCertificateFile());
+ certificate.toString()); } else {
is = new FileInputStream(certificate);
} }
this.weixinSSLExecutor = weixinExecutor if (is == null) {
.createSSLRequestExecutor( throw new WeixinException("Invalid certificate file : " + certificate.toString());
weixinAccount.getCertificateKey(), }
new FileInputStream(certificate)); this.weixinSSLExecutor = weixinExecutor.createSSLRequestExecutor(weixinAccount.getCertificateKey(), is);
} catch (IOException e) { } catch (IOException e) {
throw new WeixinException( throw new WeixinException("IO Error on createSSLRequestExecutor", e);
"IO Error on createSSLRequestExecutor", e);
} }
} }
return this.weixinSSLExecutor; 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.JSONCreator;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
import com.foxinmy.weixin4j.util.StringUtil; import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/** /**
* 微信支付账户 * 微信支付账户
@ -79,10 +78,8 @@ public class WeixinPayAccount extends WeixinAccount {
* @param certificateFile * @param certificateFile
* 商户证书文件(默认加载classpath:ca.p12) * 商户证书文件(默认加载classpath:ca.p12)
*/ */
public WeixinPayAccount(String id, String paySignKey, String mchId, public WeixinPayAccount(String id, String paySignKey, String mchId, String certificateKey, String certificateFile) {
String certificateKey, String certificateFile) { this(id, null, paySignKey, mchId, certificateKey, certificateFile, null, null, null, null);
this(id, null, paySignKey, mchId, certificateKey, certificateFile,
null, null, null, null);
} }
/** /**
@ -110,16 +107,12 @@ public class WeixinPayAccount extends WeixinAccount {
* 微信支付分配的子商户号(非必填) * 微信支付分配的子商户号(非必填)
*/ */
@JSONCreator @JSONCreator
public WeixinPayAccount(@JSONField(name = "id") String id, public WeixinPayAccount(@JSONField(name = "id") String id, @JSONField(name = "secret") String secret,
@JSONField(name = "secret") String secret, @JSONField(name = "paySignKey") String paySignKey, @JSONField(name = "mchId") String mchId,
@JSONField(name = "paySignKey") String paySignKey,
@JSONField(name = "mchId") String mchId,
@JSONField(name = "certificateKey") String certificateKey, @JSONField(name = "certificateKey") String certificateKey,
@JSONField(name = "certificateFile") String certificateFile, @JSONField(name = "certificateFile") String certificateFile,
@JSONField(name = "deviceInfo") String deviceInfo, @JSONField(name = "deviceInfo") String deviceInfo, @JSONField(name = "partnerId") String partnerId,
@JSONField(name = "partnerId") String partnerId, @JSONField(name = "subId") String subId, @JSONField(name = "subMchId") String subMchId) {
@JSONField(name = "subId") String subId,
@JSONField(name = "subMchId") String subMchId) {
super(id, secret); super(id, secret);
this.paySignKey = paySignKey; this.paySignKey = paySignKey;
this.mchId = mchId; this.mchId = mchId;
@ -164,7 +157,7 @@ public class WeixinPayAccount extends WeixinAccount {
} }
public String getCertificateFile() { public String getCertificateFile() {
return Weixin4jConfigUtil.replaceClassPathValue(certificateFile); return certificateFile;
} }
public void setCertificateFile(String certificateFile) { public void setCertificateFile(String certificateFile) {
@ -189,10 +182,8 @@ public class WeixinPayAccount extends WeixinAccount {
@Override @Override
public String toString() { public String toString() {
return "WeixinPayAccount [" + super.toString() + ", paySignKey=" return "WeixinPayAccount [" + super.toString() + ", paySignKey=" + paySignKey + ", mchId=" + mchId
+ paySignKey + ", mchId=" + mchId + ", certificateKey=" + ", certificateKey=" + certificateKey + ",certificateFile =" + certificateFile + ", deviceInfo="
+ certificateKey + ",certificateFile =" + certificateFile + deviceInfo + ", partnerId=" + partnerId + ", subId=" + subId + ", subMchId=" + subMchId + "]";
+ ", deviceInfo=" + deviceInfo + ", partnerId=" + partnerId
+ ", subId=" + subId + ", subMchId=" + subMchId + "]";
} }
} }

View File

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