申请分账账单
更新时间:2025.08.21分账账单已于2025年1月15日修复部分数据没有展示“`”的问题
微信支付按天提供分账账单文件,商户可以通过该接口获取账单文件的下载地址。文件内包含分账相关的金额、时间等信息,供商户核对到账等情况。
微信侧未成功的分账单不会出现在对账单中。
对账单中涉及金额的字段单位为“元”;
对账单接口只能下载三个月以内的账单。
账单文件包括明细数据和汇总数据两部分,每一部分都包含一行表头和若干行具体数据。
明细数据每一行对应一笔分账或一笔回退,同时每一个数据前加入了字符`,以避免数据被Excel按科学计数法处理。如需汇总金额等数据,可以批量替换掉该字符。
接口说明
支持商户:【普通服务商】
请求方式:【GET】/v3/profitsharing/bills
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
query 查询参数
sub_mchid 选填 string(12)
【子商户号】 若商户是直连商户:
无需填写该字段
若商户是服务商:
不填则默认返回服务商下的所有分账账单。
如需下载某个子商户下的分账账单,则填指定的子商户号。
bill_date 必填 string(10)
【账单日期】 格式YYYY-MM-DD。仅支持三个月内的账单下载申请。
tar_type 选填 string
【压缩类型】 不填则以不压缩的方式返回数据流
可选取值
GZIP
: 返回格式为.gzip的压缩包账单
请求示例
需配合微信支付工具库 WXPayUtility 使用,请参考 iwiki-document:inline_state
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 * 获取分账账单文件下载地址 24 */ 25public class SplitBill { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/profitsharing/bills"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 SplitBill client = new SplitBill( 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 SplitBillRequest request = new SplitBillRequest(); 41 request.subMchid = "19000000001"; 42 request.billDate = "2019-06-11"; 43 request.tarType = SplitBillTarType.GZIP; 44 try { 45 SplitBillResponse response = client.run(request); 46 47 // TODO: 请求成功,继续业务逻辑 48 System.out.println(response); 49 } catch (WXPayUtility.ApiException e) { 50 // TODO: 请求失败,根据状态码执行不同的逻辑 51 e.printStackTrace(); 52 } 53 } 54 55 public SplitBillResponse run(SplitBillRequest request) { 56 String uri = PATH; 57 Map<String, Object> args = new HashMap<>(); 58 args.put("sub_mchid", request.subMchid); 59 args.put("bill_date", request.billDate); 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, SplitBillResponse.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 SplitBill(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 SplitBillRequest { 104 @SerializedName("sub_mchid") 105 @Expose(serialize = false) 106 public String subMchid; 107 108 @SerializedName("bill_date") 109 @Expose(serialize = false) 110 public String billDate; 111 112 @SerializedName("tar_type") 113 @Expose(serialize = false) 114 public SplitBillTarType tarType; 115 } 116 117 public static class SplitBillResponse { 118 @SerializedName("hash_type") 119 public SplitBillHashType hashType; 120 121 @SerializedName("hash_value") 122 public String hashValue; 123 124 @SerializedName("download_url") 125 public String downloadUrl; 126 } 127 128 public enum SplitBillTarType { 129 @SerializedName("GZIP") 130 GZIP 131 } 132 133 public enum SplitBillHashType { 134 @SerializedName("SHA1") 135 SHA1 136 } 137 138} 139
需配合微信支付工具库 wxpay_utility 使用,请参考 iwiki-document:inline_state
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 := &SplitBillRequest{ 26 SubMchid: wxpay_utility.String("19000000001"), 27 BillDate: wxpay_utility.String("2019-06-11"), 28 TarType: SPLITBILLTARTYPE_GZIP.Ptr(), 29 } 30 31 response, err := SplitBill(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 SplitBill(config *wxpay_utility.MchConfig, request *SplitBillRequest) (response *SplitBillResponse, err error) { 43 const ( 44 host = "https://api.mch.weixin.qq.com" 45 method = "GET" 46 path = "/v3/profitsharing/bills" 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("sub_mchid", *request.SubMchid) 55 query.Add("bill_date", *request.BillDate) 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 response := &SplitBillResponse{} 94 if err := json.Unmarshal(respBody, response); err != nil { 95 return nil, err 96 } 97 98 return response, nil 99 } else { 100 return nil, wxpay_utility.NewApiException( 101 httpResponse.StatusCode, 102 httpResponse.Header, 103 respBody, 104 ) 105 } 106} 107 108type SplitBillRequest struct { 109 SubMchid *string `json:"sub_mchid,omitempty"` 110 BillDate *string `json:"bill_date,omitempty"` 111 TarType *SplitBillTarType `json:"tar_type,omitempty"` 112} 113 114func (o *SplitBillRequest) MarshalJSON() ([]byte, error) { 115 type Alias SplitBillRequest 116 a := &struct { 117 SubMchid *string `json:"sub_mchid,omitempty"` 118 BillDate *string `json:"bill_date,omitempty"` 119 TarType *SplitBillTarType `json:"tar_type,omitempty"` 120 *Alias 121 }{ 122 // 序列化时移除非 Body 字段 123 SubMchid: nil, 124 BillDate: nil, 125 TarType: nil, 126 Alias: (*Alias)(o), 127 } 128 return json.Marshal(a) 129} 130 131type SplitBillResponse struct { 132 HashType *SplitBillHashType `json:"hash_type,omitempty"` 133 HashValue *string `json:"hash_value,omitempty"` 134 DownloadUrl *string `json:"download_url,omitempty"` 135} 136 137type SplitBillTarType string 138 139func (e SplitBillTarType) Ptr() *SplitBillTarType { 140 return &e 141} 142 143const ( 144 SPLITBILLTARTYPE_GZIP SplitBillTarType = "GZIP" 145) 146 147type SplitBillHashType string 148 149func (e SplitBillHashType) Ptr() *SplitBillHashType { 150 return &e 151} 152 153const ( 154 SPLITBILLHASHTYPE_SHA1 SplitBillHashType = "SHA1" 155) 156
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/profitsharing/bills?sub_mchid=19000000001&bill_date=2019-06-11&tar_type=GZIP \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" 5
应答参数
200 OK
hash_type 必填 string
【哈希类型】 原始账单(gzip需要解压缩)的摘要算法,用于校验文件的完整性
可选取值
SHA1
: Secure Hash Algorithm 1
hash_value 必填 string(1024)
【哈希值】 原始账单(gzip需要解压缩)的摘要值,用于校验文件的完整性
download_url 必填 string(2048)
【下载地址】 供下一步请求账单文件的下载地址,该地址30s内有效
应答示例
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
错误码
公共错误码