Skip to content

Commit 8627684

Browse files
authored
Merge pull request binarywang#107 from dengerYang/develop
优化红包相关接口
2 parents 55c6ea5 + ec0d174 commit 8627684

File tree

5 files changed

+163
-18
lines changed

5 files changed

+163
-18
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpConfigStorage.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,20 @@ public interface WxMpConfigStorage {
118118

119119
File getTmpDirFile();
120120

121+
@Deprecated
121122
SSLContext getSSLContext();
123+
124+
SSLContext getSslContext();
125+
126+
void setSslContext(SSLContext sslContext);
127+
128+
/**
129+
* 在此之前,必须将partnerId进行赋值
130+
*
131+
* @param filePath
132+
* apiclient_cert.p12的文件的绝对路径
133+
*/
134+
void setSslContextFilePath(String filePath) throws Exception;
122135

123136
/**
124137
* http client builder

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpInMemoryConfigStorage.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package me.chanjar.weixin.mp.api;
22

3-
import me.chanjar.weixin.common.bean.WxAccessToken;
4-
import me.chanjar.weixin.common.util.ToStringUtils;
5-
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
6-
7-
import javax.net.ssl.SSLContext;
83
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.security.KeyStore;
96
import java.util.concurrent.locks.Lock;
107
import java.util.concurrent.locks.ReentrantLock;
118

9+
import javax.net.ssl.SSLContext;
10+
11+
import org.apache.http.ssl.SSLContexts;
12+
13+
import me.chanjar.weixin.common.bean.WxAccessToken;
14+
import me.chanjar.weixin.common.util.ToStringUtils;
15+
import me.chanjar.weixin.common.util.http.ApacheHttpClientBuilder;
16+
1217
/**
1318
* 基于内存的微信配置provider,在实际生产环境中应该将这些配置持久化
1419
* @author chanjarster
@@ -300,11 +305,33 @@ public void setTmpDirFile(File tmpDirFile) {
300305
public SSLContext getSSLContext() {
301306
return this.sslContext;
302307
}
303-
304-
public void setSSLContext(SSLContext context) {
305-
this.sslContext = context;
308+
309+
@Override
310+
public SSLContext getSslContext() {
311+
return this.sslContext;
306312
}
307313

314+
@Override
315+
public void setSslContextFilePath(String filePath) throws Exception {
316+
if (null == partnerId) {
317+
throw new Exception("请先将partnerId进行赋值");
318+
}
319+
File file = new File(filePath);
320+
if (!file.exists()) {
321+
throw new RuntimeException(file.getPath() + ":文件不存在!在设置SSLContext的时候");
322+
}
323+
FileInputStream inputStream = new FileInputStream(file);
324+
KeyStore keystore = KeyStore.getInstance("PKCS12");
325+
char[] partnerId2charArray = partnerId.toCharArray();
326+
keystore.load(inputStream, partnerId2charArray);
327+
this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
328+
}
329+
330+
@Override
331+
public void setSslContext(SSLContext context) {
332+
this.sslContext = context;
333+
}
334+
308335
@Override
309336
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
310337
return this.apacheHttpClientBuilder;

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpPayService.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,24 @@ public interface WxMpPayService {
197197
* @param request 请求对象
198198
* @param keyFile 证书文件对象(即apiclient_cert.p12 商户证书文件,详细参考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3)
199199
*/
200+
@Deprecated
200201
WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File keyFile) throws WxErrorException;
201202

203+
/**
204+
* 发送微信红包给个人用户
205+
* <pre>
206+
* 文档详见:
207+
* 发送普通红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_4&index=3
208+
* 接口地址:https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack
209+
* 发送裂变红包 https://pay.weixin.qq.com/wiki/doc/api/tools/cash_coupon.php?chapter=13_5&index=4
210+
* 接口地址:https://api.mch.weixin.qq.com/mmpaymkttransfers/sendgroupredpack
211+
* </pre>
212+
*
213+
* @param request 请求对象
214+
* @param keyFile 证书文件对象(即apiclient_cert.p12 商户证书文件,详细参考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3)
215+
*/
216+
WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxErrorException;
217+
202218
/**
203219
* <pre>
204220
* 查询红包记录
@@ -211,7 +227,22 @@ public interface WxMpPayService {
211227
* @param mchBillNo 商户发放红包的商户订单号,比如10000098201411111234567890
212228
* @param keyFile 证书文件对象(即apiclient_cert.p12 商户证书文件,详细参考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3)
213229
*/
230+
@Deprecated
214231
WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) throws WxErrorException;
232+
233+
/**
234+
* <pre>
235+
* 查询红包记录
236+
* 用于商户对已发放的红包进行查询红包的具体信息,可支持普通红包和裂变包。
237+
* 请求Url https://api.mch.weixin.qq.com/mmpaymkttransfers/gethbinfo
238+
* 是否需要证书 是(证书及使用说明详见商户证书)
239+
* 请求方式 POST
240+
* </pre>
241+
*
242+
* @param mchBillNo 商户发放红包的商户订单号,比如10000098201411111234567890
243+
* @param keyFile 证书文件对象(即apiclient_cert.p12 商户证书文件,详细参考https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=4_3)
244+
*/
245+
WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxErrorException;
215246

216247
/**
217248
* <pre>

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImpl.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxErro
160160

161161

162162
@Override
163+
@Deprecated
163164
public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File keyFile)
164165
throws WxErrorException {
165166
XStream xstream = XStreamInitializer.getInstance();
@@ -179,8 +180,30 @@ public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request, File
179180
this.checkResult(result);
180181
return result;
181182
}
183+
184+
@Override
185+
public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request)
186+
throws WxErrorException {
187+
XStream xstream = XStreamInitializer.getInstance();
188+
xstream.processAnnotations(WxPaySendRedpackRequest.class);
189+
xstream.processAnnotations(WxPaySendRedpackResult.class);
190+
191+
initRequest(request);
192+
request.setSign(this.createSign(request));
193+
194+
String url = PAY_BASE_URL + "/mmpaymkttransfers/sendredpack";
195+
if (request.getAmtType() != null) {
196+
//裂变红包
197+
url = PAY_BASE_URL + "/mmpaymkttransfers/sendgroupredpack";
198+
}
199+
String responseContent = this.executeRequestWithKeyFile(url, xstream.toXML(request));
200+
WxPaySendRedpackResult result = (WxPaySendRedpackResult) xstream.fromXML(responseContent);
201+
this.checkResult(result);
202+
return result;
203+
}
182204

183205
@Override
206+
@Deprecated
184207
public WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) throws WxErrorException {
185208
XStream xstream = XStreamInitializer.getInstance();
186209
xstream.processAnnotations(WxPayRedpackQueryRequest.class);
@@ -198,6 +221,25 @@ public WxPayRedpackQueryResult queryRedpack(String mchBillNo, File keyFile) thro
198221
this.checkResult(result);
199222
return result;
200223
}
224+
225+
@Override
226+
public WxPayRedpackQueryResult queryRedpack(String mchBillNo) throws WxErrorException {
227+
XStream xstream = XStreamInitializer.getInstance();
228+
xstream.processAnnotations(WxPayRedpackQueryRequest.class);
229+
xstream.processAnnotations(WxPayRedpackQueryResult.class);
230+
231+
WxPayRedpackQueryRequest request = new WxPayRedpackQueryRequest();
232+
request.setMchBillNo(mchBillNo);
233+
request.setBillType("MCHT");
234+
initRequest(request);
235+
request.setSign(this.createSign(request));
236+
237+
String url = PAY_BASE_URL + "/mmpaymkttransfers/gethbinfo";
238+
String responseContent = this.executeRequestWithKeyFile(url, xstream.toXML(request));
239+
WxPayRedpackQueryResult result = (WxPayRedpackQueryResult) xstream.fromXML(responseContent);
240+
this.checkResult(result);
241+
return result;
242+
}
201243

202244
@Override
203245
public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxErrorException {
@@ -383,7 +425,8 @@ private String executeRequest(String url, String requestStr) throws WxErrorExcep
383425
httpPost.releaseConnection();
384426
}
385427
}
386-
428+
429+
@Deprecated
387430
private String executeRequestWithKeyFile(String url, File keyFile, String requestStr, String mchId) throws WxErrorException {
388431
try (FileInputStream inputStream = new FileInputStream(keyFile)) {
389432
KeyStore keyStore = KeyStore.getInstance("PKCS12");
@@ -414,6 +457,37 @@ private String executeRequestWithKeyFile(String url, File keyFile, String reques
414457
}
415458
}
416459

460+
private String executeRequestWithKeyFile(String url, String requestStr) throws WxErrorException {
461+
try {
462+
463+
SSLContext sslcontext = getConfig().getSSLContext();
464+
if(null==sslcontext){
465+
throw new Exception("请将配置类(WxMpInMemoryConfigStorage)中的SSLContext初始化");
466+
}
467+
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"}, null,
468+
new DefaultHostnameVerifier());
469+
470+
HttpPost httpPost = new HttpPost(url);
471+
if (this.wxMpService.getHttpProxy() != null) {
472+
httpPost.setConfig(RequestConfig.custom().setProxy(this.wxMpService.getHttpProxy()).build());
473+
}
474+
475+
try (CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
476+
httpPost.setEntity(new StringEntity(new String(requestStr.getBytes("UTF-8"), "ISO-8859-1")));
477+
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
478+
String result = EntityUtils.toString(response.getEntity(), Consts.UTF_8);
479+
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", url, requestStr, result);
480+
return result;
481+
}
482+
} finally {
483+
httpPost.releaseConnection();
484+
}
485+
} catch (Exception e) {
486+
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", url, requestStr, e.getMessage());
487+
throw new WxErrorException(WxError.newBuilder().setErrorCode(-1).setErrorMsg(e.getMessage()).build(), e);
488+
}
489+
}
490+
417491
@Override
418492
public String createSign(Object xmlBean) {
419493
return createSign(BeanUtils.xmlBean2Map(xmlBean), getConfig().getPartnerKey());

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/WxMpServiceImpl.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import org.apache.http.client.config.RequestConfig;
2121
import org.apache.http.client.methods.CloseableHttpResponse;
2222
import org.apache.http.client.methods.HttpGet;
23-
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
24-
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
23+
//import org.apache.http.conn.ssl.DefaultHostnameVerifier;
24+
//import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
2525
import org.apache.http.impl.client.BasicResponseHandler;
2626
import org.apache.http.impl.client.CloseableHttpClient;
2727
import org.slf4j.Logger;
@@ -33,7 +33,7 @@
3333
public class WxMpServiceImpl implements WxMpService {
3434

3535
private static final JsonParser JSON_PARSER = new JsonParser();
36-
36+
3737
protected final Logger log = LoggerFactory.getLogger(this.getClass());
3838

3939
private WxMpConfigStorage configStorage;
@@ -459,12 +459,12 @@ private void initHttpClient() {
459459
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
460460
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
461461

462-
if (this.configStorage.getSSLContext() != null) {
463-
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
464-
this.configStorage.getSSLContext(), new String[] { "TLSv1" }, null,
465-
new DefaultHostnameVerifier());
466-
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
467-
}
462+
// if (this.configStorage.getSSLContext() != null) {
463+
// SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
464+
// this.configStorage.getSSLContext(), new String[] { "TLSv1" }, null,
465+
// new DefaultHostnameVerifier());
466+
// apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
467+
// }
468468

469469
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
470470
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());

0 commit comments

Comments
 (0)