医保退款通知
更新时间:2025.12.04商户调用该接口向微信医保后台通知医保订单的退款成功结果
接口说明
支持商户:【普通商户】
请求方式:【POST】/v3/med-ins/refunds/notify
请求域名:【主域名】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
query 查询参数
mix_trade_no 必填 string(32)
【医保自费混合订单号】 医保自费混合订单号
body 包体参数
med_refund_total_fee 必填 integer
【医保退款的总金额】 单位分,医保退款的总金额。
med_refund_gov_fee 必填 integer
【医保统筹退款金额】 单位分,医保统筹退款金额。
med_refund_self_fee 必填 integer
【医保个账退款金额】 单位分,医保个账退款金额。
med_refund_other_fee 必填 integer
【医保其他退款金额】 单位分,医保其他退款金额。
refund_time 必填 string(64)
【医保退款成功时间】 遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE。
out_refund_no 必填 string(64)
【医疗机构退款单号】 有自费单时,医疗机构应填与自费退款申请处一致的out_refund_no。
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/med-ins/refunds/notify?mix_trade_no=202204022005169952975171534816 \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "med_refund_total_fee" : 45000, 8 "med_refund_gov_fee" : 45000, 9 "med_refund_self_fee" : 45000, 10 "med_refund_other_fee" : 45000, 11 "refund_time" : "2015-05-20T13:29:35+08:00", 12 "out_refund_no" : "R202204022005169952975171534816" 13 }' 14
需配合微信支付工具库 WXPayUtility 使用,请参考Java
1package com.java.demo; 2 3import com.java.utils.WXPayUtility; // 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/merchant/4014931831 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 NotifyRefund { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/med-ins/refunds/notify"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 32 NotifyRefund client = new NotifyRefund( 33 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756 34 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053 35 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 36 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816 37 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 38 ); 39 40 NotifyRefundRequest request = new NotifyRefundRequest(); 41 request.mixTradeNo = "202204022005169952975171534816"; 42 request.medRefundTotalFee = 45000L; 43 request.medRefundGovFee = 45000L; 44 request.medRefundSelfFee = 45000L; 45 request.medRefundOtherFee = 45000L; 46 request.refundTime = "2015-05-20T13:29:35+08:00"; 47 request.outRefundNo = "R202204022005169952975171534816"; 48 try { 49 client.run(request); 50 } catch (WXPayUtility.ApiException e) { 51 // TODO: 请求失败,根据状态码执行不同的逻辑 52 e.printStackTrace(); 53 } 54 } 55 56 public void run(NotifyRefundRequest request) { 57 String uri = PATH; 58 Map<String, Object> args = new HashMap<>(); 59 args.put("mix_trade_no", request.mixTradeNo); 60 String queryString = WXPayUtility.urlEncode(args); 61 if (!queryString.isEmpty()) { 62 uri = uri + "?" + queryString; 63 } 64 String reqBody = WXPayUtility.toJson(request); 65 66 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 67 reqBuilder.addHeader("Accept", "application/json"); 68 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 69 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 70 reqBuilder.addHeader("Content-Type", "application/json"); 71 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 72 reqBuilder.method(METHOD, requestBody); 73 Request httpRequest = reqBuilder.build(); 74 75 // 发送HTTP请求 76 OkHttpClient client = new OkHttpClient.Builder().build(); 77 try (Response httpResponse = client.newCall(httpRequest).execute()) { 78 String respBody = WXPayUtility.extractBody(httpResponse); 79 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 80 // 2XX 成功,验证应答签名 81 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 82 httpResponse.headers(), respBody); 83 84 return; 85 } else { 86 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 87 } 88 } catch (IOException e) { 89 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 90 } 91 } 92 93 private final String mchid; 94 private final String certificateSerialNo; 95 private final PrivateKey privateKey; 96 private final String wechatPayPublicKeyId; 97 private final PublicKey wechatPayPublicKey; 98 99 public NotifyRefund(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 100 this.mchid = mchid; 101 this.certificateSerialNo = certificateSerialNo; 102 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 103 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 104 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 105 } 106 107 public static class NotifyRefundRequest { 108 @SerializedName("mix_trade_no") 109 @Expose(serialize = false) 110 public String mixTradeNo; 111 112 @SerializedName("med_refund_total_fee") 113 public Long medRefundTotalFee; 114 115 @SerializedName("med_refund_gov_fee") 116 public Long medRefundGovFee; 117 118 @SerializedName("med_refund_self_fee") 119 public Long medRefundSelfFee; 120 121 @SerializedName("med_refund_other_fee") 122 public Long medRefundOtherFee; 123 124 @SerializedName("refund_time") 125 public String refundTime; 126 127 @SerializedName("out_refund_no") 128 public String outRefundNo; 129 } 130 131} 132
需配合微信支付工具库 wxpay_utility 使用,请参考Go
1package main 2 3import ( 4 "bytes" 5 "demo/wxpay_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/v3/merchant/4015119334 6 "encoding/json" 7 "fmt" 8 "net/http" 9 "net/url" 10) 11 12func main() { 13 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 14 config, err := wxpay_utility.CreateMchConfig( 15 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756 16 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053 17 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 18 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816 19 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 20 ) 21 if err != nil { 22 fmt.Println(err) 23 return 24 } 25 26 request := &NotifyRefundRequest{ 27 MixTradeNo: wxpay_utility.String("202204022005169952975171534816"), 28 MedRefundTotalFee: wxpay_utility.Int64(45000), 29 MedRefundGovFee: wxpay_utility.Int64(45000), 30 MedRefundSelfFee: wxpay_utility.Int64(45000), 31 MedRefundOtherFee: wxpay_utility.Int64(45000), 32 RefundTime: wxpay_utility.String("2015-05-20T13:29:35+08:00"), 33 OutRefundNo: wxpay_utility.String("R202204022005169952975171534816"), 34 } 35 36 err = NotifyRefund(config, request) 37 if err != nil { 38 fmt.Printf("请求失败: %+v\n", err) 39 // TODO: 请求失败,根据状态码执行不同的处理 40 return 41 } 42 43 // TODO: 请求成功,继续业务逻辑 44 fmt.Println("请求成功") 45} 46 47func NotifyRefund(config *wxpay_utility.MchConfig, request *NotifyRefundRequest) (err error) { 48 const ( 49 host = "https://api.mch.weixin.qq.com" 50 method = "POST" 51 path = "/v3/med-ins/refunds/notify" 52 ) 53 54 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 55 if err != nil { 56 return err 57 } 58 query := reqUrl.Query() 59 if request.MixTradeNo != nil { 60 query.Add("mix_trade_no", *request.MixTradeNo) 61 } 62 reqUrl.RawQuery = query.Encode() 63 reqBody, err := json.Marshal(request) 64 if err != nil { 65 return err 66 } 67 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 68 if err != nil { 69 return err 70 } 71 httpRequest.Header.Set("Accept", "application/json") 72 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 73 httpRequest.Header.Set("Content-Type", "application/json") 74 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 75 if err != nil { 76 return err 77 } 78 httpRequest.Header.Set("Authorization", authorization) 79 80 client := &http.Client{} 81 httpResponse, err := client.Do(httpRequest) 82 if err != nil { 83 return err 84 } 85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 86 if err != nil { 87 return err 88 } 89 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 90 // 2XX 成功,验证应答签名 91 err = wxpay_utility.ValidateResponse( 92 config.WechatPayPublicKeyId(), 93 config.WechatPayPublicKey(), 94 &httpResponse.Header, 95 respBody, 96 ) 97 if err != nil { 98 return err 99 } 100 return nil 101 } else { 102 return wxpay_utility.NewApiException( 103 httpResponse.StatusCode, 104 httpResponse.Header, 105 respBody, 106 ) 107 } 108} 109 110type NotifyRefundRequest struct { 111 MixTradeNo *string `json:"mix_trade_no,omitempty"` 112 MedRefundTotalFee *int64 `json:"med_refund_total_fee,omitempty"` 113 MedRefundGovFee *int64 `json:"med_refund_gov_fee,omitempty"` 114 MedRefundSelfFee *int64 `json:"med_refund_self_fee,omitempty"` 115 MedRefundOtherFee *int64 `json:"med_refund_other_fee,omitempty"` 116 RefundTime *string `json:"refund_time,omitempty"` 117 OutRefundNo *string `json:"out_refund_no,omitempty"` 118} 119 120func (o *NotifyRefundRequest) MarshalJSON() ([]byte, error) { 121 type Alias NotifyRefundRequest 122 a := &struct { 123 MixTradeNo *string `json:"mix_trade_no,omitempty"` 124 *Alias 125 }{ 126 // 序列化时移除非 Body 字段 127 MixTradeNo: nil, 128 Alias: (*Alias)(o), 129 } 130 return json.Marshal(a) 131} 132
应答参数
无应答包体
应答示例
204 No Content
1'无应答包体' 2
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示
状态码 | 错误码 | 描述 | 解决方案 |
|---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
400 | PARAM_ERROR | 金额计算出现溢出,请检查填写的金额是否正确。 | 请检查填写的金额是否正确 |
403 | RULE_LIMIT | 医保退款总金额校验不通过,请检查请求字段是否满足规则:med_refund_total_fee = med_refund_gov_fee + med_refund_self_fee + med_refund_other_fee | 请确认是否满足规则 |
403 | RULE_LIMIT | 混合单中没有医保订单,无法接收医保退款通知,请检查系统逻辑 | 请确认是否包含医保订单 |
403 | RULE_LIMIT | 医保订单未支付,请支付后重试 | 请在医保订单支付后重试 |
403 | RULE_LIMIT | 医保退款金额与下单金额不匹配:med_ins_gov_fee != med_refund_gov_fee || med_ins_self_fee != med_refund_self_fee || med_ins_other_fee != med_refund_other_fee | 请确认金额是否匹配 |
403 | RULE_LIMIT | 请求次数超过限制,请稍后重试。 | 请稍后重试 |
404 | NOT_FOUND | 未找到订单,请确认订单号是否正确。 | 请确认订单号是否正确 |

