创建投放计划
更新时间:2025.09.27创建投放计划
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/delivery-plan/delivery-plans
请求域名:【主域名】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
body 包体参数
out_request_no 必填 string(40)
【创建请求单号】 创建投放计划的请求流水号,品牌侧需保持唯一性,可使用 数字、大小写字母、下划线_
、短横线-
组成,长度在6-40个字符之间
brand_id 必填 string
【创建投放计划的品牌ID】 创建投放计划的品牌ID,可登录品牌经营平台查看品牌 ID 信息
product_coupon_id 必填 string
【商品券ID】 传入投放的商品券ID
stock_id 必填 string
【批次ID】 传入投放的商品券批次ID
plan_name 选填 string(30)
【投放计划名称】 投放计划,该名称用于商家内部管理,不做 C端用户展示,30 个中文字符以内。
total_count 选填 integer
【总库存】 【投放总库存数量】 用于约定投放计划的总投放库存额度,创建投放计划的券可用库存需在 10000 以上;该字段可选填,若不填写,则表示投放计划层面不限制总投放库存。
user_limit 选填 integer
【单用户限领】 约定了投放计划用户维度的限领; 非必填,如未填写, 表示投放计划层不限制,且后续修改不支持修改限额。
daily_limit 选填 integer
【单日限领】 约定了投放计划单用户单日维度的限领;非必填,如未填写, 表示投放计划层不限制,且后续修改不支持修改限领限制。
delivery_start_time 选填 string
【开始可用时间】 【投放开始时间】 投放生效的开始时间,遵循[rfc3339](https://datatracker.ietf.org/doc/html/rfc3339)标准格式,格式为YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)。例如:2015-05-20T13:29:35+08:00表示,北京时间2015年5月20日 13点29分35秒。如未填写表示生效时间将以批次生效时间为准,且后续修改不支持设置开始时间。
delivery_end_time 选填 string
【结束可用时间】 【投放结束时间】 投放生效的结束时间,遵循[rfc3339](https://datatracker.ietf.org/doc/html/rfc3339)标准格式,格式为YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)。例如:2015-05-20T13:29:35+08:00表示,北京时间2015年5月20日 13点29分35秒。如未填写表示生效时间将以批次结束时间为准,且后续修改不支持设置结束时间。
recommend_word 选填 string(9)
【营销标签】 用于在优惠左上角展示的运营推荐语信息 自定义文案,不超过 9 个中文字符或18 个英文字符
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/delivery-plan/delivery-plans \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "out_request_no" : "asdf-1234-40016", 8 "brand_id" : "40016", 9 "product_coupon_id" : "701146876221757924829193824428", 10 "stock_id" : "701246876221757924829194755312", 11 "plan_name" : "冬季饮料投放", 12 "total_count" : 1, 13 "user_limit" : 1, 14 "daily_limit" : 1, 15 "delivery_start_time" : "2025-01-01T00:00:00+08:00", 16 "delivery_end_time" : "2025-01-01T00:00:00+08:00", 17 "recommend_word" : "天天有惊喜" 18 }' 19
需配合微信支付工具库 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 CreateDeliveryPlan { 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/delivery-plan/delivery-plans"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 CreateDeliveryPlan client = new CreateDeliveryPlan( 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 CreateDeliveryPlanRequest request = new CreateDeliveryPlanRequest(); 41 request.outRequestNo = "asdf-1234-40016"; 42 request.brandId = "40016"; 43 request.productCouponId = "701146876221757924829193824428"; 44 request.stockId = "701246876221757924829194755312"; 45 request.planName = "冬季饮料投放"; 46 request.totalCount = 1L; 47 request.userLimit = 1L; 48 request.dailyLimit = 1L; 49 request.deliveryStartTime = "2025-01-01T00:00:00+08:00"; 50 request.deliveryEndTime = "2025-01-01T00:00:00+08:00"; 51 request.recommendWord = "天天有惊喜"; 52 try { 53 CreateDeliveryPlanReponse response = client.run(request); 54 // TODO: 请求成功,继续业务逻辑 55 System.out.println(response); 56 } catch (WXPayUtility.ApiException e) { 57 // TODO: 请求失败,根据状态码执行不同的逻辑 58 e.printStackTrace(); 59 } 60 } 61 62 public CreateDeliveryPlanReponse run(CreateDeliveryPlanRequest request) { 63 String uri = PATH; 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 // 从HTTP应答报文构建返回数据 85 return WXPayUtility.fromJson(respBody, CreateDeliveryPlanReponse.class); 86 } else { 87 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 88 } 89 } catch (IOException e) { 90 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 91 } 92 } 93 94 private final String mchid; 95 private final String certificateSerialNo; 96 private final PrivateKey privateKey; 97 private final String wechatPayPublicKeyId; 98 private final PublicKey wechatPayPublicKey; 99 100 public CreateDeliveryPlan(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 101 this.mchid = mchid; 102 this.certificateSerialNo = certificateSerialNo; 103 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 104 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 105 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 106 } 107 108 public static class CreateDeliveryPlanRequest { 109 @SerializedName("out_request_no") 110 public String outRequestNo; 111 112 @SerializedName("brand_id") 113 public String brandId; 114 115 @SerializedName("product_coupon_id") 116 public String productCouponId; 117 118 @SerializedName("stock_id") 119 public String stockId; 120 121 @SerializedName("plan_name") 122 public String planName; 123 124 @SerializedName("total_count") 125 public Long totalCount; 126 127 @SerializedName("user_limit") 128 public Long userLimit; 129 130 @SerializedName("daily_limit") 131 public Long dailyLimit; 132 133 @SerializedName("delivery_start_time") 134 public String deliveryStartTime; 135 136 @SerializedName("delivery_end_time") 137 public String deliveryEndTime; 138 139 @SerializedName("recommend_word") 140 public String recommendWord; 141 } 142 143 public static class CreateDeliveryPlanReponse { 144 @SerializedName("plan") 145 public DeliveryPlan plan; 146 } 147 148 public static class DeliveryPlan { 149 @SerializedName("plan_id") 150 public String planId; 151 152 @SerializedName("plan_name") 153 public String planName; 154 155 @SerializedName("plan_state") 156 public PlanState planState; 157 158 @SerializedName("delivery_start_time") 159 public String deliveryStartTime; 160 161 @SerializedName("delivery_end_time") 162 public String deliveryEndTime; 163 164 @SerializedName("stock_id") 165 public String stockId; 166 167 @SerializedName("product_coupon_id") 168 public String productCouponId; 169 170 @SerializedName("recommend_word") 171 public String recommendWord; 172 173 @SerializedName("brand_id") 174 public Long brandId; 175 176 @SerializedName("total_count") 177 public Long totalCount; 178 179 @SerializedName("user_limit") 180 public Long userLimit; 181 182 @SerializedName("daily_limit") 183 public Long dailyLimit; 184 } 185 186 public enum PlanState { 187 @SerializedName("PLAN_CREATING") 188 PLAN_CREATING, 189 @SerializedName("PLAN_CREATED") 190 PLAN_CREATED, 191 @SerializedName("PLAN_TERMINATING") 192 PLAN_TERMINATING, 193 @SerializedName("PLAN_TERMINATED") 194 PLAN_TERMINATED, 195 @SerializedName("PLAN_EXPIRED") 196 PLAN_EXPIRED, 197 @SerializedName("PLAN_DELIVERING") 198 PLAN_DELIVERING, 199 @SerializedName("PLAN_PAUSED") 200 PLAN_PAUSED 201 } 202 203} 204
需配合微信支付工具库 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) 11 12func main() { 13 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 14 config, err := wxpay_utility.CreateMchConfig( 15 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 17 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 18 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 19 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 20 ) 21 if err != nil { 22 fmt.Println(err) 23 return 24 } 25 26 request := &CreateDeliveryPlanRequest{ 27 OutRequestNo: wxpay_utility.String("asdf-1234-40016"), 28 BrandId: wxpay_utility.String("40016"), 29 ProductCouponId: wxpay_utility.String("701146876221757924829193824428"), 30 StockId: wxpay_utility.String("701246876221757924829194755312"), 31 PlanName: wxpay_utility.String("冬季饮料投放"), 32 TotalCount: wxpay_utility.Int64(1), 33 UserLimit: wxpay_utility.Int64(1), 34 DailyLimit: wxpay_utility.Int64(1), 35 DeliveryStartTime: wxpay_utility.String("2025-01-01T00:00:00+08:00"), 36 DeliveryEndTime: wxpay_utility.String("2025-01-01T00:00:00+08:00"), 37 RecommendWord: wxpay_utility.String("天天有惊喜"), 38 } 39 40 response, err := CreateDeliveryPlan(config, request) 41 if err != nil { 42 fmt.Printf("请求失败: %+v\n", err) 43 // TODO: 请求失败,根据状态码执行不同的处理 44 return 45 } 46 47 // TODO: 请求成功,继续业务逻辑 48 fmt.Printf("请求成功: %+v\n", response) 49} 50 51func CreateDeliveryPlan(config *wxpay_utility.MchConfig, request *CreateDeliveryPlanRequest) (response *CreateDeliveryPlanReponse, err error) { 52 const ( 53 host = "https://api.mch.weixin.qq.com" 54 method = "POST" 55 path = "/v3/marketing/partner/delivery-plan/delivery-plans" 56 ) 57 58 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 59 if err != nil { 60 return nil, err 61 } 62 reqBody, err := json.Marshal(request) 63 if err != nil { 64 return nil, err 65 } 66 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 67 if err != nil { 68 return nil, err 69 } 70 httpRequest.Header.Set("Accept", "application/json") 71 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 72 httpRequest.Header.Set("Content-Type", "application/json") 73 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 74 if err != nil { 75 return nil, err 76 } 77 httpRequest.Header.Set("Authorization", authorization) 78 79 client := &http.Client{} 80 httpResponse, err := client.Do(httpRequest) 81 if err != nil { 82 return nil, err 83 } 84 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 85 if err != nil { 86 return nil, err 87 } 88 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 89 // 2XX 成功,验证应答签名 90 err = wxpay_utility.ValidateResponse( 91 config.WechatPayPublicKeyId(), 92 config.WechatPayPublicKey(), 93 &httpResponse.Header, 94 respBody, 95 ) 96 if err != nil { 97 return nil, err 98 } 99 response := &CreateDeliveryPlanReponse{} 100 if err := json.Unmarshal(respBody, response); err != nil { 101 return nil, err 102 } 103 104 return response, nil 105 } else { 106 return nil, wxpay_utility.NewApiException( 107 httpResponse.StatusCode, 108 httpResponse.Header, 109 respBody, 110 ) 111 } 112} 113 114type CreateDeliveryPlanRequest struct { 115 OutRequestNo *string `json:"out_request_no,omitempty"` 116 BrandId *string `json:"brand_id,omitempty"` 117 ProductCouponId *string `json:"product_coupon_id,omitempty"` 118 StockId *string `json:"stock_id,omitempty"` 119 PlanName *string `json:"plan_name,omitempty"` 120 TotalCount *int64 `json:"total_count,omitempty"` 121 UserLimit *int64 `json:"user_limit,omitempty"` 122 DailyLimit *int64 `json:"daily_limit,omitempty"` 123 DeliveryStartTime *string `json:"delivery_start_time,omitempty"` 124 DeliveryEndTime *string `json:"delivery_end_time,omitempty"` 125 RecommendWord *string `json:"recommend_word,omitempty"` 126} 127 128type CreateDeliveryPlanReponse struct { 129 Plan *DeliveryPlan `json:"plan,omitempty"` 130} 131 132type DeliveryPlan struct { 133 PlanId *string `json:"plan_id,omitempty"` 134 PlanName *string `json:"plan_name,omitempty"` 135 PlanState *PlanState `json:"plan_state,omitempty"` 136 DeliveryStartTime *string `json:"delivery_start_time,omitempty"` 137 DeliveryEndTime *string `json:"delivery_end_time,omitempty"` 138 StockId *string `json:"stock_id,omitempty"` 139 ProductCouponId *string `json:"product_coupon_id,omitempty"` 140 RecommendWord *string `json:"recommend_word,omitempty"` 141 BrandId *int64 `json:"brand_id,omitempty"` 142 TotalCount *int64 `json:"total_count,omitempty"` 143 UserLimit *int64 `json:"user_limit,omitempty"` 144 DailyLimit *int64 `json:"daily_limit,omitempty"` 145} 146 147type PlanState string 148 149func (e PlanState) Ptr() *PlanState { 150 return &e 151} 152 153const ( 154 PLANSTATE_PLAN_CREATING PlanState = "PLAN_CREATING" 155 PLANSTATE_PLAN_CREATED PlanState = "PLAN_CREATED" 156 PLANSTATE_PLAN_TERMINATING PlanState = "PLAN_TERMINATING" 157 PLANSTATE_PLAN_TERMINATED PlanState = "PLAN_TERMINATED" 158 PLANSTATE_PLAN_EXPIRED PlanState = "PLAN_EXPIRED" 159 PLANSTATE_PLAN_DELIVERING PlanState = "PLAN_DELIVERING" 160 PLANSTATE_PLAN_PAUSED PlanState = "PLAN_PAUSED" 161) 162
应答参数
200 OK
plan 必填 object
【投放计划详情】 创建成功的投放计划详情
属性 | |
plan_id 必填 string 【投放计划ID】 投放计划ID plan_name 必填 string 【投放计划名称】 投放计划名称 plan_state 必填 string 【投放计划状态】 投放计划状态 可选取值
delivery_start_time 选填 string 【投放开始时间】 【投放开始时间】 投放开始时间,遵循[rfc3339](https://datatracker.ietf.org/doc/html/rfc3339)标准格式,格式为YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)。例如:2015-05-20T13:29:35+08:00表示,北京时间2015年5月20日 13点29分35秒。 delivery_end_time 选填 string 【结束可用时间】 【投放结束时间】 投放结束时间,遵循[rfc3339](https://datatracker.ietf.org/doc/html/rfc3339)标准格式,格式为YYYY-MM-DDTHH:mm:ss+TIMEZONE,YYYY-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)。例如:2015-05-20T13:29:35+08:00表示,北京时间2015年5月20日 13点29分35秒。 stock_id 必填 string(40) 【券批次ID】 券批次ID product_coupon_id 必填 string(40) 【商品券ID】 商品券ID recommend_word 选填 string(9) 【营销标签】 用于在优惠左上角展示的运营推荐语信息 自定义文案,不超过 9 个中文字符或18 个英文字符 brand_id 必填 integer 【品牌ID】 创建投放计划的品牌ID,可登录品牌经营平台查看品牌 ID 信息 total_count 选填 integer 【投放总数量,库存】 约定了投放计划的总投放库存,创建投放计划的券可用库存需在 10000 以上;非必填,如未填写, 表示投放计划层不限制,投放将以批次库存为准,且后续修改不支持修改投放库存。 user_limit 选填 integer 【单用户领取上限】 单用户领取上限 daily_limit 选填 integer 【投放计划单日领取上限】 用于约定投放计划单日可领取的最大数量,如创建时未填写,则修改时不支持填写。 |
应答示例
200 OK
1{ 2 "plan" : { 3 "plan_id" : "12000", 4 "plan_name" : "冬季优惠投放", 5 "plan_state" : "PLAN_CREATED", 6 "delivery_start_time" : "2025-01-01T00:00:00+08:00", 7 "delivery_end_time" : "2025-01-01T00:00:00+08:00", 8 "stock_id" : "123456789", 9 "product_coupon_id" : "1000000013", 10 "recommend_word" : "天天有惊喜", 11 "brand_id" : 40016, 12 "total_count" : 11000, 13 "user_limit" : 5, 14 "daily_limit" : 100 15 } 16} 17
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示