申请交易账单
更新时间: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格式的压缩文件流
请求示例
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
需配合微信支付工具库 WXPayUtility 使用,请参考 Java
1package com.java.demo; 2 3import com.java.utils.WXPayUtility; // 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/merchant/4014931831 4 5import com.google.gson.annotations.SerializedName; 6import com.google.gson.annotations.Expose; 7import okhttp3.MediaType; 8import okhttp3.OkHttpClient; 9import okhttp3.Request; 10import okhttp3.RequestBody; 11import okhttp3.Response; 12 13import java.io.IOException; 14import java.io.UncheckedIOException; 15import java.security.PrivateKey; 16import java.security.PublicKey; 17import java.util.ArrayList; 18import java.util.HashMap; 19import java.util.List; 20import java.util.Map; 21 22/** 23 * 申请交易账单API 24 */ 25public class GetTradeBill { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/bill/tradebill"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 32 GetTradeBill client = new GetTradeBill( 33 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756 34 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053 35 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 36 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816 37 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 38 ); 39 40 GetTradeBillRequest request = new GetTradeBillRequest(); 41 request.billDate = "2019-06-11"; 42 request.billType = BillType.ALL; 43 request.tarType = TarType.GZIP; 44 try { 45 QueryBillEntity response = client.run(request); 46 // TODO: 请求成功,继续业务逻辑 47 System.out.println(response); 48 } catch (WXPayUtility.ApiException e) { 49 // TODO: 请求失败,根据状态码执行不同的逻辑 50 e.printStackTrace(); 51 } 52 } 53 54 public QueryBillEntity run(GetTradeBillRequest request) { 55 String uri = PATH; 56 Map<String, Object> args = new HashMap<>(); 57 args.put("bill_date", request.billDate); 58 args.put("bill_type", request.billType); 59 args.put("tar_type", request.tarType); 60 String queryString = WXPayUtility.urlEncode(args); 61 if (!queryString.isEmpty()) { 62 uri = uri + "?" + queryString; 63 } 64 65 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 66 reqBuilder.addHeader("Accept", "application/json"); 67 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 68 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 69 reqBuilder.method(METHOD, null); 70 Request httpRequest = reqBuilder.build(); 71 72 // 发送HTTP请求 73 OkHttpClient client = new OkHttpClient.Builder().build(); 74 try (Response httpResponse = client.newCall(httpRequest).execute()) { 75 String respBody = WXPayUtility.extractBody(httpResponse); 76 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 77 // 2XX 成功,验证应答签名 78 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 79 httpResponse.headers(), respBody); 80 81 // 从HTTP应答报文构建返回数据 82 return WXPayUtility.fromJson(respBody, QueryBillEntity.class); 83 } else { 84 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 85 } 86 } catch (IOException e) { 87 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 88 } 89 } 90 91 private final String mchid; 92 private final String certificateSerialNo; 93 private final PrivateKey privateKey; 94 private final String wechatPayPublicKeyId; 95 private final PublicKey wechatPayPublicKey; 96 97 public GetTradeBill(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 98 this.mchid = mchid; 99 this.certificateSerialNo = certificateSerialNo; 100 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 101 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 102 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 103 } 104 105 public static class GetTradeBillRequest { 106 @SerializedName("bill_date") 107 @Expose(serialize = false) 108 public String billDate; 109 110 @SerializedName("bill_type") 111 @Expose(serialize = false) 112 public BillType billType; 113 114 @SerializedName("tar_type") 115 @Expose(serialize = false) 116 public TarType tarType; 117 } 118 119 public static class QueryBillEntity { 120 @SerializedName("hash_type") 121 public HashType hashType; 122 123 @SerializedName("hash_value") 124 public String hashValue; 125 126 @SerializedName("download_url") 127 public String downloadUrl; 128 } 129 130 public enum BillType { 131 @SerializedName("ALL") 132 ALL, 133 @SerializedName("SUCCESS") 134 SUCCESS, 135 @SerializedName("REFUND") 136 REFUND 137 } 138 139 public enum TarType { 140 @SerializedName("GZIP") 141 GZIP 142 } 143 144 public enum HashType { 145 @SerializedName("SHA1") 146 SHA1 147 } 148 149} 150
需配合微信支付工具库 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 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756 15 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053 16 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 17 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816 18 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 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 if request.BillDate != nil { 55 query.Add("bill_date", *request.BillDate) 56 } 57 if request.BillType != nil { 58 query.Add("bill_type", fmt.Sprintf("%v", *request.BillType)) 59 } 60 if request.TarType != nil { 61 query.Add("tar_type", fmt.Sprintf("%v", *request.TarType)) 62 } 63 reqUrl.RawQuery = query.Encode() 64 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 65 if err != nil { 66 return nil, err 67 } 68 httpRequest.Header.Set("Accept", "application/json") 69 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 70 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 71 if err != nil { 72 return nil, err 73 } 74 httpRequest.Header.Set("Authorization", authorization) 75 76 client := &http.Client{} 77 httpResponse, err := client.Do(httpRequest) 78 if err != nil { 79 return nil, err 80 } 81 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 82 if err != nil { 83 return nil, err 84 } 85 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 86 // 2XX 成功,验证应答签名 87 err = wxpay_utility.ValidateResponse( 88 config.WechatPayPublicKeyId(), 89 config.WechatPayPublicKey(), 90 &httpResponse.Header, 91 respBody, 92 ) 93 if err != nil { 94 return nil, err 95 } 96 response := &QueryBillEntity{} 97 if err := json.Unmarshal(respBody, response); err != nil { 98 return nil, err 99 } 100 101 return response, nil 102 } else { 103 return nil, wxpay_utility.NewApiException( 104 httpResponse.StatusCode, 105 httpResponse.Header, 106 respBody, 107 ) 108 } 109} 110 111type GetTradeBillRequest struct { 112 BillDate *string `json:"bill_date,omitempty"` 113 BillType *BillType `json:"bill_type,omitempty"` 114 TarType *TarType `json:"tar_type,omitempty"` 115} 116 117func (o *GetTradeBillRequest) MarshalJSON() ([]byte, error) { 118 type Alias GetTradeBillRequest 119 a := &struct { 120 BillDate *string `json:"bill_date,omitempty"` 121 BillType *BillType `json:"bill_type,omitempty"` 122 TarType *TarType `json:"tar_type,omitempty"` 123 *Alias 124 }{ 125 // 序列化时移除非 Body 字段 126 BillDate: nil, 127 BillType: nil, 128 TarType: nil, 129 Alias: (*Alias)(o), 130 } 131 return json.Marshal(a) 132} 133 134type QueryBillEntity struct { 135 HashType *HashType `json:"hash_type,omitempty"` 136 HashValue *string `json:"hash_value,omitempty"` 137 DownloadUrl *string `json:"download_url,omitempty"` 138} 139 140type BillType string 141 142func (e BillType) Ptr() *BillType { 143 return &e 144} 145 146const ( 147 BILLTYPE_ALL BillType = "ALL" 148 BILLTYPE_SUCCESS BillType = "SUCCESS" 149 BILLTYPE_REFUND BillType = "REFUND" 150) 151 152type TarType string 153 154func (e TarType) Ptr() *TarType { 155 return &e 156} 157 158const ( 159 TARTYPE_GZIP TarType = "GZIP" 160) 161 162type HashType string 163 164func (e HashType) Ptr() *HashType { 165 return &e 166} 167 168const ( 169 HASHTYPE_SHA1 HashType = "SHA1" 170) 171
应答参数
|
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 | 请求过于频繁 | 请降低调用频率 |