获取商家券事件通知地址
更新时间:2024.09.20使用场景:用于查询设置的通知URL
限制条件:仅可以查询由请求商户号设置的商家券通知URL
接口说明
支持商户:【普通商户】
请求方式:【GET】/v3/marketing/busifavor/callbacks
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
query 查询参数
mchid 选填 string(15)
【商户号】商户号,不填默认查询调用方商户号
请求示例
GET
1curl -X GET \ 2 https://api.mch.weixin.qq.com/v3/marketing/busifavor/callbacks?mchid=98568888 \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" 5
需配合微信支付工具库 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 GetCouponNotify { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "GET"; 28 private static String PATH = "/v3/marketing/busifavor/callbacks"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 32 GetCouponNotify client = new GetCouponNotify( 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 GetCouponNotifyRequest request = new GetCouponNotifyRequest(); 41 request.mchid = "98568888"; 42 try { 43 GetCouponNotifyResponse response = client.run(request); 44 // TODO: 请求成功,继续业务逻辑 45 System.out.println(response); 46 } catch (WXPayUtility.ApiException e) { 47 // TODO: 请求失败,根据状态码执行不同的逻辑 48 e.printStackTrace(); 49 } 50 } 51 52 public GetCouponNotifyResponse run(GetCouponNotifyRequest request) { 53 String uri = PATH; 54 Map<String, Object> args = new HashMap<>(); 55 args.put("mchid", request.mchid); 56 String queryString = WXPayUtility.urlEncode(args); 57 if (!queryString.isEmpty()) { 58 uri = uri + "?" + queryString; 59 } 60 61 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 62 reqBuilder.addHeader("Accept", "application/json"); 63 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 64 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo, privateKey, METHOD, uri, null)); 65 reqBuilder.method(METHOD, null); 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, GetCouponNotifyResponse.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 GetCouponNotify(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 GetCouponNotifyRequest { 102 @SerializedName("mchid") 103 @Expose(serialize = false) 104 public String mchid; 105 } 106 107 public static class GetCouponNotifyResponse { 108 @SerializedName("mchid") 109 public String mchid; 110 111 @SerializedName("notify_url") 112 public String notifyUrl; 113 } 114 115} 116
需配合微信支付工具库 wxpay_utility 使用,请参考Go
1package main 2 3import ( 4 "demo/wxpay_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/v3/merchant/4015119334 5 "encoding/json" 6 "fmt" 7 "net/http" 8 "net/url" 9) 10 11func main() { 12 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/merchant/4013070756 13 config, err := wxpay_utility.CreateMchConfig( 14 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/merchant/4013070756 15 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013053053 16 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 17 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/merchant/4013038816 18 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 19 ) 20 if err != nil { 21 fmt.Println(err) 22 return 23 } 24 25 request := &GetCouponNotifyRequest{ 26 Mchid: wxpay_utility.String("98568888"), 27 } 28 29 response, err := GetCouponNotify(config, request) 30 if err != nil { 31 fmt.Printf("请求失败: %+v\n", err) 32 // TODO: 请求失败,根据状态码执行不同的处理 33 return 34 } 35 36 // TODO: 请求成功,继续业务逻辑 37 fmt.Printf("请求成功: %+v\n", response) 38} 39 40func GetCouponNotify(config *wxpay_utility.MchConfig, request *GetCouponNotifyRequest) (response *GetCouponNotifyResponse, err error) { 41 const ( 42 host = "https://api.mch.weixin.qq.com" 43 method = "GET" 44 path = "/v3/marketing/busifavor/callbacks" 45 ) 46 47 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 48 if err != nil { 49 return nil, err 50 } 51 query := reqUrl.Query() 52 if request.Mchid != nil { 53 query.Add("mchid", *request.Mchid) 54 } 55 reqUrl.RawQuery = query.Encode() 56 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil) 57 if err != nil { 58 return nil, err 59 } 60 httpRequest.Header.Set("Accept", "application/json") 61 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 62 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil) 63 if err != nil { 64 return nil, err 65 } 66 httpRequest.Header.Set("Authorization", authorization) 67 68 client := &http.Client{} 69 httpResponse, err := client.Do(httpRequest) 70 if err != nil { 71 return nil, err 72 } 73 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 74 if err != nil { 75 return nil, err 76 } 77 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 78 // 2XX 成功,验证应答签名 79 err = wxpay_utility.ValidateResponse( 80 config.WechatPayPublicKeyId(), 81 config.WechatPayPublicKey(), 82 &httpResponse.Header, 83 respBody, 84 ) 85 if err != nil { 86 return nil, err 87 } 88 response := &GetCouponNotifyResponse{} 89 if err := json.Unmarshal(respBody, response); err != nil { 90 return nil, err 91 } 92 93 return response, nil 94 } else { 95 return nil, wxpay_utility.NewApiException( 96 httpResponse.StatusCode, 97 httpResponse.Header, 98 respBody, 99 ) 100 } 101} 102 103type GetCouponNotifyRequest struct { 104 Mchid *string `json:"mchid,omitempty"` 105} 106 107func (o *GetCouponNotifyRequest) MarshalJSON() ([]byte, error) { 108 type Alias GetCouponNotifyRequest 109 a := &struct { 110 Mchid *string `json:"mchid,omitempty"` 111 *Alias 112 }{ 113 // 序列化时移除非 Body 字段 114 Mchid: nil, 115 Alias: (*Alias)(o), 116 } 117 return json.Marshal(a) 118} 119 120type GetCouponNotifyResponse struct { 121 Mchid *string `json:"mchid,omitempty"` 122 NotifyUrl *string `json:"notify_url,omitempty"` 123} 124
应答参数
200 OK
mchid 必填 string(15)
【商户号】商户号
notify_url 必填 string(256)
【通知URL地址】商户提供的用于接收商家券事件通知的URL地址,必须支持HTTPS。
应答示例
200 OK
1{ 2 "mchid" : "98568888", 3 "notify_url" : "https://pay.weixin.qq.com" 4} 5
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | APPID_MCHID_NOT_MATCH | AppID与请求方商户无关联关系 | AppID与请求方商户不匹配,请确认AppID与请求方商户是否有关联关系 |
400 | INVALID_REQUEST | 发券模式不合法 | 请更换支持预上传code的批次后重试 |
400 | INVALID_REQUEST | 上传的自定义code已达上限 | 请更换一个新的批次后重试 |
400 | MCH_NOT_EXISTS | 商户号不存在 | 请确认传入的商户号是否正确 |
400 | RESOURCE_ALREADY_EXISTS | 批次已存在 | 查看out_request_no字段是否重复使用 |
400 | RESOURCE_ALREADY_EXISTS | 券已被其他订单核销 | 请通过查询券API确认券是否已被其他订单核销 |
400 | SYSTEM_ERROR | 系统错误 | 请使用相同参数稍后重新调用 |
403 | NO_AUTH | 无权限 | 查看具体错误信息,确认是否有权限 |
403 | RULE_LIMIT | 券不在有效期内 | 请确认券是否能在当前时间核销 |
404 | RESOURCE_NOT_EXISTS | 查询的资源不存在 | 请检查查询资源的对应ID是否填写正确 |
404 | USER_NOT_EXISTS | OpenID不正确 | 请确认传入的OpenID是否正确 |
429 | FREQUENCY_LIMITED | 频率限制 | 调用太频繁,请降低调用接口频率 |
500 | SYSTEM_ERROR | 系统失败 | 多为网络超时引起,重试 |