向用户预发放商品券批次组
更新时间:2025.12.17品牌方可以通过本接口向用户预发放指定商品券批次组,并使用返回的token在领券组件中完成发券,当用户已领取过时使用相同参数获取的token拉起的领券组件会返回已领取状态,能否发放受限于商品券组内批次的发放限额:
商品券批次总预算
商品券批次每日预算(如果有)
商品券批次每人限领(如果有)
前置条件: 已创建商品券批次组,批次组内商品券批次处于 SENDING 状态
接口说明
支持商户:【品牌商户】
请求方式:【POST】/brand/marketing/product-coupon/users/{openid}/pre-send-coupon-bundle
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
接口限频:500/秒(品牌ID维度)
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
Content-Type 必填 string
请设置为application/json
Wechatpay-Serial 必填 string
【微信支付公钥ID】 请传入brand_id对应的微信支付公钥ID,接口将会校验两者的关联关系,参考微信支付公钥产品简介及使用说明获取微信支付公钥ID和相关的介绍。以下两种场景将使用到微信支付公钥: 1、接收到接口的返回内容,需要使用微信支付公钥进行验签; 2、调用含有敏感信息参数(如姓名、身份证号码)的接口时,需要使用微信支付公钥加密敏感信息后再传输参数,加密指引请参考微信支付公钥加密敏感信息指引。
path 路径参数
openid 必填 string
【用户OpenID】 OpenID信息,用户在AppID下的唯一标识,获取方式参考OpenID
body 包体参数
product_coupon_id 必填 string
【商品券ID】 商品券的唯一标识,创建商品券时由微信支付生成
stock_bundle_id 必填 string
【批次组ID】 商品券批次组的唯一标识,【创建商品券(多次优惠)】或【添加商品券批次组】时由微信支付生成,请确保该批次组属于 product_coupon_id 对应的商品券
appid 必填 string
【公众账号ID】 公众账号ID也称AppID,是(微信开放平台、微信公众平台)为开发者提供的一个唯一标识,用于识别开发者的应用程序(APP、小程序、公众号)。 开发者需要先在微信开放平台或微信公众平台中申请ID,然后在品牌经营平台中绑定,详见品牌经营平台指引。
send_request_no 必填 string(40)
【发券请求单号】 品牌给用户发券的请求流水号,品牌侧需保持唯一性,可使用 数字、大小写字母、下划线_、短横线- 组成,长度在6-40个字符之间
attach 选填 string(150)
【自定义附加信息】 发券时品牌方可以使用该字段附带自定义附加信息,长度限制150个UTF-8字符。微信支付不会解析该信息,仅在查询用户商品券和回调中原样返回。
注: 发券渠道多样,只有品牌方通过此接口发放的券才会在查询和回调中携带此字段,其他渠道发放的券 attach 为空。
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/brand/marketing/product-coupon/users/oh-394z-6CGkNoJrsDLTTUKiAnp4/pre-send-coupon-bundle \ 3 -H "Authorization: WECHATPAY-BRAND-SHA256-RSA2048 brand_id=\"XXXX\",..." \ 4 -H "Accept: application/json" \ 5 -H "Wechatpay-Serial: PUB_KEY_ID_XXXX" \ 6 -H "Content-Type: application/json" \ 7 -d '{ 8 "product_coupon_id" : "200000001", 9 "stock_bundle_id" : "100232301", 10 "appid" : "wx233544546545989", 11 "send_request_no" : "34657_20250101_123456", 12 "attach" : "example_attach" 13 }' 14
需配合微信支付工具库 WXPayUtility 使用,请参考Java
1package com.java.demo; 2 3import com.java.utils.WXPayBrandUtility; // 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/brand/4015826861 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 PreSendUserProductCouponBundle { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/brand/marketing/product-coupon/users/{openid}/pre-send-coupon-bundle"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/brand/4015415289 32 PreSendUserProductCouponBundle client = new PreSendUserProductCouponBundle( 33 "xxxxxxxx", // 品牌ID,是由微信支付系统生成并分配给每个品牌方的唯一标识符,品牌ID获取方式参考 https://pay.weixin.qq.com/doc/brand/4015415289 34 "1DDE55AD98Exxxxxxxxxx", // 品牌API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/brand/4015407570 35 "/path/to/apiclient_key.pem", // 品牌API证书私钥文件路径,本地文件路径 36 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/brand/4015453439 37 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 38 ); 39 40 PreSendUserProductCouponBundleRequest request = new PreSendUserProductCouponBundleRequest(); 41 request.openid = "oh-394z-6CGkNoJrsDLTTUKiAnp4"; 42 request.productCouponId = "200000001"; 43 request.stockBundleId = "100232301"; 44 request.appid = "wx233544546545989"; 45 request.sendRequestNo = "34657_20250101_123456"; 46 request.attach = "example_attach"; 47 try { 48 PreSendUserProductCouponBundleResponse response = client.run(request); 49 // TODO: 请求成功,继续业务逻辑 50 System.out.println(response); 51 } catch (WXPayBrandUtility.ApiException e) { 52 // TODO: 请求失败,根据状态码执行不同的逻辑 53 e.printStackTrace(); 54 } 55 } 56 57 public PreSendUserProductCouponBundleResponse run(PreSendUserProductCouponBundleRequest request) { 58 String uri = PATH; 59 uri = uri.replace("{openid}", WXPayBrandUtility.urlEncode(request.openid)); 60 String reqBody = WXPayBrandUtility.toJson(request); 61 62 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 63 reqBuilder.addHeader("Accept", "application/json"); 64 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 65 reqBuilder.addHeader("Authorization", WXPayBrandUtility.buildAuthorization(brand_id, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 66 reqBuilder.addHeader("Content-Type", "application/json"); 67 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 68 reqBuilder.method(METHOD, requestBody); 69 Request httpRequest = reqBuilder.build(); 70 71 // 发送HTTP请求 72 OkHttpClient client = new OkHttpClient.Builder().build(); 73 try (Response httpResponse = client.newCall(httpRequest).execute()) { 74 String respBody = WXPayBrandUtility.extractBody(httpResponse); 75 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 76 // 2XX 成功,验证应答签名 77 WXPayBrandUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 78 httpResponse.headers(), respBody); 79 80 // 从HTTP应答报文构建返回数据 81 return WXPayBrandUtility.fromJson(respBody, PreSendUserProductCouponBundleResponse.class); 82 } else { 83 throw new WXPayBrandUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 84 } 85 } catch (IOException e) { 86 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 87 } 88 } 89 90 private final String brand_id; 91 private final String certificateSerialNo; 92 private final PrivateKey privateKey; 93 private final String wechatPayPublicKeyId; 94 private final PublicKey wechatPayPublicKey; 95 96 public PreSendUserProductCouponBundle(String brand_id, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 97 this.brand_id = brand_id; 98 this.certificateSerialNo = certificateSerialNo; 99 this.privateKey = WXPayBrandUtility.loadPrivateKeyFromPath(privateKeyFilePath); 100 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 101 this.wechatPayPublicKey = WXPayBrandUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 102 } 103 104 public static class PreSendUserProductCouponBundleRequest { 105 @SerializedName("product_coupon_id") 106 public String productCouponId; 107 108 @SerializedName("stock_bundle_id") 109 public String stockBundleId; 110 111 @SerializedName("appid") 112 public String appid; 113 114 @SerializedName("openid") 115 @Expose(serialize = false) 116 public String openid; 117 118 @SerializedName("send_request_no") 119 public String sendRequestNo; 120 121 @SerializedName("attach") 122 public String attach; 123 } 124 125 public static class PreSendUserProductCouponBundleResponse { 126 @SerializedName("token") 127 public String token; 128 } 129 130} 131
需配合微信支付工具库 wxpay_utility 使用,请参考Go
1package main 2 3import ( 4 "bytes" 5 "demo/wxpay_brand_utility" // 引用微信支付工具库,参考 https://pay.weixin.qq.com/doc/brand/4015826866 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/brand/4015415289 15 config, err := wxpay_brand_utility.CreateBrandConfig( 16 "xxxxxxxx", // 品牌ID,是由微信支付系统生成并分配给每个品牌方的唯一标识符,品牌ID获取方式参考 https://pay.weixin.qq.com/doc/brand/4015415289 17 "1DDE55AD98Exxxxxxxxxx", // 品牌API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/brand/4015407570 18 "/path/to/apiclient_key.pem", // 品牌API证书私钥文件路径,本地文件路径 19 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/brand/4015453439 20 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 21 ) 22 if err != nil { 23 fmt.Println(err) 24 return 25 } 26 27 request := &PreSendUserProductCouponBundleRequest{ 28 Openid: wxpay_brand_utility.String("oh-394z-6CGkNoJrsDLTTUKiAnp4"), 29 ProductCouponId: wxpay_brand_utility.String("200000001"), 30 StockBundleId: wxpay_brand_utility.String("100232301"), 31 Appid: wxpay_brand_utility.String("wx233544546545989"), 32 SendRequestNo: wxpay_brand_utility.String("34657_20250101_123456"), 33 Attach: wxpay_brand_utility.String("example_attach"), 34 } 35 36 response, err := PreSendUserProductCouponBundle(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 PreSendUserProductCouponBundle(config *wxpay_brand_utility.BrandConfig, request *PreSendUserProductCouponBundleRequest) (response *PreSendUserProductCouponBundleResponse, err error) { 48 const ( 49 host = "https://api.mch.weixin.qq.com" 50 method = "POST" 51 path = "/brand/marketing/product-coupon/users/{openid}/pre-send-coupon-bundle" 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, "{openid}", url.PathEscape(*request.Openid), -1) 59 reqBody, err := json.Marshal(request) 60 if err != nil { 61 return nil, err 62 } 63 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 64 if err != nil { 65 return nil, err 66 } 67 httpRequest.Header.Set("Accept", "application/json") 68 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 69 httpRequest.Header.Set("Content-Type", "application/json") 70 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 71 if err != nil { 72 return nil, err 73 } 74 httpRequest.Header.Set("Authorization", authorization) 75 76 client := &http.Client{} 77 httpResponse, err := client.Do(httpRequest) 78 if err != nil { 79 return nil, err 80 } 81 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse) 82 if err != nil { 83 return nil, err 84 } 85 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 86 // 2XX 成功,验证应答签名 87 err = wxpay_brand_utility.ValidateResponse( 88 config.WechatPayPublicKeyId(), 89 config.WechatPayPublicKey(), 90 &httpResponse.Header, 91 respBody, 92 ) 93 if err != nil { 94 return nil, err 95 } 96 response := &PreSendUserProductCouponBundleResponse{} 97 if err := json.Unmarshal(respBody, response); err != nil { 98 return nil, err 99 } 100 101 return response, nil 102 } else { 103 return nil, wxpay_brand_utility.NewApiException( 104 httpResponse.StatusCode, 105 httpResponse.Header, 106 respBody, 107 ) 108 } 109} 110 111type PreSendUserProductCouponBundleRequest struct { 112 ProductCouponId *string `json:"product_coupon_id,omitempty"` 113 StockBundleId *string `json:"stock_bundle_id,omitempty"` 114 Appid *string `json:"appid,omitempty"` 115 Openid *string `json:"openid,omitempty"` 116 SendRequestNo *string `json:"send_request_no,omitempty"` 117 Attach *string `json:"attach,omitempty"` 118} 119 120func (o *PreSendUserProductCouponBundleRequest) MarshalJSON() ([]byte, error) { 121 type Alias PreSendUserProductCouponBundleRequest 122 a := &struct { 123 Openid *string `json:"openid,omitempty"` 124 *Alias 125 }{ 126 // 序列化时移除非 Body 字段 127 Openid: nil, 128 Alias: (*Alias)(o), 129 } 130 return json.Marshal(a) 131} 132 133type PreSendUserProductCouponBundleResponse struct { 134 Token *string `json:"token,omitempty"` 135} 136
应答参数
200 OK
token 必填 string(1024)
【预发券token】 商品券生成的预发券token,用于领券组件,该值有效期为5分钟
应答示例
200 OK
1{ 2 "token" : "ABBpZjCfAAABAAAAAACmTrRvExc7XKea-gTVaBAAAADSn9xnWYv5l5lkN-GZBOXXZ30Cgo-RH7BfsyWbOKgfHH0JZ9R1PgIvZOif8lg8fwPDX8D1j4TcQs0v82F5F5edPhFwgN3sn2aGo2qJGWEWYBPW4MKtKoUC8CJ8ABsk8yH3k5ULDuFRijf9GIYBL3Cn785Hab28FtyRVfsiFFZMfzuXFy_S1x4wQfDQhxgOgM4V4ITiXtd_xvmsFY7zKUFI_RtayZlqkIEd" 3} 4
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示

