Skip to content

Commit e97c15b

Browse files
committed
实现微信支付下载对账单的接口,还未完成,待调试 #65
1 parent a2398dd commit e97c15b

File tree

4 files changed

+195
-6
lines changed

4 files changed

+195
-6
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,10 @@ public interface WxMpPayService {
240240
* 其中XXXXX为商户需要填写的内容,商户将该链接生成二维码,如需要打印发布二维码,需要采用此格式。商户可调用第三方库生成二维码图片。
241241
* 文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4
242242
* </pre>
243-
* @param productId 产品Id
243+
*
244+
* @param productId 产品Id
244245
* @param sideLength 要生成的二维码的边长,如果为空,则取默认值400
245-
* @param logoFile 商户logo图片的文件对象,可以为空
246+
* @param logoFile 商户logo图片的文件对象,可以为空
246247
* @return 生成的二维码的字节数组
247248
*/
248249
byte[] createScanPayQrcodeMode1(String productId, File logoFile, Integer sideLength);
@@ -254,8 +255,9 @@ public interface WxMpPayService {
254255
* 该模式链接较短,生成的二维码打印到结账小票上的识别率较高。
255256
* 文档详见: https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5
256257
* </pre>
257-
* @param codeUrl 微信返回的交易会话的二维码链接
258-
* @param logoFile 商户logo图片的文件对象,可以为空
258+
*
259+
* @param codeUrl 微信返回的交易会话的二维码链接
260+
* @param logoFile 商户logo图片的文件对象,可以为空
259261
* @param sideLength 要生成的二维码的边长,如果为空,则取默认值400
260262
* @return 生成的二维码的字节数组
261263
*/
@@ -273,4 +275,25 @@ public interface WxMpPayService {
273275
* </pre>
274276
*/
275277
void report(WxPayReportRequest request) throws WxErrorException;
278+
279+
/**
280+
* <pre>
281+
* 下载对账单
282+
* 商户可以通过该接口下载历史交易清单。比如掉单、系统错误等导致商户侧和微信侧数据不一致,通过对账单核对后可校正支付状态。
283+
* 注意:
284+
* 1、微信侧未成功下单的交易不会出现在对账单中。支付成功后撤销的交易会出现在对账单中,跟原支付单订单号一致,bill_type为REVOKED;
285+
* 2、微信在次日9点启动生成前一天的对账单,建议商户10点后再获取;
286+
* 3、对账单中涉及金额的字段单位为“元”。
287+
* 4、对账单接口只能下载三个月以内的账单。
288+
* 接口链接:https://api.mch.weixin.qq.com/pay/downloadbill
289+
* 详情请见: <a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_6">下载对账单</a>
290+
* </pre>
291+
*
292+
* @param billDate 对账单日期 bill_date 下载对账单的日期,格式:20140603
293+
* @param billType 账单类型 bill_type ALL,返回当日所有订单信息,默认值,SUCCESS,返回当日成功支付的订单,REFUND,返回当日退款订单
294+
* @param tarType 压缩账单 tar_type 非必传参数,固定值:GZIP,返回格式为.gzip的压缩包账单。不传则默认为数据流形式。
295+
* @param deviceInfo 设备号 device_info 非必传参数,终端设备号
296+
* @return 保存到本地的临时文件
297+
*/
298+
File downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxErrorException;
276299
}

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class WxMpPayServiceImpl implements WxMpPayService {
4141
private static final String PAY_BASE_URL = "https://api.mch.weixin.qq.com";
4242
private static final String[] TRADE_TYPES = new String[]{"JSAPI", "NATIVE", "APP"};
4343
private static final String[] REFUND_ACCOUNT = new String[]{"REFUND_SOURCE_RECHARGE_FUNDS", "REFUND_SOURCE_UNSETTLED_FUNDS"};
44+
private static final String[] BILL_TYPE = new String[]{"ALL","REFUND","SUCCESS"};;
4445
private final Logger log = LoggerFactory.getLogger(this.getClass());
4546
private WxMpService wxMpService;
4647

@@ -121,6 +122,19 @@ private void checkResult(WxPayBaseResult result) throws WxErrorException {
121122
}
122123
}
123124

125+
private void checkParameters(WxPayDownloadBillRequest request) throws WxErrorException {
126+
BeanUtils.checkRequiredFields(request);
127+
128+
if (StringUtils.isNotBlank(request.getTarType()) && !"GZIP".equals(request.getTarType())) {
129+
throw new IllegalArgumentException("tar_type值如果存在,只能为GZIP");
130+
}
131+
132+
if ( !ArrayUtils.contains(BILL_TYPE, request.getBillType())) {
133+
throw new IllegalArgumentException("bill_tpye目前必须为" + Arrays.toString(BILL_TYPE) + "其中之一,实际值:" + request.getBillType());
134+
}
135+
136+
}
137+
124138
private void checkParameters(WxPayRefundRequest request) throws WxErrorException {
125139
BeanUtils.checkRequiredFields(request);
126140

@@ -373,6 +387,26 @@ public void report(WxPayReportRequest request) throws WxErrorException {
373387
this.checkResult(result);
374388
}
375389

390+
@Override
391+
public File downloadBill(String billDate, String billType, String tarType, String deviceInfo) throws WxErrorException {
392+
WxPayDownloadBillRequest request = new WxPayDownloadBillRequest();
393+
this.initRequest(request);
394+
request.setBillType(billType);
395+
request.setBillDate(billDate);
396+
request.setTarType(tarType);
397+
request.setDeviceInfo(deviceInfo);
398+
this.checkParameters(request);
399+
request.setSign(this.createSign(request));
400+
401+
String url = this.getPayBaseUrl() + "/pay/downloadbill";
402+
//TODO 返回的内容可能是文件流,也有可能是xml,需要区分对待
403+
String responseContent = this.wxMpService.post(url, request.toXML());
404+
405+
WxPayCommonResult result = WxPayBaseResult.fromXML(responseContent, WxPayCommonResult.class);
406+
this.checkResult(result);
407+
return null;
408+
}
409+
376410
private String executeRequest(String url, String requestStr) throws WxErrorException {
377411
HttpPost httpPost = new HttpPost(url);
378412
if (this.wxMpService.getHttpProxy() != null) {
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package me.chanjar.weixin.mp.bean.pay.request;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
import me.chanjar.weixin.common.annotation.Required;
5+
6+
/**
7+
* <pre>
8+
* 微信支付下载对账单请求参数类
9+
* Created by Binary Wang on 2017-01-11.
10+
* @author <a href="https://github.com/binarywang">binarywang(Binary Wang)</a>
11+
* </pre>
12+
*/
13+
@XStreamAlias("xml")
14+
public class WxPayDownloadBillRequest extends WxPayBaseRequest {
15+
/**
16+
* <pre>
17+
* 设备号
18+
* device_info
19+
* 否
20+
* String(32)
21+
* 13467007045764
22+
* 终端设备号
23+
* </pre>
24+
*/
25+
@XStreamAlias("device_info")
26+
private String deviceInfo;
27+
28+
/**
29+
* <pre>
30+
* 签名类型
31+
* sign_type
32+
* 否
33+
* String(32)
34+
* HMAC-SHA256
35+
* 签名类型,目前支持HMAC-SHA256和MD5,默认为MD5
36+
* </pre>
37+
*/
38+
@XStreamAlias("sign_type")
39+
private String signType;
40+
41+
/**
42+
* <pre>
43+
* 账单类型
44+
* bill_type
45+
* 是
46+
* ALL
47+
* String(8)
48+
* --ALL,返回当日所有订单信息,默认值
49+
* --SUCCESS,返回当日成功支付的订单
50+
* --REFUND,返回当日退款订单
51+
* </pre>
52+
*/
53+
@Required
54+
@XStreamAlias("bill_type")
55+
private String billType;
56+
57+
/**
58+
* <pre>
59+
* 对账单日期
60+
* bill_date
61+
* 是
62+
* String(8)
63+
* 20140603
64+
* 下载对账单的日期,格式:20140603
65+
* </pre>
66+
*/
67+
@Required
68+
@XStreamAlias("bill_date")
69+
private String billDate;
70+
71+
/**
72+
* <pre>
73+
* 压缩账单
74+
* tar_type
75+
* 否
76+
* String(8)
77+
* GZIP
78+
* 非必传参数,固定值:GZIP,返回格式为.gzip的压缩包账单。不传则默认为数据流形式。
79+
* </pre>
80+
*/
81+
@XStreamAlias("tar_type")
82+
private String tarType;
83+
84+
public String getDeviceInfo() {
85+
return deviceInfo;
86+
}
87+
88+
public void setDeviceInfo(String deviceInfo) {
89+
this.deviceInfo = deviceInfo;
90+
}
91+
92+
public String getSignType() {
93+
return signType;
94+
}
95+
96+
public void setSignType(String signType) {
97+
this.signType = signType;
98+
}
99+
100+
public String getBillType() {
101+
return billType;
102+
}
103+
104+
public void setBillType(String billType) {
105+
this.billType = billType;
106+
}
107+
108+
public String getBillDate() {
109+
return billDate;
110+
}
111+
112+
public void setBillDate(String billDate) {
113+
this.billDate = billDate;
114+
}
115+
116+
public String getTarType() {
117+
return tarType;
118+
}
119+
120+
public void setTarType(String tarType) {
121+
this.tarType = tarType;
122+
}
123+
}

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpPayServiceImplTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
import me.chanjar.weixin.mp.api.WxXmlMpInMemoryConfigStorage;
99
import me.chanjar.weixin.mp.bean.pay.request.*;
1010
import me.chanjar.weixin.mp.bean.pay.result.*;
11-
import org.testng.annotations.Guice;
12-
import org.testng.annotations.Test;
11+
import org.testng.annotations.*;
1312

13+
import java.io.File;
1414
import java.nio.file.Files;
1515
import java.nio.file.Path;
1616

@@ -25,6 +25,7 @@
2525
@Test
2626
@Guice(modules = ApiTestModule.class)
2727
public class WxMpPayServiceImplTest {
28+
2829
@Inject
2930
protected WxMpService wxService;
3031

@@ -33,6 +34,14 @@ public void testGetPayInfo() throws Exception {
3334

3435
}
3536

37+
@Test
38+
public void testDownloadBill() throws Exception {
39+
File file = this.wxService.getPayService().downloadBill("20170101","ALL","GZIP","1111111");
40+
assertNotNull(file);
41+
//必填字段为空时,抛出异常
42+
this.wxService.getPayService().downloadBill("","","",null);
43+
}
44+
3645
@Test
3746
public void testReport() throws Exception {
3847
WxPayReportRequest request = new WxPayReportRequest();

0 commit comments

Comments
 (0)