---
url: "https://pay.weixin.qq.com/doc/v3/partner/4017327752-go.md"
title: "提交图片生成任务-请求示例-go"
doc_id: 4017327752
example_lang: "go"
---

## Go


需配合微信支付工具库 wxpay\_utility 使用，请参考 [Go](https://pay.weixin.qq.com/doc/v3/partner/4015119446.md)

```
package main

import (
	"bytes"
	"demo/wxpay_utility" // 引用微信支付工具库，参考 https://pay.weixin.qq.com/doc/v3/partner/4015119446
	"encoding/json"
	"fmt"
	"net/http"
	"net/url"
)

func main() {
	// TODO: 请准备商户开发必要参数，参考：https://pay.weixin.qq.com/doc/v3/partner/4013080340
	config, err := wxpay_utility.CreateMchConfig(
		"19xxxxxxxx",                 // 商户号，是由微信支付系统生成并分配给每个商户的唯一标识符，商户号获取方式参考 https://pay.weixin.qq.com/doc/v3/partner/4013080340
		"1DDE55AD98Exxxxxxxxxx",      // 商户API证书序列号，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013058924
		"/path/to/apiclient_key.pem", // 商户API证书私钥文件路径，本地文件路径
		"PUB_KEY_ID_xxxxxxxxxxxxx",   // 微信支付公钥ID，如何获取请参考 https://pay.weixin.qq.com/doc/v3/partner/4013038589
		"/path/to/wxp_pub.pem",       // 微信支付公钥文件路径，本地文件路径
	)
	if err != nil {
		fmt.Println(err)
		return
	}

	request := &SubmitImageGenerationTaskRequest{
		BrandId:             wxpay_utility.String("120344"),
		TaskId:              wxpay_utility.String("image_generation_task_1"),
		ImageGenerationType: IMAGEGENERATIONTYPE_COMBINE_IMAGE.Ptr(),
		CombineImage: &CombineImage{
			Scope:     PRODUCTCOUPONSCOPE_ALL.Ptr(),
			Type:      PRODUCTCOUPONTYPE_NORMAL.Ptr(),
			UsageMode: USAGEMODE_SINGLE.Ptr(),
			NormalCoupon: &NormalCouponUsageRule{
				Threshold:      wxpay_utility.Int64(10000),
				DiscountAmount: wxpay_utility.Int64(100),
			},
			DiscountCoupon: &DiscountCouponUsageRule{
				Threshold:  wxpay_utility.Int64(10000),
				PercentOff: wxpay_utility.Int64(30),
			},
			ExchangeCoupon: &ExchangeCouponUsageRule{
				Threshold:     wxpay_utility.Int64(10000),
				ExchangePrice: wxpay_utility.Int64(100),
			},
			BackgroundColor: wxpay_utility.String("#ff5733"),
		},
		CutOut: &CutOut{
			ImageUrl: wxpay_utility.String("https://wxpaylogo.qpic.cn/wxpaylogo/example.jpg"),
		},
	}

	response, err := SubmitImageGenerationTask(config, request)
	if err != nil {
		fmt.Printf("请求失败: %+v\n", err)
		// TODO: 请求失败，根据状态码执行不同的处理
		return
	}

	// TODO: 请求成功，继续业务逻辑
	fmt.Printf("请求成功: %+v\n", response)
}

func SubmitImageGenerationTask(config *wxpay_utility.MchConfig, request *SubmitImageGenerationTaskRequest) (response *SubmitImageGenerationTaskResponse, err error) {
	const (
		host   = "https://api.mch.weixin.qq.com"
		method = "POST"
		path   = "/v3/marketing/partner/product-coupon/image-generation-tasks"
	)

	reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
	if err != nil {
		return nil, err
	}
	reqBody, err := json.Marshal(request)
	if err != nil {
		return nil, err
	}
	httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Accept", "application/json")
	httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
	httpRequest.Header.Set("Content-Type", "application/json")
	authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
	if err != nil {
		return nil, err
	}
	httpRequest.Header.Set("Authorization", authorization)

	client := &http.Client{}
	httpResponse, err := client.Do(httpRequest)
	if err != nil {
		return nil, err
	}
	respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
	if err != nil {
		return nil, err
	}
	if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
		// 2XX 成功，验证应答签名
		err = wxpay_utility.ValidateResponse(
			config.WechatPayPublicKeyId(),
			config.WechatPayPublicKey(),
			&httpResponse.Header,
			respBody,
		)
		if err != nil {
			return nil, err
		}
		response := &SubmitImageGenerationTaskResponse{}
		if err := json.Unmarshal(respBody, response); err != nil {
			return nil, err
		}

		return response, nil
	} else {
		return nil, wxpay_utility.NewApiException(
			httpResponse.StatusCode,
			httpResponse.Header,
			respBody,
		)
	}
}

type SubmitImageGenerationTaskRequest struct {
	BrandId             *string              `json:"brand_id,omitempty"`
	TaskId              *string              `json:"task_id,omitempty"`
	ImageGenerationType *ImageGenerationType `json:"image_generation_type,omitempty"`
	CombineImage        *CombineImage        `json:"combine_image,omitempty"`
	CutOut              *CutOut              `json:"cut_out,omitempty"`
}

type SubmitImageGenerationTaskResponse struct {
	TaskId  *string `json:"task_id,omitempty"`
	BrandId *string `json:"brand_id,omitempty"`
}

type ImageGenerationType string

func (e ImageGenerationType) Ptr() *ImageGenerationType {
	return &e
}

const (
	IMAGEGENERATIONTYPE_COMBINE_IMAGE ImageGenerationType = "COMBINE_IMAGE"
	IMAGEGENERATIONTYPE_CUT_OUT       ImageGenerationType = "CUT_OUT"
)

type CombineImage struct {
	Scope           *ProductCouponScope      `json:"scope,omitempty"`
	Type            *ProductCouponType       `json:"type,omitempty"`
	UsageMode       *UsageMode               `json:"usage_mode,omitempty"`
	NormalCoupon    *NormalCouponUsageRule   `json:"normal_coupon,omitempty"`
	DiscountCoupon  *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
	ExchangeCoupon  *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
	BackgroundColor *string                  `json:"background_color,omitempty"`
}

type CutOut struct {
	ImageUrl *string `json:"image_url,omitempty"`
}

type ProductCouponScope string

func (e ProductCouponScope) Ptr() *ProductCouponScope {
	return &e
}

const (
	PRODUCTCOUPONSCOPE_ALL      ProductCouponScope = "ALL"
	PRODUCTCOUPONSCOPE_SINGLE   ProductCouponScope = "SINGLE"
	PRODUCTCOUPONSCOPE_CATEGORY ProductCouponScope = "CATEGORY"
)

type ProductCouponType string

func (e ProductCouponType) Ptr() *ProductCouponType {
	return &e
}

const (
	PRODUCTCOUPONTYPE_NORMAL   ProductCouponType = "NORMAL"
	PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
	PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
)

type UsageMode string

func (e UsageMode) Ptr() *UsageMode {
	return &e
}

const (
	USAGEMODE_SINGLE             UsageMode = "SINGLE"
	USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
)

type NormalCouponUsageRule struct {
	Threshold      *int64 `json:"threshold,omitempty"`
	DiscountAmount *int64 `json:"discount_amount,omitempty"`
}

type DiscountCouponUsageRule struct {
	Threshold  *int64 `json:"threshold,omitempty"`
	PercentOff *int64 `json:"percent_off,omitempty"`
}

type ExchangeCouponUsageRule struct {
	Threshold     *int64 `json:"threshold,omitempty"`
	ExchangePrice *int64 `json:"exchange_price,omitempty"`
}

```
