请求撤销赔付

更新时间: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)

【商户单号】 商户系统内部的商家单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一

请求示例

Java
Go
curl

需配合微信支付工具库 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

应答参数

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

系统异常,请稍后重试

请稍后重试

 

 

更多技术问题
技术咨询
反馈
咨询
目录
置顶