Skip to content

Commit 499750b

Browse files
committed
binarywang#569 微信支付几个查询关闭对账下载相关接口增加重载方法,以方便客户端指定更多参数
1 parent a21fdf2 commit 499750b

File tree

2 files changed

+118
-100
lines changed

2 files changed

+118
-100
lines changed

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/WxPayService.java

Lines changed: 70 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
11
package com.github.binarywang.wxpay.service;
22

3-
import java.io.File;
4-
import java.util.Date;
5-
import java.util.Map;
6-
73
import com.github.binarywang.wxpay.bean.WxPayApiData;
8-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryRequest;
9-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryResult;
10-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendRequest;
11-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendResult;
12-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryRequest;
13-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryResult;
4+
import com.github.binarywang.wxpay.bean.coupon.*;
145
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
156
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
167
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
17-
import com.github.binarywang.wxpay.bean.request.WxPayAuthcode2OpenidRequest;
18-
import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest;
19-
import com.github.binarywang.wxpay.bean.request.WxPayOrderReverseRequest;
20-
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
21-
import com.github.binarywang.wxpay.bean.request.WxPayReportRequest;
22-
import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
23-
import com.github.binarywang.wxpay.bean.request.WxPayShorturlRequest;
24-
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
25-
import com.github.binarywang.wxpay.bean.result.WxPayBillResult;
26-
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
27-
import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult;
28-
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
29-
import com.github.binarywang.wxpay.bean.result.WxPayOrderReverseResult;
30-
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
31-
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
32-
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
33-
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
34-
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
8+
import com.github.binarywang.wxpay.bean.request.*;
9+
import com.github.binarywang.wxpay.bean.result.*;
3510
import com.github.binarywang.wxpay.config.WxPayConfig;
3611
import com.github.binarywang.wxpay.exception.WxPayException;
3712

13+
import java.io.File;
14+
import java.util.Date;
15+
import java.util.Map;
16+
3817
/**
3918
* <pre>
4019
* 微信支付相关接口.
@@ -98,6 +77,23 @@ public interface WxPayService {
9877
*/
9978
WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo) throws WxPayException;
10079

80+
/**
81+
* <pre>
82+
* 查询订单(适合于需要自定义子商户号和子商户appid的情形).
83+
* 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2
84+
* 该接口提供所有微信支付订单的查询,商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。
85+
* 需要调用查询接口的情况:
86+
* ◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知;
87+
* ◆ 调用支付接口后,返回系统错误或未知交易状态情况;
88+
* ◆ 调用被扫支付API,返回USERPAYING的状态;
89+
* ◆ 调用关单或撤销接口API之前,需确认支付状态;
90+
* 接口地址:https://api.mch.weixin.qq.com/pay/orderquery
91+
* </pre>
92+
*
93+
* @param request 查询订单请求对象
94+
*/
95+
WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) throws WxPayException;
96+
10197
/**
10298
* <pre>
10399
* 关闭订单.
@@ -114,6 +110,22 @@ public interface WxPayService {
114110
*/
115111
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException;
116112

113+
/**
114+
* <pre>
115+
* 关闭订单(适合于需要自定义子商户号和子商户appid的情形).
116+
* 应用场景
117+
* 以下情况需要调用关单接口:
118+
* 1. 商户订单支付失败需要生成新单号重新发起支付,要对原订单号调用关单,避免重复支付;
119+
* 2. 系统下单后,用户支付超时,系统退出不再受理,避免用户继续,请调用关单接口。
120+
* 注意:订单生成后不能马上调用关单接口,最短调用时间间隔为5分钟。
121+
* 接口地址:https://api.mch.weixin.qq.com/pay/closeorder
122+
* 是否需要证书: 不需要。
123+
* </pre>
124+
*
125+
* @param request 关闭订单请求对象
126+
*/
127+
WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) throws WxPayException;
128+
117129
/**
118130
* 调用统一下单接口,并组装生成支付所需参数对象.
119131
*
@@ -185,11 +197,19 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
185197
throws WxPayException;
186198

187199
/**
188-
* @see WxPayService#parseOrderNotifyResult(String).
189-
* @deprecated use {@link WxPayService#parseOrderNotifyResult(String)} instead
200+
* <pre>
201+
* 微信支付-查询退款(适合于需要自定义子商户号和子商户appid的情形).
202+
* 应用场景:
203+
* 提交退款申请后,通过调用该接口查询退款状态。退款有一定延时,用零钱支付的退款20分钟内到账,
204+
* 银行卡支付的退款3个工作日后重新查询退款状态。
205+
* 详见 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_5
206+
* 接口链接:https://api.mch.weixin.qq.com/pay/refundquery
207+
* </pre>
208+
*
209+
* @param request 微信退款单号
210+
* @return 退款信息
190211
*/
191-
@Deprecated
192-
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException;
212+
WxPayRefundQueryResult refundQuery(WxPayRefundQueryRequest request) throws WxPayException;
193213

194214
/**
195215
* 解析支付结果通知.
@@ -315,6 +335,24 @@ WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, Stri
315335
*/
316336
WxPayBillResult downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxPayException;
317337

338+
/**
339+
* <pre>
340+
* 下载对账单(适合于需要自定义子商户号和子商户appid的情形).
341+
* 商户可以通过该接口下载历史交易清单。比如掉单、系统错误等导致商户侧和微信侧数据不一致,通过对账单核对后可校正支付状态。
342+
* 注意:
343+
* 1、微信侧未成功下单的交易不会出现在对账单中。支付成功后撤销的交易会出现在对账单中,跟原支付单订单号一致,bill_type为REVOKED;
344+
* 2、微信在次日9点启动生成前一天的对账单,建议商户10点后再获取;
345+
* 3、对账单中涉及金额的字段单位为“元”。
346+
* 4、对账单接口只能下载三个月以内的账单。
347+
* 接口链接:https://api.mch.weixin.qq.com/pay/downloadbill
348+
* 详情请见: <a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6">下载对账单</a>
349+
* </pre>
350+
*
351+
* @param request 下载对账单请求
352+
* @return 保存到本地的临时文件
353+
*/
354+
WxPayBillResult downloadBill(WxPayDownloadBillRequest request) throws WxPayException;
355+
318356
/**
319357
* <pre>
320358
* 提交刷卡支付.

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/service/impl/BaseWxPayServiceImpl.java

Lines changed: 48 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,19 @@
11
package com.github.binarywang.wxpay.service.impl;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.nio.charset.StandardCharsets;
6-
import java.nio.file.Files;
7-
import java.nio.file.Path;
8-
import java.nio.file.Paths;
9-
import java.util.Date;
10-
import java.util.HashMap;
11-
import java.util.LinkedList;
12-
import java.util.List;
13-
import java.util.Map;
14-
import java.util.zip.ZipException;
15-
16-
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
17-
import org.apache.commons.lang3.StringUtils;
18-
import org.slf4j.Logger;
19-
import org.slf4j.LoggerFactory;
20-
213
import com.github.binarywang.utils.qrcode.QrcodeUtils;
224
import com.github.binarywang.wxpay.bean.WxPayApiData;
23-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryRequest;
24-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryResult;
25-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendRequest;
26-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendResult;
27-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryRequest;
28-
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryResult;
5+
import com.github.binarywang.wxpay.bean.coupon.*;
296
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
307
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
318
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
329
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
3310
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
11+
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
3412
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
35-
import com.github.binarywang.wxpay.bean.request.WxPayAuthcode2OpenidRequest;
36-
import com.github.binarywang.wxpay.bean.request.WxPayDefaultRequest;
37-
import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest;
38-
import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest;
39-
import com.github.binarywang.wxpay.bean.request.WxPayOrderCloseRequest;
40-
import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
41-
import com.github.binarywang.wxpay.bean.request.WxPayOrderReverseRequest;
42-
import com.github.binarywang.wxpay.bean.request.WxPayQueryCommentRequest;
43-
import com.github.binarywang.wxpay.bean.request.WxPayRedpackQueryRequest;
44-
import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest;
45-
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
46-
import com.github.binarywang.wxpay.bean.request.WxPayReportRequest;
47-
import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
48-
import com.github.binarywang.wxpay.bean.request.WxPayShorturlRequest;
49-
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
50-
import com.github.binarywang.wxpay.bean.result.BaseWxPayResult;
51-
import com.github.binarywang.wxpay.bean.result.WxPayAuthcode2OpenidResult;
52-
import com.github.binarywang.wxpay.bean.result.WxPayBillBaseResult;
53-
import com.github.binarywang.wxpay.bean.result.WxPayBillResult;
54-
import com.github.binarywang.wxpay.bean.result.WxPayCommonResult;
55-
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
56-
import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult;
57-
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
58-
import com.github.binarywang.wxpay.bean.result.WxPayOrderReverseResult;
59-
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
60-
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
61-
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
62-
import com.github.binarywang.wxpay.bean.result.WxPaySandboxSignKeyResult;
63-
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
64-
import com.github.binarywang.wxpay.bean.result.WxPayShorturlResult;
65-
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
13+
import com.github.binarywang.wxpay.bean.request.*;
14+
import com.github.binarywang.wxpay.bean.result.*;
6615
import com.github.binarywang.wxpay.config.WxPayConfig;
16+
import com.github.binarywang.wxpay.constant.WxPayConstants;
6717
import com.github.binarywang.wxpay.constant.WxPayConstants.BillType;
6818
import com.github.binarywang.wxpay.constant.WxPayConstants.SignType;
6919
import com.github.binarywang.wxpay.constant.WxPayConstants.TradeType;
@@ -74,6 +24,18 @@
7424
import com.google.common.base.Joiner;
7525
import com.google.common.collect.Maps;
7626
import jodd.io.ZipUtil;
27+
import org.apache.commons.lang3.StringUtils;
28+
import org.slf4j.Logger;
29+
import org.slf4j.LoggerFactory;
30+
31+
import java.io.File;
32+
import java.io.IOException;
33+
import java.nio.charset.StandardCharsets;
34+
import java.nio.file.Files;
35+
import java.nio.file.Path;
36+
import java.nio.file.Paths;
37+
import java.util.*;
38+
import java.util.zip.ZipException;
7739

7840
import static com.github.binarywang.wxpay.constant.WxPayConstants.QUERY_COMMENT_DATE_FORMAT;
7941
import static com.github.binarywang.wxpay.constant.WxPayConstants.TarType;
@@ -145,6 +107,11 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
145107
request.setOutRefundNo(StringUtils.trimToNull(outRefundNo));
146108
request.setRefundId(StringUtils.trimToNull(refundId));
147109

110+
return this.refundQuery(request);
111+
}
112+
113+
@Override
114+
public WxPayRefundQueryResult refundQuery(WxPayRefundQueryRequest request) throws WxPayException {
148115
request.checkAndSign(this.getConfig());
149116

150117
String url = this.getPayBaseUrl() + "/pay/refundquery";
@@ -155,12 +122,6 @@ public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeN
155122
return result;
156123
}
157124

158-
@Override
159-
@Deprecated
160-
public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException {
161-
return this.parseOrderNotifyResult(xmlData);
162-
}
163-
164125
@Override
165126
public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException {
166127
try {
@@ -195,7 +156,7 @@ public WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) throws Wx
195156
public WxScanPayNotifyResult parseScanPayNotifyResult(String xmlData) throws WxPayException {
196157
try {
197158
log.debug("扫码支付回调通知请求参数:{}", xmlData);
198-
WxScanPayNotifyResult result = BaseWxPayResult.fromXML(xmlData,WxScanPayNotifyResult.class);
159+
WxScanPayNotifyResult result = BaseWxPayResult.fromXML(xmlData, WxScanPayNotifyResult.class);
199160
log.debug("扫码支付回调通知解析后的对象:{}", result);
200161
result.checkResult(this, this.getConfig().getSignType(), false);
201162
return result;
@@ -243,6 +204,12 @@ public WxPayOrderQueryResult queryOrder(String transactionId, String outTradeNo)
243204
WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
244205
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
245206
request.setTransactionId(StringUtils.trimToNull(transactionId));
207+
208+
return this.queryOrder(request);
209+
}
210+
211+
@Override
212+
public WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) throws WxPayException {
246213
request.checkAndSign(this.getConfig());
247214

248215
String url = this.getPayBaseUrl() + "/pay/orderquery";
@@ -265,6 +232,12 @@ public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException
265232

266233
WxPayOrderCloseRequest request = new WxPayOrderCloseRequest();
267234
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
235+
236+
return this.closeOrder(request);
237+
}
238+
239+
@Override
240+
public WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) throws WxPayException {
268241
request.checkAndSign(this.getConfig());
269242

270243
String url = this.getPayBaseUrl() + "/pay/closeorder";
@@ -425,14 +398,15 @@ public String createScanPayQrcodeMode1(String productId) {
425398
params.put("appid", this.getConfig().getAppId());
426399
params.put("mch_id", this.getConfig().getMchId());
427400
params.put("product_id", productId);
428-
params.put("time_stamp", String.valueOf(System.currentTimeMillis() / 1000));//这里需要秒,10位数字
401+
//这里需要秒,10位数字
402+
params.put("time_stamp", String.valueOf(System.currentTimeMillis() / 1000));
429403
params.put("nonce_str", String.valueOf(System.currentTimeMillis()));
430404

431405
String sign = SignUtils.createSign(params, null, this.getConfig().getMchKey(), false);
432406
params.put("sign", sign);
433407

434408
for (String key : params.keySet()) {
435-
codeUrl.append(key + "=" + params.get(key) + "&");
409+
codeUrl.append(key).append("=").append(params.get(key)).append("&");
436410
}
437411

438412
String content = codeUrl.toString().substring(0, codeUrl.length() - 1);
@@ -475,12 +449,17 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
475449
request.setTarType(tarType);
476450
request.setDeviceInfo(deviceInfo);
477451

452+
return this.downloadBill(request);
453+
}
454+
455+
@Override
456+
public WxPayBillResult downloadBill(WxPayDownloadBillRequest request) throws WxPayException {
478457
request.checkAndSign(this.getConfig());
479458

480459
String url = this.getPayBaseUrl() + "/pay/downloadbill";
481460

482461
String responseContent;
483-
if (TarType.GZIP.equals(tarType)) {
462+
if (TarType.GZIP.equals(request.getTarType())) {
484463
responseContent = this.handleGzipBill(url, request.toXML());
485464
} else {
486465
responseContent = this.post(url, request.toXML(), false);
@@ -489,7 +468,7 @@ public WxPayBillResult downloadBill(String billDate, String billType, String tar
489468
}
490469
}
491470

492-
return this.handleBill(billType, responseContent);
471+
return this.handleBill(request.getBillType(), responseContent);
493472
}
494473

495474
private WxPayBillResult handleBill(String billType, String responseContent) {
@@ -517,9 +496,10 @@ private String handleGzipBill(String url, String requestStr) throws WxPayExcepti
517496
this.log.error("解压zip文件出错", e);
518497
}
519498
}
520-
} catch (IOException e) {
521-
e.printStackTrace();
499+
} catch (Exception e) {
500+
this.log.error("解析对账单文件时出错",e);
522501
}
502+
523503
return null;
524504
}
525505

0 commit comments

Comments
 (0)