微信对账单 java_[Java]获取微信api中的下载对账单和下载资金账单功能

本文介绍了如何使用Java调用微信API来下载对账单和资金账单。通过添加特定的maven依赖,重写配置类,设置必要的参数,并处理返回的XML数据,将其转化为Java对象并输出为JSON。
摘要由CSDN通过智能技术生成

微信官方给的sdk有点问题,通过百度找到了maven依赖,这个比较靠谱

添加依赖

com.github.wxpay

wxpay-sdk

0.0.3

com.google.code.gson

gson

2.8.2

重写config

public class MyConfig implements WXPayConfig {

private byte[] certData;

public MyConfig() throws Exception {

String certPath = "F:\\work\\WxPayAPI_JAVA\\java_sdk_v3.0.9\\src\\main\\resource\\apiclient_cert.p12";

File file = new File(certPath);

InputStream certStream = new FileInputStream(file);

this.certData = new byte[(int) file.length()];

certStream.read(this.certData);

certStream.close();

}

public String getAppID() {

return "wx888888888";

}

public String getMchID() {

return "1234567897";

}

public String getKey() {

return "00000000000000000000000";

}

public InputStream getCertStream() {

ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);

return certBis;

}

public int getHttpConnectTimeoutMs() {

return 8000;

}

public int getHttpReadTimeoutMs() {

return 10000;

}

}

创建测试文件 WXPayExample.java

MyConfig config = new MyConfig();

WXPay wxpay = new WXPay(config, HMACSHA256);

Map data = new HashMap();

data.put("bill_date", "20181117");

data.put("bill_type", "ALL");

对账单数据

请求下载对账单的api方法

Map resp = wxpay.downloadBill(data);

String s = resp.get("data");

对于数据返回的 比较麻烦,所以用bean对其进行处理,方便装换成我们需要的数据类型

接收的数据bean

public class Pay

您好,下载微信账单并将其写入数据库可以分为以下步骤: 1. 登录微信支付商户平台,进入“交易记录”页面,选择需要下载账单的日期范围和账单类型,点击“下载”按钮。 2. 通过 Java 代码将下载的对账单保存到本地磁盘上,可以使用 HttpURLConnection 进行 HTTP 请求获取账单文件。 3. 将对账单文件的数据读取出来,解析成 Java 对象。 4. 将解析出来的数据写入到数据库,可以使用 JDBC 连接数据库并执行 SQL 语句。 下面是一个简单的 Java 代码示例,用于下载微信账单并将其写入数据库: ```java import java.io.*; import java.net.*; import java.sql.*; import org.dom4j.*; import org.dom4j.io.SAXReader; public class WeChatBillDownloader { public static void main(String[] args) { try { // 设置日期范围和账单类型 String billDate = "20190101"; String billType = "ALL"; // 发送 HTTP 请求下载账单文件 URL url = new URL("https://api.mch.weixin.qq.com/pay/downloadbill"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setDoOutput(true); OutputStream out = conn.getOutputStream(); out.write(("bill_date=" + billDate + "&bill_type=" + billType).getBytes()); out.flush(); out.close(); // 保存对账单文件到本地磁盘 InputStream in = conn.getInputStream(); File billFile = new File("bill.csv"); FileOutputStream fos = new FileOutputStream(billFile); byte[] buffer = new byte[4096]; int len; while ((len = in.read(buffer)) != -1) { fos.write(buffer, 0, len); } in.close(); fos.close(); // 解析对账单文件 SAXReader reader = new SAXReader(); Document document = reader.read(billFile); Element root = document.getRootElement(); List<Element> billElements = root.elements("bill"); for (Element billElement : billElements) { // 将每条对账单数据写入到数据库 String[] fields = billElement.getText().split(","); String sql = "INSERT INTO bill VALUES (?, ?, ?, ?, ?)"; Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password"); PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, fields[0]); pstmt.setString(2, fields[1]); pstmt.setString(3, fields[2]); pstmt.setString(4, fields[3]); pstmt.setString(5, fields[4]); pstmt.executeUpdate(); pstmt.close(); conn.close(); } } catch (Exception e) { e.printStackTrace(); } } } ``` 在上面的代码,我们使用了 HttpURLConnection 发送 HTTP 请求下载账单文件,并使用 SAXReader 解析对账单文件的数据。最后,我们使用 JDBC 连接 MySQL 数据库,并将解析出来的数据写入到数据库。请注意替换代码的数据库连接信息为您自己的数据库连接信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值