重构Weixin4jSettings...

This commit is contained in:
jinyu
2016-02-02 19:48:30 +08:00
parent baff97e9db
commit 8a9f93e54e
39 changed files with 636 additions and 1020 deletions
@@ -12,6 +12,7 @@ import java.nio.charset.Charset;
* @see
*/
public final class Consts {
public static final String WEIXIN4J = "weixin4j";
public static final Charset UTF_8 = Charset.forName("UTF-8");
public static final Charset GBK = Charset.forName("GBK");
public static final String SUCCESS = "SUCCESS";
@@ -49,9 +49,9 @@ public class WeixinPayAccount extends WeixinAccount {
* 商户平台版本(V3)字段
*
* @param appId
* 公众号唯一的身份ID
* 公众号唯一的身份ID(必填)
* @param appSecret
* 调用接口的凭证
* 调用接口的凭证(最好填写)
* @param paySignKey
* 支付密钥字符串(必填)
* @param mchId
@@ -68,7 +68,7 @@ public class WeixinPayAccount extends WeixinAccount {
* @param appId
* 公众号唯一的身份ID(必填)
* @param appSecret
* 调用接口的凭证(必填)
* 调用接口的凭证(最好填写)
* @param paySignKey
* 支付密钥字符串(必填)
* @param mchId
@@ -30,11 +30,11 @@ import com.foxinmy.weixin4j.payment.mch.Redpacket;
import com.foxinmy.weixin4j.payment.mch.RedpacketRecord;
import com.foxinmy.weixin4j.payment.mch.RedpacketSendResult;
import com.foxinmy.weixin4j.payment.mch.RefundRecord;
import com.foxinmy.weixin4j.settings.Weixin4jPaySettings;
import com.foxinmy.weixin4j.type.BillType;
import com.foxinmy.weixin4j.type.CurrencyType;
import com.foxinmy.weixin4j.type.IdQuery;
import com.foxinmy.weixin4j.type.TradeType;
import com.foxinmy.weixin4j.util.Weixin4jSettings;
/**
* 微信支付接口实现
@@ -52,26 +52,26 @@ public class WeixinPayProxy {
private final CouponApi couponApi;
private final CashApi cashApi;
private final Weixin4jPaySettings settings;
private final Weixin4jSettings settings;
/**
* 使用weixin4j.properties配置的支付账号信息
*/
public WeixinPayProxy() {
this(new Weixin4jPaySettings());
this(new Weixin4jSettings());
}
/**
*
* @param settings
* 支付相关配置信息
* @see com.foxinmy.weixin4j.settings.Weixin4jPaySettings
* @see com.foxinmy.weixin4j.util.Weixin4jSettings
*/
public WeixinPayProxy(Weixin4jPaySettings settings) {
public WeixinPayProxy(Weixin4jSettings settings) {
this.settings = settings;
this.pay3Api = new Pay3Api(settings.getPayAccount());
this.couponApi = new CouponApi(settings.getPayAccount());
this.cashApi = new CashApi(settings.getPayAccount());
this.pay3Api = new Pay3Api(settings.getWeixinPayAccount());
this.couponApi = new CouponApi(settings.getWeixinPayAccount());
this.cashApi = new CashApi(settings.getWeixinPayAccount());
}
/**
@@ -80,7 +80,7 @@ public class WeixinPayProxy {
* @return
*/
public WeixinPayAccount getPayAccount() {
return this.settings.getPayAccount();
return this.settings.getWeixinPayAccount();
}
/**
@@ -467,7 +467,7 @@ public class WeixinPayProxy {
IdQuery idQuery, String outRefundNo, double totalFee)
throws WeixinException, IOException {
return pay3Api.refundApply(
new FileInputStream(settings.getCertificatePath()), idQuery,
new FileInputStream(settings.getCertificateFile0()), idQuery,
outRefundNo, totalFee);
}
@@ -514,7 +514,7 @@ public class WeixinPayProxy {
*/
public File downloadBill(Date billDate, BillType billType)
throws WeixinException {
return pay3Api.downloadBill(billDate, billType, settings.getBillPath());
return pay3Api.downloadBill(billDate, billType, settings.getTmpdir0());
}
/**
@@ -551,7 +551,7 @@ public class WeixinPayProxy {
public ApiResult reverseOrder(IdQuery idQuery) throws WeixinException,
IOException {
return pay3Api.reverseOrder(
new FileInputStream(settings.getCertificatePath()), idQuery);
new FileInputStream(settings.getCertificateFile0()), idQuery);
}
/**
@@ -655,7 +655,7 @@ public class WeixinPayProxy {
public CouponResult sendCoupon(String couponStockId, String partnerTradeNo,
String openId) throws WeixinException, IOException {
return couponApi.sendCoupon(
new FileInputStream(settings.getCertificatePath()),
new FileInputStream(settings.getCertificateFile0()),
couponStockId, partnerTradeNo, openId, null);
}
@@ -721,7 +721,7 @@ public class WeixinPayProxy {
public RedpacketSendResult sendRedpack(Redpacket redpacket)
throws WeixinException, IOException {
return cashApi.sendRedpack(
new FileInputStream(settings.getCertificatePath()), redpacket);
new FileInputStream(settings.getCertificateFile0()), redpacket);
}
/**
@@ -750,8 +750,10 @@ public class WeixinPayProxy {
*/
public RedpacketRecord queryRedpack(String outTradeNo)
throws WeixinException, IOException {
return cashApi.queryRedpack(
new FileInputStream(settings.getCertificatePath()), outTradeNo);
return cashApi
.queryRedpack(
new FileInputStream(settings.getCertificateFile0()),
outTradeNo);
}
/**
@@ -782,7 +784,7 @@ public class WeixinPayProxy {
public MPPaymentResult mpPayment(MPPayment mpPayment)
throws WeixinException, IOException {
return cashApi.mchPayment(
new FileInputStream(settings.getCertificatePath()), mpPayment);
new FileInputStream(settings.getCertificateFile0()), mpPayment);
}
/**
@@ -811,8 +813,10 @@ public class WeixinPayProxy {
*/
public MPPaymentRecord mpPaymentQuery(String outTradeNo)
throws WeixinException, IOException {
return cashApi.mchPaymentQuery(
new FileInputStream(settings.getCertificatePath()), outTradeNo);
return cashApi
.mchPaymentQuery(
new FileInputStream(settings.getCertificateFile0()),
outTradeNo);
}
/**
@@ -1,14 +0,0 @@
package com.foxinmy.weixin4j.settings;
/**
* 微信请求配置相关(待实现
*
* @className Weixin4jHttpSettings
* @author jy
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jHttpSettings {
}
@@ -1,108 +0,0 @@
package com.foxinmy.weixin4j.settings;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 微信支付配置相关
*
* @className Weixin4jPaySettings
* @author jy
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jPaySettings {
private final WeixinPayAccount payAccount;
public Weixin4jPaySettings() {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinPayAccount.class));
}
public Weixin4jPaySettings(WeixinPayAccount payAccount) {
this.payAccount = payAccount;
}
/**
* 支付账号信息
*
* @return
*/
public WeixinPayAccount getPayAccount() {
return this.payAccount;
}
private String billPath;
/**
* 对账单保存路径
*
* @param billPath
* 硬盘目录
*/
public Weixin4jPaySettings billPath(String billPath) {
this.billPath = billPath;
return this;
}
/**
* 默认对账单保存路径
*/
public static final String DEFAULT_BILL_PATH = "/tmp/weixin4j/bill";
/**
* 对账单保存路径,默认值为{@link #DEFAULT_BILL_PATH}
*
* @return
*/
public String getBillPath() {
if (StringUtil.isBlank(billPath)) {
this.billPath = Weixin4jConfigUtil.getClassPathValue("bill.path",
DEFAULT_BILL_PATH);
}
return this.billPath;
}
private String certificatePath;
/**
* ca证书存放路径
*
* @param certificatePath
* 硬盘目录
* @return
*/
public Weixin4jPaySettings certificatePath(String certificatePath) {
this.certificatePath = certificatePath;
return this;
}
/**
* 默认ca证书存放路径
*/
public static final String DEFAULT_CAFILE_PATH = "classpath:ca.p12";
/**
* ca证书存放路径,默认值为{@link #DEFAULT_CAFILE_PATH}
*
* @return
*/
public String getCertificatePath() {
if (StringUtil.isBlank(certificatePath)) {
this.certificatePath = Weixin4jConfigUtil.getClassPathValue(
"certificate.path", DEFAULT_CAFILE_PATH);
}
return this.certificatePath;
}
@Override
public String toString() {
return "Weixin4jPaySettings [payAccount=" + payAccount + ", billPath="
+ getBillPath() + ", certificatePath=" + getCertificatePath()
+ "]";
}
}
@@ -1,168 +0,0 @@
package com.foxinmy.weixin4j.settings;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenStorager;
import com.foxinmy.weixin4j.util.StringUtil;
import com.foxinmy.weixin4j.util.Weixin4jConfigUtil;
/**
* 微信基础配置相关
*
* @className Weixin4jSettings
* @author jy
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jSettings {
private final WeixinAccount account;
public Weixin4jSettings() {
this(Weixin4jConfigUtil.getWeixinAccount());
}
/**
*
* @param id
* 应用唯一标识 appid/corpid
* @param secret
* 应用接口密钥
*/
public Weixin4jSettings(String id, String secret) {
this(new WeixinAccount(id, secret));
}
public Weixin4jSettings(WeixinAccount account) {
this.account = account;
}
/**
* 微信账号信息
*/
public WeixinAccount getAccount() {
return this.account;
}
private String tokenPath;
/**
* 使用FileTokenStorager时token的存放路径
*
* @param tokenPath
* 硬盘目录
* @return
*/
public Weixin4jSettings setTokenPath(String tokenPath) {
this.tokenPath = tokenPath;
return this;
}
/**
* 默认token的存放路径
*/
public static final String DEFAULT_TOKEN_PATH = "/tmp/weixin4j/token";
/**
* 使用FileTokenStorager时token的存放路径,默认值为{@link #DEFAULT_TOKEN_PATH}
*/
public String getTokenPath() {
if (StringUtil.isBlank(tokenPath)) {
tokenPath = Weixin4jConfigUtil.getClassPathValue("token.path",
DEFAULT_TOKEN_PATH);
}
return tokenPath;
}
private String qrcodePath;
/**
* 二维码保存路径
*
* @param qrcodePath
* 硬盘目录
*/
public Weixin4jSettings setQrcodePath(String qrcodePath) {
this.qrcodePath = qrcodePath;
return this;
}
/**
* 默认二维码保存路径
*/
public static final String DEFAULT_QRCODE_PATH = "/tmp/weixin4j/qrcode";
/**
* 二维码保存路径,默认值为{@link #DEFAULT_QRCODE_PATH}
*/
public String getQrcodePath() {
if (StringUtil.isBlank(qrcodePath)) {
this.qrcodePath = Weixin4jConfigUtil.getClassPathValue(
"qrcode.path", DEFAULT_QRCODE_PATH);
}
return this.qrcodePath;
}
private String mediaPath;
/**
* 媒体文件保存路径
*
* @param mediaPath
* 硬盘目录
*/
public Weixin4jSettings setMediaPath(String mediaPath) {
this.mediaPath = mediaPath;
return this;
}
/**
* 默认媒体文件保存路径
*/
public static final String DEFAULT_MEDIA_PATH = "/tmp/weixin4j/media";
/**
* 媒体文件保存路径,默认值为{@link #DEFAULT_MEDIA_PATH}
*/
public String getMediaPath() {
if (StringUtil.isBlank(mediaPath)) {
this.mediaPath = Weixin4jConfigUtil.getClassPathValue("media.path",
DEFAULT_MEDIA_PATH);
}
return this.mediaPath;
}
private TokenStorager tokenStorager;
/**
* token存储
*
* @param tokenStorager
* @return
*/
public Weixin4jSettings setTokenStorager(TokenStorager tokenStorager) {
this.tokenStorager = tokenStorager;
return this;
}
/**
* 获取token存储方式 默认为FileTokenStorager
*
* @return
*/
public TokenStorager getTokenStorager() {
if (tokenStorager == null) {
this.tokenStorager = new FileTokenStorager(getTokenPath());
}
return this.tokenStorager;
}
@Override
public String toString() {
return "Weixin4jSettings [account=" + account + ", tokenPath="
+ getTokenPath() + ", qrcodePath=" + getQrcodePath()
+ ", mediaPath=" + getMediaPath() + ", tokenStorager="
+ getTokenStorager() + "]";
}
}
@@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.util;
import java.io.File;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
@@ -25,18 +24,6 @@ public class Weixin4jConfigUtil {
.getResource("").getPath();
try {
weixinBundle = ResourceBundle.getBundle("weixin4j");
File file = null;
for (String key : weixinBundle.keySet()) {
if (!key.endsWith(".path")) {
continue;
}
file = new File(getValue(key).replaceFirst(CLASSPATH_PREFIX,
CLASSPATH_VALUE));
if (!file.exists() && !file.mkdirs()) {
System.err.append(String.format("%s create fail.%n",
file.getAbsolutePath()));
}
}
} catch (MissingResourceException e) {
;
}
@@ -0,0 +1,155 @@
package com.foxinmy.weixin4j.util;
import com.alibaba.fastjson.JSON;
import com.foxinmy.weixin4j.http.HttpParams;
import com.foxinmy.weixin4j.model.WeixinAccount;
import com.foxinmy.weixin4j.model.WeixinPayAccount;
import com.foxinmy.weixin4j.token.FileTokenStorager;
import com.foxinmy.weixin4j.token.TokenStorager;
/**
* 微信配置相关
*
* @className Weixin4jSettings
* @author jy
* @date 2016年1月28日
* @since JDK 1.6
* @see
*/
public class Weixin4jSettings {
/**
* 微信支付账号信息
*/
private WeixinPayAccount weixinPayAccount;
/**
* 微信账号信息
*/
private WeixinAccount weixinAccount;
/**
* Http参数
*/
private HttpParams httpParams;
/**
* token存储方式 默认为FileTokenStorager
*/
private TokenStorager tokenStorager;
/**
* 系统临时目录
*/
private String tmpdir;
/**
* 支付接口需要的证书文件(*.p12)
*/
private String certificateFile;
/**
* 默认使用weixin4j.properties配置的信息
*/
public Weixin4jSettings() {
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
WeixinPayAccount.class));
}
/**
* 支付代理接口
*
* @param weixinPayAccount
* 商户信息
* @param certificateFile
* 支付接口需要的证书文件(*.p12),比如退款接口
*/
public Weixin4jSettings(WeixinPayAccount weixinPayAccount,
String certificateFile) {
this.weixinPayAccount = weixinPayAccount;
this.certificateFile = certificateFile;
this.weixinAccount = new WeixinAccount(weixinPayAccount.getId(),
weixinPayAccount.getSecret());
}
/**
* 普通代理接口
*
* @param weixinAccount
*/
public Weixin4jSettings(WeixinAccount weixinAccount) {
this.weixinAccount = weixinAccount;
}
public WeixinPayAccount getWeixinPayAccount() {
return weixinPayAccount;
}
public WeixinAccount getWeixinAccount() {
return weixinAccount;
}
public HttpParams getHttpParams() {
return httpParams;
}
public HttpParams getHttpParams0() {
if (httpParams == null) {
return new HttpParams();
}
return httpParams;
}
public String getTmpdir() {
return tmpdir;
}
public String getTmpdir0() {
if (StringUtil.isBlank(tmpdir)) {
return Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
System.getProperty("java.io.tmpdir"));
}
return tmpdir;
}
public TokenStorager getTokenStorager() {
return tokenStorager;
}
public TokenStorager getTokenStorager0() {
if (tokenStorager == null) {
return new FileTokenStorager(getTmpdir0());
}
return tokenStorager;
}
public String getCertificateFile() {
return certificateFile;
}
public String getCertificateFile0() {
if (StringUtil.isBlank(certificateFile)) {
return Weixin4jConfigUtil.getClassPathValue(
"weixin4j.certificate.file", "classpath:ca.p12");
}
return certificateFile;
}
public void setHttpParams(HttpParams httpParams) {
this.httpParams = httpParams;
}
public void setTmpdir(String tmpdir) {
this.tmpdir = tmpdir;
}
public void setTokenStorager(TokenStorager tokenStorager) {
this.tokenStorager = tokenStorager;
}
public void setCertificateFile(String certificateFile) {
this.certificateFile = certificateFile;
}
@Override
public String toString() {
return "Weixin4jSettings [weixinAccount=" + weixinAccount
+ ", httpParams=" + httpParams + ",tokenStorager="
+ tokenStorager + ", tmpdir=" + tmpdir + ", certificateFile= "
+ certificateFile + "]";
}
}