获取购付汇账单文件下载链接
更新时间:2024.11.05服务商可以调用此接口获取购付汇账单文件下载链接。在有效期内请求下载链接可以下载出账日为指定日期的购付汇账单文件。
有关账单的说明:
购付汇账单中提供的是前一天购付汇完成、最终出境成功的账单,还未完成出境或出境失败的出境单不会出现在账单中。注:在出境请求被受理之后,预计购汇时间为T+1(其中T为受理日期)。
通过下载链接获取到的账单文件为资金出境购付汇账单,出账日期为T+1,即当天下载的是前一天购付汇完成的出境成功账单。
一般在上午10点后可以下载到前一天的账单,实际账单下载的情况请关注错误码提示。
提示,下载三个月前或更早的历史数据时下载速度可能会变慢。
账单中涉及金额的字段单位为“元”。
当获取到账单文件资源的下载地址信息后,如:
1{ 2 "hash_type": "SHA1", 3 "hash_value": "79bb0f45fc4c42234a918000b2668d689e2bde04", 4 "download_url": " <https://api.mch.weixin.qq.com/v3/billdownload/file?token=xxx"> 5}
可以通过微信支付API v3标准对download_url进行签名,并发起请求,即可下载到账单文件的数据流。
注意:
账单文件的下载地址的有效时间为30s;
强烈建议商户将实际账单文件的哈希值和之前从接口获取到的哈希值进行比对,以确认数据的完整性。
接口说明
支持商户:【平台商户】
请求方式:【GET】/v3/funds-to-oversea/bill-download-url
请求域名:【主域名】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(20)
【子商户号】若商户是直连商户:
无需填写该字段
若商户是服务商:
不填则默认返回服务商下的账单数据
如需下载某个子商户下的账单数据,则该字段必填
请求示例
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/funds-to-oversea/bill-download-url?bill_date=2021-01-01&sub_mchid=19000000001 \ 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 * 获取购付汇账单文件下载链接 24 */ 25public class QueryDownloadInfo { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/funds-to-oversea/bill-download-url"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 QueryDownloadInfo client = new QueryDownloadInfo( 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 QueryDownloadInfoRequest request = new QueryDownloadInfoRequest(); 41 request.billDate = "2021-01-01"; 42 request.subMchid = "19000000001"; 43 try { 44 BillDownloadInfoEntity response = client.run(request); 45 // TODO: 请求成功,继续业务逻辑 46 System.out.println(response); 47 } catch (WXPayUtility.ApiException e) { 48 // TODO: 请求失败,根据状态码执行不同的逻辑 49 e.printStackTrace(); 50 } 51 } 52 53 public BillDownloadInfoEntity run(QueryDownloadInfoRequest request) { 54 String uri = PATH; 55 Map<String, Object> args = new HashMap<>(); 56 args.put("bill_date", request.billDate); 57 args.put("sub_mchid", request.subMchid); 58 String queryString = WXPayUtility.urlEncode(args); 59 if (!queryString.isEmpty()) { 60 uri = uri + "?" + queryString; 61 } 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, BillDownloadInfoEntity.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 QueryDownloadInfo(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 QueryDownloadInfoRequest { 104 @SerializedName("bill_date") 105 @Expose(serialize = false) 106 public String billDate; 107 108 @SerializedName("sub_mchid") 109 @Expose(serialize = false) 110 public String subMchid; 111 } 112 113 public static class BillDownloadInfoEntity { 114 @SerializedName("hash_type") 115 public HashType hashType; 116 117 @SerializedName("hash_value") 118 public String hashValue; 119 120 @SerializedName("download_url") 121 public String downloadUrl; 122 } 123 124 public enum HashType { 125 @SerializedName("SHA1") 126 SHA1 127 } 128 129} 130
需配合微信支付工具库 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 := &QueryDownloadInfoRequest{ 26 BillDate: wxpay_utility.String("2021-01-01"), 27 SubMchid: wxpay_utility.String("19000000001"), 28 } 29 30 response, err := QueryDownloadInfo(config, request) 31 if err != nil { 32 fmt.Printf("请求失败: %+v\n", err) 33 // TODO: 请求失败,根据状态码执行不同的处理 34 return 35 } 36 37 // TODO: 请求成功,继续业务逻辑 38 fmt.Printf("请求成功: %+v\n", response) 39} 40 41func QueryDownloadInfo(config *wxpay_utility.MchConfig, request *QueryDownloadInfoRequest) (response *BillDownloadInfoEntity, err error) { 42 const ( 43 host = "https://api.mch.weixin.qq.com" 44 method = "GET" 45 path = "/v3/funds-to-oversea/bill-download-url" 46 ) 47 48 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 49 if err != nil { 50 return nil, err 51 } 52 query := reqUrl.Query() 53 if request.BillDate != nil { 54 query.Add("bill_date", *request.BillDate) 55 } 56 if request.SubMchid != nil { 57 query.Add("sub_mchid", *request.SubMchid) 58 } 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 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 78 if err != nil { 79 return nil, err 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 response := &BillDownloadInfoEntity{} 93 if err := json.Unmarshal(respBody, response); err != nil { 94 return nil, err 95 } 96 97 return response, nil 98 } else { 99 return nil, wxpay_utility.NewApiException( 100 httpResponse.StatusCode, 101 httpResponse.Header, 102 respBody, 103 ) 104 } 105} 106 107type QueryDownloadInfoRequest struct { 108 BillDate *string `json:"bill_date,omitempty"` 109 SubMchid *string `json:"sub_mchid,omitempty"` 110} 111 112func (o *QueryDownloadInfoRequest) MarshalJSON() ([]byte, error) { 113 type Alias QueryDownloadInfoRequest 114 a := &struct { 115 BillDate *string `json:"bill_date,omitempty"` 116 SubMchid *string `json:"sub_mchid,omitempty"` 117 *Alias 118 }{ 119 // 序列化时移除非 Body 字段 120 BillDate: nil, 121 SubMchid: nil, 122 Alias: (*Alias)(o), 123 } 124 return json.Marshal(a) 125} 126 127type BillDownloadInfoEntity struct { 128 HashType *HashType `json:"hash_type,omitempty"` 129 HashValue *string `json:"hash_value,omitempty"` 130 DownloadUrl *string `json:"download_url,omitempty"` 131} 132 133type HashType string 134 135func (e HashType) Ptr() *HashType { 136 return &e 137} 138 139const ( 140 HASHTYPE_SHA1 HashType = "SHA1" 141) 142
应答参数
|
hash_type 必填 string
【哈希类型】哈希类型
可选取值:
SHA1
: Secure Hash Algorithm 1
hash_value 必填 string(1024)
【哈希值】原始账单(gzip需要解压缩)的摘要值,用于校验文件的完整性。
建议将实际账单文件的哈希值和该哈希值进行比对,以确认数据的完整性。
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 | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
403 | NO_AUTH | 商户无权限申请资金出境 | 商户无权限申请资金出境,请申请相关权限 |