批次取消关联门店
更新时间:2025.08.04品牌方可以通过该接口取消品牌的门店列表与商品券批次的关联关系
前置条件:已创建商品券批次且批次的 store_scope
为 SPECIFIC
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/disassociate-stores
请求域名:【主域名】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 路径参数
product_coupon_id 必填 string
【商品券ID】 商品券的唯一标识,创建商品券时由微信支付生成
stock_id 必填 string
【批次ID】 商品券批次的唯一标识,商品券批次创建时由微信支付生成(可使用创建商品券或添加商品券批次创建),请确保该批次属于 product_coupon_id
对应的商品券,且 store_scope
为 SPECIFIC
,否则无法取消关联门店
body 包体参数
store_list 必填 array[object]
【门店列表】 商户欲取消关联的门店列表,
单次最多可批量取消200个门店
属性 | |
store_id 必填 string 【门店ID】 门店的唯一标识,调用创建品牌门店时由微信支付生成 |
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/product-coupons/200000001/stocks/123456789/disassociate-stores \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "store_list" : [ 8 { 9 "store_id" : "100000001" 10 } 11 ], 12 "brand_id" : "120344" 13 }' 14
需配合微信支付工具库 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 DisassociateStores { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/disassociate-stores"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 DisassociateStores client = new DisassociateStores( 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 , 37 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 38 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 39 ); 40 41 DisassociateStoresRequest request = new DisassociateStoresRequest(); 42 request.productCouponId = "200000001"; 43 request.stockId = "123456789"; 44 request.storeList = new ArrayList<>(); 45 { 46 StoreInfo storeListItem = new StoreInfo(); 47 storeListItem.storeId = "100000001"; 48 request.storeList.add(storeListItem); 49 }; 50 request.brandId = "120344"; 51 try { 52 DisassociateStoresResponse response = client.run(request); 53 // TODO: 请求成功,继续业务逻辑 54 System.out.println(response); 55 } catch (WXPayUtility.ApiException e) { 56 // TODO: 请求失败,根据状态码执行不同的逻辑 57 e.printStackTrace(); 58 } 59 } 60 61 public DisassociateStoresResponse run(DisassociateStoresRequest request) { 62 String uri = PATH; 63 uri = uri.replace("{product_coupon_id}", WXPayUtility.urlEncode(request.productCouponId)); 64 uri = uri.replace("{stock_id}", WXPayUtility.urlEncode(request.stockId)); 65 String reqBody = WXPayUtility.toJson(request); 66 67 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 68 reqBuilder.addHeader("Accept", "application/json"); 69 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 70 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 71 reqBuilder.addHeader("Content-Type", "application/json"); 72 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 73 reqBuilder.method(METHOD, requestBody); 74 Request httpRequest = reqBuilder.build(); 75 76 // 发送HTTP请求 77 OkHttpClient client = new OkHttpClient.Builder().build(); 78 try (Response httpResponse = client.newCall(httpRequest).execute()) { 79 String respBody = WXPayUtility.extractBody(httpResponse); 80 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 81 // 2XX 成功,验证应答签名 82 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 83 httpResponse.headers(), respBody); 84 85 // 从HTTP应答报文构建返回数据 86 return WXPayUtility.fromJson(respBody, DisassociateStoresResponse.class); 87 } else { 88 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 89 } 90 } catch (IOException e) { 91 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 92 } 93 } 94 95 private final String mchid; 96 private final String certificateSerialNo; 97 private final PrivateKey privateKey; 98 private final String wechatPayPublicKeyId; 99 private final PublicKey wechatPayPublicKey; 100 101 public DisassociateStores(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 102 this.mchid = mchid; 103 this.certificateSerialNo = certificateSerialNo; 104 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 105 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 106 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 107 } 108 109 public static class DisassociateStoresRequest { 110 @SerializedName("product_coupon_id") 111 @Expose(serialize = false) 112 public String productCouponId; 113 114 @SerializedName("stock_id") 115 @Expose(serialize = false) 116 public String stockId; 117 118 @SerializedName("store_list") 119 public List<StoreInfo> storeList = new ArrayList<StoreInfo>(); 120 121 @SerializedName("brand_id") 122 public String brandId; 123 } 124 125 public static class DisassociateStoresResponse { 126 @SerializedName("total_count") 127 public Long totalCount; 128 129 @SerializedName("success_store_list") 130 public List<StoreInfo> successStoreList; 131 132 @SerializedName("failed_store_list") 133 public List<FailedStoreInfo> failedStoreList; 134 } 135 136 public static class StoreInfo { 137 @SerializedName("store_id") 138 public String storeId; 139 } 140 141 public static class FailedStoreInfo { 142 @SerializedName("store_id") 143 public String storeId; 144 145 @SerializedName("code") 146 public String code; 147 148 @SerializedName("message") 149 public String message; 150 } 151 152} 153
需配合微信支付工具库 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 := &DisassociateStoresRequest{ 28 ProductCouponId: wxpay_utility.String("200000001"), 29 StockId: wxpay_utility.String("123456789"), 30 StoreList: []StoreInfo{StoreInfo{ 31 StoreId: wxpay_utility.String("100000001"), 32 }}, 33 BrandId: wxpay_utility.String("120344"), 34 } 35 36 response, err := DisassociateStores(config, request) 37 if err != nil { 38 fmt.Printf("请求失败: %+v\n", err) 39 // TODO: 请求失败,根据状态码执行不同的处理 40 return 41 } 42 43 // TODO: 请求成功,继续业务逻辑 44 fmt.Printf("请求成功: %+v\n", response) 45} 46 47func DisassociateStores(config *wxpay_utility.MchConfig, request *DisassociateStoresRequest) (response *DisassociateStoresResponse, err error) { 48 const ( 49 host = "https://api.mch.weixin.qq.com" 50 method = "POST" 51 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/disassociate-stores" 52 ) 53 54 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 55 if err != nil { 56 return nil, err 57 } 58 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1) 59 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_id}", url.PathEscape(*request.StockId), -1) 60 reqBody, err := json.Marshal(request) 61 if err != nil { 62 return nil, err 63 } 64 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 65 if err != nil { 66 return nil, 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 nil, 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 nil, err 81 } 82 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 83 if err != nil { 84 return nil, 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 nil, err 96 } 97 response := &DisassociateStoresResponse{} 98 if err := json.Unmarshal(respBody, response); err != nil { 99 return nil, err 100 } 101 102 return response, nil 103 } else { 104 return nil, wxpay_utility.NewApiException( 105 httpResponse.StatusCode, 106 httpResponse.Header, 107 respBody, 108 ) 109 } 110} 111 112type DisassociateStoresRequest struct { 113 ProductCouponId *string `json:"product_coupon_id,omitempty"` 114 StockId *string `json:"stock_id,omitempty"` 115 StoreList []StoreInfo `json:"store_list,omitempty"` 116 BrandId *string `json:"brand_id,omitempty"` 117} 118 119func (o *DisassociateStoresRequest) MarshalJSON() ([]byte, error) { 120 type Alias DisassociateStoresRequest 121 a := &struct { 122 ProductCouponId *string `json:"product_coupon_id,omitempty"` 123 StockId *string `json:"stock_id,omitempty"` 124 *Alias 125 }{ 126 // 序列化时移除非 Body 字段 127 ProductCouponId: nil, 128 StockId: nil, 129 Alias: (*Alias)(o), 130 } 131 return json.Marshal(a) 132} 133 134type DisassociateStoresResponse struct { 135 TotalCount *int64 `json:"total_count,omitempty"` 136 SuccessStoreList []StoreInfo `json:"success_store_list,omitempty"` 137 FailedStoreList []FailedStoreInfo `json:"failed_store_list,omitempty"` 138} 139 140type StoreInfo struct { 141 StoreId *string `json:"store_id,omitempty"` 142} 143 144type FailedStoreInfo struct { 145 StoreId *string `json:"store_id,omitempty"` 146 Code *string `json:"code,omitempty"` 147 Message *string `json:"message,omitempty"` 148} 149
应答参数
200 OK
total_count 必填 integer
【去重后门店总数】 系统会自动对请求中的门店去重,这里是去重后的门店总数
success_store_list 选填 array[object]
【取消关联成功的门店列表】 取消关联成功的门店列表
属性 | |
store_id 必填 string 【门店ID】 门店的唯一标识,调用创建品牌门店时由微信支付生成 |
failed_store_list 选填 array[object]
【取消关联失败的门店列表】 每个取消关联失败门店的详细错误信息
属性 | |
store_id 必填 string 【门店ID】 关联/取消关联失败的门店ID code 必填 string(40) 【失败错误码】 门店关联/取消关联失败的错误码 message 必填 string(200) 【失败错误信息】 门店关联/取消关联失败的错误描述 |
应答示例
200 OK
1{ 2 "total_count" : 10, 3 "success_store_list" : [ 4 { 5 "store_id" : "100000001" 6 } 7 ], 8 "failed_store_list" : [ 9 { 10 "store_id" : "100000001", 11 "code" : "INVALID_REQUEST", 12 "message" : "门店ID非法或不属于当前品牌" 13 } 14 ] 15} 16
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | INVALID_REQUEST | 已过期的批次不可取消关联门店 | 请确认批次是否处于有效期 |
400 | INVALID_REQUEST | 只有 store_scope 为 SPECIFIC 的批次可以取消关联门店 | 请确认批次 store_scope 为 SPECIFIC 再调用本接口 |
403 | NO_AUTH | 缺少业务相关权限 | 请确认已开通商品券权限 |
404 | NOT_FOUND | 未找到 product_coupon_id 对应的商品券 | 请确认 product_coupon_id 存在且属于当前品牌 |
404 | NOT_FOUND | 未找到 stock_id 对应的商品券批次 | 请确认 stock_id 存在且属于当前商品券 |
429 | RATELIMIT_EXCEEDED | 请求超过接口频率限制 | 请稍后使用原参数重试 |