向用户发放商品券
更新时间:2025.11.07品牌方可以通过本接口向用户发放指定商品券批次,能否发放受限于商品券批次的发放限额:
商品券批次总预算
商品券批次每日预算(如果有)
商品券批次每人限领(如果有)
前置条件:
已创建商品券批次,商品券批次处于
SENDING状态商品券批次所属商品券的
usage_mode不为PROGRESSIVE_BUNDLEusage_mode为PROGRESSIVE_BUNDLE的批次在批次组内,不可使用本接口,请使用【向用户发放商品券批次组API]
频率限制:1000/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(128)
【发券请求单号】 品牌给用户发券的请求流水号,品牌侧需保持唯一性,可使用 数字、大小写字母、下划线_、短横线- 组成,长度在6-128个字符之间
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("return_request_no") 247 public String returnRequestNo; 248 249 @SerializedName("return_time") 250 public String returnTime; 251 252 @SerializedName("associated_order_info") 253 public UserProductCouponAssociatedOrderInfo associatedOrderInfo; 254 255 @SerializedName("associated_pay_score_order_info") 256 public UserProductCouponAssociatedPayScoreOrderInfo associatedPayScoreOrderInfo; 257 } 258 259 public static class UserProductCouponBundleInfo { 260 @SerializedName("user_coupon_bundle_id") 261 public String userCouponBundleId; 262 263 @SerializedName("user_coupon_bundle_index") 264 public Long userCouponBundleIndex; 265 266 @SerializedName("total_count") 267 public Long totalCount; 268 269 @SerializedName("used_count") 270 public Long usedCount; 271 } 272 273 public static class ProductCouponEntity { 274 @SerializedName("product_coupon_id") 275 public String productCouponId; 276 277 @SerializedName("scope") 278 public ProductCouponScope scope; 279 280 @SerializedName("type") 281 public ProductCouponType type; 282 283 @SerializedName("usage_mode") 284 public UsageMode usageMode; 285 286 @SerializedName("single_usage_info") 287 public SingleUsageInfo singleUsageInfo; 288 289 @SerializedName("progressive_bundle_usage_info") 290 public ProgressiveBundleUsageInfo progressiveBundleUsageInfo; 291 292 @SerializedName("display_info") 293 public ProductCouponDisplayInfo displayInfo; 294 295 @SerializedName("out_product_no") 296 public String outProductNo; 297 298 @SerializedName("state") 299 public ProductCouponState state; 300 301 @SerializedName("deactivate_request_no") 302 public String deactivateRequestNo; 303 304 @SerializedName("deactivate_time") 305 public String deactivateTime; 306 307 @SerializedName("deactivate_reason") 308 public String deactivateReason; 309 310 @SerializedName("brand_id") 311 public String brandId; 312 } 313 314 public static class StockEntity { 315 @SerializedName("product_coupon_id") 316 public String productCouponId; 317 318 @SerializedName("stock_id") 319 public String stockId; 320 321 @SerializedName("remark") 322 public String remark; 323 324 @SerializedName("coupon_code_mode") 325 public CouponCodeMode couponCodeMode; 326 327 @SerializedName("coupon_code_count_info") 328 public CouponCodeCountInfo couponCodeCountInfo; 329 330 @SerializedName("stock_send_rule") 331 public StockSendRule stockSendRule; 332 333 @SerializedName("single_usage_rule") 334 public SingleUsageRule singleUsageRule; 335 336 @SerializedName("usage_rule_display_info") 337 public UsageRuleDisplayInfo usageRuleDisplayInfo; 338 339 @SerializedName("coupon_display_info") 340 public CouponDisplayInfo couponDisplayInfo; 341 342 @SerializedName("notify_config") 343 public NotifyConfig notifyConfig; 344 345 @SerializedName("store_scope") 346 public StockStoreScope storeScope; 347 348 @SerializedName("sent_count_info") 349 public StockSentCountInfo sentCountInfo; 350 351 @SerializedName("state") 352 public StockState state; 353 354 @SerializedName("deactivate_request_no") 355 public String deactivateRequestNo; 356 357 @SerializedName("deactivate_time") 358 public String deactivateTime; 359 360 @SerializedName("deactivate_reason") 361 public String deactivateReason; 362 363 @SerializedName("brand_id") 364 public String brandId; 365 } 366 367 public enum UserProductCouponTag { 368 @SerializedName("MEMBER") 369 MEMBER 370 } 371 372 public static class MemberTagInfo { 373 @SerializedName("member_card_id") 374 public String memberCardId; 375 } 376 377 public static class UserProductCouponAssociatedOrderInfo { 378 @SerializedName("transaction_id") 379 public String transactionId; 380 381 @SerializedName("out_trade_no") 382 public String outTradeNo; 383 384 @SerializedName("mchid") 385 public String mchid; 386 387 @SerializedName("sub_mchid") 388 public String subMchid; 389 } 390 391 public static class UserProductCouponAssociatedPayScoreOrderInfo { 392 @SerializedName("order_id") 393 public String orderId; 394 395 @SerializedName("out_order_no") 396 public String outOrderNo; 397 398 @SerializedName("mchid") 399 public String mchid; 400 401 @SerializedName("sub_mchid") 402 public String subMchid; 403 } 404 405 public enum ProductCouponScope { 406 @SerializedName("ALL") 407 ALL, 408 @SerializedName("SINGLE") 409 SINGLE 410 } 411 412 public enum ProductCouponType { 413 @SerializedName("NORMAL") 414 NORMAL, 415 @SerializedName("DISCOUNT") 416 DISCOUNT, 417 @SerializedName("EXCHANGE") 418 EXCHANGE 419 } 420 421 public enum UsageMode { 422 @SerializedName("SINGLE") 423 SINGLE, 424 @SerializedName("PROGRESSIVE_BUNDLE") 425 PROGRESSIVE_BUNDLE 426 } 427 428 public static class SingleUsageInfo { 429 @SerializedName("normal_coupon") 430 public NormalCouponUsageRule normalCoupon; 431 432 @SerializedName("discount_coupon") 433 public DiscountCouponUsageRule discountCoupon; 434 } 435 436 public static class ProgressiveBundleUsageInfo { 437 @SerializedName("count") 438 public Long count; 439 440 @SerializedName("interval_days") 441 public Long intervalDays; 442 } 443 444 public static class ProductCouponDisplayInfo { 445 @SerializedName("name") 446 public String name; 447 448 @SerializedName("image_url") 449 public String imageUrl; 450 451 @SerializedName("background_url") 452 public String backgroundUrl; 453 454 @SerializedName("detail_image_url_list") 455 public List<String> detailImageUrlList; 456 457 @SerializedName("original_price") 458 public Long originalPrice; 459 460 @SerializedName("combo_package_list") 461 public List<ComboPackage> comboPackageList; 462 } 463 464 public enum ProductCouponState { 465 @SerializedName("AUDITING") 466 AUDITING, 467 @SerializedName("EFFECTIVE") 468 EFFECTIVE, 469 @SerializedName("DEACTIVATED") 470 DEACTIVATED 471 } 472 473 public enum CouponCodeMode { 474 @SerializedName("WECHATPAY") 475 WECHATPAY, 476 @SerializedName("UPLOAD") 477 UPLOAD, 478 @SerializedName("API_ASSIGN") 479 API_ASSIGN 480 } 481 482 public static class CouponCodeCountInfo { 483 @SerializedName("total_count") 484 public Long totalCount; 485 486 @SerializedName("available_count") 487 public Long availableCount; 488 } 489 490 public static class StockSendRule { 491 @SerializedName("max_count") 492 public Long maxCount; 493 494 @SerializedName("max_count_per_day") 495 public Long maxCountPerDay; 496 497 @SerializedName("max_count_per_user") 498 public Long maxCountPerUser; 499 } 500 501 public static class SingleUsageRule { 502 @SerializedName("coupon_available_period") 503 public CouponAvailablePeriod couponAvailablePeriod; 504 505 @SerializedName("normal_coupon") 506 public NormalCouponUsageRule normalCoupon; 507 508 @SerializedName("discount_coupon") 509 public DiscountCouponUsageRule discountCoupon; 510 511 @SerializedName("exchange_coupon") 512 public ExchangeCouponUsageRule exchangeCoupon; 513 } 514 515 public static class UsageRuleDisplayInfo { 516 @SerializedName("coupon_usage_method_list") 517 public List<CouponUsageMethod> couponUsageMethodList = new ArrayList<CouponUsageMethod>(); 518 519 @SerializedName("mini_program_appid") 520 public String miniProgramAppid; 521 522 @SerializedName("mini_program_path") 523 public String miniProgramPath; 524 525 @SerializedName("app_path") 526 public String appPath; 527 528 @SerializedName("usage_description") 529 public String usageDescription; 530 531 @SerializedName("coupon_available_store_info") 532 public CouponAvailableStoreInfo couponAvailableStoreInfo; 533 } 534 535 public static class CouponDisplayInfo { 536 @SerializedName("code_display_mode") 537 public CouponCodeDisplayMode codeDisplayMode; 538 539 @SerializedName("background_color") 540 public String backgroundColor; 541 542 @SerializedName("entrance_mini_program") 543 public EntranceMiniProgram entranceMiniProgram; 544 545 @SerializedName("entrance_official_account") 546 public EntranceOfficialAccount entranceOfficialAccount; 547 548 @SerializedName("entrance_finder") 549 public EntranceFinder entranceFinder; 550 } 551 552 public static class NotifyConfig { 553 @SerializedName("notify_appid") 554 public String notifyAppid; 555 } 556 557 public enum StockStoreScope { 558 @SerializedName("NONE") 559 NONE, 560 @SerializedName("ALL") 561 ALL, 562 @SerializedName("SPECIFIC") 563 SPECIFIC 564 } 565 566 public static class StockSentCountInfo { 567 @SerializedName("total_count") 568 public Long totalCount; 569 570 @SerializedName("today_count") 571 public Long todayCount; 572 } 573 574 public enum StockState { 575 @SerializedName("AUDITING") 576 AUDITING, 577 @SerializedName("SENDING") 578 SENDING, 579 @SerializedName("PAUSED") 580 PAUSED, 581 @SerializedName("STOPPED") 582 STOPPED, 583 @SerializedName("DEACTIVATED") 584 DEACTIVATED 585 } 586 587 public static class NormalCouponUsageRule { 588 @SerializedName("threshold") 589 public Long threshold; 590 591 @SerializedName("discount_amount") 592 public Long discountAmount; 593 } 594 595 public static class DiscountCouponUsageRule { 596 @SerializedName("threshold") 597 public Long threshold; 598 599 @SerializedName("percent_off") 600 public Long percentOff; 601 } 602 603 public static class ComboPackage { 604 @SerializedName("name") 605 public String name; 606 607 @SerializedName("pick_count") 608 public Long pickCount; 609 610 @SerializedName("choice_list") 611 public List<ComboPackageChoice> choiceList = new ArrayList<ComboPackageChoice>(); 612 } 613 614 public static class CouponAvailablePeriod { 615 @SerializedName("available_begin_time") 616 public String availableBeginTime; 617 618 @SerializedName("available_end_time") 619 public String availableEndTime; 620 621 @SerializedName("available_days") 622 public Long availableDays; 623 624 @SerializedName("wait_days_after_receive") 625 public Long waitDaysAfterReceive; 626 627 @SerializedName("weekly_available_period") 628 public FixedWeekPeriod weeklyAvailablePeriod; 629 630 @SerializedName("irregular_available_period_list") 631 public List<TimePeriod> irregularAvailablePeriodList; 632 } 633 634 public static class ExchangeCouponUsageRule { 635 @SerializedName("threshold") 636 public Long threshold; 637 638 @SerializedName("exchange_price") 639 public Long exchangePrice; 640 } 641 642 public enum CouponUsageMethod { 643 @SerializedName("OFFLINE") 644 OFFLINE, 645 @SerializedName("MINI_PROGRAM") 646 MINI_PROGRAM, 647 @SerializedName("APP") 648 APP, 649 @SerializedName("PAYMENT_CODE") 650 PAYMENT_CODE 651 } 652 653 public static class CouponAvailableStoreInfo { 654 @SerializedName("description") 655 public String description; 656 657 @SerializedName("mini_program_appid") 658 public String miniProgramAppid; 659 660 @SerializedName("mini_program_path") 661 public String miniProgramPath; 662 } 663 664 public enum CouponCodeDisplayMode { 665 @SerializedName("INVISIBLE") 666 INVISIBLE, 667 @SerializedName("BARCODE") 668 BARCODE, 669 @SerializedName("QRCODE") 670 QRCODE 671 } 672 673 public static class EntranceMiniProgram { 674 @SerializedName("appid") 675 public String appid; 676 677 @SerializedName("path") 678 public String path; 679 680 @SerializedName("entrance_wording") 681 public String entranceWording; 682 683 @SerializedName("guidance_wording") 684 public String guidanceWording; 685 } 686 687 public static class EntranceOfficialAccount { 688 @SerializedName("appid") 689 public String appid; 690 } 691 692 public static class EntranceFinder { 693 @SerializedName("finder_id") 694 public String finderId; 695 696 @SerializedName("finder_video_id") 697 public String finderVideoId; 698 699 @SerializedName("finder_video_cover_image_url") 700 public String finderVideoCoverImageUrl; 701 } 702 703 public static class ComboPackageChoice { 704 @SerializedName("name") 705 public String name; 706 707 @SerializedName("price") 708 public Long price; 709 710 @SerializedName("count") 711 public Long count; 712 713 @SerializedName("image_url") 714 public String imageUrl; 715 716 @SerializedName("mini_program_appid") 717 public String miniProgramAppid; 718 719 @SerializedName("mini_program_path") 720 public String miniProgramPath; 721 } 722 723 public static class FixedWeekPeriod { 724 @SerializedName("day_list") 725 public List<WeekEnum> dayList; 726 727 @SerializedName("day_period_list") 728 public List<PeriodOfTheDay> dayPeriodList; 729 } 730 731 public static class TimePeriod { 732 @SerializedName("begin_time") 733 public String beginTime; 734 735 @SerializedName("end_time") 736 public String endTime; 737 } 738 739 public enum WeekEnum { 740 @SerializedName("MONDAY") 741 MONDAY, 742 @SerializedName("TUESDAY") 743 TUESDAY, 744 @SerializedName("WEDNESDAY") 745 WEDNESDAY, 746 @SerializedName("THURSDAY") 747 THURSDAY, 748 @SerializedName("FRIDAY") 749 FRIDAY, 750 @SerializedName("SATURDAY") 751 SATURDAY, 752 @SerializedName("SUNDAY") 753 SUNDAY 754 } 755 756 public static class PeriodOfTheDay { 757 @SerializedName("begin_time") 758 public Long beginTime; 759 760 @SerializedName("end_time") 761 public Long endTime; 762 } 763 764} 765
需配合微信支付工具库 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 ReturnRequestNo *string `json:"return_request_no,omitempty"` 207 ReturnTime *time.Time `json:"return_time,omitempty"` 208 AssociatedOrderInfo *UserProductCouponAssociatedOrderInfo `json:"associated_order_info,omitempty"` 209 AssociatedPayScoreOrderInfo *UserProductCouponAssociatedPayScoreOrderInfo `json:"associated_pay_score_order_info,omitempty"` 210} 211 212type UserProductCouponBundleInfo struct { 213 UserCouponBundleId *string `json:"user_coupon_bundle_id,omitempty"` 214 UserCouponBundleIndex *int64 `json:"user_coupon_bundle_index,omitempty"` 215 TotalCount *int64 `json:"total_count,omitempty"` 216 UsedCount *int64 `json:"used_count,omitempty"` 217} 218 219type ProductCouponEntity struct { 220 ProductCouponId *string `json:"product_coupon_id,omitempty"` 221 Scope *ProductCouponScope `json:"scope,omitempty"` 222 Type *ProductCouponType `json:"type,omitempty"` 223 UsageMode *UsageMode `json:"usage_mode,omitempty"` 224 SingleUsageInfo *SingleUsageInfo `json:"single_usage_info,omitempty"` 225 ProgressiveBundleUsageInfo *ProgressiveBundleUsageInfo `json:"progressive_bundle_usage_info,omitempty"` 226 DisplayInfo *ProductCouponDisplayInfo `json:"display_info,omitempty"` 227 OutProductNo *string `json:"out_product_no,omitempty"` 228 State *ProductCouponState `json:"state,omitempty"` 229 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"` 230 DeactivateTime *string `json:"deactivate_time,omitempty"` 231 DeactivateReason *string `json:"deactivate_reason,omitempty"` 232 BrandId *string `json:"brand_id,omitempty"` 233} 234 235type StockEntity struct { 236 ProductCouponId *string `json:"product_coupon_id,omitempty"` 237 StockId *string `json:"stock_id,omitempty"` 238 Remark *string `json:"remark,omitempty"` 239 CouponCodeMode *CouponCodeMode `json:"coupon_code_mode,omitempty"` 240 CouponCodeCountInfo *CouponCodeCountInfo `json:"coupon_code_count_info,omitempty"` 241 StockSendRule *StockSendRule `json:"stock_send_rule,omitempty"` 242 SingleUsageRule *SingleUsageRule `json:"single_usage_rule,omitempty"` 243 UsageRuleDisplayInfo *UsageRuleDisplayInfo `json:"usage_rule_display_info,omitempty"` 244 CouponDisplayInfo *CouponDisplayInfo `json:"coupon_display_info,omitempty"` 245 NotifyConfig *NotifyConfig `json:"notify_config,omitempty"` 246 StoreScope *StockStoreScope `json:"store_scope,omitempty"` 247 SentCountInfo *StockSentCountInfo `json:"sent_count_info,omitempty"` 248 State *StockState `json:"state,omitempty"` 249 DeactivateRequestNo *string `json:"deactivate_request_no,omitempty"` 250 DeactivateTime *time.Time `json:"deactivate_time,omitempty"` 251 DeactivateReason *string `json:"deactivate_reason,omitempty"` 252 BrandId *string `json:"brand_id,omitempty"` 253} 254 255type UserProductCouponTag string 256 257func (e UserProductCouponTag) Ptr() *UserProductCouponTag { 258 return &e 259} 260 261const ( 262 USERPRODUCTCOUPONTAG_MEMBER UserProductCouponTag = "MEMBER" 263) 264 265type MemberTagInfo struct { 266 MemberCardId *string `json:"member_card_id,omitempty"` 267} 268 269type UserProductCouponAssociatedOrderInfo struct { 270 TransactionId *string `json:"transaction_id,omitempty"` 271 OutTradeNo *string `json:"out_trade_no,omitempty"` 272 Mchid *string `json:"mchid,omitempty"` 273 SubMchid *string `json:"sub_mchid,omitempty"` 274} 275 276type UserProductCouponAssociatedPayScoreOrderInfo struct { 277 OrderId *string `json:"order_id,omitempty"` 278 OutOrderNo *string `json:"out_order_no,omitempty"` 279 Mchid *string `json:"mchid,omitempty"` 280 SubMchid *string `json:"sub_mchid,omitempty"` 281} 282 283type ProductCouponScope string 284 285func (e ProductCouponScope) Ptr() *ProductCouponScope { 286 return &e 287} 288 289const ( 290 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL" 291 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE" 292) 293 294type ProductCouponType string 295 296func (e ProductCouponType) Ptr() *ProductCouponType { 297 return &e 298} 299 300const ( 301 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL" 302 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT" 303 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE" 304) 305 306type UsageMode string 307 308func (e UsageMode) Ptr() *UsageMode { 309 return &e 310} 311 312const ( 313 USAGEMODE_SINGLE UsageMode = "SINGLE" 314 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE" 315) 316 317type SingleUsageInfo struct { 318 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"` 319 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"` 320} 321 322type ProgressiveBundleUsageInfo struct { 323 Count *int64 `json:"count,omitempty"` 324 IntervalDays *int64 `json:"interval_days,omitempty"` 325} 326 327type ProductCouponDisplayInfo struct { 328 Name *string `json:"name,omitempty"` 329 ImageUrl *string `json:"image_url,omitempty"` 330 BackgroundUrl *string `json:"background_url,omitempty"` 331 DetailImageUrlList []string `json:"detail_image_url_list,omitempty"` 332 OriginalPrice *int64 `json:"original_price,omitempty"` 333 ComboPackageList []ComboPackage `json:"combo_package_list,omitempty"` 334} 335 336type ProductCouponState string 337 338func (e ProductCouponState) Ptr() *ProductCouponState { 339 return &e 340} 341 342const ( 343 PRODUCTCOUPONSTATE_AUDITING ProductCouponState = "AUDITING" 344 PRODUCTCOUPONSTATE_EFFECTIVE ProductCouponState = "EFFECTIVE" 345 PRODUCTCOUPONSTATE_DEACTIVATED ProductCouponState = "DEACTIVATED" 346) 347 348type CouponCodeMode string 349 350func (e CouponCodeMode) Ptr() *CouponCodeMode { 351 return &e 352} 353 354const ( 355 COUPONCODEMODE_WECHATPAY CouponCodeMode = "WECHATPAY" 356 COUPONCODEMODE_UPLOAD CouponCodeMode = "UPLOAD" 357 COUPONCODEMODE_API_ASSIGN CouponCodeMode = "API_ASSIGN" 358) 359 360type CouponCodeCountInfo struct { 361 TotalCount *int64 `json:"total_count,omitempty"` 362 AvailableCount *int64 `json:"available_count,omitempty"` 363} 364 365type StockSendRule struct { 366 MaxCount *int64 `json:"max_count,omitempty"` 367 MaxCountPerDay *int64 `json:"max_count_per_day,omitempty"` 368 MaxCountPerUser *int64 `json:"max_count_per_user,omitempty"` 369} 370 371type SingleUsageRule struct { 372 CouponAvailablePeriod *CouponAvailablePeriod `json:"coupon_available_period,omitempty"` 373 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"` 374 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"` 375 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"` 376} 377 378type UsageRuleDisplayInfo struct { 379 CouponUsageMethodList []CouponUsageMethod `json:"coupon_usage_method_list,omitempty"` 380 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 381 MiniProgramPath *string `json:"mini_program_path,omitempty"` 382 AppPath *string `json:"app_path,omitempty"` 383 UsageDescription *string `json:"usage_description,omitempty"` 384 CouponAvailableStoreInfo *CouponAvailableStoreInfo `json:"coupon_available_store_info,omitempty"` 385} 386 387type CouponDisplayInfo struct { 388 CodeDisplayMode *CouponCodeDisplayMode `json:"code_display_mode,omitempty"` 389 BackgroundColor *string `json:"background_color,omitempty"` 390 EntranceMiniProgram *EntranceMiniProgram `json:"entrance_mini_program,omitempty"` 391 EntranceOfficialAccount *EntranceOfficialAccount `json:"entrance_official_account,omitempty"` 392 EntranceFinder *EntranceFinder `json:"entrance_finder,omitempty"` 393} 394 395type NotifyConfig struct { 396 NotifyAppid *string `json:"notify_appid,omitempty"` 397} 398 399type StockStoreScope string 400 401func (e StockStoreScope) Ptr() *StockStoreScope { 402 return &e 403} 404 405const ( 406 STOCKSTORESCOPE_NONE StockStoreScope = "NONE" 407 STOCKSTORESCOPE_ALL StockStoreScope = "ALL" 408 STOCKSTORESCOPE_SPECIFIC StockStoreScope = "SPECIFIC" 409) 410 411type StockSentCountInfo struct { 412 TotalCount *int64 `json:"total_count,omitempty"` 413 TodayCount *int64 `json:"today_count,omitempty"` 414} 415 416type StockState string 417 418func (e StockState) Ptr() *StockState { 419 return &e 420} 421 422const ( 423 STOCKSTATE_AUDITING StockState = "AUDITING" 424 STOCKSTATE_SENDING StockState = "SENDING" 425 STOCKSTATE_PAUSED StockState = "PAUSED" 426 STOCKSTATE_STOPPED StockState = "STOPPED" 427 STOCKSTATE_DEACTIVATED StockState = "DEACTIVATED" 428) 429 430type NormalCouponUsageRule struct { 431 Threshold *int64 `json:"threshold,omitempty"` 432 DiscountAmount *int64 `json:"discount_amount,omitempty"` 433} 434 435type DiscountCouponUsageRule struct { 436 Threshold *int64 `json:"threshold,omitempty"` 437 PercentOff *int64 `json:"percent_off,omitempty"` 438} 439 440type ComboPackage struct { 441 Name *string `json:"name,omitempty"` 442 PickCount *int64 `json:"pick_count,omitempty"` 443 ChoiceList []ComboPackageChoice `json:"choice_list,omitempty"` 444} 445 446type CouponAvailablePeriod struct { 447 AvailableBeginTime *string `json:"available_begin_time,omitempty"` 448 AvailableEndTime *string `json:"available_end_time,omitempty"` 449 AvailableDays *int64 `json:"available_days,omitempty"` 450 WaitDaysAfterReceive *int64 `json:"wait_days_after_receive,omitempty"` 451 WeeklyAvailablePeriod *FixedWeekPeriod `json:"weekly_available_period,omitempty"` 452 IrregularAvailablePeriodList []TimePeriod `json:"irregular_available_period_list,omitempty"` 453} 454 455type ExchangeCouponUsageRule struct { 456 Threshold *int64 `json:"threshold,omitempty"` 457 ExchangePrice *int64 `json:"exchange_price,omitempty"` 458} 459 460type CouponUsageMethod string 461 462func (e CouponUsageMethod) Ptr() *CouponUsageMethod { 463 return &e 464} 465 466const ( 467 COUPONUSAGEMETHOD_OFFLINE CouponUsageMethod = "OFFLINE" 468 COUPONUSAGEMETHOD_MINI_PROGRAM CouponUsageMethod = "MINI_PROGRAM" 469 COUPONUSAGEMETHOD_APP CouponUsageMethod = "APP" 470 COUPONUSAGEMETHOD_PAYMENT_CODE CouponUsageMethod = "PAYMENT_CODE" 471) 472 473type CouponAvailableStoreInfo struct { 474 Description *string `json:"description,omitempty"` 475 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 476 MiniProgramPath *string `json:"mini_program_path,omitempty"` 477} 478 479type CouponCodeDisplayMode string 480 481func (e CouponCodeDisplayMode) Ptr() *CouponCodeDisplayMode { 482 return &e 483} 484 485const ( 486 COUPONCODEDISPLAYMODE_INVISIBLE CouponCodeDisplayMode = "INVISIBLE" 487 COUPONCODEDISPLAYMODE_BARCODE CouponCodeDisplayMode = "BARCODE" 488 COUPONCODEDISPLAYMODE_QRCODE CouponCodeDisplayMode = "QRCODE" 489) 490 491type EntranceMiniProgram struct { 492 Appid *string `json:"appid,omitempty"` 493 Path *string `json:"path,omitempty"` 494 EntranceWording *string `json:"entrance_wording,omitempty"` 495 GuidanceWording *string `json:"guidance_wording,omitempty"` 496} 497 498type EntranceOfficialAccount struct { 499 Appid *string `json:"appid,omitempty"` 500} 501 502type EntranceFinder struct { 503 FinderId *string `json:"finder_id,omitempty"` 504 FinderVideoId *string `json:"finder_video_id,omitempty"` 505 FinderVideoCoverImageUrl *string `json:"finder_video_cover_image_url,omitempty"` 506} 507 508type ComboPackageChoice struct { 509 Name *string `json:"name,omitempty"` 510 Price *int64 `json:"price,omitempty"` 511 Count *int64 `json:"count,omitempty"` 512 ImageUrl *string `json:"image_url,omitempty"` 513 MiniProgramAppid *string `json:"mini_program_appid,omitempty"` 514 MiniProgramPath *string `json:"mini_program_path,omitempty"` 515} 516 517type FixedWeekPeriod struct { 518 DayList []WeekEnum `json:"day_list,omitempty"` 519 DayPeriodList []PeriodOfTheDay `json:"day_period_list,omitempty"` 520} 521 522type TimePeriod struct { 523 BeginTime *string `json:"begin_time,omitempty"` 524 EndTime *string `json:"end_time,omitempty"` 525} 526 527type WeekEnum string 528 529func (e WeekEnum) Ptr() *WeekEnum { 530 return &e 531} 532 533const ( 534 WEEKENUM_MONDAY WeekEnum = "MONDAY" 535 WEEKENUM_TUESDAY WeekEnum = "TUESDAY" 536 WEEKENUM_WEDNESDAY WeekEnum = "WEDNESDAY" 537 WEEKENUM_THURSDAY WeekEnum = "THURSDAY" 538 WEEKENUM_FRIDAY WeekEnum = "FRIDAY" 539 WEEKENUM_SATURDAY WeekEnum = "SATURDAY" 540 WEEKENUM_SUNDAY WeekEnum = "SUNDAY" 541) 542 543type PeriodOfTheDay struct { 544 BeginTime *int64 `json:"begin_time,omitempty"` 545 EndTime *int64 `json:"end_time,omitempty"` 546} 547
应答参数
200 OK
coupon_code 必填 string(40)
【用户商品券Code】 用户商品券的唯一标识
coupon_state 必填 string
【用户商品券状态】
可选取值
CONFIRMING: 待确认,用户商品券发放需要品牌方调用【确认发放用户商品券API(单券)】或【确认发放用户商品券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 【券核销时间】 券被核销的时间,当且仅当用户商品券状态 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小时,即北京时间) associated_order_info 选填 object 【券核销的微信支付订单信息】 券核销对应的微信支付订单信息,当且仅当用户商品券状态
associated_pay_score_order_info 选填 object 【券核销的关联微信支付分订单信息】 券核销的关联微信支付分订单信息,当且仅当用户商品券状态
|
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 | 请求超过接口频率限制 | 请稍后使用原参数重试 |


