向用户发放商品券
更新时间:2025.08.04品牌方可以通过本接口向用户发放指定商品券批次,能否发放受限于商品券批次的发放限额:
商品券批次总预算
商品券批次每日预算(如果有)
商品券批次每人限领(如果有)
前置条件:
已创建商品券批次,商品券批次处于
SENDING状态商品券批次所属商品券的
usage_mode不为PROGRESSIVE_BUNDLEusage_mode为PROGRESSIVE_BUNDLE的批次在批次组内,不可使用本接口,请使用【向用户发放商品券批次组API]
频率限制:500/s
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/product-coupon/users/{openid}/coupons
请求域名:【主域名】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 路径参数
openid 必填 string
【用户OpenID】 OpenID信息,用户在AppID下的唯一标识,获取方式参考OpenID
body 包体参数
product_coupon_id 必填 string
【商品券ID】 商品券的唯一标识,创建商品券时由微信支付生成
stock_id 必填 string
【批次ID】 商品券批次的唯一标识,商品券批次创建时由微信支付生成(可使用【创建商品券API】或【添加商品券批次API】创建),请确保该批次属于 product_coupon_id 对应的商品券
coupon_code 选填 string(40)
【用户商品券Code】 用户商品券的唯一标识,给用户发券时需要为该用户的券分配,可使用 数字、大小写字母、下划线_、短横线- 组成,长度不超过40个字符。
当且仅当 coupon_code_mode 为 API_ASSIGN 时必传,其他模式下用户商品券Code由微信支付分配,不允许传入。
appid 必填 string
【公众账号ID】 公众账号ID也称AppID,是(微信开放平台、微信公众平台)为开发者提供的一个唯一标识,用于识别开发者的应用程序(APP、小程序、公众号)。 开发者需要先在微信开放平台或微信公众平台中申请ID,然后在服务商平台中绑定,详见服务商商户号与AppID账号关联管理。
send_request_no 必填 string(40)
【发券请求单号】 品牌给用户发券的请求流水号,品牌侧需保持唯一性,可使用 数字、大小写字母、下划线_、短横线- 组成,长度在6-40个字符之间
attach 选填 string(150)
【自定义附加信息】 发券时品牌方可以使用该字段附带自定义附加信息,长度限制150个UTF-8字符。微信支付不会解析该信息,仅在查询用户商品券和回调中原样返回。
注: 发券渠道多样,只有品牌方通过此接口发放以及调用「向用户预发放商品券」接口并在领券组件中发放的券才会在查询和回调中携带此字段,其他渠道发放的券 attach 为空。
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
coupon_tag_info 选填 object
【用户商品券标签信息】 用户商品券标签信息
| 属性 | |||||
coupon_tag_list 选填 array[string] 【用户商品券标签列表】 用户商品券标签列表 可选取值
member_tag_info 选填 object 【会员标签信息】 当用户商品券标签列表中有
|
请求示例
POST
发送会员券
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/users/oh-394z-6CGkNoJrsDLTTUKiAnp4/coupons \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "coupon_code" : "Code_123456", 8 "product_coupon_id" : "1000000013", 9 "appid" : "wx233544546545989", 10 "attach" : "any attach content", 11 "coupon_tag_info" : { 12 "coupon_tag_list" : [ 13 "MEMBER" 14 ], 15 "member_tag_info" : { 16 "member_card_id" : "MemberCardId_1234567890" 17 } 18 }, 19 "stock_id" : "1000000013001", 20 "send_request_no" : "MCHSEND202003101234", 21 "brand_id" : "120344" 22 }' 23
发放普通券
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/users/oh-394z-6CGkNoJrsDLTTUKiAnp4/coupons \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "coupon_code" : "Code_123456", 8 "product_coupon_id" : "1000000013", 9 "appid" : "wx233544546545989", 10 "attach" : "any attach content", 11 "stock_id" : "1000000013001", 12 "send_request_no" : "MCHSEND202003101234", 13 "brand_id" : "120344" 14 }' 15
需配合微信支付工具库 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 SendUserProductCoupon { 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/users/{openid}/coupons"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 SendUserProductCoupon client = new SendUserProductCoupon( 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 SendUserProductCouponRequest request = new SendUserProductCouponRequest(); 41 request.productCouponId = "1000000013"; 42 request.stockId = "1000000013001"; 43 request.couponCode = "Code_123456"; 44 request.appid = "wx233544546545989"; 45 request.openid = "oh-394z-6CGkNoJrsDLTTUKiAnp4"; 46 request.sendRequestNo = "MCHSEND202003101234"; 47 request.attach = "any attach content"; 48 request.brandId = "120344"; 49 request.couponTagInfo = new CouponTagInfo(); 50 request.couponTagInfo.couponTagList = new ArrayList<>(); 51 { 52 request.couponTagInfo.couponTagList.add(UserProductCouponTag.MEMBER); 53 }; 54 request.couponTagInfo.memberTagInfo = new MemberTagInfo(); 55 request.couponTagInfo.memberTagInfo.memberCardId = "MemberCardId_1234567890"; 56 try { 57 UserProductCouponEntity response = client.run(request); 58 // TODO: 请求成功,继续业务逻辑 59 System.out.println(response); 60 } catch (WXPayUtility.ApiException e) { 61 // TODO: 请求失败,根据状态码执行不同的逻辑 62 e.printStackTrace(); 63 } 64 } 65 66 public UserProductCouponEntity run(SendUserProductCouponRequest request) { 67 String uri = PATH; 68 uri = uri.replace("{openid}", WXPayUtility.urlEncode(request.openid)); 69 String reqBody = WXPayUtility.toJson(request); 70 71 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 72 reqBuilder.addHeader("Accept", "application/json"); 73 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 74 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 75 reqBuilder.addHeader("Content-Type", "application/json"); 76 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 77 reqBuilder.method(METHOD, requestBody); 78 Request httpRequest = reqBuilder.build(); 79 80 // 发送HTTP请求 81 OkHttpClient client = new OkHttpClient.Builder().build(); 82 try (Response httpResponse = client.newCall(httpRequest).execute()) { 83 String respBody = WXPayUtility.extractBody(httpResponse); 84 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 85 // 2XX 成功,验证应答签名 86 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 87 httpResponse.headers(), respBody); 88 89 // 从HTTP应答报文构建返回数据 90 return WXPayUtility.fromJson(respBody, UserProductCouponEntity.class); 91 } else { 92 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 93 } 94 } catch (IOException e) { 95 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 96 } 97 } 98 99 private final String mchid; 100 private final String certificateSerialNo; 101 private final PrivateKey privateKey; 102 private final String wechatPayPublicKeyId; 103 private final PublicKey wechatPayPublicKey; 104 105 public SendUserProductCoupon(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 106 this.mchid = mchid; 107 this.certificateSerialNo = certificateSerialNo; 108 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 109 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 110 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 111 } 112 113 public static class SendUserProductCouponRequest { 114 @SerializedName("product_coupon_id") 115 public String productCouponId; 116 117 @SerializedName("stock_id") 118 public String stockId; 119 120 @SerializedName("coupon_code") 121 public String couponCode; 122 123 @SerializedName("appid") 124 public String appid; 125 126 @SerializedName("openid") 127 @Expose(serialize = false) 128 public String openid; 129 130 @SerializedName("send_request_no") 131 public String sendRequestNo; 132 133 @SerializedName("attach") 134 public String attach; 135 136 @SerializedName("brand_id") 137 public String brandId; 138 139 @SerializedName("coupon_tag_info") 140 public CouponTagInfo couponTagInfo; 141 } 142 143 public static class UserProductCouponEntity { 144 @SerializedName("coupon_code") 145 public String couponCode; 146 147 @SerializedName("coupon_state") 148 public UserProductCouponState couponState; 149 150 @SerializedName("valid_begin_time") 151 public String validBeginTime; 152 153 @SerializedName("valid_end_time") 154 public String validEndTime; 155 156 @SerializedName("receive_time") 157 public String receiveTime; 158 159 @SerializedName("send_request_no") 160 public String sendRequestNo; 161 162 @SerializedName("send_channel") 163 public UserProductCouponSendChannel sendChannel; 164 165 @SerializedName("confirm_request_no") 166 public String confirmRequestNo; 167 168 @SerializedName("confirm_time") 169 public String confirmTime; 170 171 @SerializedName("deactivate_request_no") 172 public String deactivateRequestNo; 173 174 @SerializedName("deactivate_time") 175 public String deactivateTime; 176 177 @SerializedName("deactivate_reason") 178 public String deactivateReason; 179 180 @SerializedName("single_usage_detail") 181 public CouponUsageDetail singleUsageDetail; 182 183 @SerializedName("user_product_coupon_bundle_info") 184 public UserProductCouponBundleInfo userProductCouponBundleInfo; 185 186 @SerializedName("product_coupon") 187 public ProductCouponEntity productCoupon; 188 189 @SerializedName("stock") 190 public StockEntity stock; 191 192 @SerializedName("attach") 193 public String attach; 194 195 @SerializedName("channel_custom_info") 196 public String channelCustomInfo; 197 198 @SerializedName("coupon_tag_info") 199 public CouponTagInfo couponTagInfo; 200 201 @SerializedName("brand_id") 202 public String brandId; 203 } 204 205 public static class CouponTagInfo { 206 @SerializedName("coupon_tag_list") 207 public List<UserProductCouponTag> couponTagList; 208 209 @SerializedName("member_tag_info") 210 public MemberTagInfo memberTagInfo; 211 } 212 213 public enum UserProductCouponState { 214 @SerializedName("CONFIRMING") 215 CONFIRMING, 216 @SerializedName("PENDING") 217 PENDING, 218 @SerializedName("EFFECTIVE") 219 EFFECTIVE, 220 @SerializedName("USED") 221 USED, 222 @SerializedName("EXPIRED") 223 EXPIRED, 224 @SerializedName("DELETED") 225 DELETED, 226 @SerializedName("DEACTIVATED") 227 DEACTIVATED 228 } 229 230 public enum UserProductCouponSendChannel { 231 @SerializedName("BRAND_MANAGE") 232 BRAND_MANAGE, 233 @SerializedName("API") 234 API, 235 @SerializedName("RECEIVE_COMPONENT") 236 RECEIVE_COMPONENT 237 } 238 239 public static class CouponUsageDetail { 240 @SerializedName("use_request_no") 241 public String useRequestNo; 242 243 @SerializedName("use_time") 244 public String useTime; 245 246 @SerializedName("associated_order_info") 247 public UserProductCouponAssociatedOrderInfo associatedOrderInfo; 248 249 @SerializedName("return_request_no") 250 public String returnRequestNo; 251 252 @SerializedName("return_time") 253 public String returnTime; 254 } 255 256 public static class UserProductCouponBundleInfo { 257 @SerializedName("user_coupon_bundle_id") 258 public String userCouponBundleId; 259 260 @SerializedName("user_coupon_bundle_index") 261 public Long userCouponBundleIndex; 262 263 @SerializedName("total_count") 264 public Long totalCount; 265 266 @SerializedName("used_count") 267 public Long usedCount; 268 } 269 270 public static class ProductCouponEntity { 271 @SerializedName("product_coupon_id") 272 public String productCouponId; 273 274 @SerializedName("scope") 275 public ProductCouponScope scope; 276 277 @SerializedName("type") 278 public ProductCouponType type; 279 280 @SerializedName("usage_mode") 281 public UsageMode usageMode; 282 283 @SerializedName("single_usage_info") 284 public SingleUsageInfo singleUsageInfo; 285 286 @SerializedName("progressive_bundle_usage_info") 287 public ProgressiveBundleUsageInfo progressiveBundleUsageInfo; 288 289 @SerializedName("display_info") 290 public ProductCouponDisplayInfo displayInfo; 291 292 @SerializedName("out_product_no") 293 public String outProductNo; 294 295 @SerializedName("state") 296 public ProductCouponState state; 297 298 @SerializedName("deactivate_request_no") 299 public String deactivateRequestNo; 300 301 @SerializedName("deactivate_time") 302 public String deactivateTime; 303 304 @SerializedName("deactivate_reason") 305 public String deactivateReason; 306 307 @SerializedName("brand_id") 308 public String brandId; 309 } 310 311 public static class StockEntity { 312 @SerializedName("product_coupon_id") 313 public String productCouponId; 314 315 @SerializedName("stock_id") 316 public String stockId; 317 318 @SerializedName("remark") 319 public String remark; 320 321 @SerializedName("coupon_code_mode") 322 public CouponCodeMode couponCodeMode; 323 324 @SerializedName("coupon_code_count_info") 325 public CouponCodeCountInfo couponCodeCountInfo; 326 327 @SerializedName("stock_send_rule") 328 public StockSendRule stockSendRule; 329 330 @SerializedName("single_usage_rule") 331 public SingleUsageRule singleUsageRule; 332 333 @SerializedName("usage_rule_display_info") 334 public UsageRuleDisplayInfo usageRuleDisplayInfo; 335 336 @SerializedName("coupon_display_info") 337 public CouponDisplayInfo couponDisplayInfo; 338 339 @SerializedName("notify_config") 340 public NotifyConfig notifyConfig; 341 342 @SerializedName("store_scope") 343 public StockStoreScope storeScope; 344 345 @SerializedName("sent_count_info") 346 public StockSentCountInfo sentCountInfo; 347 348 @SerializedName("state") 349 public StockState state; 350 351 @SerializedName("deactivate_request_no") 352 public String deactivateRequestNo; 353 354 @SerializedName("deactivate_time") 355 public String deactivateTime; 356 357 @SerializedName("deactivate_reason") 358 public String deactivateReason; 359 360 @SerializedName("brand_id") 361 public String brandId; 362 } 363 364 public enum UserProductCouponTag { 365 @SerializedName("MEMBER") 366 MEMBER 367 } 368 369 public static class MemberTagInfo { 370 @SerializedName("member_card_id") 371 public String memberCardId; 372 } 373 374 public static class UserProductCouponAssociatedOrderInfo { 375 @SerializedName("transaction_id") 376 public String transactionId; 377 378 @SerializedName("out_trade_no") 379 public String outTradeNo; 380 381 @SerializedName("mchid") 382 public String mchid; 383 384 @SerializedName("sub_mchid") 385 public String subMchid; 386 } 387 388 public enum ProductCouponScope { 389 @SerializedName("ALL") 390 ALL, 391 @SerializedName("SINGLE") 392 SINGLE 393 } 394 395 public enum ProductCouponType { 396 @SerializedName("NORMAL") 397 NORMAL, 398 @SerializedName("DISCOUNT") 399 DISCOUNT, 400 @SerializedName("EXCHANGE") 401 EXCHANGE 402 } 403 404 public enum UsageMode { 405 @SerializedName("SINGLE") 406 SINGLE, 407 @SerializedName("PROGRESSIVE_BUNDLE") 408 PROGRESSIVE_BUNDLE 409 } 410 411 public static class SingleUsageInfo { 412 @SerializedName("normal_coupon") 413 public NormalCouponUsageRule normalCoupon; 414 415 @SerializedName("discount_coupon") 416 public DiscountCouponUsageRule discountCoupon; 417 } 418 419 public static class ProgressiveBundleUsageInfo { 420 @SerializedName("count") 421 public Long count; 422 423 @SerializedName("interval_days") 424 public Long intervalDays; 425 } 426 427 public static class ProductCouponDisplayInfo { 428 @SerializedName("name") 429 public String name; 430 431 @SerializedName("image_url") 432 public String imageUrl; 433 434 @SerializedName("background_url") 435 public String backgroundUrl; 436 437 @SerializedName("detail_image_url_list") 438 public List<String> detailImageUrlList; 439 440 @SerializedName("original_price") 441 public Long originalPrice; 442 443 @SerializedName("combo_package_list") 444 public List<ComboPackage> comboPackageList; 445 } 446 447 public enum ProductCouponState { 448 @SerializedName("AUDITING") 449 AUDITING, 450 @SerializedName("EFFECTIVE") 451 EFFECTIVE, 452 @SerializedName("DEACTIVATED") 453 DEACTIVATED 454 } 455 456 public enum CouponCodeMode { 457 @SerializedName("WECHATPAY") 458 WECHATPAY, 459 @SerializedName("UPLOAD") 460 UPLOAD, 461 @SerializedName("API_ASSIGN") 462 API_ASSIGN 463 } 464 465 public static class CouponCodeCountInfo { 466 @SerializedName("total_count") 467 public Long totalCount; 468 469 @SerializedName("available_count") 470 public Long availableCount; 471 } 472 473 public static class StockSendRule { 474 @SerializedName("max_count") 475 public Long maxCount; 476 477 @SerializedName("max_count_per_day") 478 public Long maxCountPerDay; 479 480 @SerializedName("max_count_per_user") 481 public Long maxCountPerUser; 482 } 483 484 public static class SingleUsageRule { 485 @SerializedName("coupon_available_period") 486 public CouponAvailablePeriod couponAvailablePeriod; 487 488 @SerializedName("normal_coupon") 489 public NormalCouponUsageRule normalCoupon; 490 491 @SerializedName("discount_coupon") 492 public DiscountCouponUsageRule discountCoupon; 493 494 @SerializedName("exchange_coupon") 495 public ExchangeCouponUsageRule exchangeCoupon; 496 } 497 498 public static class UsageRuleDisplayInfo { 499 @SerializedName("coupon_usage_method_list") 500 public List<CouponUsageMethod> couponUsageMethodList = new ArrayList<CouponUsageMethod>(); 501 502 @SerializedName("mini_program_appid") 503 public String miniProgramAppid; 504 505 @SerializedName("mini_program_path") 506 public String miniProgramPath; 507 508 @SerializedName("app_path") 509 public String appPath; 510 511 @SerializedName("usage_description") 512 public String usageDescription; 513 514 @SerializedName("coupon_available_store_info") 515 public CouponAvailableStoreInfo couponAvailableStoreInfo; 516 } 517 518 public static class CouponDisplayInfo { 519 @SerializedName("code_display_mode") 520 public CouponCodeDisplayMode codeDisplayMode; 521 522 @SerializedName("background_color") 523 public String backgroundColor; 524 525 @SerializedName("entrance_mini_program") 526 public EntranceMiniProgram entranceMiniProgram; 527 528 @SerializedName("entrance_official_account") 529 public EntranceOfficialAccount entranceOfficialAccount; 530 531 @SerializedName("entrance_finder") 532 public EntranceFinder entranceFinder; 533 } 534 535 public static class NotifyConfig { 536 @SerializedName("notify_appid") 537 public String notifyAppid; 538 } 539 540 public enum StockStoreScope { 541 @SerializedName("NONE") 542 NONE, 543 @SerializedName("ALL") 544 ALL, 545 @SerializedName("SPECIFIC") 546 SPECIFIC 547 } 548 549 public static class StockSentCountInfo { 550 @SerializedName("total_count") 551 public Long totalCount; 552 553 @SerializedName("today_count") 554 public Long todayCount; 555 } 556 557 public enum StockState { 558 @SerializedName("AUDITING") 559 AUDITING, 560 @SerializedName("SENDING") 561 SENDING, 562 @SerializedName("PAUSED") 563 PAUSED, 564 @SerializedName("STOPPED") 565 STOPPED, 566 @SerializedName("DEACTIVATED") 567 DEACTIVATED 568 } 569 570 public static class NormalCouponUsageRule { 571 @SerializedName("threshold") 572 public Long threshold; 573 574 @SerializedName("discount_amount") 575 public Long discountAmount; 576 } 577 578 public static class DiscountCouponUsageRule { 579 @SerializedName("threshold") 580 public Long threshold; 581 582 @SerializedName("percent_off") 583 public Long percentOff; 584 } 585 586 public static class ComboPackage { 587 @SerializedName("name") 588 public String name; 589 590 @SerializedName("pick_count") 591 public Long pickCount; 592 593 @SerializedName("choice_list") 594 public List<ComboPackageChoice> choiceList = new ArrayList<ComboPackageChoice>(); 595 } 596 597 public static class CouponAvailablePeriod { 598 @SerializedName("available_begin_time") 599 public String availableBeginTime; 600 601 @SerializedName("available_end_time") 602 public String availableEndTime; 603 604 @SerializedName("available_days") 605 public Long availableDays; 606 607 @SerializedName("wait_days_after_receive") 608 public Long waitDaysAfterReceive; 609 610 @SerializedName("weekly_available_period") 611 public FixedWeekPeriod weeklyAvailablePeriod; 612 613 @SerializedName("irregular_available_period_list") 614 public List<TimePeriod> irregularAvailablePeriodList; 615 } 616 617 public static class ExchangeCouponUsageRule { 618 @SerializedName("threshold") 619 public Long threshold; 620 621 @SerializedName("exchange_price") 622 public Long exchangePrice; 623 } 624 625 public enum CouponUsageMethod { 626 @SerializedName("OFFLINE") 627 OFFLINE, 628 @SerializedName("MINI_PROGRAM") 629 MINI_PROGRAM, 630 @SerializedName("APP") 631 APP, 632 @SerializedName("PAYMENT_CODE") 633 PAYMENT_CODE 634 } 635 636 public static class CouponAvailableStoreInfo { 637 @SerializedName("description") 638 public String description; 639 640 @SerializedName("mini_program_appid") 641 public String miniProgramAppid; 642 643 @SerializedName("mini_program_path") 644 public String miniProgramPath; 645 } 646 647 public enum CouponCodeDisplayMode { 648 @SerializedName("INVISIBLE") 649 INVISIBLE, 650 @SerializedName("BARCODE") 651 BARCODE, 652 @SerializedName("QRCODE") 653 QRCODE 654 } 655 656 public static class EntranceMiniProgram { 657 @SerializedName("appid") 658 public String appid; 659 660 @SerializedName("path") 661 public String path; 662 663 @SerializedName("entrance_wording") 664 public String entranceWording; 665 666 @SerializedName("guidance_wording") 667 public String guidanceWording; 668 } 669 670 public static class EntranceOfficialAccount { 671 @SerializedName("appid") 672 public String appid; 673 } 674 675 public static class EntranceFinder { 676 @SerializedName("finder_id") 677 public String finderId; 678 679 @SerializedName("finder_video_id") 680 public String finderVideoId; 681 682 @SerializedName("finder_video_cover_image_url") 683 public String finderVideoCoverImageUrl; 684 } 685 686 public static class ComboPackageChoice { 687 @SerializedName("name") 688 public String name; 689 690 @SerializedName("price") 691 public Long price; 692 693 @SerializedName("count") 694 public Long count; 695 696 @SerializedName("image_url") 697 public String imageUrl; 698 699 @SerializedName("mini_program_appid") 700 public String miniProgramAppid; 701 702 @SerializedName("mini_program_path") 703 public String miniProgramPath; 704 } 705 706 public static class FixedWeekPeriod { 707 @SerializedName("day_list") 708 public List<WeekEnum> dayList; 709 710 @SerializedName("day_period_list") 711 public List<PeriodOfTheDay> dayPeriodList; 712 } 713 714 public static class TimePeriod { 715 @SerializedName("begin_time") 716 public String beginTime; 717 718 @SerializedName("end_time") 719 public String endTime; 720 } 721 722 public enum WeekEnum { 723 @SerializedName("MONDAY") 724 MONDAY, 725 @SerializedName("TUESDAY") 726 TUESDAY, 727 @SerializedName("WEDNESDAY") 728 WEDNESDAY, 729 @SerializedName("THURSDAY") 730 THURSDAY, 731 @SerializedName("FRIDAY") 732 FRIDAY, 733 @SerializedName("SATURDAY") 734 SATURDAY, 735 @SerializedName("SUNDAY") 736 SUNDAY 737 } 738 739 public static class PeriodOfTheDay { 740 @SerializedName("begin_time") 741 public Long beginTime; 742 743 @SerializedName("end_time") 744 public Long endTime; 745 } 746 747} 748
需配合微信支付工具库 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 := &SendUserProductCouponRequest{ 29 ProductCouponId: wxpay_utility.String("1000000013"), 30 StockId: wxpay_utility.String("1000000013001"), 31 CouponCode: wxpay_utility.String("Code_123456"), 32 Appid: wxpay_utility.String("wx233544546545989"), 33 Openid: wxpay_utility.String("oh-394z-6CGkNoJrsDLTTUKiAnp4"), 34 SendRequestNo: wxpay_utility.String("MCHSEND202003101234"), 35 Attach: wxpay_utility.String("any attach content"), 36 BrandId: wxpay_utility.String("120344"), 37 CouponTagInfo: &CouponTagInfo{ 38 CouponTagList: []UserProductCouponTag{ 39 USERPRODUCTCOUPONTAG_MEMBER, 40 }, 41 MemberTagInfo: &MemberTagInfo{ 42 MemberCardId: wxpay_utility.String("MemberCardId_1234567890"), 43 }, 44 }, 45 } 46 47 response, err := SendUserProductCoupon(config, request) 48 if err != nil { 49 fmt.Printf("请求失败: %+v\n", err) 50 // TODO: 请求失败,根据状态码执行不同的处理 51 return 52 } 53 54 // TODO: 请求成功,继续业务逻辑 55 fmt.Printf("请求成功: %+v\n", response) 56} 57 58func SendUserProductCoupon(config *wxpay_utility.MchConfig, request *SendUserProductCouponRequest) (response *UserProductCouponEntity, err error) { 59 const ( 60 host = "https://api.mch.weixin.qq.com" 61 method = "POST" 62 path = "/v3/marketing/partner/product-coupon/users/{openid}/coupons" 63 ) 64 65 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 66 if err != nil { 67 return nil, err 68 } 69 reqUrl.Path = strings.Replace(reqUrl.Path, "{openid}", url.PathEscape(*request.Openid), -1) 70 reqBody, err := json.Marshal(request) 71 if err != nil { 72 return nil, err 73 } 74 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 75 if err != nil { 76 return nil, err 77 } 78 httpRequest.Header.Set("Accept", "application/json") 79 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 80 httpRequest.Header.Set("Content-Type", "application/json") 81 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 82 if err != nil { 83 return nil, err 84 } 85 httpRequest.Header.Set("Authorization", authorization) 86 87 client := &http.Client{} 88 httpResponse, err := client.Do(httpRequest) 89 if err != nil { 90 return nil, err 91 } 92 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 93 if err != nil { 94 return nil, err 95 } 96 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 97 // 2XX 成功,验证应答签名 98 err = wxpay_utility.ValidateResponse( 99 config.WechatPayPublicKeyId(), 100 config.WechatPayPublicKey(), 101 &httpResponse.Header, 102 respBody, 103 ) 104 if err != nil { 105 return nil, err 106 } 107 response := &UserProductCouponEntity{} 108 if err := json.Unmarshal(respBody, response); err != nil { 109 return nil, err 110 } 111 112 return response, nil 113 } else { 114 return nil, wxpay_utility.NewApiException( 115 httpResponse.StatusCode, 116 httpResponse.Header, 117 respBody, 118 ) 119 } 120} 121 122type SendUserProductCouponRequest struct { 123 ProductCouponId *string `json:"product_coupon_id,omitempty"` 124 StockId *string `json:"stock_id,omitempty"` 125 CouponCode *string `json:"coupon_code,omitempty"` 126 Appid *string `json:"appid,omitempty"` 127 Openid *string `json:"openid,omitempty"` 128 SendRequestNo *string `json:"send_request_no,omitempty"` 129 Attach *string `json:"attach,omitempty"` 130 BrandId *string `json:"brand_id,omitempty"` 131 CouponTagInfo *CouponTagInfo `json:"coupon_tag_info,omitempty"` 132} 133 134func (o *SendUserProductCouponRequest) MarshalJSON() ([]byte, error) { 135 type Alias SendUserProductCouponRequest 136 a := &struct { 137 Openid *string `json:"openid,omitempty"` 138 *Alias 139 }{ 140 // 序列化时移除非 Body 字段 141 Openid: nil, 142 Alias: (*Alias)(o), 143 } 144 return json.Marshal(a) 145} 146 147type UserProductCouponEntity struct { 148 CouponCode *string `json:"coupon_code,omitempty"` 149 CouponState *UserProductCouponState `json:"coupon_state,omitempty"` 150 ValidBeginTime *time.Time `json:"valid_begin_time,omitempty"` 151 ValidEndTime *time.Time `json:"valid_end_time,omitempty"` 152 ReceiveTime *string `json:"receive_time,omitempty"` 153 SendRequestNo *string `json:"send_request_no,omitempty"` 154 SendChannel *UserProductCouponSendChannel `json:"send_channel,omitempty"` 155 ConfirmRequestNo *string `json:"confirm_request_no,omitempty"` 156 ConfirmTime *time.Time `json:"confirm_time,omitempty"` 157 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"` 158 DeactivateTime *string `json:"deactivate_time,omitempty"` 159 DeactivateReason *string `json:"deactivate_reason,omitempty"` 160 SingleUsageDetail *CouponUsageDetail `json:"single_usage_detail,omitempty"` 161 UserProductCouponBundleInfo *UserProductCouponBundleInfo `json:"user_product_coupon_bundle_info,omitempty"` 162 ProductCoupon *ProductCouponEntity `json:"product_coupon,omitempty"` 163 Stock *StockEntity `json:"stock,omitempty"` 164 Attach *string `json:"attach,omitempty"` 165 ChannelCustomInfo *string `json:"channel_custom_info,omitempty"` 166 CouponTagInfo *CouponTagInfo `json:"coupon_tag_info,omitempty"` 167 BrandId *string `json:"brand_id,omitempty"` 168} 169 170type CouponTagInfo struct { 171 CouponTagList []UserProductCouponTag `json:"coupon_tag_list,omitempty"` 172 MemberTagInfo *MemberTagInfo `json:"member_tag_info,omitempty"` 173} 174 175type UserProductCouponState string 176 177func (e UserProductCouponState) Ptr() *UserProductCouponState { 178 return &e 179} 180 181const ( 182 USERPRODUCTCOUPONSTATE_CONFIRMING UserProductCouponState = "CONFIRMING" 183 USERPRODUCTCOUPONSTATE_PENDING UserProductCouponState = "PENDING" 184 USERPRODUCTCOUPONSTATE_EFFECTIVE UserProductCouponState = "EFFECTIVE" 185 USERPRODUCTCOUPONSTATE_USED UserProductCouponState = "USED" 186 USERPRODUCTCOUPONSTATE_EXPIRED UserProductCouponState = "EXPIRED" 187 USERPRODUCTCOUPONSTATE_DELETED UserProductCouponState = "DELETED" 188 USERPRODUCTCOUPONSTATE_DEACTIVATED UserProductCouponState = "DEACTIVATED" 189) 190 191type UserProductCouponSendChannel string 192 193func (e UserProductCouponSendChannel) Ptr() *UserProductCouponSendChannel { 194 return &e 195} 196 197const ( 198 USERPRODUCTCOUPONSENDCHANNEL_BRAND_MANAGE UserProductCouponSendChannel = "BRAND_MANAGE" 199 USERPRODUCTCOUPONSENDCHANNEL_API UserProductCouponSendChannel = "API" 200 USERPRODUCTCOUPONSENDCHANNEL_RECEIVE_COMPONENT UserProductCouponSendChannel = "RECEIVE_COMPONENT" 201) 202 203type CouponUsageDetail struct { 204 UseRequestNo *string `json:"use_request_no,omitempty"` 205 UseTime *time.Time `json:"use_time,omitempty"` 206 AssociatedOrderInfo *UserProductCouponAssociatedOrderInfo `json:"associated_order_info,omitempty"` 207 ReturnRequestNo *string `json:"return_request_no,omitempty"` 208 ReturnTime *time.Time `json:"return_time,omitempty"` 209} 210 211type UserProductCouponBundleInfo struct { 212 UserCouponBundleId *string `json:"user_coupon_bundle_id,omitempty"` 213 UserCouponBundleIndex *int64 `json:"user_coupon_bundle_index,omitempty"` 214 TotalCount *int64 `json:"total_count,omitempty"` 215 UsedCount *int64 `json:"used_count,omitempty"` 216} 217 218type ProductCouponEntity struct { 219 ProductCouponId *string `json:"product_coupon_id,omitempty"` 220 Scope *ProductCouponScope `json:"scope,omitempty"` 221 Type *ProductCouponType `json:"type,omitempty"` 222 UsageMode *UsageMode `json:"usage_mode,omitempty"` 223 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"` 224 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"` 225 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"` 226 OutProductNo *string `json:"out_product_no,omitempty"` 227 State *ProductCouponState `json:"state,omitempty"` 228 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"` 229 DeactivateTime *string `json:"deactivate_time,omitempty"` 230 DeactivateReason *string `json:"deactivate_reason,omitempty"` 231 BrandId *string `json:"brand_id,omitempty"` 232} 233 234type StockEntity struct { 235 ProductCouponId *string `json:"product_coupon_id,omitempty"` 236 StockId *string `json:"stock_id,omitempty"` 237 Remark *string `json:"remark,omitempty"` 238 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"` 239 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"` 240 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"` 241 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"` 242 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"` 243 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"` 244 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"` 245 StoreScope *StockStoreScope `json:"store_scope,omitempty"` 246 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"` 247 State *StockState `json:"state,omitempty"` 248 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"` 249 DeactivateTime *time.Time `json:"deactivate_time,omitempty"` 250 DeactivateReason *string `json:"deactivate_reason,omitempty"` 251 BrandId *string `json:"brand_id,omitempty"` 252} 253 254type UserProductCouponTag string 255 256func (e UserProductCouponTag) Ptr() *UserProductCouponTag { 257 return &e 258} 259 260const ( 261 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER" 262) 263 264type MemberTagInfo struct { 265 MemberCardId *string `json:"member_card_id,omitempty"` 266} 267 268type UserProductCouponAssociatedOrderInfo struct { 269 TransactionId *string `json:"transaction_id,omitempty"` 270 OutTradeNo *string `json:"out_trade_no,omitempty"` 271 Mchid *string `json:"mchid,omitempty"` 272 SubMchid *string `json:"sub_mchid,omitempty"` 273} 274 275type ProductCouponScope string 276 277func (e ProductCouponScope) Ptr() *ProductCouponScope { 278 return &e 279} 280 281const ( 282 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL" 283 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE" 284) 285 286type ProductCouponType string 287 288func (e ProductCouponType) Ptr() *ProductCouponType { 289 return &e 290} 291 292const ( 293 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL" 294 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT" 295 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE" 296) 297 298type UsageMode string 299 300func (e UsageMode) Ptr() *UsageMode { 301 return &e 302} 303 304const ( 305 USAGEMODE_SINGLE UsageMode = "SINGLE" 306 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE" 307) 308 309type SingleUsageInfo struct { 310 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"` 311 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"` 312} 313 314type ProgressiveBundleUsageInfo struct { 315 Count *int64 `json:"count,omitempty"` 316 IntervalDays *int64 `json:"interval_days,omitempty"` 317} 318 319type ProductCouponDisplayInfo struct { 320 Name *string `json:"name,omitempty"` 321 ImageUrl *string `json:"image_url,omitempty"` 322 BackgroundUrl *string `json:"background_url,omitempty"` 323 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"` 324 OriginalPrice *int64 `json:"original_price,omitempty"` 325 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"` 326} 327 328type ProductCouponState string 329 330func (e ProductCouponState) Ptr() *ProductCouponState { 331 return &e 332} 333 334const ( 335 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING" 336 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE" 337 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED" 338) 339 340type CouponCodeMode string 341 342func (e CouponCodeMode) Ptr() *CouponCodeMode { 343 return &e 344} 345 346const ( 347 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY" 348 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD" 349 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN" 350) 351 352type CouponCodeCountInfo struct { 353 TotalCount *int64 `json:"total_count,omitempty"` 354 AvailableCount *int64 `json:"available_count,omitempty"` 355} 356 357type StockSendRule struct { 358 MaxCount *int64 `json:"max_count,omitempty"` 359 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"` 360 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"` 361} 362 363type SingleUsageRule struct { 364 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"` 365 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"` 366 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"` 367 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"` 368} 369 370type UsageRuleDisplayInfo struct { 371 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"` 372 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 373 MiniProgramPath *string `json:"mini_program_path,omitempty"` 374 AppPath *string `json:"app_path,omitempty"` 375 UsageDescription *string `json:"usage_description,omitempty"` 376 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"` 377} 378 379type CouponDisplayInfo struct { 380 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"` 381 BackgroundColor *string `json:"background_color,omitempty"` 382 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"` 383 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"` 384 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"` 385} 386 387type NotifyConfig struct { 388 NotifyAppid *string `json:"notify_appid,omitempty"` 389} 390 391type StockStoreScope string 392 393func (e StockStoreScope) Ptr() *StockStoreScope { 394 return &e 395} 396 397const ( 398 STOCKSTORESCOPE_NONE StockStoreScope = "NONE" 399 STOCKSTORESCOPE_ALL StockStoreScope = "ALL" 400 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC" 401) 402 403type StockSentCountInfo struct { 404 TotalCount *int64 `json:"total_count,omitempty"` 405 TodayCount *int64 `json:"today_count,omitempty"` 406} 407 408type StockState string 409 410func (e StockState) Ptr() *StockState { 411 return &e 412} 413 414const ( 415 STOCKSTATE_AUDITING StockState = "AUDITING" 416 STOCKSTATE_SENDING StockState = "SENDING" 417 STOCKSTATE_PAUSED StockState = "PAUSED" 418 STOCKSTATE_STOPPED StockState = "STOPPED" 419 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED" 420) 421 422type NormalCouponUsageRule struct { 423 Threshold *int64 `json:"threshold,omitempty"` 424 DiscountAmount *int64 `json:"discount_amount,omitempty"` 425} 426 427type DiscountCouponUsageRule struct { 428 Threshold *int64 `json:"threshold,omitempty"` 429 PercentOff *int64 `json:"percent_off,omitempty"` 430} 431 432type ComboPackage struct { 433 Name *string `json:"name,omitempty"` 434 PickCount *int64 `json:"pick_count,omitempty"` 435 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"` 436} 437 438type CouponAvailablePeriod struct { 439 AvailableBeginTime *string `json:"available_begin_time,omitempty"` 440 AvailableEndTime *string `json:"available_end_time,omitempty"` 441 AvailableDays *int64 `json:"available_days,omitempty"` 442 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"` 443 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"` 444 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"` 445} 446 447type ExchangeCouponUsageRule struct { 448 Threshold *int64 `json:"threshold,omitempty"` 449 ExchangePrice *int64 `json:"exchange_price,omitempty"` 450} 451 452type CouponUsageMethod string 453 454func (e CouponUsageMethod) Ptr() *CouponUsageMethod { 455 return &e 456} 457 458const ( 459 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE" 460 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM" 461 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP" 462 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE" 463) 464 465type CouponAvailableStoreInfo struct { 466 Description *string `json:"description,omitempty"` 467 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 468 MiniProgramPath *string `json:"mini_program_path,omitempty"` 469} 470 471type CouponCodeDisplayMode string 472 473func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode { 474 return &e 475} 476 477const ( 478 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE" 479 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE" 480 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE" 481) 482 483type EntranceMiniProgram struct { 484 Appid *string `json:"appid,omitempty"` 485 Path *string `json:"path,omitempty"` 486 EntranceWording *string `json:"entrance_wording,omitempty"` 487 GuidanceWording *string `json:"guidance_wording,omitempty"` 488} 489 490type EntranceOfficialAccount struct { 491 Appid *string `json:"appid,omitempty"` 492} 493 494type EntranceFinder struct { 495 FinderId *string `json:"finder_id,omitempty"` 496 FinderVideoId *string `json:"finder_video_id,omitempty"` 497 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"` 498} 499 500type ComboPackageChoice struct { 501 Name *string `json:"name,omitempty"` 502 Price *int64 `json:"price,omitempty"` 503 Count *int64 `json:"count,omitempty"` 504 ImageUrl *string `json:"image_url,omitempty"` 505 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 506 MiniProgramPath *string `json:"mini_program_path,omitempty"` 507} 508 509type FixedWeekPeriod struct { 510 DayList []WeekEnum `json:"day_list,omitempty"` 511 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"` 512} 513 514type TimePeriod struct { 515 BeginTime *string `json:"begin_time,omitempty"` 516 EndTime *string `json:"end_time,omitempty"` 517} 518 519type WeekEnum string 520 521func (e WeekEnum) Ptr() *WeekEnum { 522 return &e 523} 524 525const ( 526 WEEKENUM_MONDAY WeekEnum = "MONDAY" 527 WEEKENUM_TUESDAY WeekEnum = "TUESDAY" 528 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY" 529 WEEKENUM_THURSDAY WeekEnum = "THURSDAY" 530 WEEKENUM_FRIDAY WeekEnum = "FRIDAY" 531 WEEKENUM_SATURDAY WeekEnum = "SATURDAY" 532 WEEKENUM_SUNDAY WeekEnum = "SUNDAY" 533) 534 535type PeriodOfTheDay struct { 536 BeginTime *int64 `json:"begin_time,omitempty"` 537 EndTime *int64 `json:"end_time,omitempty"` 538} 539
应答参数
200 OK
coupon_code 必填 string(40)
【用户商品券Code】 用户商品券的唯一标识
coupon_state 必填 string
【用户商品券状态】
可选取值
CONFIRMING: 待确认,用户商品券发放需要品牌方调用【确认发放用户商品券API】后才能生效PENDING: 已发放待生效,用户商品券已发放成功但尚未到达可用开始时间EFFECTIVE: 已生效,用户商品券已成功发放且到达可用开始时间USED: 已核销,用户商品券已核销EXPIRED: 已过期,用户商品券已超过有效期,不再可用DELETED: 已删除,用户主动删除该券DEACTIVATED: 已失效,品牌方主动调用【失效用户商品券API】或【失效用户商品券组API】使用户商品券失效
valid_begin_time 必填 string
【有效期开始时间】 用户商品券可用开始时间,遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)
valid_end_time 必填 string
【有效期结束时间】 用户商品券可用结束时间。遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)
receive_time 必填 string
【领券时间】 用户领券时间。遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)
send_request_no 必填 string(128)
【发券请求单号】 发券时传入的请求流水号
send_channel 必填 string
【发券渠道】 描述用户商品券是经由什么渠道发送的
可选取值
BRAND_MANAGE: 摇一摇有优惠,通过摇一摇有优惠渠道发放API: 服务商自主发券,服务商通过发券接口自主发券到商家名片RECEIVE_COMPONENT: 小程序领券组件,服务商通过小程序领券组件发券
confirm_request_no 选填 string
【确认请求单号】 品牌方确认发券请求时传入的的请求流水号。当且仅当 品牌方调用【确认发放用户商品券API】后提供。
confirm_time 选填 string
【确认发放时间】 品牌方确认发券时间,当且仅当 品牌方调用【确认发放用户商品券API】后提供。遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)
deactivate_request_no 选填 string(128)
【失效请求单号】 品牌方失效券请求时传入的的请求流水号。当且仅当 coupon_state 为 DEACTIVATED 时提供,返回品牌方调用【失效用户商品券API】或【失效用户商品券组API】时传入的请求流水号
deactivate_time 选填 string
【失效时间】 失效时间,当且仅当 coupon_state 为 DEACTIVATED 时提供。遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间)
deactivate_reason 选填 string(150)
【失效原因】 失效券的原因,当且仅当 coupon_state 为 DEACTIVATED 时提供,返回品牌方调用【失效用户商品券API】或【失效用户商品券组API】时传入的失效原因
single_usage_detail 选填 object
【单券使用详情】 当且仅当 usage_mode 为 SINGLE 时提供
| 属性 | |||||
use_request_no 选填 string 【券核销请求单号】 券核销的请求流水号,当且仅当用户商品券状态 use_time 选填 string 【券核销时间】 券被核销的时间,当且仅当用户商品券状态 associated_order_info 选填 object 【券核销的微信支付订单信息】 券核销对应的微信支付订单信息,当且仅当用户商品券状态
return_request_no 选填 string 【退券请求单号】 品牌退券时传入的请求流水号,当且仅当券发生了退回后提供此字段 return_time 选填 string 【退券时间】 券被退回的时间,当且仅当券发生了退回后提供此字段。遵循rfc3339标准格式,格式为yyyy-MM-DDTHH:mm:ss+TIMEZONE,yyyy-MM-DD表示年月日,T出现在字符串中,表示time元素的开头,HH:mm:ss表示时分秒,TIMEZONE表示时区(+08:00表示东八区时间,领先UTC 8小时,即北京时间) |
user_product_coupon_bundle_info 选填 object
【用户券组信息】 当前用户券所属用户券组的信息,当且仅当 usage_mode 为 PROGRESSIVE_BUNDLE 时提供
| 属性 | |
user_coupon_bundle_id 必填 string 【用户券组ID】 本券所属的用户券组ID,【向用户发放商品券批次组】时由微信支付生成 user_coupon_bundle_index 必填 integer 【用户券组内索引】 本券在所属用户券组内的序号,从 0 开始编号 total_count 必填 integer 【总可使用次数】 所属用户券组总计可用次数 used_count 必填 integer 【已使用次数】 所属用户券组已使用次数 |
product_coupon 必填 object
【商品券信息】 该用户商品券对应的商品券详情
| 属性 | |||||||||||||||||||||||||||||
product_coupon_id 必填 string(40) 【商品券ID】 商品券的唯一标识,由微信支付生成 scope 必填 string 【优惠范围】 商品券优惠范围 可选取值
type 必填 string 【商品券类型】 商品券的优惠类型 可选取值
usage_mode 必填 string 【使用模式】 商品券使用模式 可选取值
single_usage_info 选填 object 【单券模式信息】 单券模式配置信息,仅当
progressive_bundle_usage_info 选填 object 【多次优惠模式信息】 多次优惠模式配置信息,当且仅当
display_info 必填 object 【展示信息】 商品券展示信息
out_product_no 选填 string 【外部商品ID】 商户创建商品券时主动传入的外部商品ID,原样返回 state 必填 string 【商品券状态】 商品券状态 可选取值
deactivate_request_no 选填 string(128) 【失效请求单号】 当且仅当 deactivate_time 选填 string 【失效时间】 当且仅当 deactivate_reason 选填 string(150) 【失效原因】 当且仅当 brand_id 必填 string 【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系 |
stock 必填 object
【批次信息】 该用户商品券发券时使用的批次详情
| 属性 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
product_coupon_id 必填 string(40) 【商品券ID】 商品券的唯一标识,由微信支付生成 stock_id 必填 string(40) 【批次ID】 商品券批次的唯一标识,由微信支付生成 remark 选填 string(20) 【备注】 仅配置品牌可见,用于自定义信息 coupon_code_mode 必填 string 【券Code分配模式】 决定发券时用户商品券Code如何产生 可选取值
coupon_code_count_info 选填 object 【品牌方预上传的券Code数量信息】 当且仅当
stock_send_rule 必填 object 【发放规则】 发放规则
single_usage_rule 选填 object 【单券使用规则】 当且仅当
usage_rule_display_info 必填 object 【券使用规则展示信息】 券使用规则展示信息
coupon_display_info 必填 object 【用户商品券展示信息】 用户商品券在卡包中的展示详情,包括引导用户的自定义入口
notify_config 必填 object 【事件通知配置】 发生券相关事件时,微信支付会向服务商发送通知,需要提供通知相关配置
store_scope 必填 string 【可用门店范围】 控制该批次可以在品牌下哪些门店使用 可选取值
sent_count_info 必填 object 【已发放次数】 本批次已发放次数
state 必填 string 【批次状态】 商品券批次状态 可选取值
deactivate_request_no 选填 string(128) 【失效请求单号】 当且仅当 deactivate_time 选填 string 【失效时间】 当且仅当 deactivate_reason 选填 string(150) 【失效原因】 当且仅当 brand_id 必填 string 【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系 |
attach 选填 string
【自定义附加信息】 调用发券接口时品牌方使用 attach 字段主动设置的附加信息。微信支付不会解析该信息,仅在查询用户商品券和回调中原样返回。
注: 发券渠道多样,只有品牌方通过发券接口发放的券才会在查询和回调中携带此字段,其他渠道发放的券 attach 为空。
channel_custom_info 选填 string(1000)
【渠道自定义信息】 使用微信支付提供的其他渠道(比如「摇一摇有优惠」)发放商品券时,渠道可能会设置该渠道特定的自定义信息,请根据 send_channel 字段判断如何解析本字段。不同渠道的自定义信息格式不同,请根据对应渠道的文档解析。
coupon_tag_info 选填 object
【用户商品券标签信息】 用户商品券标签信息
| 属性 | |||||
coupon_tag_list 选填 array[string] 【用户商品券标签列表】 用户商品券标签列表 可选取值
member_tag_info 选填 object 【会员标签信息】 当用户商品券标签列表中有
|
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
应答示例
200 OK
发送会员券
1{ 2 "coupon_code" : "Code_123456", 3 "coupon_state" : "CONFIRMING", 4 "valid_begin_time" : "2025-08-02T00:00:00+08:00", 5 "valid_end_time" : "2025-08-31T23:59:59+08:00", 6 "receive_time" : "2025-08-02T00:00:00+08:00", 7 "send_request_no" : "MCHSEND202003101234", 8 "send_channel" : "API", 9 "single_usage_detail" : { }, 10 "product_coupon" : { 11 "product_coupon_id" : "1000000013", 12 "scope" : "ALL", 13 "type" : "DISCOUNT", 14 "usage_mode" : "SINGLE", 15 "single_usage_info" : { 16 "discount_coupon" : { 17 "threshold" : 10000, 18 "percent_off" : 20 19 } 20 }, 21 "display_info" : { 22 "name" : "全场满100立打8折-新名字", 23 "image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx", 24 "background_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx", 25 "detail_image_url_list" : [ 26 "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx" 27 ] 28 }, 29 "state" : "EFFECTIVE", 30 "out_product_no" : "Product_1234567890", 31 "brand_id" : "120344" 32 }, 33 "stock" : { 34 "product_coupon_id" : "1000000013", 35 "stock_id" : "1000000013001", 36 "remark" : "8月工作日有效批次", 37 "coupon_code_mode" : "UPLOAD", 38 "coupon_code_count_info" : { 39 "total_count" : 0, 40 "available_count" : 0 41 }, 42 "stock_send_rule" : { 43 "max_count" : 10000000, 44 "max_count_per_user" : 1 45 }, 46 "single_usage_rule" : { 47 "coupon_available_period" : { 48 "available_begin_time" : "2025-08-01T00:00:00+08:00", 49 "available_end_time" : "2025-08-31T23:59:59+08:00", 50 "available_days" : 30, 51 "weekly_available_period" : { 52 "day_list" : [ 53 "MONDAY", 54 "TUESDAY", 55 "WEDNESDAY", 56 "THURSDAY", 57 "FRIDAY" 58 ] 59 } 60 } 61 }, 62 "usage_rule_display_info" : { 63 "coupon_usage_method_list" : [ 64 "OFFLINE", 65 "MINI_PROGRAM", 66 "PAYMENT_CODE" 67 ], 68 "mini_program_appid" : "wx1234567890", 69 "mini_program_path" : "/pages/index/product", 70 "usage_description" : "工作日可用", 71 "coupon_available_store_info" : { 72 "description" : "所有门店可用,可使用小程序查看门店列表", 73 "mini_program_appid" : "wx1234567890", 74 "mini_program_path" : "/pages/index/store-list" 75 } 76 }, 77 "coupon_display_info" : { 78 "code_display_mode" : "QRCODE", 79 "background_color" : "Color010", 80 "entrance_mini_program" : { 81 "appid" : "wx1234567890", 82 "path" : "/pages/index/product", 83 "entrance_wording" : "欢迎选购", 84 "guidance_wording" : "获取更多优惠" 85 }, 86 "entrance_official_account" : { 87 "appid" : "wx1234567890" 88 }, 89 "entrance_finder" : { 90 "finder_id" : "gh_12345678", 91 "finder_video_id" : "UDFsdf24df34dD456Hdf34", 92 "finder_video_cover_image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx" 93 } 94 }, 95 "notify_config" : { 96 "notify_appid" : "wx4fd12345678" 97 }, 98 "store_scope" : "NONE", 99 "sent_count_info" : { 100 "total_count" : 0, 101 "today_count" : 0 102 }, 103 "state" : "SENDING", 104 "brand_id" : "120344" 105 }, 106 "attach" : "any attach content", 107 "coupon_tag_info" : { 108 "coupon_tag_list" : [ 109 "MEMBER" 110 ], 111 "member_tag_info" : { 112 "member_card_id" : "MemberCardId_1234567890" 113 } 114 }, 115 "brand_id" : "120344" 116} 117
发放普通券
1{ 2 "coupon_code" : "Code_123456", 3 "coupon_state" : "CONFIRMING", 4 "valid_begin_time" : "2025-08-02T00:00:00+08:00", 5 "valid_end_time" : "2025-08-31T23:59:59+08:00", 6 "receive_time" : "2025-08-02T00:00:00+08:00", 7 "send_request_no" : "MCHSEND202003101234", 8 "send_channel" : "API", 9 "single_usage_detail" : { }, 10 "product_coupon" : { 11 "product_coupon_id" : "1000000013", 12 "scope" : "ALL", 13 "type" : "DISCOUNT", 14 "usage_mode" : "SINGLE", 15 "single_usage_info" : { 16 "discount_coupon" : { 17 "threshold" : 10000, 18 "percent_off" : 20 19 } 20 }, 21 "display_info" : { 22 "name" : "全场满100立打8折-新名字", 23 "image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx", 24 "background_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx", 25 "detail_image_url_list" : [ 26 "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx" 27 ] 28 }, 29 "state" : "EFFECTIVE", 30 "out_product_no" : "Product_1234567890", 31 "brand_id" : "120344" 32 }, 33 "stock" : { 34 "product_coupon_id" : "1000000013", 35 "stock_id" : "1000000013001", 36 "remark" : "8月工作日有效批次", 37 "coupon_code_mode" : "UPLOAD", 38 "coupon_code_count_info" : { 39 "total_count" : 0, 40 "available_count" : 0 41 }, 42 "stock_send_rule" : { 43 "max_count" : 10000000, 44 "max_count_per_user" : 1 45 }, 46 "single_usage_rule" : { 47 "coupon_available_period" : { 48 "available_begin_time" : "2025-08-01T00:00:00+08:00", 49 "available_end_time" : "2025-08-31T23:59:59+08:00", 50 "available_days" : 30, 51 "weekly_available_period" : { 52 "day_list" : [ 53 "MONDAY", 54 "TUESDAY", 55 "WEDNESDAY", 56 "THURSDAY", 57 "FRIDAY" 58 ] 59 } 60 } 61 }, 62 "usage_rule_display_info" : { 63 "coupon_usage_method_list" : [ 64 "OFFLINE", 65 "MINI_PROGRAM", 66 "PAYMENT_CODE" 67 ], 68 "mini_program_appid" : "wx1234567890", 69 "mini_program_path" : "/pages/index/product", 70 "usage_description" : "工作日可用", 71 "coupon_available_store_info" : { 72 "description" : "所有门店可用,可使用小程序查看门店列表", 73 "mini_program_appid" : "wx1234567890", 74 "mini_program_path" : "/pages/index/store-list" 75 } 76 }, 77 "coupon_display_info" : { 78 "code_display_mode" : "QRCODE", 79 "background_color" : "Color010", 80 "entrance_mini_program" : { 81 "appid" : "wx1234567890", 82 "path" : "/pages/index/product", 83 "entrance_wording" : "欢迎选购", 84 "guidance_wording" : "获取更多优惠" 85 }, 86 "entrance_official_account" : { 87 "appid" : "wx1234567890" 88 }, 89 "entrance_finder" : { 90 "finder_id" : "gh_12345678", 91 "finder_video_id" : "UDFsdf24df34dD456Hdf34", 92 "finder_video_cover_image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/xxxxx/xxx" 93 } 94 }, 95 "notify_config" : { 96 "notify_appid" : "wx4fd12345678" 97 }, 98 "store_scope" : "NONE", 99 "sent_count_info" : { 100 "total_count" : 0, 101 "today_count" : 0 102 }, 103 "state" : "SENDING", 104 "brand_id" : "120344" 105 }, 106 "attach" : "any attach content", 107 "brand_id" : "120344" 108} 109
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示
状态码 | 错误码 | 描述 | 解决方案 |
|---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
400 | INVALID_REQUEST | 传入参数不符合业务规则 | 请参考文档中对每个字段的要求以及组合要求,确认请求参数是否满足 |
400 | INVALID_REQUEST | 用户领取已达到上限 | 请参考文档中对每个字段的要求以及组合要求,确认请求参数是否满足 |
400 | ALREADY_EXISTS | out_request_no 冲突 | 确认 out_request_no 字段是否重复使用,确保不同请求的 out_request_no 唯一 |
400 | ALREADY_EXISTS | coupon_code 冲突 | 用户商品券Code冲突,请使用其他券Code |
403 | NOT_ENOUGH | 商品券批次发放预算不足 | 请确认商品券批次发放预算是否充足 |
403 | NOT_ENOUGH | 商品券批次预上传券Code剩余数量不足 | 请确认商品券批次预上传券Code剩余数量是否充足,并及时补充上传 |
404 | NOT_FOUND | 未找到 product_coupon_id 对应的商品券 | 请确认 product_coupon_id 存在且属于当前品牌 |
404 | NOT_FOUND | 未找到 stock_id 对应的商品券批次 | 请确认 stock_id 存在且属于当前商品券 |
429 | RATELIMIT_EXCEEDED | 请求超过接口频率限制 | 请稍后使用原参数重试 |


