申请所有/单个子商户交易账单
更新时间:2025.01.16下载接口说明
微信支付在每日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,仅支持三个月内的账单下载申请。
sub_mchid 选填 string(12)
【子商户号(也叫特约商户号)】服务商可通过该参数指定获取某个子商户的账单,不填则返回服务商下所有子商户的全量账单。
注:该参数无法用于指定获取小微商户账单,小微商户的账单数据只能通过全量账单获取。
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/partner/4014985777 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/partner/4013080340 30 GetTradeBill client = new GetTradeBill( 31 "填入 商户号", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 "填入 商户API证书序列号", // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013058924 33 "填入 商户API证书私钥文件路径", // 商户API证书私钥文件路径,本地文件路径 34 "填入 微信支付公钥ID", // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013038589 35 "填入 微信支付公钥文件路径" // 微信支付公钥文件路径,本地文件路径 36 ); 37 38 GetTradeBillRequest request = new GetTradeBillRequest(); 39 request.billDate = "2019-06-11"; 40 request.subMchid = "19000000001"; 41 request.billType = BillType.ALL; 42 request.tarType = TarType.GZIP; 43 try { 44 QueryBillEntity response = client.run(request); 45 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("sub_mchid", request.subMchid); 59 args.put("bill_type", request.billType); 60 args.put("tar_type", request.tarType); 61 uri = uri + "?" + WXPayUtility.urlEncode(args); 62 63 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 64 reqBuilder.addHeader("Accept", "application/json"); 65 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 66 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 67 reqBuilder.method(METHOD, null); 68 Request httpRequest = reqBuilder.build(); 69 70 // 发送HTTP请求 71 OkHttpClient client = new OkHttpClient.Builder().build(); 72 try (Response httpResponse = client.newCall(httpRequest).execute()) { 73 String respBody = WXPayUtility.extractBody(httpResponse); 74 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 75 // 2XX 成功,验证应答签名 76 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 77 httpResponse.headers(), respBody); 78 79 // 从HTTP应答报文构建返回数据 80 return WXPayUtility.fromJson(respBody, QueryBillEntity.class); 81 } else { 82 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 83 } 84 } catch (IOException e) { 85 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 86 } 87 } 88 89 private final String mchid; 90 private final String certificateSerialNo; 91 private final PrivateKey privateKey; 92 private final String wechatPayPublicKeyId; 93 private final PublicKey wechatPayPublicKey; 94 95 public GetTradeBill(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 96 this.mchid = mchid; 97 this.certificateSerialNo = certificateSerialNo; 98 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 99 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 100 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 101 } 102 103 public static class QueryBillEntity { 104 @SerializedName("hash_type") 105 public HashType hashType; 106 107 @SerializedName("hash_value") 108 public String hashValue; 109 110 @SerializedName("download_url") 111 public String downloadUrl; 112 } 113 114 public enum BillType { 115 @SerializedName("ALL") 116 ALL, 117 @SerializedName("SUCCESS") 118 SUCCESS, 119 @SerializedName("REFUND") 120 REFUND, 121 @SerializedName("RECHARGE_REFUND") 122 RECHARGE_REFUND, 123 @SerializedName("ALL_SPECIAL") 124 ALL_SPECIAL, 125 @SerializedName("SUC_SPECIAL") 126 SUC_SPECIAL, 127 @SerializedName("REF_SPECIAL") 128 REF_SPECIAL 129 } 130 131 public enum HashType { 132 @SerializedName("SHA1") 133 SHA1 134 } 135 136 public static class GetTradeBillRequest { 137 @SerializedName("bill_date") 138 @Expose(serialize = false) 139 public String billDate; 140 141 @SerializedName("sub_mchid") 142 @Expose(serialize = false) 143 public String subMchid; 144 145 @SerializedName("bill_type") 146 @Expose(serialize = false) 147 public BillType billType; 148 149 @SerializedName("tar_type") 150 @Expose(serialize = false) 151 public TarType tarType; 152 } 153 154 public enum TarType { 155 @SerializedName("GZIP") 156 GZIP 157 } 158}
需配合微信支付工具库 wxpay_utility 使用,请参考 Go
1package main 2 3import ( 4 "demo/wxpay_utility"// 引用微信支付工具库 参考:https://pay.weixin.qq.com/doc/v3/partner/4015119446 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/url" 9) 10 11func main() { 12 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 13 config, err := wxpay_utility.CreateMchConfig( 14 "填入 商户号", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考https://pay.weixin.qq.com/doc/v3/partner/4013080340 15 "填入 商户API证书序列号", // 商户API证书序列号,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013058924 16 "填入 商户API证书私钥文件路径", // 商户API证书私钥文件路径,本地文件路径 17 "填入 微信支付公钥ID", // 微信支付公钥ID,如何获取请参考https://pay.weixin.qq.com/doc/v3/partner/4013038589 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 SubMchid: wxpay_utility.String("19000000001"), 28 BillType: BILLTYPE_ALL.Ptr(), 29 TarType: TARTYPE_GZIP.Ptr(), 30 } 31 32 response, err := GetTradeBill(config, request) 33 if err != nil { 34 fmt.Printf("请求失败: %+v\n", err) 35 // TODO: 请求失败,根据状态码执行不同的处理 36 return 37 } 38 39 // TODO: 请求成功,继续业务逻辑 40 fmt.Printf("请求成功: %+v\n", response) 41} 42 43func GetTradeBill(config *wxpay_utility.MchConfig, request *GetTradeBillRequest) (response *QueryBillEntity, err error) { 44 const ( 45 host = "https://api.mch.weixin.qq.com" 46 method = "GET" 47 path = "/v3/bill/tradebill" 48 ) 49 50 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 51 if err != nil { 52 return nil, err 53 } 54 query := reqUrl.Query() 55 query.Add("bill_date", *request.BillDate) 56 query.Add("sub_mchid", *request.SubMchid) 57 query.Add("bill_type", fmt.Sprintf("%v", *request.BillType)) 58 query.Add("tar_type", fmt.Sprintf("%v", *request.TarType)) 59 reqUrl.RawQuery = query.Encode() 60 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 61 if err != nil { 62 return nil, err 63 } 64 httpRequest.Header.Set("Accept", "application/json") 65 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 66 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 67 if err != nil { 68 return nil, err 69 } 70 httpRequest.Header.Set("Authorization", authorization) 71 72 client := &http.Client{} 73 httpResponse, err := client.Do(httpRequest) 74 if err != nil { 75 return nil, err 76 } 77 78 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 79 if err != nil { 80 return nil, err 81 } 82 83 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 84 // 2XX 成功,验证应答签名 85 err = wxpay_utility.ValidateResponse( 86 config.WechatPayPublicKeyId(), 87 config.WechatPayPublicKey(), 88 &httpResponse.Header, 89 respBody, 90 ) 91 if err != nil { 92 return nil, err 93 } 94 95 if err := json.Unmarshal(respBody, response); err != nil { 96 return nil, err 97 } 98 99 return response, nil 100 } else { 101 return nil, &wxpay_utility.ApiException{ 102 StatusCode: httpResponse.StatusCode, 103 Header: httpResponse.Header, 104 Body: respBody, 105 } 106 } 107} 108 109type QueryBillEntity struct { 110 HashType *HashType `json:"hash_type,omitempty"` 111 HashValue *string `json:"hash_value,omitempty"` 112 DownloadUrl *string `json:"download_url,omitempty"` 113} 114 115type BillType string 116 117func (e BillType) Ptr() *BillType { 118 return &e 119} 120 121const ( 122 BILLTYPE_ALL BillType = "ALL" 123 BILLTYPE_SUCCESS BillType = "SUCCESS" 124 BILLTYPE_REFUND BillType = "REFUND" 125 BILLTYPE_RECHARGE_REFUND BillType = "RECHARGE_REFUND" 126 BILLTYPE_ALL_SPECIAL BillType = "ALL_SPECIAL" 127 BILLTYPE_SUC_SPECIAL BillType = "SUC_SPECIAL" 128 BILLTYPE_REF_SPECIAL BillType = "REF_SPECIAL" 129) 130 131type HashType string 132 133func (e HashType) Ptr() *HashType { 134 return &e 135} 136 137const ( 138 HASHTYPE_SHA1 HashType = "SHA1" 139) 140 141type GetTradeBillRequest struct { 142 BillDate *string `json:"bill_date,omitempty"` 143 SubMchid *string `json:"sub_mchid,omitempty"` 144 BillType *BillType `json:"bill_type,omitempty"` 145 TarType *TarType `json:"tar_type,omitempty"` 146} 147 148func (o *GetTradeBillRequest) MarshalJSON() ([]byte, error) { 149 type Alias GetTradeBillRequest 150 a := &struct { 151 BillDate *string `json:"bill_date,omitempty"` 152 SubMchid *string `json:"sub_mchid,omitempty"` 153 BillType *BillType `json:"bill_type,omitempty"` 154 TarType *TarType `json:"tar_type,omitempty"` 155 *Alias 156 }{ 157 // 序列化时移除非 Body 字段 158 BillDate: nil, 159 SubMchid: nil, 160 BillType: nil, 161 TarType: nil, 162 Alias: (*Alias)(o), 163 } 164 return json.Marshal(a) 165} 166 167type TarType string 168 169func (e TarType) Ptr() *TarType { 170 return &e 171} 172 173const ( 174 TARTYPE_GZIP TarType = "GZIP" 175)
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=2019-06-11&sub_mchid=19000000001&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 | 请求过于频繁 | 请降低调用频率 |