SSL请求复用
This commit is contained in:
parent
7dd0cfe009
commit
adf50b186a
@ -66,7 +66,7 @@ public class CashApi extends MchApi {
|
||||
obj.put("wxappid", obj.remove("appid"));
|
||||
obj.put("sign", weixinSignature.sign(obj));
|
||||
String param = XmlStream.map2xml(obj);
|
||||
WeixinResponse response = createSSLRequestExecutor()
|
||||
WeixinResponse response = getWeixinSSLExecutor()
|
||||
.post(redpacket.getTotalNum() > 1 ? getRequestUri("groupredpack_send_uri")
|
||||
: getRequestUri("redpack_send_uri"), param);
|
||||
String text = response.getAsString()
|
||||
@ -97,7 +97,7 @@ public class CashApi extends MchApi {
|
||||
para.put("mch_billno", outTradeNo);
|
||||
para.put("sign", weixinSignature.sign(para));
|
||||
String param = XmlStream.map2xml(para);
|
||||
WeixinResponse response = createSSLRequestExecutor().post(
|
||||
WeixinResponse response = getWeixinSSLExecutor().post(
|
||||
getRequestUri("redpack_query_uri"), param);
|
||||
return response.getAsObject(new TypeReference<RedpacketRecord>() {
|
||||
});
|
||||
@ -133,7 +133,7 @@ public class CashApi extends MchApi {
|
||||
obj.put("mch_appid", obj.remove("appid"));
|
||||
obj.put("sign", weixinSignature.sign(obj));
|
||||
String param = XmlStream.map2xml(obj);
|
||||
WeixinResponse response = createSSLRequestExecutor().post(
|
||||
WeixinResponse response = getWeixinSSLExecutor().post(
|
||||
getRequestUri("corppayment_send_uri"), param);
|
||||
String text = response.getAsString()
|
||||
.replaceFirst("<mch_appid>", "<appid>")
|
||||
@ -164,7 +164,7 @@ public class CashApi extends MchApi {
|
||||
obj.put("partner_trade_no", outTradeNo);
|
||||
obj.put("sign", weixinSignature.sign(obj));
|
||||
String param = XmlStream.map2xml(obj);
|
||||
WeixinResponse response = createSSLRequestExecutor().post(
|
||||
WeixinResponse response = getWeixinSSLExecutor().post(
|
||||
getRequestUri("corppayment_query_uri"), param);
|
||||
return response.getAsObject(new TypeReference<CorpPaymentRecord>() {
|
||||
});
|
||||
|
||||
@ -62,7 +62,7 @@ public class CouponApi extends MchApi {
|
||||
map.put("type", "XML");
|
||||
map.put("sign", weixinSignature.sign(map));
|
||||
String param = XmlStream.map2xml(map);
|
||||
WeixinResponse response = createSSLRequestExecutor().post(
|
||||
WeixinResponse response = getWeixinSSLExecutor().post(
|
||||
getRequestUri("coupon_send_uri"), param);
|
||||
return response.getAsObject(new TypeReference<CouponResult>() {
|
||||
});
|
||||
|
||||
@ -3,7 +3,6 @@ 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;
|
||||
@ -38,6 +37,7 @@ public class MchApi extends BaseApi {
|
||||
|
||||
protected final WeixinPayAccount weixinAccount;
|
||||
protected final WeixinSignature weixinSignature;
|
||||
private volatile WeixinRequestExecutor weixinSSLExecutor;
|
||||
|
||||
public MchApi(WeixinPayAccount weixinAccount) {
|
||||
this.weixinAccount = weixinAccount;
|
||||
@ -50,32 +50,6 @@ public class MchApi extends BaseApi {
|
||||
return WEIXIN_BUNDLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建 SSL支付请求
|
||||
*
|
||||
* @param certificate
|
||||
* *.p12证书文件
|
||||
* @return
|
||||
* @throws WeixinException
|
||||
*/
|
||||
protected WeixinRequestExecutor createSSLRequestExecutor()
|
||||
throws WeixinException {
|
||||
InputStream certificateFile;
|
||||
try {
|
||||
File certificate = new File(weixinAccount.getCertificateFile());
|
||||
if (!certificate.exists() || !certificate.isFile()) {
|
||||
throw new WeixinException("invalid certificate file : "
|
||||
+ certificate.toString());
|
||||
}
|
||||
certificateFile = new FileInputStream(
|
||||
weixinAccount.getCertificateFile());
|
||||
} catch (IOException e) {
|
||||
throw new WeixinException("IO Error on createSSLRequestExecutor", e);
|
||||
}
|
||||
return weixinExecutor.createSSLRequestExecutor(
|
||||
weixinAccount.getCertificateKey(), certificateFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付接口请求基本数据
|
||||
*
|
||||
@ -112,6 +86,32 @@ public class MchApi extends BaseApi {
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置商户信息
|
||||
*
|
||||
|
||||
@ -478,7 +478,7 @@ public class PayApi extends MchApi {
|
||||
map.put("refund_fee_type", refundFeeType.name());
|
||||
map.put("sign", weixinSignature.sign(map));
|
||||
String param = XmlStream.map2xml(map);
|
||||
response = createSSLRequestExecutor().post(
|
||||
response = getWeixinSSLExecutor().post(
|
||||
getRequestUri("refund_apply_uri"), param);
|
||||
return response.getAsObject(new TypeReference<RefundResult>() {
|
||||
});
|
||||
@ -518,7 +518,7 @@ public class PayApi extends MchApi {
|
||||
Map<String, String> map = createBaseRequestMap(idQuery);
|
||||
map.put("sign", weixinSignature.sign(map));
|
||||
String param = XmlStream.map2xml(map);
|
||||
WeixinResponse response = createSSLRequestExecutor().post(
|
||||
WeixinResponse response = getWeixinSSLExecutor().post(
|
||||
getRequestUri("order_reverse_uri"), param);
|
||||
return response.getAsObject(new TypeReference<MerchantResult>() {
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user