申请二级商户资金账单
更新时间:2024.10.23下载接口说明
微信支付按天提供微信支付账户的资金流水账单文件,电商平台可以通过该接口获取二级商户账单文件的下载地址。文件内包含电商平台二级商户资金操作相关的业务单号、收支金额、记账时间等信息,供电商平台进行核对。
|
文件格式说明
账单文件包括明细数据和汇总数据两部分,每一部分都包含一行表头和若干行具体数据。
明细数据每一行对应一笔资金操作,同时每一个数据前加入了字符`,以避免数据被Excel按科学计数法处理。如需汇总金额等数据,可以批量替换掉该字符。
接口说明
支持商户:【平台商户】
请求方式:【GET】/v3/ecommerce/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
【资金账户类型】 资金账户类型,本接口只支持填ALL
可选取值
ALL
: 所有账户(该枚举值只限电商平台下载二级商户资金流水账单场景使用)
tar_type 选填 string
【压缩类型】 压缩类型,不填则以不压缩的方式返回数据流
可选取值
GZIP
: GZIP格式压缩,返回格式为.gzip的压缩包账单
algorithm 必填 string
【账单文件加密算法】 账单文件加密算法
可选取值
AEAD_AES_256_GCM
: AEAD_AES_256_GCM加密算法SM4_GCM
: SM4_GCM加密算法,密钥长度128bit
请求示例
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/ecommerce/bill/fundflowbill?bill_date=2019-06-11&account_type=ALL&tar_type=GZIP&algorithm=AEAD_AES_256_GCM \ 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 GetAllSubMchFundFlowBill { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/ecommerce/bill/fundflowbill"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 GetAllSubMchFundFlowBill client = new GetAllSubMchFundFlowBill( 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 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 37 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 38 ); 39 40 GetAllSubMchFundFlowBillRequest request = new GetAllSubMchFundFlowBillRequest(); 41 request.billDate = "2019-06-11"; 42 request.accountType = SubMerchantFundFlowBillAccountType.ALL; 43 request.tarType = TarType.GZIP; 44 request.algorithm = Algorithm.AEAD_AES_256_GCM; 45 try { 46 QueryEncryptBillEntity 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 QueryEncryptBillEntity run(GetAllSubMchFundFlowBillRequest 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 args.put("algorithm", request.algorithm); 62 String queryString = WXPayUtility.urlEncode(args); 63 if (!queryString.isEmpty()) { 64 uri = uri + "?" + queryString; 65 } 66 67 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 68 reqBuilder.addHeader("Accept", "application/json"); 69 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 70 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 71 reqBuilder.method(METHOD, null); 72 Request httpRequest = reqBuilder.build(); 73 74 // 发送HTTP请求 75 OkHttpClient client = new OkHttpClient.Builder().build(); 76 try (Response httpResponse = client.newCall(httpRequest).execute()) { 77 String respBody = WXPayUtility.extractBody(httpResponse); 78 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 79 // 2XX 成功,验证应答签名 80 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 81 httpResponse.headers(), respBody); 82 83 // 从HTTP应答报文构建返回数据 84 return WXPayUtility.fromJson(respBody, QueryEncryptBillEntity.class); 85 } else { 86 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 87 } 88 } catch (IOException e) { 89 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 90 } 91 } 92 93 private final String mchid; 94 private final String certificateSerialNo; 95 private final PrivateKey privateKey; 96 private final String wechatPayPublicKeyId; 97 private final PublicKey wechatPayPublicKey; 98 99 public GetAllSubMchFundFlowBill(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 100 this.mchid = mchid; 101 this.certificateSerialNo = certificateSerialNo; 102 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 103 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 104 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 105 } 106 107 public static class GetAllSubMchFundFlowBillRequest { 108 @SerializedName("bill_date") 109 @Expose(serialize = false) 110 public String billDate; 111 112 @SerializedName("account_type") 113 @Expose(serialize = false) 114 public SubMerchantFundFlowBillAccountType accountType; 115 116 @SerializedName("tar_type") 117 @Expose(serialize = false) 118 public TarType tarType; 119 120 @SerializedName("algorithm") 121 @Expose(serialize = false) 122 public Algorithm algorithm; 123 } 124 125 public static class QueryEncryptBillEntity { 126 @SerializedName("download_bill_count") 127 public Long downloadBillCount; 128 129 @SerializedName("download_bill_list") 130 public List<EncryptBillEntity> downloadBillList; 131 } 132 133 public enum SubMerchantFundFlowBillAccountType { 134 @SerializedName("ALL") 135 ALL 136 } 137 138 public enum TarType { 139 @SerializedName("GZIP") 140 GZIP 141 } 142 143 public enum Algorithm { 144 @SerializedName("AEAD_AES_256_GCM") 145 AEAD_AES_256_GCM, 146 @SerializedName("SM4_GCM") 147 SM4_GCM 148 } 149 150 public static class EncryptBillEntity { 151 @SerializedName("bill_sequence") 152 public Long billSequence; 153 154 @SerializedName("hash_type") 155 public HashType hashType; 156 157 @SerializedName("hash_value") 158 public String hashValue; 159 160 @SerializedName("download_url") 161 public String downloadUrl; 162 163 @SerializedName("encrypt_key") 164 public String encryptKey; 165 166 @SerializedName("nonce") 167 public String nonce; 168 } 169 170 public enum HashType { 171 @SerializedName("SHA1") 172 SHA1 173 } 174 175} 176
需配合微信支付工具库 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 := &GetAllSubMchFundFlowBillRequest{ 26 BillDate: wxpay_utility.String("2019-06-11"), 27 AccountType: SUBMERCHANTFUNDFLOWBILLACCOUNTTYPE_ALL.Ptr(), 28 TarType: TARTYPE_GZIP.Ptr(), 29 Algorithm: ALGORITHM_AEAD_AES_256_GCM.Ptr(), 30 } 31 32 response, err := GetAllSubMchFundFlowBill(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 GetAllSubMchFundFlowBill(config *wxpay_utility.MchConfig, request *GetAllSubMchFundFlowBillRequest) (response *QueryEncryptBillEntity, err error) { 44 const ( 45 host = "https://api.mch.weixin.qq.com" 46 method = "GET" 47 path = "/v3/ecommerce/bill/fundflowbill" 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 if request.BillDate != nil { 56 query.Add("bill_date", *request.BillDate) 57 } 58 if request.AccountType != nil { 59 query.Add("account_type", fmt.Sprintf("%v", *request.AccountType)) 60 } 61 if request.TarType != nil { 62 query.Add("tar_type", fmt.Sprintf("%v", *request.TarType)) 63 } 64 if request.Algorithm != nil { 65 query.Add("algorithm", fmt.Sprintf("%v", *request.Algorithm)) 66 } 67 reqUrl.RawQuery = query.Encode() 68 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 69 if err != nil { 70 return nil, err 71 } 72 httpRequest.Header.Set("Accept", "application/json") 73 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 74 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 75 if err != nil { 76 return nil, err 77 } 78 httpRequest.Header.Set("Authorization", authorization) 79 80 client := &http.Client{} 81 httpResponse, err := client.Do(httpRequest) 82 if err != nil { 83 return nil, err 84 } 85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 86 if err != nil { 87 return nil, err 88 } 89 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 90 // 2XX 成功,验证应答签名 91 err = wxpay_utility.ValidateResponse( 92 config.WechatPayPublicKeyId(), 93 config.WechatPayPublicKey(), 94 &httpResponse.Header, 95 respBody, 96 ) 97 if err != nil { 98 return nil, err 99 } 100 response := &QueryEncryptBillEntity{} 101 if err := json.Unmarshal(respBody, response); err != nil { 102 return nil, err 103 } 104 105 return response, nil 106 } else { 107 return nil, wxpay_utility.NewApiException( 108 httpResponse.StatusCode, 109 httpResponse.Header, 110 respBody, 111 ) 112 } 113} 114 115type GetAllSubMchFundFlowBillRequest struct { 116 BillDate *string `json:"bill_date,omitempty"` 117 AccountType *SubMerchantFundFlowBillAccountType `json:"account_type,omitempty"` 118 TarType *TarType `json:"tar_type,omitempty"` 119 Algorithm *Algorithm `json:"algorithm,omitempty"` 120} 121 122func (o *GetAllSubMchFundFlowBillRequest) MarshalJSON() ([]byte, error) { 123 type Alias GetAllSubMchFundFlowBillRequest 124 a := &struct { 125 BillDate *string `json:"bill_date,omitempty"` 126 AccountType *SubMerchantFundFlowBillAccountType `json:"account_type,omitempty"` 127 TarType *TarType `json:"tar_type,omitempty"` 128 Algorithm *Algorithm `json:"algorithm,omitempty"` 129 *Alias 130 }{ 131 // 序列化时移除非 Body 字段 132 BillDate: nil, 133 AccountType: nil, 134 TarType: nil, 135 Algorithm: nil, 136 Alias: (*Alias)(o), 137 } 138 return json.Marshal(a) 139} 140 141type QueryEncryptBillEntity struct { 142 DownloadBillCount *int64 `json:"download_bill_count,omitempty"` 143 DownloadBillList []EncryptBillEntity `json:"download_bill_list,omitempty"` 144} 145 146type SubMerchantFundFlowBillAccountType string 147 148func (e SubMerchantFundFlowBillAccountType) Ptr() *SubMerchantFundFlowBillAccountType { 149 return &e 150} 151 152const ( 153 SUBMERCHANTFUNDFLOWBILLACCOUNTTYPE_ALL SubMerchantFundFlowBillAccountType = "ALL" 154) 155 156type TarType string 157 158func (e TarType) Ptr() *TarType { 159 return &e 160} 161 162const ( 163 TARTYPE_GZIP TarType = "GZIP" 164) 165 166type Algorithm string 167 168func (e Algorithm) Ptr() *Algorithm { 169 return &e 170} 171 172const ( 173 ALGORITHM_AEAD_AES_256_GCM Algorithm = "AEAD_AES_256_GCM" 174 ALGORITHM_SM4_GCM Algorithm = "SM4_GCM" 175) 176 177type EncryptBillEntity struct { 178 BillSequence *int64 `json:"bill_sequence,omitempty"` 179 HashType *HashType `json:"hash_type,omitempty"` 180 HashValue *string `json:"hash_value,omitempty"` 181 DownloadUrl *string `json:"download_url,omitempty"` 182 EncryptKey *string `json:"encrypt_key,omitempty"` 183 Nonce *string `json:"nonce,omitempty"` 184} 185 186type HashType string 187 188func (e HashType) Ptr() *HashType { 189 return &e 190} 191 192const ( 193 HASHTYPE_SHA1 HashType = "SHA1" 194) 195
应答参数
200 OK
download_bill_count 必填 integer
【下载信息总数】 下载信息总数
download_bill_list 选填 array[object]
【下载信息明细】 下载信息明细
属性 | |
bill_sequence 必填 integer 【账单文件序号】 账单文件序号,商户将多个文件按账单文件序号的顺序合并为完整的资金账单文件,起始值为1 hash_type 必填 string 【哈希类型】 哈希类型 可选取值
hash_value 必填 string(1024) 【哈希值】 原始账单(gzip需要解压缩)的摘要值,用于校验文件的完整性。GCM算法加密的账单解密成功表示校验完整性通过。 download_url 必填 string(2048) 【下载地址】 供下一步请求账单文件的下载地址,该地址5min内有效。 encrypt_key 必填 string(512) 【加密密钥】 加密账单文件使用的加密密钥。密钥用商户证书的公钥进行加密,然后进行Base64编码 nonce 必填 string(16) 【随机字符串】 加密账单文件使用的随机字符串 |
应答示例
200 OK
1{ 2 "download_bill_count" : 1, 3 "download_bill_list" : [ 4 { 5 "bill_sequence" : 1, 6 "hash_type" : "SHA1", 7 "hash_value" : "79bb0f45fc4c42234a918000b2668d689e2bde04", 8 "download_url" : "https://api.mch.weixin.qq.com/v3/bill/downloadurl?token=xxx", 9 "encrypt_key" : "a0YK7p+9XaKzE9N4qtFfG/9za1oqKlLXXJWBkH+kX84onAs2Ol/E1fk+6S+FuBXczGDRU8I8D+6PfbwKYBGm0wANUTqHOSezzfbieIo2t51UIId7sP9SoN38W2+IcYDviIsu59KSdyiL3TY2xqZNT8UDcnMWzTNZdSv+CLsSgblB6OKGN9JONTadOFGfv1OKkTp86Li+X7S9bG62wsa572/5Rm4MmDCiKwY4bX2EynWQHBEOExD5URxT6/MX3F1D3BNYrE4fUu1F03k25xVlXnZDjksy6Rf3SCgadR+Cepc6mdfF9b2gTxNsJFMEdYXbqL0W1WQZ3UqSPQCguK6uLA==", 10 "nonce" : "a8607ef79034c49c" 11 } 12 ] 13} 14
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | INVALID_REQUEST | 参数错误 | 请检查bill_date,仅支持下载3个月以内的资金流水账单 |
400 | NO_STATEMENT_EXIST | 请求的账单文件不存在 | 请检查电商平台二级商户在指定日期是否有资金操作 |
400 | STATEMENT_CREATING | 账单生成中 | 请先检查电商平台二级商户在指定日期内是否有资金操作,若有,则在T+1日上午10点后再重新下载 |
403 | NO_AUTH | 权限异常 | 请检查mchid是否为电商平台 |
403 | NO_AUTH | 权限异常 | 请在产品中心开通下载二级商户资金账单产品权限 |
429 | FREQUENCY_LIMITED | 请求过于频繁 | 请降低调用频率 |