提交图片生成任务
更新时间:2026.01.26根据类型生成图片,结果将通过回调的方式告知,或通过主动查询接口获取生成结果
频率限制:10/s
接口说明
支持商户:【普通服务商】
请求方式:【POST】/v3/marketing/partner/product-coupon/image-generation-tasks
请求域名:【主域名】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
body 包体参数
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
task_id 必填 string
【图片生成的任务ID】 服务商创建图片生成任务的请求流水号,服务商侧需保持唯一性,可使用 数字、大小写字母、下划线_、短横线- 组成,长度在6-40个字符之间
image_generation_type 必填 string
【图片生成类型】 图片生成类型
可选取值
COMBINE_IMAGE: 适合全场类优惠头图,将品牌元素与商品券优惠等信息拼接生成头图CUT_OUT: 适合单品类优惠头图,用于去除图片背景,保留商品主体
combine_image 选填 object
【拼图数据】 当 image_generation_type 为 COMBINE_IMAGE 时,必填
| 属性 | |||||||||||||
scope 必填 string 【优惠范围】 商品券的优惠范围 可选取值
type 必填 string 【商品券类型】 商品券的优惠类型 可选取值
usage_mode 必填 string 【使用模式】 商品券使用模式,当前仅支持 可选取值
normal_coupon 选填 object 【满减券使用规则】 当且仅当
discount_coupon 选填 object 【折扣券使用规则】 当且仅当
exchange_coupon 选填 object 【兑换券使用规则】 当且仅当
background_color 选填 string 【背景颜色色值】 指定背景色(要求6字节Hex描述以#开头),用于指定拼图品牌色 |
cut_out 选填 object
【抠图数据】 当 image_generation_type 为 CUT_OUT 时,必填
| 属性 | |
image_url 必填 string 【商品图URL】 填写待处理图片的URL,仅支持储存在https://wxpaylogo.qpic.cn域名的图片 |
请求示例
POST
1curl -X POST \ 2 https://api.mch.weixin.qq.com/v3/marketing/partner/product-coupon/image-generation-tasks \ 3 -H "Authorization: WECHATPAY2-SHA256-RSA2048 mchid=\"1900000001\",..." \ 4 -H "Accept: application/json" \ 5 -H "Content-Type: application/json" \ 6 -d '{ 7 "brand_id" : "120344", 8 "task_id" : "image_generation_task_1", 9 "image_generation_type" : "COMBINE_IMAGE", 10 "combine_image" : { 11 "scope" : "ALL", 12 "type" : "NORMAL", 13 "usage_mode" : "SINGLE", 14 "normal_coupon" : { 15 "threshold" : 10000, 16 "discount_amount" : 100 17 }, 18 "discount_coupon" : { 19 "threshold" : 10000, 20 "percent_off" : 30 21 }, 22 "exchange_coupon" : { 23 "threshold" : 10000, 24 "exchange_price" : 100 25 }, 26 "background_color" : "#ff5733" 27 }, 28 "cut_out" : { 29 "image_url" : "https://wxpaylogo.qpic.cn/wxpaylogo/example.jpg" 30 } 31 }' 32
需配合微信支付工具库 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 SubmitImageGenerationTask { 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/image-generation-tasks"; 29 30 public static void main(String[] args) { 31 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 32 SubmitImageGenerationTask client = new SubmitImageGenerationTask( 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 SubmitImageGenerationTaskRequest request = new SubmitImageGenerationTaskRequest(); 41 request.brandId = "120344"; 42 request.taskId = "image_generation_task_1"; 43 request.imageGenerationType = ImageGenerationType.COMBINE_IMAGE; 44 request.combineImage = new CombineImage(); 45 request.combineImage.scope = ProductCouponScope.ALL; 46 request.combineImage.type = ProductCouponType.NORMAL; 47 request.combineImage.usageMode = UsageMode.SINGLE; 48 request.combineImage.normalCoupon = new NormalCouponUsageRule(); 49 request.combineImage.normalCoupon.threshold = 10000L; 50 request.combineImage.normalCoupon.discountAmount = 100L; 51 request.combineImage.discountCoupon = new DiscountCouponUsageRule(); 52 request.combineImage.discountCoupon.threshold = 10000L; 53 request.combineImage.discountCoupon.percentOff = 30L; 54 request.combineImage.exchangeCoupon = new ExchangeCouponUsageRule(); 55 request.combineImage.exchangeCoupon.threshold = 10000L; 56 request.combineImage.exchangeCoupon.exchangePrice = 100L; 57 request.combineImage.backgroundColor = "#ff5733"; 58 request.cutOut = new CutOut(); 59 request.cutOut.imageUrl = "https://wxpaylogo.qpic.cn/wxpaylogo/example.jpg"; 60 try { 61 SubmitImageGenerationTaskResponse response = client.run(request); 62 // TODO: 请求成功,继续业务逻辑 63 System.out.println(response); 64 } catch (WXPayUtility.ApiException e) { 65 // TODO: 请求失败,根据状态码执行不同的逻辑 66 e.printStackTrace(); 67 } 68 } 69 70 public SubmitImageGenerationTaskResponse run(SubmitImageGenerationTaskRequest request) { 71 String uri = PATH; 72 String reqBody = WXPayUtility.toJson(request); 73 74 Request.Builder reqBuilder = new Request.Builder().url(HOST + uri); 75 reqBuilder.addHeader("Accept", "application/json"); 76 reqBuilder.addHeader("Wechatpay-Serial", wechatPayPublicKeyId); 77 reqBuilder.addHeader("Authorization", WXPayUtility.buildAuthorization(mchid, certificateSerialNo,privateKey, METHOD, uri, reqBody)); 78 reqBuilder.addHeader("Content-Type", "application/json"); 79 RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), reqBody); 80 reqBuilder.method(METHOD, requestBody); 81 Request httpRequest = reqBuilder.build(); 82 83 // 发送HTTP请求 84 OkHttpClient client = new OkHttpClient.Builder().build(); 85 try (Response httpResponse = client.newCall(httpRequest).execute()) { 86 String respBody = WXPayUtility.extractBody(httpResponse); 87 if (httpResponse.code() >= 200 && httpResponse.code() < 300) { 88 // 2XX 成功,验证应答签名 89 WXPayUtility.validateResponse(this.wechatPayPublicKeyId, this.wechatPayPublicKey, 90 httpResponse.headers(), respBody); 91 92 // 从HTTP应答报文构建返回数据 93 return WXPayUtility.fromJson(respBody, SubmitImageGenerationTaskResponse.class); 94 } else { 95 throw new WXPayUtility.ApiException(httpResponse.code(), respBody, httpResponse.headers()); 96 } 97 } catch (IOException e) { 98 throw new UncheckedIOException("Sending request to " + uri + " failed.", e); 99 } 100 } 101 102 private final String mchid; 103 private final String certificateSerialNo; 104 private final PrivateKey privateKey; 105 private final String wechatPayPublicKeyId; 106 private final PublicKey wechatPayPublicKey; 107 108 public SubmitImageGenerationTask(String mchid, String certificateSerialNo, String privateKeyFilePath, String wechatPayPublicKeyId, String wechatPayPublicKeyFilePath) { 109 this.mchid = mchid; 110 this.certificateSerialNo = certificateSerialNo; 111 this.privateKey = WXPayUtility.loadPrivateKeyFromPath(privateKeyFilePath); 112 this.wechatPayPublicKeyId = wechatPayPublicKeyId; 113 this.wechatPayPublicKey = WXPayUtility.loadPublicKeyFromPath(wechatPayPublicKeyFilePath); 114 } 115 116 public static class SubmitImageGenerationTaskRequest { 117 @SerializedName("brand_id") 118 public String brandId; 119 120 @SerializedName("task_id") 121 public String taskId; 122 123 @SerializedName("image_generation_type") 124 public ImageGenerationType imageGenerationType; 125 126 @SerializedName("combine_image") 127 public CombineImage combineImage; 128 129 @SerializedName("cut_out") 130 public CutOut cutOut; 131 } 132 133 public static class SubmitImageGenerationTaskResponse { 134 @SerializedName("task_id") 135 public String taskId; 136 137 @SerializedName("brand_id") 138 public String brandId; 139 } 140 141 public enum ImageGenerationType { 142 @SerializedName("COMBINE_IMAGE") 143 COMBINE_IMAGE, 144 @SerializedName("CUT_OUT") 145 CUT_OUT 146 } 147 148 public static class CombineImage { 149 @SerializedName("scope") 150 public ProductCouponScope scope; 151 152 @SerializedName("type") 153 public ProductCouponType type; 154 155 @SerializedName("usage_mode") 156 public UsageMode usageMode; 157 158 @SerializedName("normal_coupon") 159 public NormalCouponUsageRule normalCoupon; 160 161 @SerializedName("discount_coupon") 162 public DiscountCouponUsageRule discountCoupon; 163 164 @SerializedName("exchange_coupon") 165 public ExchangeCouponUsageRule exchangeCoupon; 166 167 @SerializedName("background_color") 168 public String backgroundColor; 169 } 170 171 public static class CutOut { 172 @SerializedName("image_url") 173 public String imageUrl; 174 } 175 176 public enum ProductCouponScope { 177 @SerializedName("ALL") 178 ALL, 179 @SerializedName("SINGLE") 180 SINGLE 181 } 182 183 public enum ProductCouponType { 184 @SerializedName("NORMAL") 185 NORMAL, 186 @SerializedName("DISCOUNT") 187 DISCOUNT, 188 @SerializedName("EXCHANGE") 189 EXCHANGE 190 } 191 192 public enum UsageMode { 193 @SerializedName("SINGLE") 194 SINGLE, 195 @SerializedName("PROGRESSIVE_BUNDLE") 196 PROGRESSIVE_BUNDLE 197 } 198 199 public static class NormalCouponUsageRule { 200 @SerializedName("threshold") 201 public Long threshold; 202 203 @SerializedName("discount_amount") 204 public Long discountAmount; 205 } 206 207 public static class DiscountCouponUsageRule { 208 @SerializedName("threshold") 209 public Long threshold; 210 211 @SerializedName("percent_off") 212 public Long percentOff; 213 } 214 215 public static class ExchangeCouponUsageRule { 216 @SerializedName("threshold") 217 public Long threshold; 218 219 @SerializedName("exchange_price") 220 public Long exchangePrice; 221 } 222 223} 224
需配合微信支付工具库 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) 11 12func main() { 13 // TODO: 请准备商户开发必要参数,参考:https://pay.weixin.qq.com/doc/v3/partner/4013080340 14 config, err := wxpay_utility.CreateMchConfig( 15 "19xxxxxxxx", // 商户号,是由微信支付系统生成并分配给每个商户的唯一标识符,商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340 16 "1DDE55AD98Exxxxxxxxxx", // 商户API证书序列号,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924 17 "/path/to/apiclient_key.pem", // 商户API证书私钥文件路径,本地文件路径 18 "PUB_KEY_ID_xxxxxxxxxxxxx", // 微信支付公钥ID,如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589 19 "/path/to/wxp_pub.pem", // 微信支付公钥文件路径,本地文件路径 20 ) 21 if err != nil { 22 fmt.Println(err) 23 return 24 } 25 26 request := &SubmitImageGenerationTaskRequest{ 27 BrandId: wxpay_utility.String("120344"), 28 TaskId: wxpay_utility.String("image_generation_task_1"), 29 ImageGenerationType: IMAGEGENERATIONTYPE_COMBINE_IMAGE.Ptr(), 30 CombineImage: &CombineImage{ 31 Scope: PRODUCTCOUPONSCOPE_ALL.Ptr(), 32 Type: PRODUCTCOUPONTYPE_NORMAL.Ptr(), 33 UsageMode: USAGEMODE_SINGLE.Ptr(), 34 NormalCoupon: &NormalCouponUsageRule{ 35 Threshold: wxpay_utility.Int64(10000), 36 DiscountAmount: wxpay_utility.Int64(100), 37 }, 38 DiscountCoupon: &DiscountCouponUsageRule{ 39 Threshold: wxpay_utility.Int64(10000), 40 PercentOff: wxpay_utility.Int64(30), 41 }, 42 ExchangeCoupon: &ExchangeCouponUsageRule{ 43 Threshold: wxpay_utility.Int64(10000), 44 ExchangePrice: wxpay_utility.Int64(100), 45 }, 46 BackgroundColor: wxpay_utility.String("#ff5733"), 47 }, 48 CutOut: &CutOut{ 49 ImageUrl: wxpay_utility.String("https://wxpaylogo.qpic.cn/wxpaylogo/example.jpg"), 50 }, 51 } 52 53 response, err := SubmitImageGenerationTask(config, request) 54 if err != nil { 55 fmt.Printf("请求失败: %+v\n", err) 56 // TODO: 请求失败,根据状态码执行不同的处理 57 return 58 } 59 60 // TODO: 请求成功,继续业务逻辑 61 fmt.Printf("请求成功: %+v\n", response) 62} 63 64func SubmitImageGenerationTask(config *wxpay_utility.MchConfig, request *SubmitImageGenerationTaskRequest) (response *SubmitImageGenerationTaskResponse, err error) { 65 const ( 66 host = "https://api.mch.weixin.qq.com" 67 method = "POST" 68 path = "/v3/marketing/partner/product-coupon/image-generation-tasks" 69 ) 70 71 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path)) 72 if err != nil { 73 return nil, err 74 } 75 reqBody, err := json.Marshal(request) 76 if err != nil { 77 return nil, err 78 } 79 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody)) 80 if err != nil { 81 return nil, err 82 } 83 httpRequest.Header.Set("Accept", "application/json") 84 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId()) 85 httpRequest.Header.Set("Content-Type", "application/json") 86 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody) 87 if err != nil { 88 return nil, err 89 } 90 httpRequest.Header.Set("Authorization", authorization) 91 92 client := &http.Client{} 93 httpResponse, err := client.Do(httpRequest) 94 if err != nil { 95 return nil, err 96 } 97 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse) 98 if err != nil { 99 return nil, err 100 } 101 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 { 102 // 2XX 成功,验证应答签名 103 err = wxpay_utility.ValidateResponse( 104 config.WechatPayPublicKeyId(), 105 config.WechatPayPublicKey(), 106 &httpResponse.Header, 107 respBody, 108 ) 109 if err != nil { 110 return nil, err 111 } 112 response := &SubmitImageGenerationTaskResponse{} 113 if err := json.Unmarshal(respBody, response); err != nil { 114 return nil, err 115 } 116 117 return response, nil 118 } else { 119 return nil, wxpay_utility.NewApiException( 120 httpResponse.StatusCode, 121 httpResponse.Header, 122 respBody, 123 ) 124 } 125} 126 127type SubmitImageGenerationTaskRequest struct { 128 BrandId *string `json:"brand_id,omitempty"` 129 TaskId *string `json:"task_id,omitempty"` 130 ImageGenerationType *ImageGenerationType `json:"image_generation_type,omitempty"` 131 CombineImage *CombineImage `json:"combine_image,omitempty"` 132 CutOut *CutOut `json:"cut_out,omitempty"` 133} 134 135type SubmitImageGenerationTaskResponse struct { 136 TaskId *string `json:"task_id,omitempty"` 137 BrandId *string `json:"brand_id,omitempty"` 138} 139 140type ImageGenerationType string 141 142func (e ImageGenerationType) Ptr() *ImageGenerationType { 143 return &e 144} 145 146const ( 147 IMAGEGENERATIONTYPE_COMBINE_IMAGE ImageGenerationType = "COMBINE_IMAGE" 148 IMAGEGENERATIONTYPE_CUT_OUT ImageGenerationType = "CUT_OUT" 149) 150 151type CombineImage struct { 152 Scope *ProductCouponScope `json:"scope,omitempty"` 153 Type *ProductCouponType `json:"type,omitempty"` 154 UsageMode *UsageMode `json:"usage_mode,omitempty"` 155 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"` 156 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"` 157 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"` 158 BackgroundColor *string `json:"background_color,omitempty"` 159} 160 161type CutOut struct { 162 ImageUrl *string `json:"image_url,omitempty"` 163} 164 165type ProductCouponScope string 166 167func (e ProductCouponScope) Ptr() *ProductCouponScope { 168 return &e 169} 170 171const ( 172 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL" 173 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE" 174) 175 176type ProductCouponType string 177 178func (e ProductCouponType) Ptr() *ProductCouponType { 179 return &e 180} 181 182const ( 183 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL" 184 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT" 185 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE" 186) 187 188type UsageMode string 189 190func (e UsageMode) Ptr() *UsageMode { 191 return &e 192} 193 194const ( 195 USAGEMODE_SINGLE UsageMode = "SINGLE" 196 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE" 197) 198 199type NormalCouponUsageRule struct { 200 Threshold *int64 `json:"threshold,omitempty"` 201 DiscountAmount *int64 `json:"discount_amount,omitempty"` 202} 203 204type DiscountCouponUsageRule struct { 205 Threshold *int64 `json:"threshold,omitempty"` 206 PercentOff *int64 `json:"percent_off,omitempty"` 207} 208 209type ExchangeCouponUsageRule struct { 210 Threshold *int64 `json:"threshold,omitempty"` 211 ExchangePrice *int64 `json:"exchange_price,omitempty"` 212} 213
应答参数
200 OK
task_id 必填 string
【图片生成的任务ID】 服务商创建图片生成任务的请求流水号,服务商侧需保持唯一性,可使用 数字、大小写字母、下划线_、短横线- 组成,长度在6-40个字符之间
brand_id 必填 string
【品牌ID】 微信支付为品牌方分配的唯一标识,该品牌应与服务商存在授权关系
应答示例
200 OK
1{ 2 "task_id" : "image_generation_task_1", 3 "brand_id" : "120344" 4} 5
错误码
以下是本接口返回的错误码列表。详细错误码规则,请参考微信支付接口规则-错误码和错误提示

