请求撤销赔付
更新时间:2025.07.25商户可通过该接口撤销赔付用户,在用户确认收款之前可以通过该接口撤销赔付。该接口返回成功仅表示撤销请求已受理,系统会异步处理退款等操作,以最终查询单据返回状态为准。
接口说明
支持商户:【平台商户】
请求方式:【POST】/v3/platsolution/ecommerce/mch-transfer/compensate-bills/out-bill-no/{out_bill_no}/cancel
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
path 路径参数
out_bill_no 必填 string(32)
【商户单号】 商户系统内部的商家单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/platsolution/ecommerce/mch-transfer/compensate-bills/out-bill-no/plfk2020042013/cancel \ 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 CompensationBillCancel { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/platsolution/ecommerce/mch-transfer/compensate-bills/out-bill-no/{out_bill_no}/cancel"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 CompensationBillCancel client = new CompensationBillCancel( 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 CompensationBillCancelRequest request = new CompensationBillCancelRequest(); 41 request.outBillNo = "plfk2020042013"; 42 try { 43 CancelTransferResponse response = client.run(request); 44 // TODO: 请求成功,继续业务逻辑 45 System.out.println(response); 46 } catch (WXPayUtility.ApiException e) { 47 // TODO: 请求失败,根据状态码执行不同的逻辑 48 e.printStackTrace(); 49 } 50 } 51 52 public CancelTransferResponse run(CompensationBillCancelRequest request) { 53 String uri = PATH; 54 uri = uri.replace("{out_bill_no}", WXPayUtility.urlEncode(request.outBillNo)); 55 56 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 57 reqBuilder.addHeader("Accept", "application/json"); 58 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 59 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 60 reqBuilder.addHeader("Content-Type", "application/json"); 61 RequestBody emptyBody = RequestBody.create(null, ""); 62 reqBuilder.method(METHOD, emptyBody); 63 Request httpRequest = reqBuilder.build(); 64 65 // 发送HTTP请求 66 OkHttpClient client = new OkHttpClient.Builder().build(); 67 try (Response httpResponse = client.newCall(httpRequest).execute()) { 68 String respBody = WXPayUtility.extractBody(httpResponse); 69 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 70 // 2XX 成功,验证应答签名 71 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 72 httpResponse.headers(), respBody); 73 74 // 从HTTP应答报文构建返回数据 75 return WXPayUtility.fromJson(respBody, CancelTransferResponse.class); 76 } else { 77 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 78 } 79 } catch (IOException e) { 80 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 81 } 82 } 83 84 private final String mchid; 85 private final String certificateSerialNo; 86 private final PrivateKey privateKey; 87 private final String wechatPayPublicKeyId; 88 private final PublicKey wechatPayPublicKey; 89 90 public CompensationBillCancel(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 91 this.mchid = mchid; 92 this.certificateSerialNo = certificateSerialNo; 93 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 94 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 95 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 96 } 97 98 public static class CompensationBillCancelRequest { 99 @SerializedName("out_bill_no") 100 @Expose(serialize = false) 101 public String outBillNo; 102 } 103 104 public static class CancelTransferResponse { 105 @SerializedName("out_bill_no") 106 public String outBillNo; 107 108 @SerializedName("bill_id") 109 public String billId; 110 111 @SerializedName("state") 112 public TransferStatus state; 113 114 @SerializedName("cancel_accept_time") 115 public String cancelAcceptTime; 116 } 117 118 public enum TransferStatus { 119 @SerializedName("ACCEPTED") 120 ACCEPTED, 121 @SerializedName("SUCCESS") 122 SUCCESS, 123 @SerializedName("CLOSED") 124 CLOSED, 125 @SerializedName("WAIT_USER_CONFIRM") 126 WAIT_USER_CONFIRM, 127 @SerializedName("CANCELING") 128 CANCELING, 129 @SerializedName("CANCELLED") 130 CANCELLED 131 } 132 133} 134
需配合微信支付工具库 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 "strings" 10 "time" 11) 12 13func main() { 14 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 15 config, err := wxpay_utility.CreateMchConfig( 16 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 17 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 18 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 19 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 20 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 21 ) 22 if err != nil { 23 fmt.Println(err) 24 return 25 } 26 27 request := &CompensationBillCancelRequest{ 28 OutBillNo: wxpay_utility.String("plfk2020042013"), 29 } 30 31 response, err := CompensationBillCancel(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 CompensationBillCancel(config *wxpay_utility.MchConfig, request *CompensationBillCancelRequest) (response *CancelTransferResponse, err error) { 43 const ( 44 host = "https://api.mch.weixin.qq.com" 45 method = "POST" 46 path = "/v3/platsolution/ecommerce/mch-transfer/compensate-bills/out-bill-no/{out_bill_no}/cancel" 47 ) 48 49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 50 if err != nil { 51 return nil, err 52 } 53 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_bill_no}", url.PathEscape(*request.OutBillNo), -1) 54 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 55 if err != nil { 56 return nil, err 57 } 58 httpRequest.Header.Set("Accept", "application/json") 59 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 60 httpRequest.Header.Set("Content-Type", "application/json") 61 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 62 if err != nil { 63 return nil, err 64 } 65 httpRequest.Header.Set("Authorization", authorization) 66 67 client := &http.Client{} 68 httpResponse, err := client.Do(httpRequest) 69 if err != nil { 70 return nil, err 71 } 72 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 73 if err != nil { 74 return nil, err 75 } 76 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 77 // 2XX 成功,验证应答签名 78 err = wxpay_utility.ValidateResponse( 79 config.WechatPayPublicKeyId(), 80 config.WechatPayPublicKey(), 81 &httpResponse.Header, 82 respBody, 83 ) 84 if err != nil { 85 return nil, err 86 } 87 response := &CancelTransferResponse{} 88 if err := json.Unmarshal(respBody, response); err != nil { 89 return nil, err 90 } 91 92 return response, nil 93 } else { 94 return nil, wxpay_utility.NewApiException( 95 httpResponse.StatusCode, 96 httpResponse.Header, 97 respBody, 98 ) 99 } 100} 101 102type CompensationBillCancelRequest struct { 103 OutBillNo *string `json:"out_bill_no,omitempty"` 104} 105 106func (o *CompensationBillCancelRequest) MarshalJSON() ([]byte, error) { 107 type Alias CompensationBillCancelRequest 108 a := &struct { 109 OutBillNo *string `json:"out_bill_no,omitempty"` 110 *Alias 111 }{ 112 // 序列化时移除非 Body 字段 113 OutBillNo: nil, 114 Alias: (*Alias)(o), 115 } 116 return json.Marshal(a) 117} 118 119type CancelTransferResponse struct { 120 OutBillNo *string `json:"out_bill_no,omitempty"` 121 BillId *string `json:"bill_id,omitempty"` 122 State *TransferStatus `json:"state,omitempty"` 123 CancelAcceptTime *time.Time `json:"cancel_accept_time,omitempty"` 124} 125 126type TransferStatus string 127 128func (e TransferStatus) Ptr() *TransferStatus { 129 return &e 130} 131 132const ( 133 TRANSFERSTATUS_ACCEPTED TransferStatus = "ACCEPTED" 134 TRANSFERSTATUS_SUCCESS TransferStatus = "SUCCESS" 135 TRANSFERSTATUS_CLOSED TransferStatus = "CLOSED" 136 TRANSFERSTATUS_WAIT_USER_CONFIRM TransferStatus = "WAIT_USER_CONFIRM" 137 TRANSFERSTATUS_CANCELING TransferStatus = "CANCELING" 138 TRANSFERSTATUS_CANCELLED TransferStatus = "CANCELLED" 139) 140
应答参数
200 OK
out_bill_no 必填 string(32)
【商户单号】 商户系统内部的商家单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
bill_id 必填 string(64)
【微信支付付款单号】 微信支付付款单号
state 必填 string
【付款状态】 付款状态
可选取值
ACCEPTED: 请求已受理,处理结果尚未明确,如一直处于此状态,建议检查账户余额是否足够SUCCESS: 已成功CLOSED: 系统关闭(余额不足、转账失败等原因)。WAIT_USER_CONFIRM: 预下单成功,等待用户确认CANCELING: 商户撤销请求受理成功,该笔付款正在撤销中CANCELLED: 付款撤销完成
cancel_accept_time 必填 string(32)
【撤销受理时间】 撤销受理成功时会返回,按照使用rfc3339所定义的格式,格式为yyyy-MM-DDThh:mm:ss+TIMEZONE
应答示例
200 OK
1{ 2 "out_bill_no" : "plfk2020042013", 3 "bill_id" : "1330000071100999991182020050700019480001", 4 "state" : "SUCCESS", 5 "cancel_accept_time" : "2015-05-20T13:29:35+08:00" 6} 7
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示
