PayOldApi优化
This commit is contained in:
parent
f956c66481
commit
d518eef5f8
@ -13,7 +13,7 @@ import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;
|
||||
|
||||
/**
|
||||
* 签名工具类
|
||||
*
|
||||
*
|
||||
* @className MapUtil
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年10月31日
|
||||
@ -23,7 +23,7 @@ import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;
|
||||
public class MapUtil {
|
||||
/**
|
||||
* 连接字符串
|
||||
*
|
||||
*
|
||||
* @param object
|
||||
* 对象
|
||||
* @param encoder
|
||||
@ -39,7 +39,7 @@ public class MapUtil {
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
JSONObject obj = null;
|
||||
if (object instanceof String) {
|
||||
obj = JSONObject.parseObject(object.toString());
|
||||
obj = JSONObject.parseObject((String) object);
|
||||
} else {
|
||||
obj = ListsuffixResultSerializer.serializeToJSON(object);
|
||||
}
|
||||
@ -51,7 +51,7 @@ public class MapUtil {
|
||||
|
||||
/**
|
||||
* 拼接字符串
|
||||
*
|
||||
*
|
||||
* @param map
|
||||
* 对象
|
||||
* @param encoder
|
||||
|
||||
@ -25,8 +25,6 @@ import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.fastjson.parser.Feature;
|
||||
import com.foxinmy.weixin4j.cache.CacheStorager;
|
||||
import com.foxinmy.weixin4j.cache.FileCacheStorager;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||
@ -42,6 +40,7 @@ import com.foxinmy.weixin4j.mp.oldpayment.WeixinOldPayAccount;
|
||||
import com.foxinmy.weixin4j.mp.oldpayment.WeixinOldPaymentSignature;
|
||||
import com.foxinmy.weixin4j.mp.token.WeixinTokenCreator;
|
||||
import com.foxinmy.weixin4j.payment.PayRequest;
|
||||
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
||||
import com.foxinmy.weixin4j.sign.WeixinPaymentSignature;
|
||||
import com.foxinmy.weixin4j.sign.WeixinSignature;
|
||||
import com.foxinmy.weixin4j.token.TokenManager;
|
||||
@ -68,6 +67,7 @@ import com.foxinmy.weixin4j.xml.ListsuffixResultDeserializer;
|
||||
*/
|
||||
public class PayOldApi extends MpApi {
|
||||
|
||||
private final Weixin4jSettings<WeixinOldPayAccount> settings;
|
||||
private final WeixinOldPayAccount weixinAccount;
|
||||
private final TokenManager tokenManager;
|
||||
private final WeixinSignature weixinMD5Signature;
|
||||
@ -77,32 +77,26 @@ public class PayOldApi extends MpApi {
|
||||
* 默认使用weixin4j.properties配置信息
|
||||
*/
|
||||
public PayOldApi() {
|
||||
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
|
||||
WeixinOldPayAccount.class), new FileCacheStorager<Token>(
|
||||
Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
|
||||
System.getProperty("java.io.tmpdir"))));
|
||||
this(new Weixin4jSettings<WeixinOldPayAccount>(JSON.parseObject(
|
||||
Weixin4jConfigUtil.getValue("account"),
|
||||
WeixinOldPayAccount.class)));
|
||||
}
|
||||
|
||||
public PayOldApi(WeixinOldPayAccount payAccount) {
|
||||
this(payAccount, new FileCacheStorager<Token>(
|
||||
Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
|
||||
System.getProperty("java.io.tmpdir"))));
|
||||
}
|
||||
|
||||
public PayOldApi(CacheStorager<Token> cacheStorager) {
|
||||
this(JSON.parseObject(Weixin4jConfigUtil.getValue("account"),
|
||||
WeixinOldPayAccount.class), cacheStorager);
|
||||
}
|
||||
|
||||
public PayOldApi(WeixinOldPayAccount weixinAccount,
|
||||
CacheStorager<Token> cacheStorager) {
|
||||
this.weixinAccount = weixinAccount;
|
||||
/**
|
||||
*
|
||||
* @param settings
|
||||
* 微信配置信息
|
||||
*/
|
||||
public PayOldApi(Weixin4jSettings<WeixinOldPayAccount> settings) {
|
||||
this.settings = settings;
|
||||
this.weixinAccount = settings.getAccount();
|
||||
this.tokenManager = new TokenManager(new WeixinTokenCreator(
|
||||
weixinAccount.getId(), weixinAccount.getSecret()),
|
||||
cacheStorager);
|
||||
settings.getCacheStorager0());
|
||||
this.weixinMD5Signature = new WeixinPaymentSignature(
|
||||
weixinAccount.getPartnerKey());
|
||||
this.weixinOldSignature = new WeixinOldPaymentSignature();
|
||||
this.weixinOldSignature = new WeixinOldPaymentSignature(
|
||||
weixinAccount.getPaySignKey(), weixinAccount.getPartnerKey());
|
||||
}
|
||||
|
||||
public WeixinOldPayAccount getWeixinPayAccount() {
|
||||
@ -145,10 +139,8 @@ public class PayOldApi extends MpApi {
|
||||
*/
|
||||
public String createPayJsRequestJson(PayPackageV2 payPackage) {
|
||||
PayRequest payRequest = new PayRequest(weixinAccount.getId(),
|
||||
weixinOldSignature.sign(payPackage,
|
||||
weixinAccount.getPartnerKey()));
|
||||
payRequest.setPaySign(weixinOldSignature.sign(payRequest,
|
||||
weixinAccount.getPaySignKey()));
|
||||
weixinOldSignature.sign(payPackage));
|
||||
payRequest.setPaySign(weixinOldSignature.sign(payRequest));
|
||||
payRequest.setSignType(SignType.SHA1);
|
||||
return JSON.toJSONString(payRequest);
|
||||
}
|
||||
@ -287,9 +279,7 @@ public class PayOldApi extends MpApi {
|
||||
KeyStore ks = null;
|
||||
String jksPwd = "";
|
||||
File jksFile = new File(String.format("%s%stenpay_cacert.jks",
|
||||
Weixin4jConfigUtil.getClassPathValue("weixin4j.tmpdir",
|
||||
System.getProperty("java.io.tmpdir")),
|
||||
File.separator));
|
||||
settings.getTmpdir0(), File.separator));
|
||||
// create jks ca
|
||||
if (!jksFile.exists()) {
|
||||
CertificateFactory cf = CertificateFactory
|
||||
|
||||
@ -13,7 +13,7 @@ import com.foxinmy.weixin4j.payment.PayRequest;
|
||||
|
||||
/**
|
||||
* V2 Native支付时的回调响应
|
||||
*
|
||||
*
|
||||
* @className NativePayResponseV2
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年10月28日
|
||||
@ -44,7 +44,7 @@ public class NativePayResponseV2 extends PayRequest {
|
||||
|
||||
/**
|
||||
* 响应错误信息
|
||||
*
|
||||
*
|
||||
* @param errorMsg
|
||||
* 错误信息
|
||||
*/
|
||||
@ -55,7 +55,7 @@ public class NativePayResponseV2 extends PayRequest {
|
||||
|
||||
/**
|
||||
* 正确响应
|
||||
*
|
||||
*
|
||||
* @param weixinAccount
|
||||
* @param payPackage
|
||||
* 订单信息
|
||||
@ -65,9 +65,9 @@ public class NativePayResponseV2 extends PayRequest {
|
||||
super(weixinAccount.getId(), null);
|
||||
this.retCode = "0";
|
||||
this.retMsg = "OK";
|
||||
WeixinOldPaymentSignature weixinSignature = new WeixinOldPaymentSignature();
|
||||
setPackageInfo(weixinSignature.sign(payPackage,
|
||||
weixinAccount.getPartnerKey()));
|
||||
WeixinOldPaymentSignature weixinSignature = new WeixinOldPaymentSignature(
|
||||
weixinAccount.getPaySignKey(), weixinAccount.getPartnerKey());
|
||||
setPackageInfo(weixinSignature.sign(payPackage));
|
||||
Map<String, String> map = new HashMap<String, String>();
|
||||
map.put("appid", weixinAccount.getId());
|
||||
map.put("appkey", weixinAccount.getPaySignKey());
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
package com.foxinmy.weixin4j.mp.oldpayment;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.JSONPath;
|
||||
@ -11,7 +9,7 @@ import com.foxinmy.weixin4j.util.MapUtil;
|
||||
|
||||
/**
|
||||
* 老版本支付签名
|
||||
*
|
||||
*
|
||||
* @className WeixinOldPaymentSignature
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年3月26日
|
||||
@ -20,6 +18,20 @@ import com.foxinmy.weixin4j.util.MapUtil;
|
||||
*/
|
||||
public class WeixinOldPaymentSignature extends AbstractWeixinSignature {
|
||||
|
||||
/**
|
||||
* 支付签名密钥
|
||||
*/
|
||||
private final String paySignKey;
|
||||
/**
|
||||
* package签名密钥
|
||||
*/
|
||||
private final String partnerKey;
|
||||
|
||||
public WeixinOldPaymentSignature(String paySignKey, String partnerKey) {
|
||||
this.paySignKey = paySignKey;
|
||||
this.partnerKey = partnerKey;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean lowerCase() {
|
||||
return true;
|
||||
@ -27,28 +39,23 @@ public class WeixinOldPaymentSignature extends AbstractWeixinSignature {
|
||||
|
||||
@Override
|
||||
public String sign(Object obj) {
|
||||
return DigestUtil.SHA1(join(obj).toString());
|
||||
}
|
||||
|
||||
public String sign(Object obj, String paySignKey) {
|
||||
if (obj instanceof Map) {
|
||||
JSONPath.set(obj, "appKey", paySignKey);
|
||||
if (obj instanceof String) {
|
||||
obj = JSON.parse((String) obj);
|
||||
} else {
|
||||
((JSONObject) JSON.toJSON(obj)).put("appKey", paySignKey);
|
||||
obj = ((JSONObject) JSON.toJSON(obj));
|
||||
}
|
||||
JSONPath.set(obj, "appKey", paySignKey);
|
||||
return DigestUtil.SHA1(join(obj).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* package拼接签名
|
||||
*
|
||||
*
|
||||
* @param packageInfo
|
||||
* package对象
|
||||
* @param partnerKey
|
||||
* 签名key
|
||||
* @return
|
||||
*/
|
||||
public String sign(PayPackageV2 packageInfo, String partnerKey) {
|
||||
public String sign(PayPackageV2 packageInfo) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
// a.对所有传入参数按照字段名的 ASCII 码从小到大排序(字典序) 后,
|
||||
// 使用 URL 键值 对的格式(即 key1=value1&key2=value2...)拼接成字符串 string1
|
||||
|
||||
@ -22,8 +22,8 @@ import com.foxinmy.weixin4j.mp.model.KfSession.KfSessionCounter;
|
||||
|
||||
/**
|
||||
* 客服消息测试
|
||||
*
|
||||
* @className MessageNotifyTest
|
||||
*
|
||||
* @className CustomTest
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2014年4月10日
|
||||
* @since JDK 1.6
|
||||
|
||||
@ -10,12 +10,13 @@ import org.junit.Test;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.mp.api.PayOldApi;
|
||||
import com.foxinmy.weixin4j.mp.oldpayment.WeixinOldPayAccount;
|
||||
import com.foxinmy.weixin4j.setting.Weixin4jSettings;
|
||||
import com.foxinmy.weixin4j.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.type.IdType;
|
||||
|
||||
/**
|
||||
* 支付测试(V2版本 2014年9月之前申请微信支付的公众号)
|
||||
*
|
||||
*
|
||||
* @className PayTest
|
||||
* @author jinyu(foxinmy@gmail.com)
|
||||
* @date 2016年1月30日
|
||||
@ -24,12 +25,13 @@ import com.foxinmy.weixin4j.type.IdType;
|
||||
*/
|
||||
public class PayTest {
|
||||
protected final static PayOldApi PAY2;
|
||||
protected final static WeixinOldPayAccount ACCOUNT2;
|
||||
protected final static Weixin4jSettings<WeixinOldPayAccount> settings;
|
||||
static {
|
||||
ACCOUNT2 = new WeixinOldPayAccount("请填入v2版本的appid",
|
||||
"请填入v2版本的appSecret", "请填入v2版本的paysignkey", "请填入v2版本的partnerId",
|
||||
"请填入v2版本的partnerKey");
|
||||
PAY2 = new PayOldApi(ACCOUNT2);
|
||||
settings = new Weixin4jSettings<WeixinOldPayAccount>(
|
||||
new WeixinOldPayAccount("请填入v2版本的appid", "请填入v2版本的appSecret",
|
||||
"请填入v2版本的paysignkey", "请填入v2版本的partnerId",
|
||||
"请填入v2版本的partnerKey"));
|
||||
PAY2 = new PayOldApi(settings);
|
||||
}
|
||||
/**
|
||||
* 商户证书文件
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user