
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10)
11
12func main() {
13
14 config, err := wxpay_utility.CreateMchConfig(
15 "19xxxxxxxx",
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 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
57 return
58 }
59
60
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
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 PRODUCTCOUPONSCOPE_CATEGORY ProductCouponScope = "CATEGORY"
175)
176
177type ProductCouponType string
178
179func (e ProductCouponType) Ptr() *ProductCouponType {
180 return &e
181}
182
183const (
184 PRODUCTCOUPONTYPE_NORMAL ProductCouponType = "NORMAL"
185 PRODUCTCOUPONTYPE_DISCOUNT ProductCouponType = "DISCOUNT"
186 PRODUCTCOUPONTYPE_EXCHANGE ProductCouponType = "EXCHANGE"
187)
188
189type UsageMode string
190
191func (e UsageMode) Ptr() *UsageMode {
192 return &e
193}
194
195const (
196 USAGEMODE_SINGLE UsageMode = "SINGLE"
197 USAGEMODE_PROGRESSIVE_BUNDLE UsageMode = "PROGRESSIVE_BUNDLE"
198)
199
200type NormalCouponUsageRule struct {
201 Threshold *int64 `json:"threshold,omitempty"`
202 DiscountAmount *int64 `json:"discount_amount,omitempty"`
203}
204
205type DiscountCouponUsageRule struct {
206 Threshold *int64 `json:"threshold,omitempty"`
207 PercentOff *int64 `json:"percent_off,omitempty"`
208}
209
210type ExchangeCouponUsageRule struct {
211 Threshold *int64 `json:"threshold,omitempty"`
212 ExchangePrice *int64 `json:"exchange_price,omitempty"`
213}
214