java下载微信对账单并且解析入库代码
时间: 2024-03-04 07:10:36 浏览: 167
JAVA实现的企业对账单下载
好的,下面是Java下载微信对账单并解析入库的示例代码:
```java
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.csv.*;
public class WeChatBillParser {
private static final String MCH_ID = "your_mch_id";
private static final String APP_ID = "your_app_id";
private static final String KEY = "your_key";
private static final String URL = "https://api.mch.weixin.qq.com/pay/downloadbill";
public static void main(String[] args) throws Exception {
String dateString = "20210823"; // 对账单日期,格式为yyyyMMdd
File file = downloadBill(dateString);
List<WeChatBill> bills = parseBill(file);
saveToDatabase(bills);
}
public static File downloadBill(String dateString) throws Exception {
// 构造请求参数
String nonceStr = String.valueOf(System.currentTimeMillis());
String signType = "MD5";
String sign = WeChatPayUtil.signParam(buildParams(dateString, nonceStr), KEY, signType);
String xml = WeChatPayUtil.buildXml(buildParams(dateString, nonceStr, signType, sign));
// 发送请求
HttpURLConnection conn = (HttpURLConnection) new URL(URL).openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setDoOutput(true);
conn.getOutputStream().write(xml.getBytes(StandardCharsets.UTF_8));
conn.getOutputStream().flush();
conn.getOutputStream().close();
// 解析响应
InputStream is = conn.getInputStream();
byte[] buf = new byte[1024];
int len;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = is.read(buf)) != -1) {
bos.write(buf, 0, len);
}
is.close();
File file = File.createTempFile("wechat_bill_", ".csv");
FileOutputStream fos = new FileOutputStream(file);
fos.write(bos.toByteArray());
fos.flush();
fos.close();
return file;
}
public static List<WeChatBill> parseBill(File file) throws IOException {
List<WeChatBill> list = new ArrayList<>();
CSVParser parser = CSVParser.parse(file, StandardCharsets.UTF_8, CSVFormat.DEFAULT.withHeader());
for (CSVRecord record : parser) {
WeChatBill bill = new WeChatBill();
bill.setTradeTime(record.get("交易时间"));
bill.setAppId(record.get("公众账号ID"));
bill.setMchId(record.get("商户号"));
bill.setDeviceInfo(record.get("设备号"));
bill.setTransactionId(record.get("微信订单号"));
bill.setOutTradeNo(record.get("商户订单号"));
bill.setOpenId(record.get("用户标识"));
bill.setTradeType(record.get("交易类型"));
bill.setTradeStatus(record.get("交易状态"));
bill.setBankType(record.get("付款银行"));
bill.setTotalFee(record.get("订单金额"));
bill.setFeeType(record.get("货币种类"));
bill.setSettlementTotalFee(record.get("应结订单金额"));
bill.setCouponFee(record.get("代金券金额"));
bill.setCouponCount(record.get("代金券使用数量"));
bill.setCouponBatchId(record.get("代金券批次ID"));
bill.setCouponId(record.get("单个代金券ID"));
bill.setRefundId(record.get("退款单号"));
bill.setOutRefundNo(record.get("商户退款单号"));
bill.setRefundFee(record.get("退款金额"));
bill.setSettlementRefundFee(record.get("应结退款金额"));
bill.setRefundType(record.get("退款类型"));
bill.setRefundStatus(record.get("退款状态"));
bill.setProductInfo(record.get("商品名称"));
bill.setAttach(record.get("商户数据包"));
bill.setFeeRate(record.get("费率"));
bill.setCreateDate(record.get("交易创建时间"));
bill.setPayDate(record.get("支付完成时间"));
list.add(bill);
}
parser.close();
return list;
}
public static void saveToDatabase(List<WeChatBill> bills) {
// TODO: 将账单数据存储到数据库中
}
private static String buildParams(String dateString, String nonceStr) {
StringBuilder sb = new StringBuilder();
sb.append("appid=").append(APP_ID)
.append("&bill_date=").append(dateString)
.append("&bill_type=ALL")
.append("&mch_id=").append(MCH_ID)
.append("&nonce_str=").append(nonceStr)
.append("&sign_type=MD5");
return sb.toString();
}
private static String buildParams(String dateString, String nonceStr, String signType, String sign) {
StringBuilder sb = new StringBuilder();
sb.append("<xml>")
.append("<appid>").append(APP_ID).append("</appid>")
.append("<mch_id>").append(MCH_ID).append("</mch_id>")
.append("<nonce_str>").append(nonceStr).append("</nonce_str>")
.append("<sign>").append(sign).append("</sign>")
.append("<sign_type>").append(signType).append("</sign_type>")
.append("<bill_date>").append(dateString).append("</bill_date>")
.append("<bill_type>ALL</bill_type>")
.append("</xml>");
return sb.toString();
}
}
```
上面的代码中,WeChatBill是一个POJO类,用于存储解析后的对账单数据,WeChatPayUtil是一个工具类,用于生成签名等辅助操作。请根据自己的实际情况修改代码中的常量和TODO注释。
阅读全文