添加商品券批次
更新时间:2025.08.04服务商可以通过该接口为已有的商品券添加更多批次,多个批次可以实现品牌方多样化的投放需求。
注:同一个商品券下,最多只能有200个生效中的批次,已过期和已失效的批次不计入统计。
前置条件:商品券处于 EFFECTIVE 状态
频率限制:20/s
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks
请求域名:【主域名】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】 商品券的唯一标识,创建商品券时由微信支付生成
body 包体参数
out_request_no 必填 string(40)
【创建请求单号】 品牌创建批次请求流水号,品牌侧需保持唯一性,可使用 数字、大小写字母、下划线_、短横线- 组成,长度在6-40个字符之间
stock 必填 object
【批次】 为商品券创建的批次详情
| 属性 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
remark 选填 string(20) 【备注】 仅配置品牌方可见,用于自定义信息,最多20个UTF-8字符 coupon_code_mode 必填 string 【券Code分配模式】 决定发券时用户商品券Code如何产生 可选取值
stock_send_rule 必填 object 【发放规则】 批次的发放规则
single_usage_rule 选填 object 【单券使用规则】 当且仅当
usage_rule_display_info 必填 object 【券使用规则展示信息】 券使用规则展示信息
coupon_display_info 必填 object 【用户商品券展示信息】 用户商品券在卡包中的展示详情,包括引导用户的自定义入口
notify_config 必填 object 【事件通知配置】 发生券相关事件时,微信支付会向服务商发送通知,需要提供通知相关配置
store_scope 必填 string 【可用门店范围】 控制该批次可以在品牌下哪些门店使用 可选取值
|
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
请求示例
POST
为满100可用的全场8折券添加批次
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/product-coupons/1000000013/stocks \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "stock" : { 8 "remark" : "8月工作日有效批次", 9 "coupon_code_mode" : "UPLOAD", 10 "stock_send_rule" : { 11 "max_count" : 10000000, 12 "max_count_per_user" : 1 13 }, 14 "single_usage_rule" : { 15 "coupon_available_period" : { 16 "available_begin_time" : "2025-08-01T00:00:00+08:00", 17 "available_end_time" : "2025-08-31T23:59:59+08:00", 18 "available_days" : 30, 19 "weekly_available_period" : { 20 "day_list" : [ 21 "MONDAY", 22 "TUESDAY", 23 "WEDNESDAY", 24 "THURSDAY", 25 "FRIDAY" 26 ] 27 } 28 } 29 }, 30 "usage_rule_display_info" : { 31 "coupon_usage_method_list" : [ 32 "OFFLINE", 33 "MINI_PROGRAM", 34 "PAYMENT_CODE" 35 ], 36 "mini_program_appid" : "wx1234567890", 37 "mini_program_path" : "/pages/index/product", 38 "usage_description" : "工作日可用", 39 "coupon_available_store_info" : { 40 "description" : "所有门店可用,可使用小程序查看门店列表", 41 "mini_program_appid" : "wx1234567890", 42 "mini_program_path" : "/pages/index/store-list" 43 } 44 }, 45 "coupon_display_info" : { 46 "code_display_mode" : "QRCODE", 47 "background_color" : "Color010", 48 "entrance_mini_program" : { 49 "appid" : "wx1234567890", 50 "path" : "/pages/index/product", 51 "entrance_wording" : "欢迎选购", 52 "guidance_wording" : "获取更多优惠" 53 }, 54 "entrance_official_account" : { 55 "appid" : "wx1234567890" 56 }, 57 "entrance_finder" : { 58 "finder_id" : "gh_12345678", 59 "finder_video_id" : "UDFsdf24df34dD456Hdf34", 60 "finder_video_cover_image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx" 61 } 62 }, 63 "store_scope" : "NONE", 64 "notify_config" : { 65 "notify_appid" : "wx4fd12345678" 66 } 67 }, 68 "out_request_no" : "12345_20250101_A3489", 69 "brand_id" : "120344" 70 }' 71
需配合微信支付工具库 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 CreateStock { 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"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 CreateStock client = new CreateStock( 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 CreateStockRequest request = new CreateStockRequest(); 41 request.outRequestNo = "12345_20250101_A3489"; 42 request.productCouponId = "1000000013"; 43 request.stock = new StockForCreate(); 44 request.stock.remark = "8月工作日有效批次"; 45 request.stock.couponCodeMode = CouponCodeMode.UPLOAD; 46 request.stock.stockSendRule = new StockSendRule(); 47 request.stock.stockSendRule.maxCount = 10000000L; 48 request.stock.stockSendRule.maxCountPerUser = 1L; 49 request.stock.singleUsageRule = new SingleUsageRule(); 50 request.stock.singleUsageRule.couponAvailablePeriod = new CouponAvailablePeriod(); 51 request.stock.singleUsageRule.couponAvailablePeriod.availableBeginTime = "2025-08-01T00:00:00+08:00"; 52 request.stock.singleUsageRule.couponAvailablePeriod.availableEndTime = "2025-08-31T23:59:59+08:00"; 53 request.stock.singleUsageRule.couponAvailablePeriod.availableDays = 30L; 54 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod = new FixedWeekPeriod(); 55 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod.dayList = new ArrayList<>(); 56 { 57 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod.dayList.add(WeekEnum.MONDAY); 58 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod.dayList.add(WeekEnum.TUESDAY); 59 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod.dayList.add(WeekEnum.WEDNESDAY); 60 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod.dayList.add(WeekEnum.THURSDAY); 61 request.stock.singleUsageRule.couponAvailablePeriod.weeklyAvailablePeriod.dayList.add(WeekEnum.FRIDAY); 62 }; 63 request.stock.usageRuleDisplayInfo = new UsageRuleDisplayInfo(); 64 request.stock.usageRuleDisplayInfo.couponUsageMethodList = new ArrayList<>(); 65 { 66 request.stock.usageRuleDisplayInfo.couponUsageMethodList.add(CouponUsageMethod.OFFLINE); 67 request.stock.usageRuleDisplayInfo.couponUsageMethodList.add(CouponUsageMethod.MINI_PROGRAM); 68 request.stock.usageRuleDisplayInfo.couponUsageMethodList.add(CouponUsageMethod.PAYMENT_CODE); 69 }; 70 request.stock.usageRuleDisplayInfo.miniProgramAppid = "wx1234567890"; 71 request.stock.usageRuleDisplayInfo.miniProgramPath = "/pages/index/product"; 72 request.stock.usageRuleDisplayInfo.usageDescription = "工作日可用"; 73 request.stock.usageRuleDisplayInfo.couponAvailableStoreInfo = new CouponAvailableStoreInfo(); 74 request.stock.usageRuleDisplayInfo.couponAvailableStoreInfo.description = "所有门店可用,可使用小程序查看门店列表"; 75 request.stock.usageRuleDisplayInfo.couponAvailableStoreInfo.miniProgramAppid = "wx1234567890"; 76 request.stock.usageRuleDisplayInfo.couponAvailableStoreInfo.miniProgramPath = "/pages/index/store-list"; 77 request.stock.couponDisplayInfo = new CouponDisplayInfo(); 78 request.stock.couponDisplayInfo.codeDisplayMode = CouponCodeDisplayMode.QRCODE; 79 request.stock.couponDisplayInfo.backgroundColor = "Color010"; 80 request.stock.couponDisplayInfo.entranceMiniProgram = new EntranceMiniProgram(); 81 request.stock.couponDisplayInfo.entranceMiniProgram.appid = "wx1234567890"; 82 request.stock.couponDisplayInfo.entranceMiniProgram.path = "/pages/index/product"; 83 request.stock.couponDisplayInfo.entranceMiniProgram.entranceWording = "欢迎选购"; 84 request.stock.couponDisplayInfo.entranceMiniProgram.guidanceWording = "获取更多优惠"; 85 request.stock.couponDisplayInfo.entranceOfficialAccount = new EntranceOfficialAccount(); 86 request.stock.couponDisplayInfo.entranceOfficialAccount.appid = "wx1234567890"; 87 request.stock.couponDisplayInfo.entranceFinder = new EntranceFinder(); 88 request.stock.couponDisplayInfo.entranceFinder.finderId = "gh_12345678"; 89 request.stock.couponDisplayInfo.entranceFinder.finderVideoId = "UDFsdf24df34dD456Hdf34"; 90 request.stock.couponDisplayInfo.entranceFinder.finderVideoCoverImageUrl = "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx"; 91 request.stock.notifyConfig = new NotifyConfig(); 92 request.stock.notifyConfig.notifyAppid = "wx4fd12345678"; 93 request.stock.storeScope = StockStoreScope.NONE; 94 request.brandId = "120344"; 95 try { 96 StockEntity response = client.run(request); 97 // TODO: 请求成功,继续业务逻辑 98 System.out.println(response); 99 } catch (WXPayUtility.ApiException e) { 100 // TODO: 请求失败,根据状态码执行不同的逻辑 101 e.printStackTrace(); 102 } 103 } 104 105 public StockEntity run(CreateStockRequest request) { 106 String uri = PATH; 107 uri = uri.replace("{product_coupon_id}", WXPayUtility.urlEncode(request.productCouponId)); 108 String reqBody = WXPayUtility.toJson(request); 109 110 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 111 reqBuilder.addHeader("Accept", "application/json"); 112 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 113 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 114 reqBuilder.addHeader("Content-Type", "application/json"); 115 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 116 reqBuilder.method(METHOD, requestBody); 117 Request httpRequest = reqBuilder.build(); 118 119 // 发送HTTP请求 120 OkHttpClient client = new OkHttpClient.Builder().build(); 121 try (Response httpResponse = client.newCall(httpRequest).execute()) { 122 String respBody = WXPayUtility.extractBody(httpResponse); 123 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 124 // 2XX 成功,验证应答签名 125 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 126 httpResponse.headers(), respBody); 127 128 // 从HTTP应答报文构建返回数据 129 return WXPayUtility.fromJson(respBody, StockEntity.class); 130 } else { 131 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 132 } 133 } catch (IOException e) { 134 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 135 } 136 } 137 138 private final String mchid; 139 private final String certificateSerialNo; 140 private final PrivateKey privateKey; 141 private final String wechatPayPublicKeyId; 142 private final PublicKey wechatPayPublicKey; 143 144 public CreateStock(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 145 this.mchid = mchid; 146 this.certificateSerialNo = certificateSerialNo; 147 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 148 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 149 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 150 } 151 152 public static class CreateStockRequest { 153 @SerializedName("out_request_no") 154 public String outRequestNo; 155 156 @SerializedName("product_coupon_id") 157 @Expose(serialize = false) 158 public String productCouponId; 159 160 @SerializedName("stock") 161 public StockForCreate stock; 162 163 @SerializedName("brand_id") 164 public String brandId; 165 } 166 167 public static class StockEntity { 168 @SerializedName("product_coupon_id") 169 public String productCouponId; 170 171 @SerializedName("stock_id") 172 public String stockId; 173 174 @SerializedName("remark") 175 public String remark; 176 177 @SerializedName("coupon_code_mode") 178 public CouponCodeMode couponCodeMode; 179 180 @SerializedName("coupon_code_count_info") 181 public CouponCodeCountInfo couponCodeCountInfo; 182 183 @SerializedName("stock_send_rule") 184 public StockSendRule stockSendRule; 185 186 @SerializedName("single_usage_rule") 187 public SingleUsageRule singleUsageRule; 188 189 @SerializedName("usage_rule_display_info") 190 public UsageRuleDisplayInfo usageRuleDisplayInfo; 191 192 @SerializedName("coupon_display_info") 193 public CouponDisplayInfo couponDisplayInfo; 194 195 @SerializedName("notify_config") 196 public NotifyConfig notifyConfig; 197 198 @SerializedName("store_scope") 199 public StockStoreScope storeScope; 200 201 @SerializedName("sent_count_info") 202 public StockSentCountInfo sentCountInfo; 203 204 @SerializedName("state") 205 public StockState state; 206 207 @SerializedName("deactivate_request_no") 208 public String deactivateRequestNo; 209 210 @SerializedName("deactivate_time") 211 public String deactivateTime; 212 213 @SerializedName("deactivate_reason") 214 public String deactivateReason; 215 216 @SerializedName("brand_id") 217 public String brandId; 218 } 219 220 public static class StockForCreate { 221 @SerializedName("remark") 222 public String remark; 223 224 @SerializedName("coupon_code_mode") 225 public CouponCodeMode couponCodeMode; 226 227 @SerializedName("stock_send_rule") 228 public StockSendRule stockSendRule; 229 230 @SerializedName("single_usage_rule") 231 public SingleUsageRule singleUsageRule; 232 233 @SerializedName("usage_rule_display_info") 234 public UsageRuleDisplayInfo usageRuleDisplayInfo; 235 236 @SerializedName("coupon_display_info") 237 public CouponDisplayInfo couponDisplayInfo; 238 239 @SerializedName("notify_config") 240 public NotifyConfig notifyConfig; 241 242 @SerializedName("store_scope") 243 public StockStoreScope storeScope; 244 } 245 246 public enum CouponCodeMode { 247 @SerializedName("WECHATPAY") 248 WECHATPAY, 249 @SerializedName("UPLOAD") 250 UPLOAD, 251 @SerializedName("API_ASSIGN") 252 API_ASSIGN 253 } 254 255 public static class CouponCodeCountInfo { 256 @SerializedName("total_count") 257 public Long totalCount; 258 259 @SerializedName("available_count") 260 public Long availableCount; 261 } 262 263 public static class StockSendRule { 264 @SerializedName("max_count") 265 public Long maxCount; 266 267 @SerializedName("max_count_per_day") 268 public Long maxCountPerDay; 269 270 @SerializedName("max_count_per_user") 271 public Long maxCountPerUser; 272 } 273 274 public static class SingleUsageRule { 275 @SerializedName("coupon_available_period") 276 public CouponAvailablePeriod couponAvailablePeriod; 277 278 @SerializedName("normal_coupon") 279 public NormalCouponUsageRule normalCoupon; 280 281 @SerializedName("discount_coupon") 282 public DiscountCouponUsageRule discountCoupon; 283 284 @SerializedName("exchange_coupon") 285 public ExchangeCouponUsageRule exchangeCoupon; 286 } 287 288 public static class UsageRuleDisplayInfo { 289 @SerializedName("coupon_usage_method_list") 290 public List<CouponUsageMethod> couponUsageMethodList = new ArrayList<CouponUsageMethod>(); 291 292 @SerializedName("mini_program_appid") 293 public String miniProgramAppid; 294 295 @SerializedName("mini_program_path") 296 public String miniProgramPath; 297 298 @SerializedName("app_path") 299 public String appPath; 300 301 @SerializedName("usage_description") 302 public String usageDescription; 303 304 @SerializedName("coupon_available_store_info") 305 public CouponAvailableStoreInfo couponAvailableStoreInfo; 306 } 307 308 public static class CouponDisplayInfo { 309 @SerializedName("code_display_mode") 310 public CouponCodeDisplayMode codeDisplayMode; 311 312 @SerializedName("background_color") 313 public String backgroundColor; 314 315 @SerializedName("entrance_mini_program") 316 public EntranceMiniProgram entranceMiniProgram; 317 318 @SerializedName("entrance_official_account") 319 public EntranceOfficialAccount entranceOfficialAccount; 320 321 @SerializedName("entrance_finder") 322 public EntranceFinder entranceFinder; 323 } 324 325 public static class NotifyConfig { 326 @SerializedName("notify_appid") 327 public String notifyAppid; 328 } 329 330 public enum StockStoreScope { 331 @SerializedName("NONE") 332 NONE, 333 @SerializedName("ALL") 334 ALL, 335 @SerializedName("SPECIFIC") 336 SPECIFIC 337 } 338 339 public static class StockSentCountInfo { 340 @SerializedName("total_count") 341 public Long totalCount; 342 343 @SerializedName("today_count") 344 public Long todayCount; 345 } 346 347 public enum StockState { 348 @SerializedName("AUDITING") 349 AUDITING, 350 @SerializedName("SENDING") 351 SENDING, 352 @SerializedName("PAUSED") 353 PAUSED, 354 @SerializedName("STOPPED") 355 STOPPED, 356 @SerializedName("DEACTIVATED") 357 DEACTIVATED 358 } 359 360 public static class CouponAvailablePeriod { 361 @SerializedName("available_begin_time") 362 public String availableBeginTime; 363 364 @SerializedName("available_end_time") 365 public String availableEndTime; 366 367 @SerializedName("available_days") 368 public Long availableDays; 369 370 @SerializedName("wait_days_after_receive") 371 public Long waitDaysAfterReceive; 372 373 @SerializedName("weekly_available_period") 374 public FixedWeekPeriod weeklyAvailablePeriod; 375 376 @SerializedName("irregular_available_period_list") 377 public List<TimePeriod> irregularAvailablePeriodList; 378 } 379 380 public static class NormalCouponUsageRule { 381 @SerializedName("threshold") 382 public Long threshold; 383 384 @SerializedName("discount_amount") 385 public Long discountAmount; 386 } 387 388 public static class DiscountCouponUsageRule { 389 @SerializedName("threshold") 390 public Long threshold; 391 392 @SerializedName("percent_off") 393 public Long percentOff; 394 } 395 396 public static class ExchangeCouponUsageRule { 397 @SerializedName("threshold") 398 public Long threshold; 399 400 @SerializedName("exchange_price") 401 public Long exchangePrice; 402 } 403 404 public enum CouponUsageMethod { 405 @SerializedName("OFFLINE") 406 OFFLINE, 407 @SerializedName("MINI_PROGRAM") 408 MINI_PROGRAM, 409 @SerializedName("APP") 410 APP, 411 @SerializedName("PAYMENT_CODE") 412 PAYMENT_CODE 413 } 414 415 public static class CouponAvailableStoreInfo { 416 @SerializedName("description") 417 public String description; 418 419 @SerializedName("mini_program_appid") 420 public String miniProgramAppid; 421 422 @SerializedName("mini_program_path") 423 public String miniProgramPath; 424 } 425 426 public enum CouponCodeDisplayMode { 427 @SerializedName("INVISIBLE") 428 INVISIBLE, 429 @SerializedName("BARCODE") 430 BARCODE, 431 @SerializedName("QRCODE") 432 QRCODE 433 } 434 435 public static class EntranceMiniProgram { 436 @SerializedName("appid") 437 public String appid; 438 439 @SerializedName("path") 440 public String path; 441 442 @SerializedName("entrance_wording") 443 public String entranceWording; 444 445 @SerializedName("guidance_wording") 446 public String guidanceWording; 447 } 448 449 public static class EntranceOfficialAccount { 450 @SerializedName("appid") 451 public String appid; 452 } 453 454 public static class EntranceFinder { 455 @SerializedName("finder_id") 456 public String finderId; 457 458 @SerializedName("finder_video_id") 459 public String finderVideoId; 460 461 @SerializedName("finder_video_cover_image_url") 462 public String finderVideoCoverImageUrl; 463 } 464 465 public static class FixedWeekPeriod { 466 @SerializedName("day_list") 467 public List<WeekEnum> dayList; 468 469 @SerializedName("day_period_list") 470 public List<PeriodOfTheDay> dayPeriodList; 471 } 472 473 public static class TimePeriod { 474 @SerializedName("begin_time") 475 public String beginTime; 476 477 @SerializedName("end_time") 478 public String endTime; 479 } 480 481 public enum WeekEnum { 482 @SerializedName("MONDAY") 483 MONDAY, 484 @SerializedName("TUESDAY") 485 TUESDAY, 486 @SerializedName("WEDNESDAY") 487 WEDNESDAY, 488 @SerializedName("THURSDAY") 489 THURSDAY, 490 @SerializedName("FRIDAY") 491 FRIDAY, 492 @SerializedName("SATURDAY") 493 SATURDAY, 494 @SerializedName("SUNDAY") 495 SUNDAY 496 } 497 498 public static class PeriodOfTheDay { 499 @SerializedName("begin_time") 500 public Long beginTime; 501 502 @SerializedName("end_time") 503 public Long endTime; 504 } 505 506} 507
需配合微信支付工具库 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 "time" 12) 13 14func main() { 15 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 config, err := wxpay_utility.CreateMchConfig( 17 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 18 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 19 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 20 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 21 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 22 ) 23 if err != nil { 24 fmt.Println(err) 25 return 26 } 27 28 request := &CreateStockRequest{ 29 OutRequestNo: wxpay_utility.String("12345_20250101_A3489"), 30 ProductCouponId: wxpay_utility.String("1000000013"), 31 Stock: &StockForCreate{ 32 Remark: wxpay_utility.String("8月工作日有效批次"), 33 CouponCodeMode: COUPONCODEMODE_UPLOAD.Ptr(), 34 StockSendRule: &StockSendRule{ 35 MaxCount: wxpay_utility.Int64(10000000), 36 MaxCountPerUser: wxpay_utility.Int64(1), 37 }, 38 SingleUsageRule: &SingleUsageRule{ 39 CouponAvailablePeriod: &CouponAvailablePeriod{ 40 AvailableBeginTime: wxpay_utility.String("2025-08-01T00:00:00+08:00"), 41 AvailableEndTime: wxpay_utility.String("2025-08-31T23:59:59+08:00"), 42 AvailableDays: wxpay_utility.Int64(30), 43 WeeklyAvailablePeriod: &FixedWeekPeriod{ 44 DayList: []WeekEnum{ 45 WEEKENUM_MONDAY, 46 WEEKENUM_TUESDAY, 47 WEEKENUM_WEDNESDAY, 48 WEEKENUM_THURSDAY, 49 WEEKENUM_FRIDAY, 50 }, 51 }, 52 }, 53 }, 54 UsageRuleDisplayInfo: &UsageRuleDisplayInfo{ 55 CouponUsageMethodList: []CouponUsageMethod{ 56 COUPONUSAGEMETHOD_OFFLINE, 57 COUPONUSAGEMETHOD_MINI_PROGRAM, 58 COUPONUSAGEMETHOD_PAYMENT_CODE, 59 }, 60 MiniProgramAppid: wxpay_utility.String("wx1234567890"), 61 MiniProgramPath: wxpay_utility.String("/pages/index/product"), 62 UsageDescription: wxpay_utility.String("工作日可用"), 63 CouponAvailableStoreInfo: &CouponAvailableStoreInfo{ 64 Description: wxpay_utility.String("所有门店可用,可使用小程序查看门店列表"), 65 MiniProgramAppid: wxpay_utility.String("wx1234567890"), 66 MiniProgramPath: wxpay_utility.String("/pages/index/store-list"), 67 }, 68 }, 69 CouponDisplayInfo: &CouponDisplayInfo{ 70 CodeDisplayMode: COUPONCODEDISPLAYMODE_QRCODE.Ptr(), 71 BackgroundColor: wxpay_utility.String("Color010"), 72 EntranceMiniProgram: &EntranceMiniProgram{ 73 Appid: wxpay_utility.String("wx1234567890"), 74 Path: wxpay_utility.String("/pages/index/product"), 75 EntranceWording: wxpay_utility.String("欢迎选购"), 76 GuidanceWording: wxpay_utility.String("获取更多优惠"), 77 }, 78 EntranceOfficialAccount: &EntranceOfficialAccount{ 79 Appid: wxpay_utility.String("wx1234567890"), 80 }, 81 EntranceFinder: &EntranceFinder{ 82 FinderId: wxpay_utility.String("gh_12345678"), 83 FinderVideoId: wxpay_utility.String("UDFsdf24df34dD456Hdf34"), 84 FinderVideoCoverImageUrl: wxpay_utility.String("https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx"), 85 }, 86 }, 87 NotifyConfig: &NotifyConfig{ 88 NotifyAppid: wxpay_utility.String("wx4fd12345678"), 89 }, 90 StoreScope: STOCKSTORESCOPE_NONE.Ptr(), 91 }, 92 BrandId: wxpay_utility.String("120344"), 93 } 94 95 response, err := CreateStock(config, request) 96 if err != nil { 97 fmt.Printf("请求失败: %+v\n", err) 98 // TODO: 请求失败,根据状态码执行不同的处理 99 return 100 } 101 102 // TODO: 请求成功,继续业务逻辑 103 fmt.Printf("请求成功: %+v\n", response) 104} 105 106func CreateStock(config *wxpay_utility.MchConfig, request *CreateStockRequest) (response *StockEntity, err error) { 107 const ( 108 host = "https://api.mch.weixin.qq.com" 109 method = "POST" 110 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks" 111 ) 112 113 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 114 if err != nil { 115 return nil, err 116 } 117 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1) 118 reqBody, err := json.Marshal(request) 119 if err != nil { 120 return nil, err 121 } 122 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 123 if err != nil { 124 return nil, err 125 } 126 httpRequest.Header.Set("Accept", "application/json") 127 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 128 httpRequest.Header.Set("Content-Type", "application/json") 129 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 130 if err != nil { 131 return nil, err 132 } 133 httpRequest.Header.Set("Authorization", authorization) 134 135 client := &http.Client{} 136 httpResponse, err := client.Do(httpRequest) 137 if err != nil { 138 return nil, err 139 } 140 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 141 if err != nil { 142 return nil, err 143 } 144 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 145 // 2XX 成功,验证应答签名 146 err = wxpay_utility.ValidateResponse( 147 config.WechatPayPublicKeyId(), 148 config.WechatPayPublicKey(), 149 &httpResponse.Header, 150 respBody, 151 ) 152 if err != nil { 153 return nil, err 154 } 155 response := &StockEntity{} 156 if err := json.Unmarshal(respBody, response); err != nil { 157 return nil, err 158 } 159 160 return response, nil 161 } else { 162 return nil, wxpay_utility.NewApiException( 163 httpResponse.StatusCode, 164 httpResponse.Header, 165 respBody, 166 ) 167 } 168} 169 170type CreateStockRequest struct { 171 OutRequestNo *string `json:"out_request_no,omitempty"` 172 ProductCouponId *string `json:"product_coupon_id,omitempty"` 173 Stock *StockForCreate `json:"stock,omitempty"` 174 BrandId *string `json:"brand_id,omitempty"` 175} 176 177func (o *CreateStockRequest) MarshalJSON() ([]byte, error) { 178 type Alias CreateStockRequest 179 a := &struct { 180 ProductCouponId *string `json:"product_coupon_id,omitempty"` 181 *Alias 182 }{ 183 // 序列化时移除非 Body 字段 184 ProductCouponId: nil, 185 Alias: (*Alias)(o), 186 } 187 return json.Marshal(a) 188} 189 190type StockEntity struct { 191 ProductCouponId *string `json:"product_coupon_id,omitempty"` 192 StockId *string `json:"stock_id,omitempty"` 193 Remark *string `json:"remark,omitempty"` 194 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"` 195 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"` 196 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"` 197 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"` 198 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"` 199 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"` 200 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"` 201 StoreScope *StockStoreScope `json:"store_scope,omitempty"` 202 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"` 203 State *StockState `json:"state,omitempty"` 204 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"` 205 DeactivateTime *time.Time `json:"deactivate_time,omitempty"` 206 DeactivateReason *string `json:"deactivate_reason,omitempty"` 207 BrandId *string `json:"brand_id,omitempty"` 208} 209 210type StockForCreate struct { 211 Remark *string `json:"remark,omitempty"` 212 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"` 213 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"` 214 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"` 215 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"` 216 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"` 217 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"` 218 StoreScope *StockStoreScope `json:"store_scope,omitempty"` 219} 220 221type CouponCodeMode string 222 223func (e CouponCodeMode) Ptr() *CouponCodeMode { 224 return &e 225} 226 227const ( 228 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY" 229 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD" 230 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN" 231) 232 233type CouponCodeCountInfo struct { 234 TotalCount *int64 `json:"total_count,omitempty"` 235 AvailableCount *int64 `json:"available_count,omitempty"` 236} 237 238type StockSendRule struct { 239 MaxCount *int64 `json:"max_count,omitempty"` 240 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"` 241 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"` 242} 243 244type SingleUsageRule struct { 245 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"` 246 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"` 247 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"` 248 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"` 249} 250 251type UsageRuleDisplayInfo struct { 252 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"` 253 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 254 MiniProgramPath *string `json:"mini_program_path,omitempty"` 255 AppPath *string `json:"app_path,omitempty"` 256 UsageDescription *string `json:"usage_description,omitempty"` 257 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"` 258} 259 260type CouponDisplayInfo struct { 261 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"` 262 BackgroundColor *string `json:"background_color,omitempty"` 263 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"` 264 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"` 265 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"` 266} 267 268type NotifyConfig struct { 269 NotifyAppid *string `json:"notify_appid,omitempty"` 270} 271 272type StockStoreScope string 273 274func (e StockStoreScope) Ptr() *StockStoreScope { 275 return &e 276} 277 278const ( 279 STOCKSTORESCOPE_NONE StockStoreScope = "NONE" 280 STOCKSTORESCOPE_ALL StockStoreScope = "ALL" 281 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC" 282) 283 284type StockSentCountInfo struct { 285 TotalCount *int64 `json:"total_count,omitempty"` 286 TodayCount *int64 `json:"today_count,omitempty"` 287} 288 289type StockState string 290 291func (e StockState) Ptr() *StockState { 292 return &e 293} 294 295const ( 296 STOCKSTATE_AUDITING StockState = "AUDITING" 297 STOCKSTATE_SENDING StockState = "SENDING" 298 STOCKSTATE_PAUSED StockState = "PAUSED" 299 STOCKSTATE_STOPPED StockState = "STOPPED" 300 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED" 301) 302 303type CouponAvailablePeriod struct { 304 AvailableBeginTime *string `json:"available_begin_time,omitempty"` 305 AvailableEndTime *string `json:"available_end_time,omitempty"` 306 AvailableDays *int64 `json:"available_days,omitempty"` 307 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"` 308 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"` 309 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"` 310} 311 312type NormalCouponUsageRule struct { 313 Threshold *int64 `json:"threshold,omitempty"` 314 DiscountAmount *int64 `json:"discount_amount,omitempty"` 315} 316 317type DiscountCouponUsageRule struct { 318 Threshold *int64 `json:"threshold,omitempty"` 319 PercentOff *int64 `json:"percent_off,omitempty"` 320} 321 322type ExchangeCouponUsageRule struct { 323 Threshold *int64 `json:"threshold,omitempty"` 324 ExchangePrice *int64 `json:"exchange_price,omitempty"` 325} 326 327type CouponUsageMethod string 328 329func (e CouponUsageMethod) Ptr() *CouponUsageMethod { 330 return &e 331} 332 333const ( 334 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE" 335 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM" 336 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP" 337 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE" 338) 339 340type CouponAvailableStoreInfo struct { 341 Description *string `json:"description,omitempty"` 342 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 343 MiniProgramPath *string `json:"mini_program_path,omitempty"` 344} 345 346type CouponCodeDisplayMode string 347 348func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode { 349 return &e 350} 351 352const ( 353 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE" 354 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE" 355 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE" 356) 357 358type EntranceMiniProgram struct { 359 Appid *string `json:"appid,omitempty"` 360 Path *string `json:"path,omitempty"` 361 EntranceWording *string `json:"entrance_wording,omitempty"` 362 GuidanceWording *string `json:"guidance_wording,omitempty"` 363} 364 365type EntranceOfficialAccount struct { 366 Appid *string `json:"appid,omitempty"` 367} 368 369type EntranceFinder struct { 370 FinderId *string `json:"finder_id,omitempty"` 371 FinderVideoId *string `json:"finder_video_id,omitempty"` 372 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"` 373} 374 375type FixedWeekPeriod struct { 376 DayList []WeekEnum `json:"day_list,omitempty"` 377 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"` 378} 379 380type TimePeriod struct { 381 BeginTime *string `json:"begin_time,omitempty"` 382 EndTime *string `json:"end_time,omitempty"` 383} 384 385type WeekEnum string 386 387func (e WeekEnum) Ptr() *WeekEnum { 388 return &e 389} 390 391const ( 392 WEEKENUM_MONDAY WeekEnum = "MONDAY" 393 WEEKENUM_TUESDAY WeekEnum = "TUESDAY" 394 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY" 395 WEEKENUM_THURSDAY WeekEnum = "THURSDAY" 396 WEEKENUM_FRIDAY WeekEnum = "FRIDAY" 397 WEEKENUM_SATURDAY WeekEnum = "SATURDAY" 398 WEEKENUM_SUNDAY WeekEnum = "SUNDAY" 399) 400 401type PeriodOfTheDay struct { 402 BeginTime *int64 `json:"begin_time,omitempty"` 403 EndTime *int64 `json:"end_time,omitempty"` 404} 405
应答参数
200 OK
product_coupon_id 必填 string(40)
【商品券ID】 商品券的唯一标识,由微信支付生成
stock_id 必填 string(40)
【批次ID】 商品券批次的唯一标识,由微信支付生成
remark 选填 string(20)
【备注】 仅配置品牌可见,用于自定义信息
coupon_code_mode 必填 string
【券Code分配模式】 决定发券时用户商品券Code如何产生
可选取值
WECHATPAY: 微信支付随机生成,发券时由微信支付系统自动随机生成券码,品牌方无需干预,微信支付随机生成的券Code长度限制为40个字符以内UPLOAD: 品牌方预上传Code,品牌方需先使用【预上传券Code API】上传自定义Code,微信支付系统发券时从中随机选取,当预存Code不足时可能影响发券,品牌应及时补充API_ASSIGN: 品牌方自行指定,品牌方通过【向用户发放商品券API】自行发券时指定,微信支付系统不再进行自动分配。特别注意:这种模式的商品批次只可由品牌方自行发券,无法在摇一摇有优惠等微信支付渠道投放;商品券的usage_mode为PROGRESSIVE_BUNDLE时不可使用本模式
coupon_code_count_info 选填 object
【品牌方预上传的券Code数量信息】 当且仅当 coupon_code_mode 为 UPLOAD 时存在此字段
| 属性 | |
total_count 必填 integer 【已上传的Code总数】 品牌方为此批次已经上传过的券Code总数量 available_count 必填 integer 【当前可用的Code数】 品牌方为此批次已经上传过的券Code中,当前剩余可用的券Code数量 |
stock_send_rule 必填 object
【发放规则】 发放规则
| 属性 | |
max_count 必填 integer 【发放次数总上限】 批次在生命周期内总发放次数,最多 100,000,000 次 max_count_per_day 选填 integer 【每日发放次数上限】 批次在每天内发放次数上限,每日刷新,最多 100,000,000 次。默认不限制每日发放上限 max_count_per_user 必填 integer 【每个用户领取次数上限】 每个用户最多可领取本批次的次数,最多 100 次。 |
single_usage_rule 选填 object
【单券使用规则】 当且仅当 usage_mode 为 SINGLE 时提供,其他场景不提供
| 属性 | |||||||||||||||||||||||||||||
coupon_available_period 必填 object 【券可核销时间】 确定用户领券后在什么时间段内可以核销
normal_coupon 选填 object 【满减券使用规则】 当且仅当
discount_coupon 选填 object 【折扣券使用规则】 当且仅当
exchange_coupon 选填 object 【兑换券使用规则】 当且仅当
|
usage_rule_display_info 必填 object
【券使用规则展示信息】 券使用规则展示信息
| 属性 | |||||
coupon_usage_method_list 必填 array[string] 【券使用方式列表】 可以配置多种使用方式 可选取值
mini_program_appid 选填 string 【小程序AppID】 品牌方小程序AppID,可在微信公众平台查看,该小程序与品牌存在绑定关系。 在 mini_program_path 选填 string 【小程序跳转路径】 品牌方小程序内部跳转路径。 在 app_path 选填 string 【APP跳转路径】 品牌方APP跳转路径,在 usage_description 必填 string(1000) 【券使用说明】 用于说明详细的券规则,长度不超过1000个UTF-8字符 coupon_available_store_info 选填 object 【券可用门店信息】 用于描述全部可用门店信息,可配置小程序跳转地址进行展示
|
coupon_display_info 必填 object
【用户商品券展示信息】 用户商品券在卡包中的展示详情,包括引导用户的自定义入口
| 属性 | |||||||||||||
code_display_mode 必填 string 【用户商品券Code展示模式】 决定用户商品券Code在卡包中的展示形态 可选取值
background_color 选填 string 【背景颜色】 券的背景颜色,可设置10种颜色,色值请参考下方说明。颜色取值为颜色图中的颜色名称,不填默认为
entrance_mini_program 选填 object 【小程序入口】 展示跳转小程序的入口
entrance_official_account 选填 object 【公众号入口】 展示跳转公众号的入口
entrance_finder 选填 object 【视频号入口】 展示跳转视频号的入口
|
notify_config 必填 object
【事件通知配置】 发生券相关事件时,微信支付会向服务商发送通知,需要提供通知相关配置
| 属性 | |
notify_appid 必填 string 【事件通知AppID】 服务商或者品牌的AppID,支持小程序、服务号、公众号、APP类型的AppID,可在微信公众平台查看,用于券事件通知时计算用户的OpenID,需要与服务商或者品牌存在绑定关系 |
store_scope 必填 string
【可用门店范围】 控制该批次可以在品牌下哪些门店使用
可选取值
NONE: 无关联门店,该批次对外不展示可用门店信息ALL: 所有门店可用,该批次在品牌下的所有门店可用,品牌无需为该批次关联门店列表SPECIFIC: 特定门店可用,品牌需调用关联门店接口关联门店,关联后该批次在关联的门店可用
sent_count_info 必填 object
【已发放次数】 本批次已发放次数
| 属性 | |
total_count 必填 integer 【已发放总次数】 批次在生命周期内已发放次数 today_count 必填 integer 【当天已发放次数】 批次在当天已发放次数 |
state 必填 string
【批次状态】 商品券批次状态
可选取值
AUDITING: 审批中SENDING: 发放中PAUSED: 已暂停STOPPED: 已停止,当前已到达结束时间DEACTIVATED: 已失效,品牌方主动调用失效接口使批次失效
deactivate_request_no 选填 string(128)
【失效请求单号】 当且仅当 state 为 DEACTIVATED 时提供,返回品牌方调用失效接口时传入的请求流水号
deactivate_time 选填 string
【失效时间】 当且仅当 state 为 DEACTIVATED 时提供,遵循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秒。
deactivate_reason 选填 string(150)
【失效原因】 当且仅当 state 为 DEACTIVATED 时提供,返回品牌方调用【失效商品券批次API】时传入的失效原因
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
应答示例
200 OK
为满100可用的全场8折券添加批次
1{ 2 "product_coupon_id" : "1000000013", 3 "stock_id" : "1000000013001", 4 "remark" : "8月工作日有效批次", 5 "coupon_code_mode" : "UPLOAD", 6 "coupon_code_count_info" : { 7 "total_count" : 0, 8 "available_count" : 0 9 }, 10 "stock_send_rule" : { 11 "max_count" : 10000000, 12 "max_count_per_user" : 1 13 }, 14 "single_usage_rule" : { 15 "coupon_available_period" : { 16 "available_begin_time" : "2025-08-01T00:00:00+08:00", 17 "available_end_time" : "2025-08-31T23:59:59+08:00", 18 "available_days" : 30, 19 "weekly_available_period" : { 20 "day_list" : [ 21 "MONDAY", 22 "TUESDAY", 23 "WEDNESDAY", 24 "THURSDAY", 25 "FRIDAY" 26 ] 27 } 28 } 29 }, 30 "usage_rule_display_info" : { 31 "coupon_usage_method_list" : [ 32 "OFFLINE", 33 "MINI_PROGRAM", 34 "PAYMENT_CODE" 35 ], 36 "mini_program_appid" : "wx1234567890", 37 "mini_program_path" : "/pages/index/product", 38 "usage_description" : "工作日可用", 39 "coupon_available_store_info" : { 40 "description" : "所有门店可用,可使用小程序查看门店列表", 41 "mini_program_appid" : "wx1234567890", 42 "mini_program_path" : "/pages/index/store-list" 43 } 44 }, 45 "coupon_display_info" : { 46 "code_display_mode" : "QRCODE", 47 "background_color" : "Color010", 48 "entrance_mini_program" : { 49 "appid" : "wx1234567890", 50 "path" : "/pages/index/product", 51 "entrance_wording" : "欢迎选购", 52 "guidance_wording" : "获取更多优惠" 53 }, 54 "entrance_official_account" : { 55 "appid" : "wx1234567890" 56 }, 57 "entrance_finder" : { 58 "finder_id" : "gh_12345678", 59 "finder_video_id" : "UDFsdf24df34dD456Hdf34", 60 "finder_video_cover_image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx" 61 } 62 }, 63 "notify_config" : { 64 "notify_appid" : "wx4fd12345678" 65 }, 66 "store_scope" : "NONE", 67 "sent_count_info" : { 68 "total_count" : 0, 69 "today_count" : 0 70 }, 71 "state" : "SENDING", 72 "brand_id" : "120344" 73} 74
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示
状态码 | 错误码 | 描述 | 解决方案 |
|---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
400 | INVALID_REQUEST | 传入参数不符合业务规则 | 请参考文档中对每个字段的要求以及组合要求,确认请求参数是否满足 |
400 | ALREADY_EXISTS | 商品券批次已存在 | 确认 out_request_no 字段是否重复使用,确保不同请求的 out_request_no 唯一 |
404 | NOT_FOUND | 未找到 product_coupon_id 对应的商品券 | 请确认 product_coupon_id 存在且属于当前品牌 |
429 | RATELIMIT_EXCEEDED | 请求超过接口频率限制 | 请稍后使用原参数重试 |


