mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-18 14:16:25 +08:00
#503 微信支付增加资金账单下载接口
This commit is contained in:
@@ -1,44 +1,19 @@
|
||||
package com.github.binarywang.wxpay.service;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.WxPayApiData;
|
||||
import com.github.binarywang.wxpay.bean.coupon.*;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.request.*;
|
||||
import com.github.binarywang.wxpay.bean.result.*;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.WxPayApiData;
|
||||
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryRequest;
|
||||
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponInfoQueryResult;
|
||||
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendRequest;
|
||||
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponSendResult;
|
||||
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryRequest;
|
||||
import com.github.binarywang.wxpay.bean.coupon.WxPayCouponStockQueryResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxScanPayNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayAuthcode2OpenidRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayDownloadBillRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayMicropayRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayOrderCloseRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayOrderQueryRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayOrderReverseRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayRefundQueryRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayReportRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPaySendRedpackRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayShorturlRequest;
|
||||
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayBillResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayMicropayResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayOrderCloseResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayOrderQueryResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayOrderReverseResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayRedpackQueryResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayRefundQueryResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPaySendRedpackResult;
|
||||
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 微信支付相关接口.
|
||||
@@ -378,6 +353,42 @@ public interface WxPayService {
|
||||
*/
|
||||
WxPayBillResult downloadBill(WxPayDownloadBillRequest request) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 下载资金账单.
|
||||
* 商户可以通过该接口下载自2017年6月1日起 的历史资金流水账单。
|
||||
* 注意:
|
||||
* 1、资金账单中的数据反映的是商户微信账户资金变动情况;
|
||||
* 2、当日账单在次日上午9点开始生成,建议商户在上午10点以后获取;
|
||||
* 3、资金账单中涉及金额的字段单位为“元”。
|
||||
* 接口链接:https://api.mch.weixin.qq.com/pay/downloadfundflow
|
||||
* 详情请见: <a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_18">下载对账单</a>
|
||||
* </pre>
|
||||
*
|
||||
* @param billDate 资金账单日期 bill_date 下载对账单的日期,格式:20140603
|
||||
* @param accountType 资金账户类型 account_type Basic,基本账户,Operation,运营账户,Fees,手续费账户
|
||||
* @param tarType 压缩账单 tar_type 非必传参数,固定值:GZIP,返回格式为.gzip的压缩包账单。不传则默认为数据流形式。
|
||||
* @return WxPayFundFlowResult对象
|
||||
*/
|
||||
WxPayFundFlowResult downloadFundFlow(String billDate, String accountType, String tarType) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 下载资金账单.
|
||||
* 商户可以通过该接口下载自2017年6月1日起 的历史资金流水账单。
|
||||
* 注意:
|
||||
* 1、资金账单中的数据反映的是商户微信账户资金变动情况;
|
||||
* 2、当日账单在次日上午9点开始生成,建议商户在上午10点以后获取;
|
||||
* 3、资金账单中涉及金额的字段单位为“元”。
|
||||
* 接口链接:https://api.mch.weixin.qq.com/pay/downloadfundflow
|
||||
* 详情请见: <a href="https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_18">下载对账单</a>
|
||||
* </pre>
|
||||
*
|
||||
* @param request 下载资金流水请求
|
||||
* @return WxPayFundFlowResult对象
|
||||
*/
|
||||
WxPayFundFlowResult downloadFundFlow(WxPayDownloadFundFlowRequest request) throws WxPayException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 提交刷卡支付.
|
||||
|
||||
@@ -581,6 +581,125 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
|
||||
return wxPayBillResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayFundFlowResult downloadFundFlow(String billDate, String accountType, String tarType) throws WxPayException {
|
||||
|
||||
WxPayDownloadFundFlowRequest request = new WxPayDownloadFundFlowRequest();
|
||||
request.setBillDate(billDate);
|
||||
request.setAccountType(accountType);
|
||||
request.setTarType(tarType);
|
||||
|
||||
return this.downloadFundFlow(request);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayFundFlowResult downloadFundFlow(WxPayDownloadFundFlowRequest request) throws WxPayException {
|
||||
request.checkAndSign(this.getConfig());
|
||||
|
||||
String url = this.getPayBaseUrl() + "/pay/downloadfundflow";
|
||||
|
||||
String responseContent;
|
||||
if (TarType.GZIP.equals(request.getTarType())) {
|
||||
responseContent = this.handleGzipFundFlow(url, request.toXML());
|
||||
} else {
|
||||
responseContent = this.post(url, request.toXML(), true);
|
||||
if (responseContent.startsWith("<")) {
|
||||
throw WxPayException.from(BaseWxPayResult.fromXML(responseContent, WxPayCommonResult.class));
|
||||
}
|
||||
}
|
||||
|
||||
return this.handleFundFlow(responseContent);
|
||||
}
|
||||
|
||||
private String handleGzipFundFlow(String url, String requestStr) throws WxPayException {
|
||||
try {
|
||||
byte[] responseBytes = this.postForBytes(url, requestStr, true);
|
||||
Path tempDirectory = Files.createTempDirectory("fundFlow");
|
||||
Path path = Paths.get(tempDirectory.toString(), System.currentTimeMillis() + ".gzip");
|
||||
Files.write(path, responseBytes);
|
||||
|
||||
try {
|
||||
List<String> allLines = Files.readAllLines(ZipUtil.ungzip(path.toFile()).toPath(), StandardCharsets.UTF_8);
|
||||
return Joiner.on("\n").join(allLines);
|
||||
} catch (ZipException e) {
|
||||
if (e.getMessage().contains("Not in GZIP format")) {
|
||||
throw WxPayException.from(BaseWxPayResult.fromXML(new String(responseBytes, StandardCharsets.UTF_8),
|
||||
WxPayCommonResult.class));
|
||||
} else {
|
||||
this.log.error("解压zip文件出错", e);
|
||||
throw new WxPayException("解压zip文件出错");
|
||||
}
|
||||
}
|
||||
} catch (WxPayException wxPayException) {
|
||||
throw wxPayException;
|
||||
} catch (Exception e){
|
||||
this.log.error("解析对账单文件时出错", e);
|
||||
throw new WxPayException("解压zip文件出错");
|
||||
}
|
||||
}
|
||||
|
||||
private WxPayFundFlowResult handleFundFlow(String responseContent) {
|
||||
WxPayFundFlowResult wxPayFundFlowResult = new WxPayFundFlowResult();
|
||||
|
||||
String listStr = "";
|
||||
String objStr = "";
|
||||
|
||||
if (StringUtils.isNotBlank(responseContent) && responseContent.contains("资金流水总笔数")) {
|
||||
listStr = responseContent.substring(0, responseContent.indexOf("资金流水总笔数"));
|
||||
objStr = responseContent.substring(responseContent.indexOf("资金流水总笔数"));
|
||||
}
|
||||
/*
|
||||
* 记账时间:2018-02-01 04:21:23 微信支付业务单号:50000305742018020103387128253 资金流水单号:1900009231201802015884652186 业务名称:退款
|
||||
* 业务类型:退款 收支类型:支出 收支金额(元):0.02 账户结余(元):0.17 资金变更提交申请人:system 备注:缺货 业务凭证号:REF4200000068201801293084726067
|
||||
* 参考以上格式进行取值
|
||||
*/
|
||||
List<WxPayFundFlowBaseResult> wxPayFundFlowBaseResultList = new LinkedList<>();
|
||||
// 去空格
|
||||
String newStr = listStr.replaceAll(",", " ");
|
||||
// 数据分组
|
||||
String[] tempStr = newStr.split("`");
|
||||
// 分组标题
|
||||
String[] t = tempStr[0].split(" ");
|
||||
// 计算循环次数
|
||||
int j = tempStr.length / t.length;
|
||||
// 纪录数组下标
|
||||
int k = 1;
|
||||
for (int i = 0; i < j; i++) {
|
||||
WxPayFundFlowBaseResult wxPayFundFlowBaseResult = new WxPayFundFlowBaseResult();
|
||||
|
||||
wxPayFundFlowBaseResult.setBillingTime(tempStr[k].trim());
|
||||
wxPayFundFlowBaseResult.setBizTransactionId(tempStr[k + 1].trim());
|
||||
wxPayFundFlowBaseResult.setFundFlowId(tempStr[k + 2].trim());
|
||||
wxPayFundFlowBaseResult.setBizName(tempStr[k + 3].trim());
|
||||
wxPayFundFlowBaseResult.setBizType(tempStr[k + 4].trim());
|
||||
wxPayFundFlowBaseResult.setFinancialType(tempStr[k + 5].trim());
|
||||
wxPayFundFlowBaseResult.setFinancialFee(tempStr[k + 6].trim());
|
||||
wxPayFundFlowBaseResult.setAccountBalance(tempStr[k + 7].trim());
|
||||
wxPayFundFlowBaseResult.setFundApplicant(tempStr[k + 8].trim());
|
||||
wxPayFundFlowBaseResult.setMemo(tempStr[k + 9].trim());
|
||||
wxPayFundFlowBaseResult.setBizVoucherId(tempStr[k + 10].trim());
|
||||
|
||||
wxPayFundFlowBaseResultList.add(wxPayFundFlowBaseResult);
|
||||
k += t.length;
|
||||
}
|
||||
wxPayFundFlowResult.setWxPayFundFlowBaseResultList(wxPayFundFlowBaseResultList);
|
||||
|
||||
/*
|
||||
* 资金流水总笔数,收入笔数,收入金额,支出笔数,支出金额 `20.0,`17.0,`0.35,`3.0,`0.18
|
||||
* 参考以上格式进行取值
|
||||
*/
|
||||
String totalStr = objStr.replaceAll(",", " ");
|
||||
String[] totalTempStr = totalStr.split("`");
|
||||
wxPayFundFlowResult.setTotalRecord(totalTempStr[1]);
|
||||
wxPayFundFlowResult.setIncomeRecord(totalTempStr[2]);
|
||||
wxPayFundFlowResult.setIncomeAmount(totalTempStr[3]);
|
||||
wxPayFundFlowResult.setExpenditureRecord(totalTempStr[4]);
|
||||
wxPayFundFlowResult.setExpenditureAmount(totalTempStr[5]);
|
||||
|
||||
return wxPayFundFlowResult;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayMicropayResult micropay(WxPayMicropayRequest request) throws WxPayException {
|
||||
request.checkAndSign(this.getConfig());
|
||||
|
||||
Reference in New Issue
Block a user