预上传券Code
更新时间:2025.11.07服务商可以通过该接口为商品券批次预上传券Code
前置条件:
批次的
coupon_code_mode配置为UPLOAD批次未失效或过期
频率限制:20/s
接口说明
支持商户:【普通服务商】
请求方式:【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
上传自定义券Code
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/product-coupons/1000000013/stocks/1000000013001/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" : "upload_34657_20250101_123456", 8 "code_list" : [ 9 "code_0000001", 10 "code_0000002", 11 "code_0000003" 12 ], 13 "brand_id" : "120344" 14 }' 15
需配合微信支付工具库 WXPayUtility 使用,请参考Java
1package com.java.demo; 2 3import com.java.utils.WXPayUtility; // 引用微信支付工具库,参考:https://pay.weixin.qq.com/doc/v3/partner/4014985777 4 5import com.google.gson.annotations.SerializedName; 6import com.google.gson.annotations.Expose; 7import okhttp3.MediaType; 8import okhttp3.OkHttpClient; 9import okhttp3.Request; 10import okhttp3.RequestBody; 11import okhttp3.Response; 12 13import java.io.IOException; 14import java.io.UncheckedIOException; 15import java.security.PrivateKey; 16import java.security.PublicKey; 17import java.util.ArrayList; 18import java.util.HashMap; 19import java.util.List; 20import java.util.Map; 21 22/** 23 * 预上传券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.outRequestNo = "upload_34657_20250101_123456"; 42 request.productCouponId = "1000000013"; 43 request.stockId = "1000000013001"; 44 request.codeList = new ArrayList<>(); 45 { 46 request.codeList.add("code_0000001"); 47 request.codeList.add("code_0000002"); 48 request.codeList.add("code_0000003"); 49 }; 50 request.brandId = "120344"; 51 try { 52 UploadCouponCodesResponse 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 UploadCouponCodesResponse run(UploadCouponCodesRequest request) { 62 String uri = PATH; 63 uri = uri.replace("{product_coupon_id}", WXPayUtility.urlEncode(request.productCouponId)); 64 uri = uri.replace("{stock_id}", WXPayUtility.urlEncode(request.stockId)); 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, UploadCouponCodesResponse.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 UploadCouponCodes(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 UploadCouponCodesRequest { 110 @SerializedName("out_request_no") 111 public String outRequestNo; 112 113 @SerializedName("product_coupon_id") 114 @Expose(serialize = false) 115 public String productCouponId; 116 117 @SerializedName("stock_id") 118 @Expose(serialize = false) 119 public String stockId; 120 121 @SerializedName("code_list") 122 public List<String> codeList = new ArrayList<String>(); 123 124 @SerializedName("brand_id") 125 public String brandId; 126 } 127 128 public static class UploadCouponCodesResponse { 129 @SerializedName("total_count") 130 public Long totalCount; 131 132 @SerializedName("success_code_list") 133 public List<String> successCodeList; 134 135 @SerializedName("failed_code_list") 136 public List<FailedCouponCodeInfo> failedCodeList; 137 138 @SerializedName("already_exist_code_list") 139 public List<String> alreadyExistCodeList; 140 141 @SerializedName("duplicate_code_list") 142 public List<String> duplicateCodeList; 143 } 144 145 public static class FailedCouponCodeInfo { 146 @SerializedName("coupon_code") 147 public String couponCode; 148 149 @SerializedName("code") 150 public String code; 151 152 @SerializedName("message") 153 public String message; 154 } 155 156} 157
需配合微信支付工具库 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 OutRequestNo: wxpay_utility.String("upload_34657_20250101_123456"), 29 ProductCouponId: wxpay_utility.String("1000000013"), 30 StockId: wxpay_utility.String("1000000013001"), 31 CodeList: []string{ 32 "code_0000001", 33 "code_0000002", 34 "code_0000003", 35 }, 36 BrandId: wxpay_utility.String("120344"), 37 } 38 39 response, err := UploadCouponCodes(config, request) 40 if err != nil { 41 fmt.Printf("请求失败: %+v\n", err) 42 // TODO: 请求失败,根据状态码执行不同的处理 43 return 44 } 45 46 // TODO: 请求成功,继续业务逻辑 47 fmt.Printf("请求成功: %+v\n", response) 48} 49 50func UploadCouponCodes(config *wxpay_utility.MchConfig, request *UploadCouponCodesRequest) (response *UploadCouponCodesResponse, err error) { 51 const ( 52 host = "https://api.mch.weixin.qq.com" 53 method = "POST" 54 path = "/v3/marketing/partner/product-coupon/product-coupons/{product_coupon_id}/stocks/{stock_id}/upload-coupon-codes" 55 ) 56 57 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 58 if err != nil { 59 return nil, err 60 } 61 reqUrl.Path = strings.Replace(reqUrl.Path, "{product_coupon_id}", url.PathEscape(*request.ProductCouponId), -1) 62 reqUrl.Path = strings.Replace(reqUrl.Path, "{stock_id}", url.PathEscape(*request.StockId), -1) 63 reqBody, err := json.Marshal(request) 64 if err != nil { 65 return nil, err 66 } 67 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 68 if err != nil { 69 return nil, err 70 } 71 httpRequest.Header.Set("Accept", "application/json") 72 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 73 httpRequest.Header.Set("Content-Type", "application/json") 74 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 75 if err != nil { 76 return nil, err 77 } 78 httpRequest.Header.Set("Authorization", authorization) 79 80 client := &http.Client{} 81 httpResponse, err := client.Do(httpRequest) 82 if err != nil { 83 return nil, err 84 } 85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 86 if err != nil { 87 return nil, err 88 } 89 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 90 // 2XX 成功,验证应答签名 91 err = wxpay_utility.ValidateResponse( 92 config.WechatPayPublicKeyId(), 93 config.WechatPayPublicKey(), 94 &httpResponse.Header, 95 respBody, 96 ) 97 if err != nil { 98 return nil, err 99 } 100 response := &UploadCouponCodesResponse{} 101 if err := json.Unmarshal(respBody, response); err != nil { 102 return nil, err 103 } 104 105 return response, nil 106 } else { 107 return nil, wxpay_utility.NewApiException( 108 httpResponse.StatusCode, 109 httpResponse.Header, 110 respBody, 111 ) 112 } 113} 114 115type UploadCouponCodesRequest struct { 116 OutRequestNo *string `json:"out_request_no,omitempty"` 117 ProductCouponId *string `json:"product_coupon_id,omitempty"` 118 StockId *string `json:"stock_id,omitempty"` 119 CodeList []string `json:"code_list,omitempty"` 120 BrandId *string `json:"brand_id,omitempty"` 121} 122 123func (o *UploadCouponCodesRequest) MarshalJSON() ([]byte, error) { 124 type Alias UploadCouponCodesRequest 125 a := &struct { 126 ProductCouponId *string `json:"product_coupon_id,omitempty"` 127 StockId *string `json:"stock_id,omitempty"` 128 *Alias 129 }{ 130 // 序列化时移除非 Body 字段 131 ProductCouponId: nil, 132 StockId: nil, 133 Alias: (*Alias)(o), 134 } 135 return json.Marshal(a) 136} 137 138type UploadCouponCodesResponse struct { 139 TotalCount *int64 `json:"total_count,omitempty"` 140 SuccessCodeList []string `json:"success_code_list,omitempty"` 141 FailedCodeList []FailedCouponCodeInfo `json:"failed_code_list,omitempty"` 142 AlreadyExistCodeList []string `json:"already_exist_code_list,omitempty"` 143 DuplicateCodeList []string `json:"duplicate_code_list,omitempty"` 144} 145 146type FailedCouponCodeInfo struct { 147 CouponCode *string `json:"coupon_code,omitempty"` 148 Code *string `json:"code,omitempty"` 149 Message *string `json:"message,omitempty"` 150} 151
应答参数
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
上传自定义券Code
1{ 2 "total_count" : 3, 3 "success_code_list" : [ 4 "code_0000002", 5 "code_0000003" 6 ], 7 "already_exist_code_list" : [ 8 "code_0000001" 9 ], 10 "duplicate_code_list" : [ 11 "code_0000002" 12 ] 13} 14
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示
状态码 | 错误码 | 描述 | 解决方案 |
|---|---|---|---|
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 再调用本接口 |
404 | NOT_FOUND | 未找到 product_coupon_id 对应的商品券 | 请确认 product_coupon_id 存在且属于当前品牌 |
404 | NOT_FOUND | 未找到 stock_id 对应的商品券批次 | 请确认 stock_id 存在且属于当前商品券 |
429 | RATELIMIT_EXCEEDED | 请求超过接口频率限制 | 请稍后使用原参数重试 |
