撤销转账
更新时间:2025.07.29商户通过转账接口发起付款后,在用户确认收款之前可以通过该接口撤销付款。该接口返回成功仅表示撤销请求已受理,系统会异步处理退款等操作,以最终查询单据返回状态为准。
注:单个商户的接口频率限制为100次/s
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/fund-app/mch-transfer/partner/transfer-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
Content-Type 必填 string
请设置为application/json
path 路径参数
out_bill_no 必填 string(32)
【商户单号】 商户系统内部的商家单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
body 包体参数
sub_mchid 必填 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 TransferBillCancel { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/fund-app/mch-transfer/partner/transfer-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 TransferBillCancel client = new TransferBillCancel( 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 TransferBillCancelRequest request = new TransferBillCancelRequest(); 41 request.outBillNo = "plfk2020042013"; 42 request.subMchid = "1900001109"; 43 try { 44 TransferBillEntity response = client.run(request); 45 46 // TODO: 请求成功,继续业务逻辑 47 System.out.println(response); 48 } catch (WXPayUtility.ApiException e) { 49 // TODO: 请求失败,根据状态码执行不同的逻辑 50 e.printStackTrace(); 51 } 52 } 53 54 public TransferBillEntity run(TransferBillCancelRequest request) { 55 String uri = PATH; 56 uri = uri.replace("{out_bill_no}", WXPayUtility.urlEncode(request.outBillNo)); 57 String reqBody = WXPayUtility.toJson(request); 58 59 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 60 reqBuilder.addHeader("Accept", "application/json"); 61 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 62 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 63 reqBuilder.addHeader("Content-Type", "application/json"); 64 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 65 reqBuilder.method(METHOD, requestBody); 66 Request httpRequest = reqBuilder.build(); 67 68 // 发送HTTP请求 69 OkHttpClient client = new OkHttpClient.Builder().build(); 70 try (Response httpResponse = client.newCall(httpRequest).execute()) { 71 String respBody = WXPayUtility.extractBody(httpResponse); 72 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 73 // 2XX 成功,验证应答签名 74 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 75 httpResponse.headers(), respBody); 76 77 // 从HTTP应答报文构建返回数据 78 return WXPayUtility.fromJson(respBody, TransferBillEntity.class); 79 } else { 80 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 81 } 82 } catch (IOException e) { 83 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 84 } 85 } 86 87 private final String mchid; 88 private final String certificateSerialNo; 89 private final PrivateKey privateKey; 90 private final String wechatPayPublicKeyId; 91 private final PublicKey wechatPayPublicKey; 92 93 public TransferBillCancel(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 94 this.mchid = mchid; 95 this.certificateSerialNo = certificateSerialNo; 96 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 97 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 98 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 99 } 100 101 public static class TransferBillCancelRequest { 102 @SerializedName("sub_mchid") 103 public String subMchid; 104 105 @SerializedName("out_bill_no") 106 @Expose(serialize = false) 107 public String outBillNo; 108 } 109 110 public static class TransferBillEntity { 111 @SerializedName("sub_mchid") 112 public String subMchid; 113 114 @SerializedName("mchid") 115 public String mchid; 116 117 @SerializedName("out_bill_no") 118 public String outBillNo; 119 120 @SerializedName("transfer_bill_no") 121 public String transferBillNo; 122 123 @SerializedName("appid") 124 public String appid; 125 126 @SerializedName("state") 127 public TransferBillStatus state; 128 129 @SerializedName("transfer_amount") 130 public Long transferAmount; 131 132 @SerializedName("transfer_remark") 133 public String transferRemark; 134 135 @SerializedName("fail_reason") 136 public String failReason; 137 138 @SerializedName("openid") 139 public String openid; 140 141 @SerializedName("user_name") 142 public String userName; 143 144 @SerializedName("create_time") 145 public String createTime; 146 147 @SerializedName("update_time") 148 public String updateTime; 149 } 150 151 public enum TransferBillStatus { 152 @SerializedName("ACCEPTED") 153 ACCEPTED, 154 @SerializedName("PROCESSING") 155 PROCESSING, 156 @SerializedName("WAIT_USER_CONFIRM") 157 WAIT_USER_CONFIRM, 158 @SerializedName("TRANSFERING") 159 TRANSFERING, 160 @SerializedName("SUCCESS") 161 SUCCESS, 162 @SerializedName("FAIL") 163 FAIL, 164 @SerializedName("CANCELING") 165 CANCELING, 166 @SerializedName("CANCELLED") 167 CANCELLED 168 } 169 170} 171
需配合微信支付工具库 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 "strings" 11 "time" 12) 13 14func main() { 15 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 config, err := wxpay_utility.CreateMchConfig( 17 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 18 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 19 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 20 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 21 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 22 ) 23 if err != nil { 24 fmt.Println(err) 25 return 26 } 27 28 request := &TransferBillCancelRequest{ 29 OutBillNo: wxpay_utility.String("plfk2020042013"), 30 SubMchid: wxpay_utility.String("1900001109"), 31 } 32 33 response, err := TransferBillCancel(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 TransferBillCancel(config *wxpay_utility.MchConfig, request *TransferBillCancelRequest) (response *TransferBillEntity, err error) { 45 const ( 46 host = "https://api.mch.weixin.qq.com" 47 method = "POST" 48 path = "/v3/fund-app/mch-transfer/partner/transfer-bills/out-bill-no/{out_bill_no}/cancel" 49 ) 50 51 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 52 if err != nil { 53 return nil, err 54 } 55 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_bill_no}", url.PathEscape(*request.OutBillNo), -1) 56 reqBody, err := json.Marshal(request) 57 if err != nil { 58 return nil, err 59 } 60 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 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 httpRequest.Header.Set("Content-Type", "application/json") 67 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 68 if err != nil { 69 return nil, err 70 } 71 httpRequest.Header.Set("Authorization", authorization) 72 73 client := &http.Client{} 74 httpResponse, err := client.Do(httpRequest) 75 if err != nil { 76 return nil, err 77 } 78 79 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 80 if err != nil { 81 return nil, err 82 } 83 84 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 85 // 2XX 成功,验证应答签名 86 err = wxpay_utility.ValidateResponse( 87 config.WechatPayPublicKeyId(), 88 config.WechatPayPublicKey(), 89 &httpResponse.Header, 90 respBody, 91 ) 92 if err != nil { 93 return nil, err 94 } 95 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 TransferBillCancelRequest struct { 111 SubMchid *string `json:"sub_mchid,omitempty"` 112 OutBillNo *string `json:"out_bill_no,omitempty"` 113} 114 115func (o *TransferBillCancelRequest) MarshalJSON() ([]byte, error) { 116 type Alias TransferBillCancelRequest 117 a := &struct { 118 OutBillNo *string `json:"out_bill_no,omitempty"` 119 *Alias 120 }{ 121 // 序列化时移除非 Body 字段 122 OutBillNo: nil, 123 Alias: (*Alias)(o), 124 } 125 return json.Marshal(a) 126} 127 128type TransferBillEntity struct { 129 SubMchid *string `json:"sub_mchid,omitempty"` 130 Mchid *string `json:"mchid,omitempty"` 131 OutBillNo *string `json:"out_bill_no,omitempty"` 132 TransferBillNo *string `json:"transfer_bill_no,omitempty"` 133 Appid *string `json:"appid,omitempty"` 134 State *TransferBillStatus `json:"state,omitempty"` 135 TransferAmount *int64 `json:"transfer_amount,omitempty"` 136 TransferRemark *string `json:"transfer_remark,omitempty"` 137 FailReason *string `json:"fail_reason,omitempty"` 138 Openid *string `json:"openid,omitempty"` 139 UserName *string `json:"user_name,omitempty"` 140 CreateTime *time.Time `json:"create_time,omitempty"` 141 UpdateTime *string `json:"update_time,omitempty"` 142} 143 144type TransferBillStatus string 145 146func (e TransferBillStatus) Ptr() *TransferBillStatus { 147 return &e 148} 149 150const ( 151 TRANSFERBILLSTATUS_ACCEPTED TransferBillStatus = "ACCEPTED" 152 TRANSFERBILLSTATUS_PROCESSING TransferBillStatus = "PROCESSING" 153 TRANSFERBILLSTATUS_WAIT_USER_CONFIRM TransferBillStatus = "WAIT_USER_CONFIRM" 154 TRANSFERBILLSTATUS_TRANSFERING TransferBillStatus = "TRANSFERING" 155 TRANSFERBILLSTATUS_SUCCESS TransferBillStatus = "SUCCESS" 156 TRANSFERBILLSTATUS_FAIL TransferBillStatus = "FAIL" 157 TRANSFERBILLSTATUS_CANCELING TransferBillStatus = "CANCELING" 158 TRANSFERBILLSTATUS_CANCELLED TransferBillStatus = "CANCELLED" 159) 160
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/fund-app/mch-transfer/partner/transfer-bills/out-bill-no/plfk2020042013/cancel \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "sub_mchid" : "1900001109" 8 }' 9
应答参数
200 OK
sub_mchid 必填 string(32)
【子商户】 微信支付分配的商户号,出资商户号
mchid 必填 string
【商户号】 微信支付分配的商户号
out_bill_no 必填 string(32)
【商户单号】 商户系统内部的商家单号,要求此参数只能由数字、大小写字母组成,在商户系统内部唯一
transfer_bill_no 必填 string
【商家转账订单号】 商家转账订单的主键,唯一定义此资源的标识
appid 必填 string
【商户AppID】 申请商户号的AppID或商户号绑定的AppID(企业号corpid即为此AppID)
state 必填 string
【单据状态】
可选取值
ACCEPTED
: 转账已受理PROCESSING
: 转账处理中,转账结果尚未明确,如一直处于此状态,建议检查账户余额是否足够WAIT_USER_CONFIRM
: 待收款用户确认,可拉起微信收款确认页面进行收款确认TRANSFERING
: 转账结果尚未明确,可拉起微信收款确认页面再次重试确认收款SUCCESS
: 转账成功FAIL
: 转账失败CANCELING
: 商户撤销请求受理成功,该笔转账正在撤销中CANCELLED
: 转账撤销完成
transfer_amount 必填 integer
【转账金额】 转账金额单位为“分”。
transfer_remark 必填 string(32)
【转账备注】 单条转账备注(微信用户会收到该备注),UTF8编码,最多允许32个字符
fail_reason 选填 string
【失败原因】 订单已失败或者已退资金时,返回失败原因
openid 选填 string(64)
【收款用户OpenID】 商户AppID下,某用户的OpenID
user_name 选填 string
【收款用户姓名】 收款方真实姓名。支持标准RSA算法和国密算法,公钥由微信侧提供转账金额 >= 2,000元时,该笔明细必须填写若商户传入收款用户姓名,微信支付会校验用户OpenID与姓名是否一致,并提供电子回单
create_time 必填 string
【单据创建时间】 单据受理成功时返回,按照使用rfc3339所定义的格式,格式为yyyy-MM-DDThh:mm:ss+TIMEZONE
update_time 必填 string
【最后一次状态变更时间】 单据最后更新时间,按照使用rfc3339所定义的格式,格式为yyyy-MM-DDThh:mm:ss+TIMEZONE
应答示例
200 OK
1{ 2 "sub_mchid" : "1900001109", 3 "mchid" : "1900001109", 4 "out_bill_no" : "plfk2020042013", 5 "transfer_bill_no" : "1330000071100999991182020050700019480001", 6 "appid" : "wxf636efh567hg4356", 7 "state" : "SUCCESS", 8 "transfer_amount" : 400000, 9 "transfer_remark" : "2020年4月报销", 10 "fail_reason" : "PAYEE_ACCOUNT_ABNORMAL", 11 "openid" : "o-MYE42l80oelYMDE34nYD456Xoy", 12 "user_name" : "757b340b45ebef5467rter35gf464344v3542sdf4t6re4tb4f54ty45t4yyry45", 13 "create_time" : "2024-04-21T13:29:35.120+08:00", 14 "update_time" : "2015-04-21T13:31:35.120+08:00" 15} 16
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
404 | NOT_FOUND | 转账单不存在 | 检查传入的转账单号 |