https://opendocs.alipay.com/apis/api_15/alipay.data.dataservice.bill.downloadurl.query
为方便商户快速查账,支持商户通过本接口获取商户离线账单下载地址
pom:
cn.hutool
hutool-all
5.0.7
工具类:
package com.lwjwlkj.common.util;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
/**
* @Author: CL
* @Date: 2020/6/23 13:11
* @Description: 解压工具类
*/
@Slf4j
public class ZipUtil {
public static void unzip(File zipFile, String filePath) {
try (ZipArchiveInputStream inputStream = getZipFile(zipFile)) {
ZipArchiveEntry entry = null;
while ((entry = inputStream.getNextZipEntry()) != null) {
if (entry.isDirectory()) {
File directory = new File(filePath, entry.getName());
directory.mkdirs();
} else {
OutputStream os = null;
try {
os = new BufferedOutputStream(new FileOutputStream(new File(filePath, entry.getName())));
//输出文件路径信息
System.out.println(entry.getName());
IOUtils.copy(inputStream, os);
} finally {
IOUtils.closeQuietly(os);
}
}
}
} catch (Exception e) {
log.error("unzip" + e);
}
}
private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
return new ZipArchiveInputStream(new FileInputStream(zipFile), "GBK");
}
public static void makdirs(String filePath) {
File pathFile = new File(filePath);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
}
public static void delAllFile(String path) {
File file = new File(path);
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
}
}
}
}
#### 实体类
package com.lwjwlkj.common.pojo.mysql.bill;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.persistence.Column;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;
@Data
public class SysAlipayBillRecord implements Serializable {
private static final long serialVersionUID = 1L;
public SysAlipayBillRecord() {
super();
}
private Integer id;
/**
* 支付宝交易号
*/
@ApiModelProperty(value = "支付宝交易号")
@Column(name = "trade_no")
private String tradeNo;
/**
* 商户订单号
*/
@ApiModelProperty(value = "商户订单号")
@Column(name = "out_trade_no")
private String outTradeNo;
/**
* 业务类型
*/
@ApiModelProperty(value = "业务类型")
@Column(name = "business_type")
private String businessType;
/**
* 商品名称
*/
@ApiModelProperty(value = "商品名称")
@Column(name = "goods_name")
private String goodsName;
/**
* 交易创建时间
*/
@ApiModelProperty(value = "交易创建时间")
@Column(name = "trade_create_time")
private Date tradeCreateTime;
/**
* 交易完成时间
*/
@ApiModelProperty(value = "交易完成时间")
@Column(name = "trade_finsh_time")
private Date tradeFinshTime;
/**
* 门店编号
*/
@ApiModelProperty(value = "门店编号")
@Column(name = "store_id")
private String storeId;
/**
* 门店名称
*/
@ApiModelProperty(value = "门店名称")
@Column(name = "store_name")
private String storeName;
/**
* 操作员
*/
@ApiModelProperty(value = "操作员")
@Column(name = "operator_id")
private String operatorId;
/**
* 终端号
*/
@ApiModelProperty(value = "终端号")
@Column(name = "terminal_id")
private String terminalId;
/**
* 对方账户
*/
@ApiModelProperty(value = "对方账户")
@Column(name = "trans_no")
private String transNo;
/**
* 订单金额
*/
@ApiModelProperty(value = "订单金额")
@Column(name = "total_amount")
private BigDecimal totalAmount;
/**
* 商家实收
*/
@ApiModelProperty(value = "商家实收")
@Column(name = "out_total_amount")
private BigDecimal outTotalAmount;
/**
* 支付宝红包
*/
@ApiModelProperty(value = "支付宝红包")
@Column(name = "alipay_red_amount")
private BigDecimal alipayRedAmount;
/**
* 集分宝
*/
@ApiModelProperty(value = "集分宝")
@Column(name = "point_amount")
private BigDecimal pointAmount;
/**
* 支付宝优惠
*/
@ApiModelProperty(value = "支付宝优惠")
@Column(name = "alipay_discounts_amount")
private BigDecimal alipayDiscountsAmount;
/**
* 商家优惠
*/
@ApiModelProperty(value = "商家优惠")
@Column(name = "out_discounts_amount")
private BigDecimal outDiscountsAmount;
/**
* 券核销金额
*/
@ApiModelProperty(value = "券核销金额")
@Column(name = "coupon_amount")
private BigDecimal couponAmount;
/**
* 券名称
*/
@ApiModelProperty(value = "券名称")
@Column(name = "coupon_name")
private String couponName;
/**
* 商家红包消费
*/
@ApiModelProperty(value = "商家红包消费")
@Column(name = "out_red_amount")
private BigDecimal outRedAmount;
/**
* 卡消费金额
*/
@ApiModelProperty(value = "卡消费金额")
@Column(name = "card_amount")
private BigDecimal cardAmount;
/**
* 退款批次号
*/
@ApiModelProperty(value = "退款批次号")
@Column(name = "refund_no")
private String refundNo;
/**
* 服务费
*/
@ApiModelProperty(value = "服务费")
@Column(name = "cover_charge_amount")
private BigDecimal coverChargeAmount;
/**
* 分润
*/
@ApiModelProperty(value = "分润")
@Column(name = "share_benefit_amount")
private BigDecimal shareBenefitAmount;
/**
* 备注
*/
@ApiModelProperty(value = "备注")
@Column(name = "remark")
private String remark;
/**
* 创建时间
*/
@ApiModelProperty(value = "创建时间")
@Column(name = "create_time")
private Date createTime;
}
```
#### 实现方法:
<<我查的是前两天的
```java
public void queryAliData() throws Exception {
//查询前天的数据
String beforeYesterday = DateUtil.beforeYesterday();
AlipayClient alipayClient = new DefaultAlipayClient(ResourcesUtil.ALI_PAY_URL, ResourcesUtil.ALI_APP_ID, ResourcesUtil.ALI_PRIVATE_KEY, "json", ResourcesUtil.ALICHART2, ResourcesUtil.ALI_PUBLIC_KEY, "RSA2");
AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
AlipayDataDataserviceBillDownloadurlQueryModel model = new AlipayDataDataserviceBillDownloadurlQueryModel();
//trade指商户基于支付宝交易收单的业务账单
model.setBillType("trade");
model.setBillDate(beforeYesterday);
request.setBizModel(model);
AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);
log.info("AlipayDataDataserviceBillDownloadurlQueryResponse:{}", response);
if ("10000".equals(response.getCode()) && "Success".equals(response.getMsg())) {
ZipUtil.makdirs(ResourcesUtil.ALI_PAY_DOWNLOAD_URL);
HttpUtil.downloadFile(response.getBillDownloadUrl(), FileUtil.file(ResourcesUtil.ALI_PAY_DOWNLOAD_URL));
ZipUtil.unzip(new File(ResourcesUtil.ALI_PAY_DOWNLOAD_URL + "20887310750458600156_" + beforeYesterday.replaceAll("-", "") + ".csv.zip"), ResourcesUtil.ALI_PAY_DOWNLOAD_URL);
try {
//从文件中读取CSV数据
DataInputStream dataInputStream = new DataInputStream(new FileInputStream(new File(ResourcesUtil.ALI_PAY_DOWNLOAD_URL + "20887310750458600156_" + beforeYesterday.replaceAll("-", "") + "_业务明细.csv")));
//设置格式
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(dataInputStream, "GBK"));
//去标头
for (int i = 0; i < 5; i++) {
String title = bufferedReader.readLine();
log.info("title-msg:" + title);
}
String rowRecord = null;
//舍标尾
while ((rowRecord = bufferedReader.readLine()) != null) {
if ("#-----------------------------------------业务明细列表结束------------------------------------".equals(rowRecord)) {
bufferedReader.close();
return;
}
log.info("content-msg:" + rowRecord);
String[] alipayBill = rowRecord.split(",");
SysAlipayBillRecord sysAlipayBillRecord = new SysAlipayBillRecord();
sysAlipayBillRecord.setTradeNo(alipayBill[0].replaceAll("\t", ""));
sysAlipayBillRecord.setOutTradeNo(alipayBill[1].replaceAll("\t", ""));
sysAlipayBillRecord.setBusinessType(alipayBill[2].replaceAll("\t", ""));
sysAlipayBillRecord.setGoodsName(alipayBill[3].replaceAll("\t", ""));
if (!StringUtils.isEmpty(alipayBill[4])) {
sysAlipayBillRecord.setTradeCreateTime(DateUtil.strToDateStr(alipayBill[4].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[5])) {
sysAlipayBillRecord.setTradeFinshTime(DateUtil.strToDateStr(alipayBill[5].replaceAll("\t", "")));
}
sysAlipayBillRecord.setStoreId(alipayBill[6].replaceAll("\t", ""));
sysAlipayBillRecord.setStoreName(alipayBill[7].replaceAll("\t", ""));
sysAlipayBillRecord.setOperatorId(alipayBill[8].replaceAll("\t", ""));
sysAlipayBillRecord.setTerminalId(alipayBill[9].replaceAll("\t", ""));
sysAlipayBillRecord.setTransNo(alipayBill[10].replaceAll("\t", ""));
if (!StringUtils.isEmpty(alipayBill[11])) {
sysAlipayBillRecord.setTotalAmount(new BigDecimal(alipayBill[11].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[12])) {
sysAlipayBillRecord.setOutTotalAmount(new BigDecimal(alipayBill[12].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[13])) {
sysAlipayBillRecord.setAlipayRedAmount(new BigDecimal(alipayBill[13].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[14])) {
sysAlipayBillRecord.setPointAmount(new BigDecimal(alipayBill[14].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[15])) {
sysAlipayBillRecord.setAlipayDiscountsAmount(new BigDecimal(alipayBill[15].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[16])) {
sysAlipayBillRecord.setOutDiscountsAmount(new BigDecimal(alipayBill[16].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[17])) {
sysAlipayBillRecord.setCouponAmount(new BigDecimal(alipayBill[17].replaceAll("\t", "")));
}
sysAlipayBillRecord.setCouponName(alipayBill[18].replaceAll("\t", ""));
if (!StringUtils.isEmpty(alipayBill[19])) {
sysAlipayBillRecord.setOutRedAmount(new BigDecimal(alipayBill[19].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[20])) {
sysAlipayBillRecord.setCardAmount(new BigDecimal(alipayBill[20].replaceAll("\t", "")));
}
sysAlipayBillRecord.setRefundNo(alipayBill[21].replaceAll("\t", ""));
if (!StringUtils.isEmpty(alipayBill[22])) {
sysAlipayBillRecord.setCoverChargeAmount(new BigDecimal(alipayBill[22].replaceAll("\t", "")));
}
if (!StringUtils.isEmpty(alipayBill[23])) {
sysAlipayBillRecord.setShareBenefitAmount(new BigDecimal(alipayBill[23].replaceAll("\t", "")));
}
if (25 == alipayBill.length) {
sysAlipayBillRecord.setRemark((alipayBill[24].replaceAll("\t", "")));
}
sysAlipayBillRecord.setCreateTime(new Date());
alipayBillRecordMapper.insertSelective(sysAlipayBillRecord);
}
} finally {
ZipUtil.delAllFile(ResourcesUtil.ALI_PAY_DOWNLOAD_URL);
}
} else {
log.info("对账请求异常:{}", response);
}
}
```
以上是我个人的实现方法,如果有疑问欢迎和我交流~