申请服务商资金账单
更新时间:2025.01.16微信支付按天提供服务商各账户的资金流水账单文件,服务商可以通过该接口获取账单文件的下载地址。账单文件详细记录了服务商账户资金操作的相关信息,包括业务单号、收支金额及记账时间等,以便服务商进行核对与确认。详细介绍参考:下载账单-产品介绍。
注意:
资金账单中的数据反映的是服务商微信账户资金变动情况;
当日账单将在次日上午9点开始生成,建议服务商在次日上午10点以后获取;
资金账单中所有涉及金额的字段均以“元”为单位。
以服务商商户号维度频率限制为3QPS。
文件格式说明
账单文件主要由明细数据和汇总数据两大部分构成,每部分均包含一行表头以及多行详细数据。
明细数据的每一行都代表一笔具体的资金操作。为防止数据在Excel中被自动转换为科学计数法,每项数据前均添加了字符`
。若需汇总计算金额等数据,可以批量移除该字符。
接口说明
支持商户:【普通服务商】【平台商户】
请求方式:【GET】/v3/bill/fundflowbill
请求域名:【主域名】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,仅支持三个月内的账单下载申请。
account_type 选填 string
【资金账户类型】 资金账户类型,不填默认是BASIC
可选取值:
BASIC
: 基本账户OPERATION
: 运营账户FEES
: 手续费账户
tar_type 选填 string
【压缩类型】 压缩类型,不填则以不压缩的方式返回账单文件流。
可选取值:GZIP
: 下载账单时返回.gzip格式的压缩文件流
请求示例
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/bill/fundflowbill?bill_date=2019-06-11&account_type=BASIC&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/partner/4014985777 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 GetFundFlowBill { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/bill/fundflowbill"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 GetFundFlowBill client = new GetFundFlowBill( 33 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 34 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 35 "/path/to/apiclient_key.pem" // 商户API证书私钥文件路径,本地文件路径 36 , 37 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 38 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 39 ); 40 41 GetFundFlowBillRequest request = new GetFundFlowBillRequest(); 42 request.billDate = "2019-06-11"; 43 request.accountType = FundFlowBillAccountType.BASIC; 44 request.tarType = TarType.GZIP; 45 try { 46 QueryBillEntity response = client.run(request); 47 // TODO: 请求成功,继续业务逻辑 48 System.out.println(response); 49 } catch (WXPayUtility.ApiException e) { 50 // TODO: 请求失败,根据状态码执行不同的逻辑 51 e.printStackTrace(); 52 } 53 } 54 55 public QueryBillEntity run(GetFundFlowBillRequest request) { 56 String uri = PATH; 57 Map<String, Object> args = new HashMap<>(); 58 args.put("bill_date", request.billDate); 59 args.put("account_type", request.accountType); 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 GetFundFlowBill(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 GetFundFlowBillRequest { 104 @SerializedName("bill_date") 105 @Expose(serialize = false) 106 public String billDate; 107 108 @SerializedName("account_type") 109 @Expose(serialize = false) 110 public FundFlowBillAccountType accountType; 111 112 @SerializedName("tar_type") 113 @Expose(serialize = false) 114 public TarType tarType; 115 } 116 117 public static class QueryBillEntity { 118 @SerializedName("hash_type") 119 public HashType hashType; 120 121 @SerializedName("hash_value") 122 public String hashValue; 123 124 @SerializedName("download_url") 125 public String downloadUrl; 126 } 127 128 public enum FundFlowBillAccountType { 129 @SerializedName("BASIC") 130 BASIC, 131 @SerializedName("OPERATION") 132 OPERATION, 133 @SerializedName("FEES") 134 FEES 135 } 136 137 public enum TarType { 138 @SerializedName("GZIP") 139 GZIP 140 } 141 142 public enum HashType { 143 @SerializedName("SHA1") 144 SHA1 145 } 146 147} 148
需配合微信支付工具库 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 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 15 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 16 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 17 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 18 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 19 ) 20 if err != nil { 21 fmt.Println(err) 22 return 23 } 24 25 request := &GetFundFlowBillRequest{ 26 BillDate: wxpay_utility.String("2019-06-11"), 27 AccountType: FUNDFLOWBILLACCOUNTTYPE_BASIC.Ptr(), 28 TarType: TARTYPE_GZIP.Ptr(), 29 } 30 31 response, err := GetFundFlowBill(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 GetFundFlowBill(config *wxpay_utility.MchConfig, request *GetFundFlowBillRequest) (response *QueryBillEntity, err error) { 43 const ( 44 host = "https://api.mch.weixin.qq.com" 45 method = "GET" 46 path = "/v3/bill/fundflowbill" 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("account_type", fmt.Sprintf("%v", *request.AccountType)) 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 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 76 if err != nil { 77 return nil, err 78 } 79 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 80 // 2XX 成功,验证应答签名 81 err = wxpay_utility.ValidateResponse( 82 config.WechatPayPublicKeyId(), 83 config.WechatPayPublicKey(), 84 &httpResponse.Header, 85 respBody, 86 ) 87 if err != nil { 88 return nil, err 89 } 90 response := &QueryBillEntity{} 91 if err := json.Unmarshal(respBody, response); err != nil { 92 return nil, err 93 } 94 95 return response, nil 96 } else { 97 return nil, wxpay_utility.NewApiException( 98 httpResponse.StatusCode, 99 httpResponse.Header, 100 respBody, 101 ) 102 } 103} 104 105type GetFundFlowBillRequest struct { 106 BillDate *string `json:"bill_date,omitempty"` 107 AccountType *FundFlowBillAccountType `json:"account_type,omitempty"` 108 TarType *TarType `json:"tar_type,omitempty"` 109} 110 111func (o *GetFundFlowBillRequest) MarshalJSON() ([]byte, error) { 112 type Alias GetFundFlowBillRequest 113 a := &struct { 114 BillDate *string `json:"bill_date,omitempty"` 115 AccountType *FundFlowBillAccountType `json:"account_type,omitempty"` 116 TarType *TarType `json:"tar_type,omitempty"` 117 *Alias 118 }{ 119 // 序列化时移除非 Body 字段 120 BillDate: nil, 121 AccountType: nil, 122 TarType: nil, 123 Alias: (*Alias)(o), 124 } 125 return json.Marshal(a) 126} 127 128type QueryBillEntity struct { 129 HashType *HashType `json:"hash_type,omitempty"` 130 HashValue *string `json:"hash_value,omitempty"` 131 DownloadUrl *string `json:"download_url,omitempty"` 132} 133 134type FundFlowBillAccountType string 135 136func (e FundFlowBillAccountType) Ptr() *FundFlowBillAccountType { 137 return &e 138} 139 140const ( 141 FUNDFLOWBILLACCOUNTTYPE_BASIC FundFlowBillAccountType = "BASIC" 142 FUNDFLOWBILLACCOUNTTYPE_OPERATION FundFlowBillAccountType = "OPERATION" 143 FUNDFLOWBILLACCOUNTTYPE_FEES FundFlowBillAccountType = "FEES" 144) 145 146type TarType string 147 148func (e TarType) Ptr() *TarType { 149 return &e 150} 151 152const ( 153 TARTYPE_GZIP TarType = "GZIP" 154) 155 156type HashType string 157 158func (e HashType) Ptr() *HashType { 159 return &e 160} 161 162const ( 163 HASHTYPE_SHA1 HashType = "SHA1" 164) 165
应答参数
|
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 | 请求过于频繁 | 请降低调用频率 |