请求撤销赔付
更新时间: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)
【商户单号】 商户系统内部的商家单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
请求示例
需配合微信支付工具库 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 45 // TODO: 请求成功,继续业务逻辑 46 System.out.println(response); 47 } catch (WXPayUtility.ApiException e) { 48 // TODO: 请求失败,根据状态码执行不同的逻辑 49 e.printStackTrace(); 50 } 51 } 52 53 public CancelTransferResponse run(CompensationBillCancelRequest request) { 54 String uri = PATH; 55 uri = uri.replace("{out_bill_no}", WXPayUtility.urlEncode(request.outBillNo)); 56 57 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 58 reqBuilder.addHeader("Accept", "application/json"); 59 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 60 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 61 reqBuilder.addHeader("Content-Type", "application/json"); 62 RequestBody emptyBody = RequestBody.create(null, ""); 63 reqBuilder.method(METHOD, emptyBody); 64 Request httpRequest = reqBuilder.build(); 65 66 // 发送HTTP请求 67 OkHttpClient client = new OkHttpClient.Builder().build(); 68 try (Response httpResponse = client.newCall(httpRequest).execute()) { 69 String respBody = WXPayUtility.extractBody(httpResponse); 70 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 71 // 2XX 成功,验证应答签名 72 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 73 httpResponse.headers(), respBody); 74 75 // 从HTTP应答报文构建返回数据 76 return WXPayUtility.fromJson(respBody, CancelTransferResponse.class); 77 } else { 78 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 79 } 80 } catch (IOException e) { 81 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 82 } 83 } 84 85 private final String mchid; 86 private final String certificateSerialNo; 87 private final PrivateKey privateKey; 88 private final String wechatPayPublicKeyId; 89 private final PublicKey wechatPayPublicKey; 90 91 public CompensationBillCancel(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 92 this.mchid = mchid; 93 this.certificateSerialNo = certificateSerialNo; 94 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 95 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 96 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 97 } 98 99 public static class CompensationBillCancelRequest { 100 @SerializedName("out_bill_no") 101 @Expose(serialize = false) 102 public String outBillNo; 103 } 104 105 public static class CancelTransferResponse { 106 @SerializedName("out_bill_no") 107 public String outBillNo; 108 109 @SerializedName("bill_id") 110 public String billId; 111 112 @SerializedName("state") 113 public TransferStatus state; 114 115 @SerializedName("cancel_accept_time") 116 public String cancelAcceptTime; 117 } 118 119 public enum TransferStatus { 120 @SerializedName("ACCEPTED") 121 ACCEPTED, 122 @SerializedName("SUCCESS") 123 SUCCESS, 124 @SerializedName("CLOSED") 125 CLOSED, 126 @SerializedName("WAIT_USER_CONFIRM") 127 WAIT_USER_CONFIRM, 128 @SerializedName("CANCELING") 129 CANCELING, 130 @SerializedName("CANCELLED") 131 CANCELLED 132 } 133 134} 135
需配合微信支付工具库 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 73 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 74 if err != nil { 75 return nil, err 76 } 77 78 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 79 // 2XX 成功,验证应答签名 80 err = wxpay_utility.ValidateResponse( 81 config.WechatPayPublicKeyId(), 82 config.WechatPayPublicKey(), 83 &httpResponse.Header, 84 respBody, 85 ) 86 if err != nil { 87 return nil, err 88 } 89 90 if err := json.Unmarshal(respBody, response); err != nil { 91 return nil, err 92 } 93 94 return response, nil 95 } else { 96 return nil, wxpay_utility.NewApiException( 97 httpResponse.StatusCode, 98 httpResponse.Header, 99 respBody, 100 ) 101 } 102} 103 104type CompensationBillCancelRequest struct { 105 OutBillNo *string `json:"out_bill_no,omitempty"` 106} 107 108func (o *CompensationBillCancelRequest) MarshalJSON() ([]byte, error) { 109 type Alias CompensationBillCancelRequest 110 a := &struct { 111 OutBillNo *string `json:"out_bill_no,omitempty"` 112 *Alias 113 }{ 114 // 序列化时移除非 Body 字段 115 OutBillNo: nil, 116 Alias: (*Alias)(o), 117 } 118 return json.Marshal(a) 119} 120 121type CancelTransferResponse struct { 122 OutBillNo *string `json:"out_bill_no,omitempty"` 123 BillId *string `json:"bill_id,omitempty"` 124 State *TransferStatus `json:"state,omitempty"` 125 CancelAcceptTime *time.Time `json:"cancel_accept_time,omitempty"` 126} 127 128type TransferStatus string 129 130func (e TransferStatus) Ptr() *TransferStatus { 131 return &e 132} 133 134const ( 135 TRANSFERSTATUS_ACCEPTED TransferStatus = "ACCEPTED" 136 TRANSFERSTATUS_SUCCESS TransferStatus = "SUCCESS" 137 TRANSFERSTATUS_CLOSED TransferStatus = "CLOSED" 138 TRANSFERSTATUS_WAIT_USER_CONFIRM TransferStatus = "WAIT_USER_CONFIRM" 139 TRANSFERSTATUS_CANCELING TransferStatus = "CANCELING" 140 TRANSFERSTATUS_CANCELLED TransferStatus = "CANCELLED" 141) 142
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
应答参数
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
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |