完结分账
更新时间:2025.08.21不需要进行分账的订单,可直接调用本接口将订单的金额全部解冻给分账方商户
注意:
调用分账接口后,需要解冻剩余资金时,调用本接口将订单剩余的分账金额全部解冻给分账方商户。
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/brand/profitsharing/finish-order
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
Content-Type 必填 string
请设置为application/json
body 包体参数
sub_mchid 必填 string(32)
【子商户号】 订单收款方商户号,填写微信支付分配的商户号
transaction_id 必填 string(32)
【微信订单号】 微信支付订单号
out_order_no 必填 string(64)
【商户分账单号】 商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次。只能是数字、大小写字母_-|*@
description 必填 string(80)
【分账描述】 分账的原因描述,分账账单中需要体现
请求示例
需配合微信支付工具库 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 FinishOrder { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/brand/profitsharing/finish-order"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 FinishOrder client = new FinishOrder( 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 FinishOrderRequest request = new FinishOrderRequest(); 41 request.subMchid = "1900000109"; 42 request.transactionId = "4208450740201411110007820472"; 43 request.outOrderNo = "P20150806125346"; 44 request.description = "分账完结"; 45 try { 46 FinishOrderResponse response = client.run(request); 47 48 // TODO: 请求成功,继续业务逻辑 49 System.out.println(response); 50 } catch (WXPayUtility.ApiException e) { 51 // TODO: 请求失败,根据状态码执行不同的逻辑 52 e.printStackTrace(); 53 } 54 } 55 56 public FinishOrderResponse run(FinishOrderRequest request) { 57 String uri = PATH; 58 String reqBody = WXPayUtility.toJson(request); 59 60 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 61 reqBuilder.addHeader("Accept", "application/json"); 62 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 63 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 64 reqBuilder.addHeader("Content-Type", "application/json"); 65 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 66 reqBuilder.method(METHOD, requestBody); 67 Request httpRequest = reqBuilder.build(); 68 69 // 发送HTTP请求 70 OkHttpClient client = new OkHttpClient.Builder().build(); 71 try (Response httpResponse = client.newCall(httpRequest).execute()) { 72 String respBody = WXPayUtility.extractBody(httpResponse); 73 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 74 // 2XX 成功,验证应答签名 75 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 76 httpResponse.headers(), respBody); 77 78 // 从HTTP应答报文构建返回数据 79 return WXPayUtility.fromJson(respBody, FinishOrderResponse.class); 80 } else { 81 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 82 } 83 } catch (IOException e) { 84 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 85 } 86 } 87 88 private final String mchid; 89 private final String certificateSerialNo; 90 private final PrivateKey privateKey; 91 private final String wechatPayPublicKeyId; 92 private final PublicKey wechatPayPublicKey; 93 94 public FinishOrder(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 95 this.mchid = mchid; 96 this.certificateSerialNo = certificateSerialNo; 97 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 98 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 99 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 100 } 101 102 public static class FinishOrderRequest { 103 @SerializedName("sub_mchid") 104 public String subMchid; 105 106 @SerializedName("transaction_id") 107 public String transactionId; 108 109 @SerializedName("out_order_no") 110 public String outOrderNo; 111 112 @SerializedName("description") 113 public String description; 114 } 115 116 public static class FinishOrderResponse { 117 @SerializedName("sub_mchid") 118 public String subMchid; 119 120 @SerializedName("transaction_id") 121 public String transactionId; 122 123 @SerializedName("out_order_no") 124 public String outOrderNo; 125 126 @SerializedName("order_id") 127 public String orderId; 128 } 129 130} 131
需配合微信支付工具库 wxpay_utility 使用,请参考 Go
1package main 2 3import ( 4 "bytes" 5 "demo/wxpay_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "net/url" 10) 11 12func main() { 13 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 14 config, err := wxpay_utility.CreateMchConfig( 15 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 17 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 18 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 19 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 20 ) 21 if err != nil { 22 fmt.Println(err) 23 return 24 } 25 26 request := &FinishOrderRequest{ 27 SubMchid: wxpay_utility.String("1900000109"), 28 TransactionId: wxpay_utility.String("4208450740201411110007820472"), 29 OutOrderNo: wxpay_utility.String("P20150806125346"), 30 Description: wxpay_utility.String("分账完结"), 31 } 32 33 response, err := FinishOrder(config, request) 34 if err != nil { 35 fmt.Printf("请求失败: %+v\n", err) 36 // TODO: 请求失败,根据状态码执行不同的处理 37 return 38 } 39 40 // TODO: 请求成功,继续业务逻辑 41 fmt.Printf("请求成功: %+v\n", response) 42} 43 44func FinishOrder(config *wxpay_utility.MchConfig, request *FinishOrderRequest) (response *FinishOrderResponse, err error) { 45 const ( 46 host = "https://api.mch.weixin.qq.com" 47 method = "POST" 48 path = "/v3/brand/profitsharing/finish-order" 49 ) 50 51 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 52 if err != nil { 53 return nil, err 54 } 55 reqBody, err := json.Marshal(request) 56 if err != nil { 57 return nil, err 58 } 59 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 60 if err != nil { 61 return nil, err 62 } 63 httpRequest.Header.Set("Accept", "application/json") 64 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 65 httpRequest.Header.Set("Content-Type", "application/json") 66 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 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 response := &FinishOrderResponse{} 96 if err := json.Unmarshal(respBody, response); err != nil { 97 return nil, err 98 } 99 100 return response, nil 101 } else { 102 return nil, wxpay_utility.NewApiException( 103 httpResponse.StatusCode, 104 httpResponse.Header, 105 respBody, 106 ) 107 } 108} 109 110type FinishOrderRequest struct { 111 SubMchid *string `json:"sub_mchid,omitempty"` 112 TransactionId *string `json:"transaction_id,omitempty"` 113 OutOrderNo *string `json:"out_order_no,omitempty"` 114 Description *string `json:"description,omitempty"` 115} 116 117type FinishOrderResponse struct { 118 SubMchid *string `json:"sub_mchid,omitempty"` 119 TransactionId *string `json:"transaction_id,omitempty"` 120 OutOrderNo *string `json:"out_order_no,omitempty"` 121 OrderId *string `json:"order_id,omitempty"` 122} 123
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/brand/profitsharing/finish-order \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "sub_mchid" : "1900000109", 8 "transaction_id" : "4208450740201411110007820472", 9 "out_order_no" : "P20150806125346", 10 "description" : "分账完结" 11 }' 12
应答参数
200 OK
sub_mchid 必填 string
【子商户号】 订单收款方商户号,填写微信支付分配的商户号
transaction_id 必填 string
【微信订单号】 微信支付订单号
out_order_no 必填 string
【商户分账单号】 商户系统内部的分账单号,在商户系统内部唯一(单次分账、多次分账、完结分账应使用不同的商户分账单号),同一分账单号多次请求等同一次,只能是数字、大小写字母_-|*@。
order_id 必填 string
【微信分账单号】 微信分账单号,微信系统返回的唯一标识
应答示例
200 OK
1{ 2 "sub_mchid" : "1900000109", 3 "transaction_id" : "4208450740201411110007820472", 4 "out_order_no" : "P20150806125346", 5 "order_id" : "3008450740201411110007820472" 6} 7
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |