上传电子小票开发指引

更新时间:2023.03.13

本文档主要涉及开发指引文档超链接中对上传电子小票场景的处理流程。

# 前置步骤

在阅读本开发指引前,请确保您已经:

  • 已联系微信支付运营将上传商户号加入白名单

# 上传电子小票

用户在支付完成后,商户可以为该笔支付订单上传电子小票。上传电子小票需要一些基础的交易信息来指定需要上传小票的订单,这部分信息商户可自己通过支付成功回调、查询订单信息或从其他商家处获取。为了避免商户图片信息在上传过程中被篡改,需要用户在文本信息中携带图片的sha256哈希码,电子小票系统在收到图片信息后会计算sha256与文本信息中的sha256进行比对。

1// sdk会根据文件后缀填写file的content_type,请确保后缀填写正确。
2 String filePath = "/your/home/test.png";
3 URI uri = new URI("https://api.mch.weixin.qq.com/v3/marketing/shopping-receipt/shoppingreceipts");
4
5 File file = new File(filePath);
6 try (FileInputStream fileIs = new FileInputStream(file)) {
7 String transaction_id = "420000153220220···158964";
8 String transaction_mchid = "1900006#";
9 String transaction_sub_mchid = "";
10 String out_trade_no = "sdk123456789202205#809";
11 String openid = "oK7fFt8zzEZ909XH-LE2#";
12 String upload_time = "2022-05-07T15:39:35.000+08:00";
13 String meta = "";
14 String sha256 = DigestUtils.sha256Hex(fileIs);
15 if (transaction_sub_mchid == "") {
16 meta = String.format("{\"transaction_id\":\"%s\",\"transaction_mchid\":\"%s\",\"out_trade_no\":\"%s\",\"openid\":\"%s\",\"sha256\":\"%s\",\"upload_time\":\"%s\"}}", transaction_id, transaction_mchid, out_trade_no, openid, sha256, upload_time);
17 } else {
18 meta = String.format("{\"transaction_id\":\"%s\",\"transaction_mchid\":\"%s\",\"transaction_sub_mchid\":\"%s\",\"out_trade_no\":\"%s\",\"openid\":\"%s\",\"sha256\":\"%s\",\"upload_time\":\"%s\"}}", transaction_id, transaction_mchid, transaction_sub_mchid, out_trade_no, openid, sha256, upload_time);
19 }
20 try (InputStream is = new FileInputStream(file)) {
21 WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri)
22 .withFile(file.getName(), meta, is)
23 .build();
24 try (CloseableHttpResponse response = httpClient.execute(request)) {
25 // do something useful with the response body
26 // and ensure it is fully consumed
27 String s = EntityUtils.toString(response.getEntity());
28 System.out.println("result: "+s);
29 }
30 }
31 }

# 扩展阅读

请参考上传电子小票,了解上传电子小票场景的开发流程。

更多技术问题
技术咨询
反馈
咨询
目录