
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_brand_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10)
11
12func main() {
13
14 config, err := wxpay_brand_utility.CreateBrandConfig(
15 "xxxxxxxx",
16 "1DDE55AD98Exxxxxxxxxx",
17 "/path/to/apiclient_key.pem",
18 "PUB_KEY_ID_xxxxxxxxxxxxx",
19 "/path/to/wxp_pub.pem",
20 )
21 if err != nil {
22 fmt.Println(err)
23 return
24 }
25
26 request := &SubmitImageGenerationTaskRequest{
27 TaskId: wxpay_brand_utility.String("image_generation_task_1"),
28 ImageGenerationType: IMAGEGENERATIONTYPE_COMBINE_IMAGE.Ptr(),
29 CombineImage: &CombineImage{
30 Scope: PRODUCTCOUPONSCOPE_ALL.Ptr(),
31 Type: PRODUCTCOUPONTYPE_NORMAL.Ptr(),
32 UsageMode: USAGEMODE_SINGLE.Ptr(),
33 NormalCoupon: &NormalCouponUsageRule{
34 Threshold: wxpay_brand_utility.Int64(10000),
35 DiscountAmount: wxpay_brand_utility.Int64(100),
36 },
37 DiscountCoupon: &DiscountCouponUsageRule{
38 Threshold: wxpay_brand_utility.Int64(10000),
39 PercentOff: wxpay_brand_utility.Int64(30),
40 },
41 ExchangeCoupon: &ExchangeCouponUsageRule{
42 Threshold: wxpay_brand_utility.Int64(10000),
43 ExchangePrice: wxpay_brand_utility.Int64(100),
44 },
45 BackgroundColor: wxpay_brand_utility.String("#ff5733"),
46 },
47 CutOut: &CutOut{
48 ImageUrl: wxpay_brand_utility.String("https://wxpaylogo.qpic.cn/wxpaylogo/example.jpg"),
49 },
50 }
51
52 response, err := SubmitImageGenerationTask(config, request)
53 if err != nil {
54 fmt.Printf("请求失败: %+v\n", err)
55
56 return
57 }
58
59
60 fmt.Printf("请求成功: %+v\n", response)
61}
62
63func SubmitImageGenerationTask(config *wxpay_brand_utility.BrandConfig, request *SubmitImageGenerationTaskRequest) (response *SubmitImageGenerationTaskResponse, err error) {
64 const (
65 host = "https://api.mch.weixin.qq.com"
66 method = "POST"
67 path = "/brand/marketing/product-coupon/image-generation-tasks"
68 )
69
70 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
71 if err != nil {
72 return nil, err
73 }
74 reqBody, err := json.Marshal(request)
75 if err != nil {
76 return nil, err
77 }
78 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
79 if err != nil {
80 return nil, err
81 }
82 httpRequest.Header.Set("Accept", "application/json")
83 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
84 httpRequest.Header.Set("Content-Type", "application/json")
85 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
86 if err != nil {
87 return nil, err
88 }
89 httpRequest.Header.Set("Authorization", authorization)
90
91 client := &http.Client{}
92 httpResponse, err := client.Do(httpRequest)
93 if err != nil {
94 return nil, err
95 }
96 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
97 if err != nil {
98 return nil, err
99 }
100 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
101
102 err = wxpay_brand_utility.ValidateResponse(
103 config.WechatPayPublicKeyId(),
104 config.WechatPayPublicKey(),
105 &httpResponse.Header,
106 respBody,
107 )
108 if err != nil {
109 return nil, err
110 }
111 response := &SubmitImageGenerationTaskResponse{}
112 if err := json.Unmarshal(respBody, response); err != nil {
113 return nil, err
114 }
115
116 return response, nil
117 } else {
118 return nil, wxpay_brand_utility.NewApiException(
119 httpResponse.StatusCode,
120 httpResponse.Header,
121 respBody,
122 )
123 }
124}
125
126type SubmitImageGenerationTaskRequest struct {
127 TaskId *string `json:"task_id,omitempty"`
128 ImageGenerationType *ImageGenerationType `json:"image_generation_type,omitempty"`
129 CombineImage *CombineImage `json:"combine_image,omitempty"`
130 CutOut *CutOut `json:"cut_out,omitempty"`
131}
132
133type SubmitImageGenerationTaskResponse struct {
134 TaskId *string `json:"task_id,omitempty"`
135}
136
137type ImageGenerationType string
138
139func (e ImageGenerationType) Ptr() *ImageGenerationType {
140 return &e
141}
142
143const (
144 IMAGEGENERATIONTYPE_COMBINE_IMAGE ImageGenerationType = "COMBINE_IMAGE"
145 IMAGEGENERATIONTYPE_CUT_OUT ImageGenerationType = "CUT_OUT"
146)
147
148type CombineImage struct {
149 Scope *ProductCouponScope `json:"scope,omitempty"`
150 Type *ProductCouponType `json:"type,omitempty"`
151 UsageMode *UsageMode `json:"usage_mode,omitempty"`
152 NormalCoupon *NormalCouponUsageRule `json:"normal_coupon,omitempty"`
153 DiscountCoupon *DiscountCouponUsageRule `json:"discount_coupon,omitempty"`
154 ExchangeCoupon *ExchangeCouponUsageRule `json:"exchange_coupon,omitempty"`
155 BackgroundColor *string `json:"background_color,omitempty"`
156}
157
158type CutOut struct {
159 ImageUrl *string `json:"image_url,omitempty"`
160}
161
162type ProductCouponScope string
163
164func (e ProductCouponScope) Ptr() *ProductCouponScope {
165 return &e
166}
167
168const (
169 PRODUCTCOUPONSCOPE_ALL ProductCouponScope = "ALL"
170 PRODUCTCOUPONSCOPE_SINGLE ProductCouponScope = "SINGLE"
171)
172
173type ProductCouponType string
174
175func (e ProductCouponType) Ptr() *ProductCouponType {
176 return &e
177}
178
179const (
180 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
181 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
182 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
183)
184
185type UsageMode string
186
187func (e UsageMode) Ptr() *UsageMode {
188 return &e
189}
190
191const (
192 USAGEMODE_SINGLE UsageMode = "SINGLE"
193 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
194)
195
196type NormalCouponUsageRule struct {
197 Threshold *int64 `json:"threshold,omitempty"`
198 DiscountAmount *int64 `json:"discount_amount,omitempty"`
199}
200
201type DiscountCouponUsageRule struct {
202 Threshold *int64 `json:"threshold,omitempty"`
203 PercentOff *int64 `json:"percent_off,omitempty"`
204}
205
206type ExchangeCouponUsageRule struct {
207 Threshold *int64 `json:"threshold,omitempty"`
208 ExchangePrice *int64 `json:"exchange_price,omitempty"`
209}
210