微信对账单功能开发(V2)
微信对账单功能开发(V2)
概述
微信对账单功能是微信支付系统中的一项重要组成部分,它允许商户和用户查看交易历史记录,方便管理和核实。该功能的开发需要遵循微信支付平台提供的接口规范和要求。
下载交易账单接口开发
接口描述下载交易账单接口用于获取指定时间范围内的交易记录。该接口支持 JSON 和 XML 格式的数据返回。
请求参数| 参数名称 | 类型 | 必填 |说明 |
| --- | --- | --- | --- |
| appid | string | 是 | 商户appid |
| mch_id | string | 是 | 商户号 |
| bill_date | string | 是 | 账单日期(格式:YYYYMMDD) |
| bill_type | string | 是 | 账单类型(ALL、SUCCESS、REFUND) |
返回参数| 参数名称 | 类型 |说明 |
| --- | --- | --- |
| appid | string | 商户appid |
| mch_id | string | 商户号 |
| bill_date | string | 账单日期(格式:YYYYMMDD) |
| bill_type | string | 账单类型(ALL、SUCCESS、REFUND) |
| total_count | int | 总笔数 |
| start_bill_date | string | 起始账单日期(格式:YYYYMMDD) |
| end_bill_date | string | 结束账单日期(格式:YYYYMMDD) |
| bill_list | array | 账单列表 |
账单列表| 参数名称 | 类型 |说明 |
| --- | --- | --- |
| bill_date | string | 账单日期(格式:YYYYMMDD) |
| bill_type | string | 账单类型(ALL、SUCCESS、REFUND) |
| total_amount | int | 总金额 |
| settle_amount | int | 结算金额 |
| cash_fee | int | 现金支付金额 |
| refund_count | int |退款笔数 |
接口实现 Java 实现```javaimport javax.servlet. javax.servlet. javax.servlet. class DownloadBillServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取请求参数 String appid = req.getParameter("appid");
String mch_id = req.getParameter("mch_id");
String bill_date = req.getParameter("bill_date");
String bill_type = req.getParameter("bill_type");
// 验证参数有效性 if (StringUtils.isEmpty(appid) || StringUtils.isEmpty(mch_id) || StringUtils.isEmpty(bill_date) || StringUtils.isEmpty(bill_type)) {
resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// 获取账单列表 List
// 构造返回 JSON JSONObject jsonObject = new JSONObject();
jsonObject.put("appid", appid);
jsonObject.put("mch_id", mch_id);
jsonObject.put("bill_date", bill_date);
jsonObject.put("bill_type", bill_type);
jsonObject.put("total_count", billList.size());
jsonObject.put("start_bill_date", billList.get(0).getBillDate());
jsonObject.put("end_bill_date", billList.get(billList.size() -1).getBillDate());
jsonObject.put("bill_list", new JSONArray(billList));
// 返回 JSON resp.setContentType("application/json");
resp.getWriter().write(jsonObject.toString());
}
private List
// 实现获取账单列表的逻辑 return null;
}
}
```
Python 实现```pythonfrom flask import Flask, request, jsonifyapp = Flask(__name__)
@app.route("/download_bill", methods=["GET"])
def download_bill():
获取请求参数 appid = request.args.get("appid")
mch_id = request.args.get("mch_id")
bill_date = request.args.get("bill_date")
bill_type = request.args.get("bill_type")
验证参数有效性 if not all([appid, mch_id, bill_date, bill_type]):
return jsonify({"error": "Invalid parameters"}),400 获取账单列表 bill_list = get_bill_list(appid, mch_id, bill_date, bill_type)
构造返回 JSON data = {
"appid": appid,
"mch_id": mch_id,
"bill_date": bill_date,
"bill_type": bill_type,
"total_count": len(bill_list),
"start_bill_date": bill_list[0].get("bill_date"),
"end_bill_date": bill_list[-1].get("bill_date"),
"bill_list": bill_list }
返回 JSON return jsonify(data)
def get_bill_list(appid, mch_id, bill_date, bill_type):
实现获取账单列表的逻辑 return []
```
接口测试 Java 测试```javaimport org.junit.Test;
import static org.junit.Assert.*;
public class DownloadBillServletTest {
@Test public void testDownloadBill() {
// 创建 HTTP 请求 HttpServletRequest req = mock(HttpServletRequest.class);
HttpServletResponse resp = mock(HttpServletResponse.class);
// 设置请求参数 req.setParameter("appid", "1234567890");
req.setParameter("mch_id", "9876543210");
req.setParameter("bill_date", "20220101");
req.setParameter("bill_type", "ALL");
// 执行下载账单接口 DownloadBillServlet servlet = new DownloadBillServlet();
servlet.doGet(req, resp);
// 验证返回 JSON JSONObject jsonObject = (JSONObject) resp.getReader().readObject();
assertEquals("1234567890", jsonObject.getString("appid"));
assertEquals("9876543210", jsonObject.getString("mch_id"));
assertEquals("20220101", jsonObject.getString("bill_date"));
assertEquals("ALL", jsonObject.getString("bill_type"));
}
}
```
Python 测试```pythonimport unittestclass TestDownloadBill(unittest.TestCase):
def test_download_bill(self):
创建 HTTP 请求 req = mock_request()
resp = mock_response()
设置请求参数 req.args["appid"] = "1234567890"
req.args["mch_id"] = "9876543210"
req.args["bill_date"] = "20220101"
req.args["bill_type"] = "ALL"
执行下载账单接口 download_bill(req, resp)
验证返回 JSON data = resp.json()
self.assertEqual(data["appid"], "1234567890")
self.assertEqual(data["mch_id"], "9876543210")
self.assertEqual(data["bill_date"], "20220101")
self.assertEqual(data["bill_type"], "ALL")
if __name__ == "__main__":
unittest.main()
```
接口部署 Java 部署```bash 编译代码mvn clean package 部署到 Tomcatcp target/DownloadBillServlet.war /path/to/tomcat/webapps/
```
Python 部署```bash 编译代码python setup.py sdist 部署到 Gunicorngunicorn -w4 -b0.0.0.0:8000 download_bill:app```
接口维护 Java 维护```javaimport org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class DownloadBillApplication {
public static void main(String[] args) {
SpringApplication.run(DownloadBillApplication.class, args);
}
}
```
Python 维护```pythonfrom flask import Flaskapp = Flask(__name__)
@app.route("/")
def index():
return "Hello, World!"
if __name__ == "__main__":
app.run(debug=True)
```
接口监控 Java 监控```javaimport org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Componentpublic class DownloadBillHealthIndicator implements HealthIndicator {
@Override public boolean isHealthy() {
// 实现健康检查逻辑 return true;
}
}
```
Python 监控```pythonfrom flask import Flaskapp = Flask(__name__)
@app.route("/health")
def health():
实现健康检查逻辑 return "OK"
if __name__ == "__main__":
app.run(debug=True)
```
接口安全 Java 安全```javaimport javax.servlet. javax.servlet. javax.servlet. class DownloadBillServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 实现安全检查逻辑 if (req.getParameter("appid") != null && req.getParameter("mch_id") != null) {
// 执行