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


