冲红电子发票
更新时间:2025.09.03商户对已开具的电子发票进行冲红(开具红票),并将其从微信用户的卡包中删除。仅在微信支付侧开具的电子发票才允许发起冲红。
注意:本接口成功返回仅代表冲红请求已被受理,当冲红完成时,微信支付会根据商户配置的回调地址进行回调通知,商户也可以通过【查询电子发票】接口获取冲红结果及红票信息。
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/new-tax-control-fapiao/fapiao-applications/{fapiao_apply_id}/reverse
请求域名:【主域名】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 路径参数
fapiao_apply_id 必填 string(32)
【发票申请单号】 开票时指定的发票申请单号
body 包体参数
sub_mchid 必填 string(32)
【子商户号】 微信支付分配的子商户号,具体请参考服务商模式开发必要参数说明
reverse_reason 必填 string(200)
【冲红原因】 非数电模式下,填入开票失败原因;
数电模式下,填入可选失败原因枚举:
ISSUE_ERROR: 开票有误
SALES_RETURN: 销货退回
SERVICE_SUSPENTION: 服务中止
SALES_DISCOUNT: 销售折让
fapiao_information 选填 array[object]
【需要冲红的发票信息】 需要冲红的发票信息
属性 | |
fapiao_id 必填 string(32) 【商户发票单号】 商户发票单号,唯一标识一张已开具的发票,必须与该发票在开票时提交的商户发票单号一致 fapiao_number 必填 string(20) 【发票号码】 发票号码 |
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/new-tax-control-fapiao/fapiao-applications/4200000444201910177461284488/reverse \ 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 "reverse_reason" : "非数电模式:退款;数电模式:ISSUE_ERROR|SALES_RETURN|SERVICE_SUSPENSION|SALES_DISCOUNT", 9 "fapiao_information" : [ 10 { 11 "fapiao_id" : "20200701123456", 12 "fapiao_number" : "12897794" 13 } 14 ] 15 }' 16
需配合微信支付工具库 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 ReverseFapiaoApplications { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/new-tax-control-fapiao/fapiao-applications/{fapiao_apply_id}/reverse"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 ReverseFapiaoApplications client = new ReverseFapiaoApplications( 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 ReverseFapiaoApplicationsRequest request = new ReverseFapiaoApplicationsRequest(); 41 request.fapiaoApplyId = "4200000444201910177461284488"; 42 request.subMchid = "1900000109"; 43 request.reverseReason = "非数电模式:退款;数电模式:ISSUE_ERROR|SALES_RETURN|SERVICE_SUSPENSION|SALES_DISCOUNT"; 44 request.fapiaoInformation = new ArrayList<>(); 45 { 46 ReverseFapiaoInfo fapiaoInformationItem = new ReverseFapiaoInfo(); 47 fapiaoInformationItem.fapiaoId = "20200701123456"; 48 fapiaoInformationItem.fapiaoNumber = "12897794"; 49 request.fapiaoInformation.add(fapiaoInformationItem); 50 }; 51 try { 52 client.run(request); 53 } catch (WXPayUtility.ApiException e) { 54 // TODO: 请求失败,根据状态码执行不同的逻辑 55 e.printStackTrace(); 56 } 57 } 58 59 public void run(ReverseFapiaoApplicationsRequest request) { 60 String uri = PATH; 61 uri = uri.replace("{fapiao_apply_id}", WXPayUtility.urlEncode(request.fapiaoApplyId)); 62 String reqBody = WXPayUtility.toJson(request); 63 64 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 65 reqBuilder.addHeader("Accept", "application/json"); 66 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 67 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 68 reqBuilder.addHeader("Content-Type", "application/json"); 69 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 70 reqBuilder.method(METHOD, requestBody); 71 Request httpRequest = reqBuilder.build(); 72 73 // 发送HTTP请求 74 OkHttpClient client = new OkHttpClient.Builder().build(); 75 try (Response httpResponse = client.newCall(httpRequest).execute()) { 76 String respBody = WXPayUtility.extractBody(httpResponse); 77 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 78 // 2XX 成功,验证应答签名 79 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 80 httpResponse.headers(), respBody); 81 82 return; 83 } else { 84 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 85 } 86 } catch (IOException e) { 87 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 88 } 89 } 90 91 private final String mchid; 92 private final String certificateSerialNo; 93 private final PrivateKey privateKey; 94 private final String wechatPayPublicKeyId; 95 private final PublicKey wechatPayPublicKey; 96 97 public ReverseFapiaoApplications(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 98 this.mchid = mchid; 99 this.certificateSerialNo = certificateSerialNo; 100 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 101 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 102 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 103 } 104 105 public static class ReverseFapiaoApplicationsRequest { 106 @SerializedName("sub_mchid") 107 public String subMchid; 108 109 @SerializedName("fapiao_apply_id") 110 @Expose(serialize = false) 111 public String fapiaoApplyId; 112 113 @SerializedName("reverse_reason") 114 public String reverseReason; 115 116 @SerializedName("fapiao_information") 117 public List<ReverseFapiaoInfo> fapiaoInformation; 118 } 119 120 public static class ReverseFapiaoInfo { 121 @SerializedName("fapiao_id") 122 public String fapiaoId; 123 124 @SerializedName("fapiao_number") 125 public String fapiaoNumber; 126 } 127 128} 129
需配合微信支付工具库 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) 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 := &ReverseFapiaoApplicationsRequest{ 28 FapiaoApplyId: wxpay_utility.String("4200000444201910177461284488"), 29 SubMchid: wxpay_utility.String("1900000109"), 30 ReverseReason: wxpay_utility.String("非数电模式:退款;数电模式:ISSUE_ERROR|SALES_RETURN|SERVICE_SUSPENSION|SALES_DISCOUNT"), 31 FapiaoInformation: []ReverseFapiaoInfo{ReverseFapiaoInfo{ 32 FapiaoId: wxpay_utility.String("20200701123456"), 33 FapiaoNumber: wxpay_utility.String("12897794"), 34 }}, 35 } 36 37 err = ReverseFapiaoApplications(config, request) 38 if err != nil { 39 fmt.Printf("请求失败: %+v\n", err) 40 // TODO: 请求失败,根据状态码执行不同的处理 41 return 42 } 43 44 // TODO: 请求成功,继续业务逻辑 45 fmt.Println("请求成功") 46} 47 48func ReverseFapiaoApplications(config *wxpay_utility.MchConfig, request *ReverseFapiaoApplicationsRequest) (err error) { 49 const ( 50 host = "https://api.mch.weixin.qq.com" 51 method = "POST" 52 path = "/v3/new-tax-control-fapiao/fapiao-applications/{fapiao_apply_id}/reverse" 53 ) 54 55 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 56 if err != nil { 57 return err 58 } 59 reqUrl.Path = strings.Replace(reqUrl.Path, "{fapiao_apply_id}", url.PathEscape(*request.FapiaoApplyId), -1) 60 reqBody, err := json.Marshal(request) 61 if err != nil { 62 return err 63 } 64 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 65 if err != nil { 66 return err 67 } 68 httpRequest.Header.Set("Accept", "application/json") 69 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 70 httpRequest.Header.Set("Content-Type", "application/json") 71 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 72 if err != nil { 73 return err 74 } 75 httpRequest.Header.Set("Authorization", authorization) 76 77 client := &http.Client{} 78 httpResponse, err := client.Do(httpRequest) 79 if err != nil { 80 return err 81 } 82 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 83 if err != nil { 84 return err 85 } 86 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 87 // 2XX 成功,验证应答签名 88 err = wxpay_utility.ValidateResponse( 89 config.WechatPayPublicKeyId(), 90 config.WechatPayPublicKey(), 91 &httpResponse.Header, 92 respBody, 93 ) 94 if err != nil { 95 return err 96 } 97 return nil 98 } else { 99 return wxpay_utility.NewApiException( 100 httpResponse.StatusCode, 101 httpResponse.Header, 102 respBody, 103 ) 104 } 105} 106 107type ReverseFapiaoApplicationsRequest struct { 108 SubMchid *string `json:"sub_mchid,omitempty"` 109 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"` 110 ReverseReason *string `json:"reverse_reason,omitempty"` 111 FapiaoInformation []ReverseFapiaoInfo `json:"fapiao_information,omitempty"` 112} 113 114func (o *ReverseFapiaoApplicationsRequest) MarshalJSON() ([]byte, error) { 115 type Alias ReverseFapiaoApplicationsRequest 116 a := &struct { 117 FapiaoApplyId *string `json:"fapiao_apply_id,omitempty"` 118 *Alias 119 }{ 120 // 序列化时移除非 Body 字段 121 FapiaoApplyId: nil, 122 Alias: (*Alias)(o), 123 } 124 return json.Marshal(a) 125} 126 127type ReverseFapiaoInfo struct { 128 FapiaoId *string `json:"fapiao_id,omitempty"` 129 FapiaoNumber *string `json:"fapiao_number,omitempty"` 130} 131
应答参数
无应答包体
应答示例
202 Accepted
1'无应答包体' 2
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | INVALID_REQUEST | 请求参数符合参数格式,但不符合业务规则 | 请使用正确的参数重新调用 |
400 | RESOURCE_ALREADY_EXISTS | 发票申请单已存在 | 请稍后调用【查询电子发票】接口获取开票结果 |
403 | NO_AUTH | 商户无权限 | 请检查是否已经开通电子发票产品相关功能权限,并检查子商户是否接受了服务商的邀请 |
403 | RULE_LIMIT | 发票当前状态不允许冲红 | 请检查发票状态是否为“发票已开具”,可调用【查询电子发票】接口获取当前的发票状态 |
404 | RESOURCE_NOT_EXISTS | 发票申请单不存在 | 请检查发票申请单号是否正确 |
404 | RESOURCE_NOT_EXISTS | 要冲红的发票不存在 | 请检查商户发票单号、发票代码、发票号码是否正确 |
429 | FREQUENCY_LIMITED | 频率超限 | 请降低请求接口频率 |