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


