预上传券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 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 37 "/path/to/wxp_pub.pem" // 微信支付公钥文件路径,本地文件路径 38 ); 39 40 UploadCouponCodesRequest request = new UploadCouponCodesRequest(); 41 request.productCouponId = "200000001"; 42 request.stockId = "123456789"; 43 request.outRequestNo = "34657_20250101_123456"; 44 request.codeList = new ArrayList<>(); 45 { 46 request.codeList.add("vCode_1234567890"); 47 }; 48 request.brandId = "120344"; 49 try { 50 UploadCouponCodesResponse response = client.run(request); 51 // TODO: 请求成功,继续业务逻辑 52 System.out.println(response); 53 } catch (WXPayUtility.ApiException e) { 54 // TODO: 请求失败,根据状态码执行不同的逻辑 55 e.printStackTrace(); 56 } 57 } 58 59 public UploadCouponCodesResponse run(UploadCouponCodesRequest request) { 60 String uri = PATH; 61 uri = uri.replace("{product_coupon_id}", WXPayUtility.urlEncode(request.productCouponId)); 62 uri = uri.replace("{stock_id}", WXPayUtility.urlEncode(request.stockId)); 63 String reqBody = WXPayUtility.toJson(request); 64 65 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 66 reqBuilder.addHeader("Accept", "application/json"); 67 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 68 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 69 reqBuilder.addHeader("Content-Type", "application/json"); 70 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 71 reqBuilder.method(METHOD, requestBody); 72 Request httpRequest = reqBuilder.build(); 73 74 // 发送HTTP请求 75 OkHttpClient client = new OkHttpClient.Builder().build(); 76 try (Response httpResponse = client.newCall(httpRequest).execute()) { 77 String respBody = WXPayUtility.extractBody(httpResponse); 78 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 79 // 2XX 成功,验证应答签名 80 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 81 httpResponse.headers(), respBody); 82 83 // 从HTTP应答报文构建返回数据 84 return WXPayUtility.fromJson(respBody, UploadCouponCodesResponse.class); 85 } else { 86 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 87 } 88 } catch (IOException e) { 89 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 90 } 91 } 92 93 private final String mchid; 94 private final String certificateSerialNo; 95 private final PrivateKey privateKey; 96 private final String wechatPayPublicKeyId; 97 private final PublicKey wechatPayPublicKey; 98 99 public UploadCouponCodes(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 100 this.mchid = mchid; 101 this.certificateSerialNo = certificateSerialNo; 102 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 103 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 104 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 105 } 106 107 public static class UploadCouponCodesRequest { 108 @SerializedName("out_request_no") 109 public String outRequestNo; 110 111 @SerializedName("product_coupon_id") 112 @Expose(serialize = false) 113 public String productCouponId; 114 115 @SerializedName("stock_id") 116 @Expose(serialize = false) 117 public String stockId; 118 119 @SerializedName("code_list") 120 public List<String> codeList = new ArrayList<String>(); 121 122 @SerializedName("brand_id") 123 public String brandId; 124 } 125 126 public static class UploadCouponCodesResponse { 127 @SerializedName("total_count") 128 public Long totalCount; 129 130 @SerializedName("success_code_list") 131 public List<String> successCodeList; 132 133 @SerializedName("failed_code_list") 134 public List<FailedCouponCodeInfo> failedCodeList; 135 136 @SerializedName("already_exist_code_list") 137 public List<String> alreadyExistCodeList; 138 139 @SerializedName("duplicate_code_list") 140 public List<String> duplicateCodeList; 141 } 142 143 public static class FailedCouponCodeInfo { 144 @SerializedName("coupon_code") 145 public String couponCode; 146 147 @SerializedName("code") 148 public String code; 149 150 @SerializedName("message") 151 public String message; 152 } 153 154} 155
需配合微信支付工具库 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 | 请求超过接口频率限制 | 请稍后使用原参数重试 |
