申请交易账单
更新时间:2025.01.10微信支付在每日10点后生成昨日交易账单文件,商户可通过接口获取账单下载链接。账单包含交易金额、时间及营销信息,利于订单核对、退款审查及银行到账确认。详细介绍参考:下载账单-产品介绍。
注意:
仅支付成功的订单包含在账单内。
账单金额单位为人民币“元”。
此接口仅可下载三个月内的账单。
接口说明
支持商户:【普通商户】
请求方式:【GET】/v3/bill/tradebill
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
query 查询参数
bill_date 必填 string(10)
【账单日期】 账单日期,格式yyyy-MM-DD,仅支持三个月内的账单下载申请。
bill_type 选填 string
【账单类型】 账单类型,不填则默认是ALL
可选取值
ALL
: 返回当日所有订单信息(不含充值退款订单)SUCCESS
: 返回当日成功支付的订单(不含充值退款订单)REFUND
: 返回当日退款订单(不含充值退款订单)
tar_type 选填 string
【压缩类型】 压缩类型,不填则以不压缩的方式返回账单文件流。
可选取值:GZIP
: 下载账单时返回.gzip格式的压缩文件流
请求示例
需配合微信支付工具库 WXPayUtility 使用,请参考 Java
1package com.java.demo; 2 3import com.google.gson.annotations.Expose; 4import com.google.gson.annotations.SerializedName; 5import com.java.utils.WXPayUtility; // 引用微信支付工具库 参考:https://pay.weixin.qq.com/doc/v3/merchant/4014931831 6import java.io.IOException; 7import java.io.UncheckedIOException; 8import java.security.PrivateKey; 9import java.security.PublicKey; 10import java.util.ArrayList; 11import java.util.HashMap; 12import java.util.List; 13import java.util.Map; 14import okhttp3.MediaType; 15import okhttp3.OkHttpClient; 16import okhttp3.Request; 17import okhttp3.RequestBody; 18import okhttp3.Response; 19 20/** 21 * 申请交易账单API 22 */ 23public class GetTradeBill { 24 private static String HOST = "https://api.mch.weixin.qq.com"; 25 private static String METHOD = "GET"; 26 private static String PATH = "/v3/bill/tradebill"; 27 28 public static void main(String[] args) { 29 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 30 GetTradeBill client = new GetTradeBill( 31 "填入 商户号", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/merchant/4013070756 32 "填入 商户API证书序列号", // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/merchant/4013053053 33 "填入 商户API证书私钥文件路径", // 商户API证书私钥文件路径,本地文件路径 34 "填入 微信支付公钥ID", // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/merchant/4013038816 35 "填入 微信支付公钥文件路径" // 微信支付公钥文件路径,本地文件路径 36 ); 37 38 GetTradeBillRequest request = new GetTradeBillRequest(); 39 request.billDate = "2019-06-11"; 40 request.billType = BillType.ALL; 41 request.tarType = TarType.GZIP; 42 try { 43 QueryBillEntity response = client.run(request); 44 45 // TODO: 请求成功,继续业务逻辑 46 System.out.println(response); 47 } catch (WXPayUtility.ApiException e) { 48 // TODO: 请求失败,根据状态码执行不同的逻辑 49 e.printStackTrace(); 50 } 51 } 52 53 public QueryBillEntity run(GetTradeBillRequest request) { 54 String uri = PATH; 55 Map<String, Object> args = new HashMap<>(); 56 args.put("bill_date", request.billDate); 57 args.put("bill_type", request.billType); 58 args.put("tar_type", request.tarType); 59 uri = uri + "?" + WXPayUtility.urlEncode(args); 60 61 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 62 reqBuilder.addHeader("Accept", "application/json"); 63 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 64 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 65 reqBuilder.method(METHOD, null); 66 Request httpRequest = reqBuilder.build(); 67 68 // 发送HTTP请求 69 OkHttpClient client = new OkHttpClient.Builder().build(); 70 try (Response httpResponse = client.newCall(httpRequest).execute()) { 71 String respBody = WXPayUtility.extractBody(httpResponse); 72 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 73 // 2XX 成功,验证应答签名 74 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 75 httpResponse.headers(), respBody); 76 77 // 从HTTP应答报文构建返回数据 78 return WXPayUtility.fromJson(respBody, QueryBillEntity.class); 79 } else { 80 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 81 } 82 } catch (IOException e) { 83 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 84 } 85 } 86 87 private final String mchid; 88 private final String certificateSerialNo; 89 private final PrivateKey privateKey; 90 private final String wechatPayPublicKeyId; 91 private final PublicKey wechatPayPublicKey; 92 93 public GetTradeBill(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 94 this.mchid = mchid; 95 this.certificateSerialNo = certificateSerialNo; 96 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 97 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 98 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 99 } 100 101 public static class QueryBillEntity { 102 @SerializedName("hash_type") 103 public HashType hashType; 104 105 @SerializedName("hash_value") 106 public String hashValue; 107 108 @SerializedName("download_url") 109 public String downloadUrl; 110 } 111 112 public enum BillType { 113 @SerializedName("ALL") 114 ALL, 115 @SerializedName("SUCCESS") 116 SUCCESS, 117 @SerializedName("REFUND") 118 REFUND, 119 @SerializedName("RECHARGE_REFUND") 120 RECHARGE_REFUND, 121 @SerializedName("ALL_SPECIAL") 122 ALL_SPECIAL, 123 @SerializedName("SUC_SPECIAL") 124 SUC_SPECIAL, 125 @SerializedName("REF_SPECIAL") 126 REF_SPECIAL 127 } 128 129 public enum HashType { 130 @SerializedName("SHA1") 131 SHA1 132 } 133 134 public static class GetTradeBillRequest { 135 @SerializedName("bill_date") 136 @Expose(serialize = false) 137 public String billDate; 138 139 @SerializedName("bill_type") 140 @Expose(serialize = false) 141 public BillType billType; 142 143 @SerializedName("tar_type") 144 @Expose(serialize = false) 145 public TarType tarType; 146 } 147 148 public enum TarType { 149 @SerializedName("GZIP") 150 GZIP 151 } 152}
需配合微信支付工具库 wxpay_utility 使用,请参考 Go
1package main 2 3import ( 4 "demo/wxpay_utility"// 引用微信支付工具库 参考:https://pay.weixin.qq.com/doc/v3/merchant/4015119334 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/url" 9) 10 11func main() { 12 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 13 config, err := wxpay_utility.CreateMchConfig( 14 "填入 商户号", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/merchant/4013070756 15 "填入 商户API证书序列号", // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/merchant/4013053053 16 "填入 商户API证书私钥文件路径", // 商户API证书私钥文件路径,本地文件路径 17 "填入 微信支付公钥ID", // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/merchant/4013038816 18 "填入 微信支付公钥文件路径", // 微信支付公钥文件路径,本地文件路径 19 ) 20 if err != nil { 21 fmt.Println(err) 22 return 23 } 24 25 request := &GetTradeBillRequest{ 26 BillDate: wxpay_utility.String("2019-06-11"), 27 BillType: BILLTYPE_ALL.Ptr(), 28 TarType: TARTYPE_GZIP.Ptr(), 29 } 30 31 response, err := GetTradeBill(config, request) 32 if err != nil { 33 fmt.Printf("请求失败: %+v\n", err) 34 // TODO: 请求失败,根据状态码执行不同的处理 35 return 36 } 37 38 // TODO: 请求成功,继续业务逻辑 39 fmt.Printf("请求成功: %+v\n", response) 40} 41 42func GetTradeBill(config *wxpay_utility.MchConfig, request *GetTradeBillRequest) (response *QueryBillEntity, err error) { 43 const ( 44 host = "https://api.mch.weixin.qq.com" 45 method = "GET" 46 path = "/v3/bill/tradebill" 47 ) 48 49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 50 if err != nil { 51 return nil, err 52 } 53 query := reqUrl.Query() 54 query.Add("bill_date", *request.BillDate) 55 query.Add("bill_type", fmt.Sprintf("%v", *request.BillType)) 56 query.Add("tar_type", fmt.Sprintf("%v", *request.TarType)) 57 reqUrl.RawQuery = query.Encode() 58 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 59 if err != nil { 60 return nil, err 61 } 62 httpRequest.Header.Set("Accept", "application/json") 63 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 64 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 65 if err != nil { 66 return nil, err 67 } 68 httpRequest.Header.Set("Authorization", authorization) 69 70 client := &http.Client{} 71 httpResponse, err := client.Do(httpRequest) 72 if err != nil { 73 return nil, err 74 } 75 76 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 77 if err != nil { 78 return nil, err 79 } 80 81 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 82 // 2XX 成功,验证应答签名 83 err = wxpay_utility.ValidateResponse( 84 config.WechatPayPublicKeyId(), 85 config.WechatPayPublicKey(), 86 &httpResponse.Header, 87 respBody, 88 ) 89 if err != nil { 90 return nil, err 91 } 92 93 if err := json.Unmarshal(respBody, response); err != nil { 94 return nil, err 95 } 96 97 return response, nil 98 } else { 99 return nil, &wxpay_utility.ApiException{ 100 StatusCode: httpResponse.StatusCode, 101 Header: httpResponse.Header, 102 Body: respBody, 103 } 104 } 105} 106 107type QueryBillEntity struct { 108 HashType *HashType `json:"hash_type,omitempty"` 109 HashValue *string `json:"hash_value,omitempty"` 110 DownloadUrl *string `json:"download_url,omitempty"` 111} 112 113type BillType string 114 115func (e BillType) Ptr() *BillType { 116 return &e 117} 118 119const ( 120 BILLTYPE_ALL BillType = "ALL" 121 BILLTYPE_SUCCESS BillType = "SUCCESS" 122 BILLTYPE_REFUND BillType = "REFUND" 123 BILLTYPE_RECHARGE_REFUND BillType = "RECHARGE_REFUND" 124 BILLTYPE_ALL_SPECIAL BillType = "ALL_SPECIAL" 125 BILLTYPE_SUC_SPECIAL BillType = "SUC_SPECIAL" 126 BILLTYPE_REF_SPECIAL BillType = "REF_SPECIAL" 127) 128 129type HashType string 130 131func (e HashType) Ptr() *HashType { 132 return &e 133} 134 135const ( 136 HASHTYPE_SHA1 HashType = "SHA1" 137) 138 139type GetTradeBillRequest struct { 140 BillDate *string `json:"bill_date,omitempty"` 141 BillType *BillType `json:"bill_type,omitempty"` 142 TarType *TarType `json:"tar_type,omitempty"` 143} 144 145func (o *GetTradeBillRequest) MarshalJSON() ([]byte, error) { 146 type Alias GetTradeBillRequest 147 a := &struct { 148 BillDate *string `json:"bill_date,omitempty"` 149 BillType *BillType `json:"bill_type,omitempty"` 150 TarType *TarType `json:"tar_type,omitempty"` 151 *Alias 152 }{ 153 // 序列化时移除非 Body 字段 154 BillDate: nil, 155 BillType: nil, 156 TarType: nil, 157 Alias: (*Alias)(o), 158 } 159 return json.Marshal(a) 160} 161 162type TarType string 163 164func (e TarType) Ptr() *TarType { 165 return &e 166} 167 168const ( 169 TARTYPE_GZIP TarType = "GZIP" 170)
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=2019-06-11&bill_type=ALL&tar_type=GZIP \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" 5
应答参数
|
hash_type 必填 string
【哈希类型】 哈希类型,固定为SHA1。
hash_value 必填 string(1024)
【哈希值】 账单文件的SHA1摘要值,用于商户侧校验文件的一致性。
download_url 必填 string(2048)
【下载地址】 供下一步请求账单文件的下载地址,该地址5min内有效。参考下载账单
应答示例
200 OK
1{ 2 "hash_type" : "SHA1", 3 "hash_value" : "79bb0f45fc4c42234a918000b2668d689e2bde04", 4 "download_url" : "https://api.mch.weixin.qq.com/v3/bill/downloadurl?token=xxx" 5} 6
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | INVALID_REQUEST | 参数错误 | 请根据接口返回的错误描述检查参数,参数需按文档要求进行填写 |
400 | NO_STATEMENT_EXIST | 账单文件不存在 | 请检查当前商户号是否在指定日期有交易或退款发生 |
400 | STATEMENT_CREATING | 账单生成中 | 请先检查当前商户号在指定日期内是否有成功的交易或退款,若有,则在T+1日上午10点后再重新下载 |
429 | FREQUENCY_LIMITED | 请求过于频繁 | 请降低调用频率 |