diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java index 86a878fc..505a668f 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/api/MchApi.java @@ -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; /** * 商户支付 @@ -28,101 +30,100 @@ import com.foxinmy.weixin4j.util.StringUtil; */ public class MchApi extends BaseApi { - private final static ResourceBundle WEIXIN_BUNDLE; + private final static ResourceBundle WEIXIN_BUNDLE; - static { - WEIXIN_BUNDLE = ResourceBundle - .getBundle("com/foxinmy/weixin4j/payment/weixin"); - } + static { + WEIXIN_BUNDLE = ResourceBundle.getBundle("com/foxinmy/weixin4j/payment/weixin"); + } - protected final WeixinPayAccount weixinAccount; - protected final WeixinSignature weixinSignature; - private volatile WeixinRequestExecutor weixinSSLExecutor; + protected final WeixinPayAccount weixinAccount; + protected final WeixinSignature weixinSignature; + private volatile WeixinRequestExecutor weixinSSLExecutor; - public MchApi(WeixinPayAccount weixinAccount) { - this.weixinAccount = weixinAccount; - this.weixinSignature = new WeixinPaymentSignature( - weixinAccount.getPaySignKey()); - } + public MchApi(WeixinPayAccount weixinAccount) { + this.weixinAccount = weixinAccount; + this.weixinSignature = new WeixinPaymentSignature(weixinAccount.getPaySignKey()); + } - @Override - protected ResourceBundle weixinBundle() { - return WEIXIN_BUNDLE; - } + @Override + protected ResourceBundle weixinBundle() { + return WEIXIN_BUNDLE; + } - /** - * 支付接口请求基本数据 - * - * @param idQuery - * ID信息 可为空 - * @return 基础map - */ - protected Map createBaseRequestMap(IdQuery idQuery) { - Map map = new HashMap(); - map.put("appid", weixinAccount.getId()); - map.put("mch_id", weixinAccount.getMchId()); - map.put("nonce_str", RandomUtil.generateString(16)); - if (StringUtil.isNotBlank(weixinAccount.getDeviceInfo())) { - map.put("device_info", weixinAccount.getDeviceInfo()); - } - if (StringUtil.isNotBlank(weixinAccount.getSubId())) { - map.put("sub_appid", weixinAccount.getSubId()); - } - if (StringUtil.isNotBlank(weixinAccount.getSubMchId())) { - map.put("sub_mch_id", weixinAccount.getSubMchId()); - } - if (idQuery != null) { - map.put(idQuery.getType().getName(), idQuery.getId()); - } - return map; - } + /** + * 支付接口请求基本数据 + * + * @param idQuery + * ID信息 可为空 + * @return 基础map + */ + protected Map createBaseRequestMap(IdQuery idQuery) { + Map map = new HashMap(); + map.put("appid", weixinAccount.getId()); + map.put("mch_id", weixinAccount.getMchId()); + map.put("nonce_str", RandomUtil.generateString(16)); + if (StringUtil.isNotBlank(weixinAccount.getDeviceInfo())) { + map.put("device_info", weixinAccount.getDeviceInfo()); + } + if (StringUtil.isNotBlank(weixinAccount.getSubId())) { + map.put("sub_appid", weixinAccount.getSubId()); + } + if (StringUtil.isNotBlank(weixinAccount.getSubMchId())) { + map.put("sub_mch_id", weixinAccount.getSubMchId()); + } + if (idQuery != null) { + map.put(idQuery.getType().getName(), idQuery.getId()); + } + return map; + } - /** - * 微信签名类 - * - * @return - */ - public WeixinSignature getWeixinSignature() { - return this.weixinSignature; - } + /** + * 微信签名类 + * + * @return + */ + public WeixinSignature getWeixinSignature() { + return this.weixinSignature; + } - /** - * 微信SSL - * - * @return - */ - protected WeixinRequestExecutor getWeixinSSLExecutor() - throws WeixinException { - if (weixinSSLExecutor == null) { - try { - File certificate = new File(weixinAccount.getCertificateFile()); - if (!certificate.exists() || !certificate.isFile()) { - throw new WeixinException("Invalid certificate file : " - + certificate.toString()); - } - this.weixinSSLExecutor = weixinExecutor - .createSSLRequestExecutor( - weixinAccount.getCertificateKey(), - new FileInputStream(certificate)); - } catch (IOException e) { - throw new WeixinException( - "IO Error on createSSLRequestExecutor", e); - } - } - return this.weixinSSLExecutor; - } + /** + * 微信SSL + * + * @return + */ + protected WeixinRequestExecutor getWeixinSSLExecutor() throws WeixinException { + if (weixinSSLExecutor == null) { + try { + InputStream is = null; + File certificate = new File( + Weixin4jConfigUtil.replaceClassPathValue(weixinAccount.getCertificateFile())); + if (!certificate.exists() || !certificate.isFile()) { + is = Weixin4jConfigUtil.CLASSLOADER.getResourceAsStream(weixinAccount.getCertificateFile()); + } else { + is = 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); + } + } + return this.weixinSSLExecutor; + } - /** - * 设置商户信息 - * - * @param merchant - */ - protected void declareMerchant(T merchant) { - merchant.setAppId(weixinAccount.getId()); - merchant.setMchId(weixinAccount.getMchId()); - merchant.setDeviceInfo(weixinAccount.getDeviceInfo()); - merchant.setSubAppId(weixinAccount.getSubId()); - merchant.setSubMchId(weixinAccount.getSubMchId()); - merchant.setNonceStr(RandomUtil.generateString(16)); - } + /** + * 设置商户信息 + * + * @param merchant + */ + protected void declareMerchant(T merchant) { + merchant.setAppId(weixinAccount.getId()); + merchant.setMchId(weixinAccount.getMchId()); + merchant.setDeviceInfo(weixinAccount.getDeviceInfo()); + merchant.setSubAppId(weixinAccount.getSubId()); + merchant.setSubMchId(weixinAccount.getSubMchId()); + merchant.setNonceStr(RandomUtil.generateString(16)); + } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/WeixinPayAccount.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/WeixinPayAccount.java index 2f2d0a7b..46a713a4 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/WeixinPayAccount.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/model/WeixinPayAccount.java @@ -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; /** * 微信支付账户 @@ -16,183 +15,175 @@ import com.foxinmy.weixin4j.util.Weixin4jConfigUtil; */ public class WeixinPayAccount extends WeixinAccount { - private static final long serialVersionUID = -2791256176906048632L; - /** - * 公众号支付请求中用于加密的密钥 - */ - private final String paySignKey; - /** - * 微信支付分配的商户号 - */ - private final String mchId; - /** - * 加载支付证书文件的密码(默认为商户号) - */ - private String certificateKey; - /** - * 商户证书文件(默认加载classpath:ca.p12) - */ - private String certificateFile; - /** - * 微信支付分配的设备号 - */ - private String deviceInfo; - /** - * 财付通商户身份的标识 - */ - private String partnerId; + private static final long serialVersionUID = -2791256176906048632L; + /** + * 公众号支付请求中用于加密的密钥 + */ + private final String paySignKey; + /** + * 微信支付分配的商户号 + */ + private final String mchId; + /** + * 加载支付证书文件的密码(默认为商户号) + */ + private String certificateKey; + /** + * 商户证书文件(默认加载classpath:ca.p12) + */ + private String certificateFile; + /** + * 微信支付分配的设备号 + */ + private String deviceInfo; + /** + * 财付通商户身份的标识 + */ + private String partnerId; - /** - * 微信分配的子商户公众账号ID - */ - private String subId; - /** - * 微信支付分配的子商户号 - */ - private String subMchId; + /** + * 微信分配的子商户公众账号ID + */ + private String subId; + /** + * 微信支付分配的子商户号 + */ + private String subMchId; - /** - * 支付商户信息 - * - * @param id - * 公众号唯一的身份ID(必填) - * @param paySignKey - * 支付密钥字符串(必填) - * @param mchId - * 微信支付分配的商户号(必填) - */ - public WeixinPayAccount(String id, String paySignKey, String mchId) { - this(id, paySignKey, mchId, mchId, "classpath:ca.p12"); - } + /** + * 支付商户信息 + * + * @param id + * 公众号唯一的身份ID(必填) + * @param paySignKey + * 支付密钥字符串(必填) + * @param mchId + * 微信支付分配的商户号(必填) + */ + public WeixinPayAccount(String id, String paySignKey, String mchId) { + this(id, paySignKey, mchId, mchId, "classpath:ca.p12"); + } - /** - * 支付商户信息 - * - * @param id - * 公众号唯一的身份ID(必填) - * @param paySignKey - * 支付密钥字符串(必填) - * @param mchId - * 微信支付分配的商户号(必填) - * @param certificateKey - * 加载支付证书文件的密码(默认为商户号) - * @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); - } + /** + * 支付商户信息 + * + * @param id + * 公众号唯一的身份ID(必填) + * @param paySignKey + * 支付密钥字符串(必填) + * @param mchId + * 微信支付分配的商户号(必填) + * @param certificateKey + * 加载支付证书文件的密码(默认为商户号) + * @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); + } - /** - * 支付商户信息 - * - * @param id - * 公众号唯一的身份ID(必填) - * @param secret - * 公众号调用接口的凭证(最好填写) - * @param paySignKey - * 支付密钥字符串(必填) - * @param mchId - * 微信支付分配的商户号(必填) - * @param certificateKey - * 加载支付证书文件的密码(默认为商户号) - * @param certificateFile - * 商户证书文件(默认加载classpath:ca.p12) - * @param deviceInfo - * 微信支付分配的设备号(非必填) - * @param partnerId - * 财付通的商户号(非必填) - * @param subId - * 微信分配的子商户公众账号ID(非必填) - * @param subMchId - * 微信支付分配的子商户号(非必填) - */ - @JSONCreator - 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) { - super(id, secret); - this.paySignKey = paySignKey; - this.mchId = mchId; - this.certificateKey = certificateKey; - this.certificateFile = certificateFile; - this.deviceInfo = deviceInfo; - this.partnerId = partnerId; - this.subId = subId; - this.subMchId = subMchId; - } + /** + * 支付商户信息 + * + * @param id + * 公众号唯一的身份ID(必填) + * @param secret + * 公众号调用接口的凭证(最好填写) + * @param paySignKey + * 支付密钥字符串(必填) + * @param mchId + * 微信支付分配的商户号(必填) + * @param certificateKey + * 加载支付证书文件的密码(默认为商户号) + * @param certificateFile + * 商户证书文件(默认加载classpath:ca.p12) + * @param deviceInfo + * 微信支付分配的设备号(非必填) + * @param partnerId + * 财付通的商户号(非必填) + * @param subId + * 微信分配的子商户公众账号ID(非必填) + * @param subMchId + * 微信支付分配的子商户号(非必填) + */ + @JSONCreator + 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) { + super(id, secret); + this.paySignKey = paySignKey; + this.mchId = mchId; + this.certificateKey = certificateKey; + this.certificateFile = certificateFile; + this.deviceInfo = deviceInfo; + this.partnerId = partnerId; + this.subId = subId; + this.subMchId = subMchId; + } - public String getPaySignKey() { - return paySignKey; - } + public String getPaySignKey() { + return paySignKey; + } - public String getMchId() { - return mchId; - } + public String getMchId() { + return mchId; + } - public String getDeviceInfo() { - return deviceInfo; - } + public String getDeviceInfo() { + return deviceInfo; + } - public String getCertificateKey() { - return StringUtil.isBlank(certificateKey) ? mchId : certificateKey; - } + public String getCertificateKey() { + return StringUtil.isBlank(certificateKey) ? mchId : certificateKey; + } - public String getPartnerId() { - return partnerId; - } + public String getPartnerId() { + return partnerId; + } - public String getSubId() { - return subId; - } + public String getSubId() { + return subId; + } - public String getSubMchId() { - return subMchId; - } + public String getSubMchId() { + return subMchId; + } - public void setCertificateKey(String certificateKey) { - this.certificateKey = certificateKey; - } + public void setCertificateKey(String certificateKey) { + this.certificateKey = certificateKey; + } - public String getCertificateFile() { - return Weixin4jConfigUtil.replaceClassPathValue(certificateFile); - } + public String getCertificateFile() { + return certificateFile; + } - public void setCertificateFile(String certificateFile) { - this.certificateFile = certificateFile; - } + public void setCertificateFile(String certificateFile) { + this.certificateFile = certificateFile; + } - public void setDeviceInfo(String deviceInfo) { - this.deviceInfo = deviceInfo; - } + public void setDeviceInfo(String deviceInfo) { + this.deviceInfo = deviceInfo; + } - public void setPartnerId(String partnerId) { - this.partnerId = partnerId; - } + public void setPartnerId(String partnerId) { + this.partnerId = partnerId; + } - public void setSubId(String subId) { - this.subId = subId; - } + public void setSubId(String subId) { + this.subId = subId; + } - public void setSubMchId(String subMchId) { - this.subMchId = subMchId; - } + public void setSubMchId(String subMchId) { + this.subMchId = subMchId; + } - @Override - public String toString() { - return "WeixinPayAccount [" + super.toString() + ", paySignKey=" - + paySignKey + ", mchId=" + mchId + ", certificateKey=" - + certificateKey + ",certificateFile =" + certificateFile - + ", deviceInfo=" + deviceInfo + ", partnerId=" + partnerId - + ", subId=" + subId + ", subMchId=" + subMchId + "]"; - } + @Override + public String toString() { + return "WeixinPayAccount [" + super.toString() + ", paySignKey=" + paySignKey + ", mchId=" + mchId + + ", certificateKey=" + certificateKey + ",certificateFile =" + certificateFile + ", deviceInfo=" + + deviceInfo + ", partnerId=" + partnerId + ", subId=" + subId + ", subMchId=" + subMchId + "]"; + } } diff --git a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java index 91cc81da..ea3d502e 100644 --- a/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java +++ b/weixin4j-base/src/main/java/com/foxinmy/weixin4j/util/Weixin4jConfigUtil.java @@ -16,102 +16,100 @@ import com.foxinmy.weixin4j.model.WeixinAccount; * @see */ public class Weixin4jConfigUtil { - private final static String CLASSPATH_PREFIX = "classpath:"; - private final static String CLASSPATH_VALUE; - private static ResourceBundle weixinBundle; - static { - CLASSPATH_VALUE = Thread.currentThread().getContextClassLoader() - .getResource("").getPath(); - try { - weixinBundle = ResourceBundle.getBundle(Consts.WEIXIN4J); - } catch (MissingResourceException e) { - ; - } - } + private final static String CLASSPATH_PREFIX = "classpath:"; + private final static String CLASSPATH_VALUE; + public final static ClassLoader CLASSLOADER; + private static ResourceBundle weixinBundle; + static { + CLASSLOADER = Thread.currentThread().getContextClassLoader(); + CLASSPATH_VALUE = CLASSLOADER.getResource("").getPath(); + try { + weixinBundle = ResourceBundle.getBundle(Consts.WEIXIN4J); + } catch (MissingResourceException e) { + ; + } + } - private final static String WEIXIN4J_PREFIX = "weixin4j"; + private final static String WEIXIN4J_PREFIX = "weixin4j"; - private static String wrapKeyName(String key) { - if (!key.startsWith(WEIXIN4J_PREFIX)) { - return String.format("%s.%s", WEIXIN4J_PREFIX, key); - } - return key; - } + private static String wrapKeyName(String key) { + if (!key.startsWith(WEIXIN4J_PREFIX)) { + return String.format("%s.%s", WEIXIN4J_PREFIX, key); + } + return key; + } - /** - * 获取weixin4j.properties文件中的key值 - * - * @param key - * @return - */ - public static String getValue(String key) { - String wrapKey = wrapKeyName(key); - return System.getProperty(wrapKey, weixinBundle.getString(wrapKey)); - } + /** + * 获取weixin4j.properties文件中的key值 + * + * @param key + * @return + */ + public static String getValue(String key) { + String wrapKey = wrapKeyName(key); + return System.getProperty(wrapKey, weixinBundle.getString(wrapKey)); + } - /** - * key不存在时则返回传入的默认值 - * - * @param key - * @param defaultValue - * @return - */ - public static String getValue(String key, String defaultValue) { - String value = defaultValue; - try { - value = getValue(key); - if (StringUtil.isBlank(value)) { - value = defaultValue; - } - } catch (MissingResourceException e) { - ; - } catch (NullPointerException e) { - ; - } - return value; - } + /** + * key不存在时则返回传入的默认值 + * + * @param key + * @param defaultValue + * @return + */ + public static String getValue(String key, String defaultValue) { + String value = defaultValue; + try { + value = getValue(key); + if (StringUtil.isBlank(value)) { + value = defaultValue; + } + } catch (MissingResourceException e) { + ; + } catch (NullPointerException e) { + ; + } + return value; + } - /** - * 判断属性是否存在[classpath:]如果存在则拼接项目路径后返回 一般用于文件的绝对路径获取 - * - * @param key - * @return - */ - public static String getClassPathValue(String key) { - return replaceClassPathValue(getValue(key)); - } + /** + * 判断属性是否存在[classpath:]如果存在则拼接项目路径后返回 一般用于文件的绝对路径获取 + * + * @param key + * @return + */ + public static String getClassPathValue(String key) { + return replaceClassPathValue(getValue(key)); + } - /** - * - * @param key - * @param defaultValue - * @return - */ - public static String getClassPathValue(String key, String defaultValue) { - return replaceClassPathValue(getValue(key, defaultValue)); - } + /** + * + * @param key + * @param defaultValue + * @return + */ + public static String getClassPathValue(String key, String defaultValue) { + return replaceClassPathValue(getValue(key, defaultValue)); + } - public static String replaceClassPathValue(String value) { - return value.replaceFirst(CLASSPATH_PREFIX, CLASSPATH_VALUE); - } + public static String replaceClassPathValue(String value) { + return value.replaceFirst(CLASSPATH_PREFIX, CLASSPATH_VALUE); + } - /** - * 获取微信账号信息 - * - * @return 微信账号信息 - */ - public static WeixinAccount getWeixinAccount() { - WeixinAccount account = null; - try { - account = JSON - .parseObject(getValue("account"), WeixinAccount.class); - } catch (NullPointerException e) { - 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."); - } - return account; - } + /** + * 获取微信账号信息 + * + * @return 微信账号信息 + */ + public static WeixinAccount getWeixinAccount() { + WeixinAccount account = null; + try { + account = JSON.parseObject(getValue("account"), WeixinAccount.class); + } catch (NullPointerException e) { + 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."); + } + return account; + } }