预上传券Code
更新时间:2025.08.04品牌方可以通过该接口为商品券批次预上传券Code
前置条件:已创建商品券批次,商品券批次的 coupon_code_mode
配置为 UPLOAD
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/upload-coupon-codes
请求域名:【主域名】https://api.mch.weixin.qq.com 使用该域名将访问就近的接入点
【备域名】https://api2.mch.weixin.qq.com 使用该域名将访问异地的接入点 ,指引点击查看
请求参数
Header HTTP头参数
Authorization 必填 string
请参考签名认证生成认证信息
Accept 必填 string
请设置为application/json
Content-Type 必填 string
请设置为application/json
path 路径参数
product_coupon_id 必填 string
【商品券ID】 商品券的唯一标识,创建商品券时由微信支付生成
stock_id 必填 string
【批次ID】 商品券批次的唯一标识,商品券批次创建时由微信支付生成(可使用创建商品券或添加商品券批次创建),请确保该批次属于 product_coupon_id
对应的商品券
body 包体参数
out_request_no 必填 string(40)
【请求单号】 品牌上传券Code的请求流水号,品牌侧需保持唯一性,可使用 数字、大小写字母、下划线_
、短横线-
组成,长度在6-40个字符之间
code_list 必填 array[string(40)]
【券Code列表】 商户待上传的券Code列表,券Code需保持唯一性,可使用 数字、大小写字母、下划线_
、短横线-
组成,长度不超过40个字符。
单次最多可上传200个券Code
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/product-coupons/200000001/stocks/123456789/upload-coupon-codes \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "out_request_no" : "34657_20250101_123456", 8 "code_list" : [ 9 "vCode_1234567890" 10 ], 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 * 预上传券Code 24 */ 25public class UploadCouponCodes { 26 private static String HOST = "https://api.mch.weixin.qq.com"; 27 private static String METHOD = "POST"; 28 private static String PATH = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/upload-coupon-codes"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 UploadCouponCodes client = new UploadCouponCodes( 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 , 37 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 38 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 39 ); 40 41 UploadCouponCodesRequest request = new UploadCouponCodesRequest(); 42 request.productCouponId = "200000001"; 43 request.stockId = "123456789"; 44 request.outRequestNo = "34657_20250101_123456"; 45 request.codeList = new ArrayList<>(); 46 { 47 request.codeList.add("vCode_1234567890"); 48 }; 49 request.brandId = "120344"; 50 try { 51 UploadCouponCodesResponse response = client.run(request); 52 // TODO: 请求成功,继续业务逻辑 53 System.out.println(response); 54 } catch (WXPayUtility.ApiException e) { 55 // TODO: 请求失败,根据状态码执行不同的逻辑 56 e.printStackTrace(); 57 } 58 } 59 60 public UploadCouponCodesResponse run(UploadCouponCodesRequest request) { 61 String uri = PATH; 62 uri = uri.replace("{product_coupon_id}", WXPayUtility.urlEncode(request.productCouponId)); 63 uri = uri.replace("{stock_id}", WXPayUtility.urlEncode(request.stockId)); 64 String reqBody = WXPayUtility.toJson(request); 65 66 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 67 reqBuilder.addHeader("Accept", "application/json"); 68 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 69 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 70 reqBuilder.addHeader("Content-Type", "application/json"); 71 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 72 reqBuilder.method(METHOD, requestBody); 73 Request httpRequest = reqBuilder.build(); 74 75 // 发送HTTP请求 76 OkHttpClient client = new OkHttpClient.Builder().build(); 77 try (Response httpResponse = client.newCall(httpRequest).execute()) { 78 String respBody = WXPayUtility.extractBody(httpResponse); 79 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 80 // 2XX 成功,验证应答签名 81 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 82 httpResponse.headers(), respBody); 83 84 // 从HTTP应答报文构建返回数据 85 return WXPayUtility.fromJson(respBody, UploadCouponCodesResponse.class); 86 } else { 87 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 88 } 89 } catch (IOException e) { 90 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 91 } 92 } 93 94 private final String mchid; 95 private final String certificateSerialNo; 96 private final PrivateKey privateKey; 97 private final String wechatPayPublicKeyId; 98 private final PublicKey wechatPayPublicKey; 99 100 public UploadCouponCodes(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 101 this.mchid = mchid; 102 this.certificateSerialNo = certificateSerialNo; 103 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 104 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 105 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 106 } 107 108 public static class UploadCouponCodesRequest { 109 @SerializedName("out_request_no") 110 public String outRequestNo; 111 112 @SerializedName("product_coupon_id") 113 @Expose(serialize = false) 114 public String productCouponId; 115 116 @SerializedName("stock_id") 117 @Expose(serialize = false) 118 public String stockId; 119 120 @SerializedName("code_list") 121 public List<String> codeList = new ArrayList<String>(); 122 123 @SerializedName("brand_id") 124 public String brandId; 125 } 126 127 public static class UploadCouponCodesResponse { 128 @SerializedName("total_count") 129 public Long totalCount; 130 131 @SerializedName("success_code_list") 132 public List<String> successCodeList; 133 134 @SerializedName("failed_code_list") 135 public List<FailedCouponCodeInfo> failedCodeList; 136 137 @SerializedName("already_exist_code_list") 138 public List<String> alreadyExistCodeList; 139 140 @SerializedName("duplicate_code_list") 141 public List<String> duplicateCodeList; 142 } 143 144 public static class FailedCouponCodeInfo { 145 @SerializedName("coupon_code") 146 public String couponCode; 147 148 @SerializedName("code") 149 public String code; 150 151 @SerializedName("message") 152 public String message; 153 } 154 155} 156
需配合微信支付工具库 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) 12 13func main() { 14 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 15 config, err := wxpay_utility.CreateMchConfig( 16 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 17 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 18 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 19 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 20 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 21 ) 22 if err != nil { 23 fmt.Println(err) 24 return 25 } 26 27 request := &UploadCouponCodesRequest{ 28 ProductCouponId: wxpay_utility.String("200000001"), 29 StockId: wxpay_utility.String("123456789"), 30 OutRequestNo: wxpay_utility.String("34657_20250101_123456"), 31 CodeList: []string{"vCode_1234567890"}, 32 BrandId: wxpay_utility.String("120344"), 33 } 34 35 response, err := UploadCouponCodes(config, request) 36 if err != nil { 37 fmt.Printf("请求失败: %+v\n", err) 38 // TODO: 请求失败,根据状态码执行不同的处理 39 return 40 } 41 42 // TODO: 请求成功,继续业务逻辑 43 fmt.Printf("请求成功: %+v\n", response) 44} 45 46func UploadCouponCodes(config *wxpay_utility.MchConfig, request *UploadCouponCodesRequest) (response *UploadCouponCodesResponse, err error) { 47 const ( 48 host = "https://api.mch.weixin.qq.com" 49 method = "POST" 50 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/upload-coupon-codes" 51 ) 52 53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 54 if err != nil { 55 return nil, err 56 } 57 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1) 58 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_id}", url.PathEscape(*request.StockId), -1) 59 reqBody, err := json.Marshal(request) 60 if err != nil { 61 return nil, err 62 } 63 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 64 if err != nil { 65 return nil, err 66 } 67 httpRequest.Header.Set("Accept", "application/json") 68 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 69 httpRequest.Header.Set("Content-Type", "application/json") 70 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 71 if err != nil { 72 return nil, err 73 } 74 httpRequest.Header.Set("Authorization", authorization) 75 76 client := &http.Client{} 77 httpResponse, err := client.Do(httpRequest) 78 if err != nil { 79 return nil, err 80 } 81 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 82 if err != nil { 83 return nil, err 84 } 85 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 86 // 2XX 成功,验证应答签名 87 err = wxpay_utility.ValidateResponse( 88 config.WechatPayPublicKeyId(), 89 config.WechatPayPublicKey(), 90 &httpResponse.Header, 91 respBody, 92 ) 93 if err != nil { 94 return nil, err 95 } 96 response := &UploadCouponCodesResponse{} 97 if err := json.Unmarshal(respBody, response); err != nil { 98 return nil, err 99 } 100 101 return response, nil 102 } else { 103 return nil, wxpay_utility.NewApiException( 104 httpResponse.StatusCode, 105 httpResponse.Header, 106 respBody, 107 ) 108 } 109} 110 111type UploadCouponCodesRequest struct { 112 OutRequestNo *string `json:"out_request_no,omitempty"` 113 ProductCouponId *string `json:"product_coupon_id,omitempty"` 114 StockId *string `json:"stock_id,omitempty"` 115 CodeList []string `json:"code_list,omitempty"` 116 BrandId *string `json:"brand_id,omitempty"` 117} 118 119func (o *UploadCouponCodesRequest) MarshalJSON() ([]byte, error) { 120 type Alias UploadCouponCodesRequest 121 a := &struct { 122 ProductCouponId *string `json:"product_coupon_id,omitempty"` 123 StockId *string `json:"stock_id,omitempty"` 124 *Alias 125 }{ 126 // 序列化时移除非 Body 字段 127 ProductCouponId: nil, 128 StockId: nil, 129 Alias: (*Alias)(o), 130 } 131 return json.Marshal(a) 132} 133 134type UploadCouponCodesResponse struct { 135 TotalCount *int64 `json:"total_count,omitempty"` 136 SuccessCodeList []string `json:"success_code_list,omitempty"` 137 FailedCodeList []FailedCouponCodeInfo `json:"failed_code_list,omitempty"` 138 AlreadyExistCodeList []string `json:"already_exist_code_list,omitempty"` 139 DuplicateCodeList []string `json:"duplicate_code_list,omitempty"` 140} 141 142type FailedCouponCodeInfo struct { 143 CouponCode *string `json:"coupon_code,omitempty"` 144 Code *string `json:"code,omitempty"` 145 Message *string `json:"message,omitempty"` 146} 147
应答参数
200 OK
total_count 必填 integer
【去重后券Code总数】 系统会自动对请求中的券Code去重,这里是去重后的券Code总数
success_code_list 选填 array[string]
【上传成功的券Code列表】 上传成功的券Code列表,原样返回
failed_code_list 选填 array[object]
【上传失败的券Code列表】 每个失败券Code的详细错误信息
属性 | |
coupon_code 必填 string 【券Code】 上传失败的券Code,原样返回 code 必填 string 【错误码】 上传失败的错误码 message 必填 string 【错误信息】 上传失败的错误描述 |
already_exist_code_list 选填 array[string]
【已经存在的券Code列表】 这些券Code已经存在于系统中,不会重复上传
duplicate_code_list 选填 array[string]
【重复的券Code列表】 这些券Code在请求中重复出现,不会重复上传
应答示例
200 OK
1{ 2 "total_count" : 1, 3 "success_code_list" : [ 4 "vCode_8888888888" 5 ], 6 "failed_code_list" : [ 7 { 8 "coupon_code" : "vCode_1234567890", 9 "code" : "SYSTEM_ERROR", 10 "message" : "系统失败" 11 } 12 ], 13 "already_exist_code_list" : [ 14 "vCode_1111111111" 15 ], 16 "duplicate_code_list" : [ 17 "vCode_8888888888" 18 ] 19} 20
错误码
公共错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | PARAM_ERROR | 参数错误 | 请根据错误提示正确传入参数 |
400 | INVALID_REQUEST | HTTP 请求不符合微信支付 APIv3 接口规则 | 请参阅 接口规则 |
401 | SIGN_ERROR | 验证不通过 | 请参阅 签名常见问题 |
500 | SYSTEM_ERROR | 系统异常,请稍后重试 | 请稍后重试 |
业务错误码
状态码 | 错误码 | 描述 | 解决方案 |
---|---|---|---|
400 | INVALID_REQUEST | 已过期的批次不可预上传券Code | 请确认批次是否处于有效期 |
400 | INVALID_REQUEST | 只有 coupon_code_mode 为 UPLOAD 的批次可以预上传券Code | 请确认批次 coupon_code_mode 为 UPLOAD 再调用本接口 |
403 | NO_AUTH | 缺少业务相关权限 | 请确认已开通商品券权限 |
404 | NOT_FOUND | 未找到 product_coupon_id 对应的商品券 | 请确认 product_coupon_id 存在且属于当前品牌 |
404 | NOT_FOUND | 未找到 stock_id 对应的商品券批次 | 请确认 stock_id 存在且属于当前商品券 |
429 | RATELIMIT_EXCEEDED | 请求超过接口频率限制 | 请稍后使用原参数重试 |