
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9)
10
11func main() {
12
13 config, err := wxpay_utility.CreateMchConfig(
14 "19xxxxxxxx",
15 "1DDE55AD98Exxxxxxxxxx",
16 "/path/to/apiclient_key.pem",
17 "PUB_KEY_ID_xxxxxxxxxxxxx",
18 "/path/to/wxp_pub.pem",
19 )
20 if err != nil {
21 fmt.Println(err)
22 return
23 }
24
25 request := &ListActivitiesRequest{
26 Offset: wxpay_utility.Int64(1),
27 Limit: wxpay_utility.Int64(20),
28 ActivityName: wxpay_utility.String("良品铺子回馈活动"),
29 ActivityStatus: ACTSTATUS_ACT_STATUS_UNKNOWN.Ptr(),
30 AwardType: AWARDTYPE_BUSIFAVOR.Ptr(),
31 }
32
33 response, err := ListActivities(config, request)
34 if err != nil {
35 fmt.Printf("请求失败: %+v\n", err)
36
37 return
38 }
39
40
41 fmt.Printf("请求成功: %+v\n", response)
42}
43
44func ListActivities(config *wxpay_utility.MchConfig, request *ListActivitiesRequest) (response *ListActivitiesResponse, err error) {
45 const (
46 host = "https://api.mch.weixin.qq.com"
47 method = "GET"
48 path = "/v3/marketing/paygiftactivity/activities"
49 )
50
51 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
52 if err != nil {
53 return nil, err
54 }
55 query := reqUrl.Query()
56 if request.Offset != nil {
57 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
58 }
59 if request.Limit != nil {
60 query.Add("limit", fmt.Sprintf("%v", *request.Limit))
61 }
62 if request.ActivityName != nil {
63 query.Add("activity_name", *request.ActivityName)
64 }
65 if request.ActivityStatus != nil {
66 query.Add("activity_status", fmt.Sprintf("%v", *request.ActivityStatus))
67 }
68 if request.AwardType != nil {
69 query.Add("award_type", fmt.Sprintf("%v", *request.AwardType))
70 }
71 reqUrl.RawQuery = query.Encode()
72 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
73 if err != nil {
74 return nil, err
75 }
76 httpRequest.Header.Set("Accept", "application/json")
77 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
78 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
79 if err != nil {
80 return nil, err
81 }
82 httpRequest.Header.Set("Authorization", authorization)
83
84 client := &http.Client{}
85 httpResponse, err := client.Do(httpRequest)
86 if err != nil {
87 return nil, err
88 }
89 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
90 if err != nil {
91 return nil, err
92 }
93 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
94
95 err = wxpay_utility.ValidateResponse(
96 config.WechatPayPublicKeyId(),
97 config.WechatPayPublicKey(),
98 &httpResponse.Header,
99 respBody,
100 )
101 if err != nil {
102 return nil, err
103 }
104 response := &ListActivitiesResponse{}
105 if err := json.Unmarshal(respBody, response); err != nil {
106 return nil, err
107 }
108
109 return response, nil
110 } else {
111 return nil, wxpay_utility.NewApiException(
112 httpResponse.StatusCode,
113 httpResponse.Header,
114 respBody,
115 )
116 }
117}
118
119type ListActivitiesRequest struct {
120 Offset *int64 `json:"offset,omitempty"`
121 Limit *int64 `json:"limit,omitempty"`
122 ActivityName *string `json:"activity_name,omitempty"`
123 ActivityStatus *ActStatus `json:"activity_status,omitempty"`
124 AwardType *AwardType `json:"award_type,omitempty"`
125}
126
127func (o *ListActivitiesRequest) MarshalJSON() ([]byte, error) {
128 type Alias ListActivitiesRequest
129 a := &struct {
130 Offset *int64 `json:"offset,omitempty"`
131 Limit *int64 `json:"limit,omitempty"`
132 ActivityName *string `json:"activity_name,omitempty"`
133 ActivityStatus *ActStatus `json:"activity_status,omitempty"`
134 AwardType *AwardType `json:"award_type,omitempty"`
135 *Alias
136 }{
137
138 Offset: nil,
139 Limit: nil,
140 ActivityName: nil,
141 ActivityStatus: nil,
142 AwardType: nil,
143 Alias: (*Alias)(o),
144 }
145 return json.Marshal(a)
146}
147
148type ListActivitiesResponse struct {
149 Data []ActivityInformation `json:"data,omitempty"`
150 TotalCount *int64 `json:"total_count,omitempty"`
151 Offset *int64 `json:"offset,omitempty"`
152 Limit *int64 `json:"limit,omitempty"`
153}
154
155type ActStatus string
156
157func (e ActStatus) Ptr() *ActStatus {
158 return &e
159}
160
161const (
162 ACTSTATUS_ACT_STATUS_UNKNOWN ActStatus = "ACT_STATUS_UNKNOWN"
163 ACTSTATUS_CREATE_ACT_STATUS ActStatus = "CREATE_ACT_STATUS"
164 ACTSTATUS_ONGOING_ACT_STATUS ActStatus = "ONGOING_ACT_STATUS"
165 ACTSTATUS_TERMINATE_ACT_STATUS ActStatus = "TERMINATE_ACT_STATUS"
166 ACTSTATUS_STOP_ACT_STATUS ActStatus = "STOP_ACT_STATUS"
167 ACTSTATUS_OVER_TIME_ACT_STATUS ActStatus = "OVER_TIME_ACT_STATUS"
168 ACTSTATUS_CREATE_ACT_FAILED ActStatus = "CREATE_ACT_FAILED"
169)
170
171type AwardType string
172
173func (e AwardType) Ptr() *AwardType {
174 return &e
175}
176
177const (
178 AWARDTYPE_BUSIFAVOR AwardType = "BUSIFAVOR"
179)
180
181type ActivityInformation struct {
182 ActivityId *string `json:"activity_id,omitempty"`
183 ActivityType *ActType `json:"activity_type,omitempty"`
184 ActivityBaseInfo *ActBaseInfo `json:"activity_base_info,omitempty"`
185 AwardSendRule *AwardSendRule `json:"award_send_rule,omitempty"`
186 AdvancedSetting *ActAdvancedSetting `json:"advanced_setting,omitempty"`
187 ActivityStatus *ActStatus `json:"activity_status,omitempty"`
188 CreatorMerchantId *string `json:"creator_merchant_id,omitempty"`
189 BelongMerchantId *string `json:"belong_merchant_id,omitempty"`
190 CreateTime *string `json:"create_time,omitempty"`
191 UpdateTime *string `json:"update_time,omitempty"`
192}
193
194type ActType string
195
196func (e ActType) Ptr() *ActType {
197 return &e
198}
199
200const (
201 ACTTYPE_FULL_SEND_ACT_TYPE ActType = "FULL_SEND_ACT_TYPE"
202 ACTTYPE_STEP_SEND_ACT_TYPE ActType = "STEP_SEND_ACT_TYPE"
203 ACTTYPE_SPECIFIC_SEND_ACT_TYPE ActType = "SPECIFIC_SEND_ACT_TYPE"
204)
205
206type ActBaseInfo struct {
207 ActivityName *string `json:"activity_name,omitempty"`
208 ActivitySecondTitle *string `json:"activity_second_title,omitempty"`
209 MerchantLogoUrl *string `json:"merchant_logo_url,omitempty"`
210 BackgroundColor *string `json:"background_color,omitempty"`
211 BeginTime *string `json:"begin_time,omitempty"`
212 EndTime *string `json:"end_time,omitempty"`
213 AvailablePeriods *AvailablePeriod `json:"available_periods,omitempty"`
214 OutRequestNo *string `json:"out_request_no,omitempty"`
215 DeliveryPurpose *DeliveryPurposeCategory `json:"delivery_purpose,omitempty"`
216 MiniProgramsAppid *string `json:"mini_programs_appid,omitempty"`
217 MiniProgramsPath *string `json:"mini_programs_path,omitempty"`
218}
219
220type AwardSendRule struct {
221 FullSendRule *FullSendRule `json:"full_send_rule,omitempty"`
222}
223
224type ActAdvancedSetting struct {
225 DeliveryUserCategory *DeliveryUserCategory `json:"delivery_user_category,omitempty"`
226 MerchantMemberAppid *string `json:"merchant_member_appid,omitempty"`
227 PaymentMode *PaymentMode `json:"payment_mode,omitempty"`
228 PaymentMethodInformation *PaymentMethodInfo `json:"payment_method_information,omitempty"`
229 GoodsTags []string `json:"goods_tags,omitempty"`
230}
231
232type AvailablePeriod struct {
233 AvailableTime []AvailableTime `json:"available_time,omitempty"`
234 AvailableDayTime []AvailableDayTime `json:"available_day_time,omitempty"`
235}
236
237type DeliveryPurposeCategory string
238
239func (e DeliveryPurposeCategory) Ptr() *DeliveryPurposeCategory {
240 return &e
241}
242
243const (
244 DELIVERYPURPOSECATEGORY_OFF_LINE_PAY DeliveryPurposeCategory = "OFF_LINE_PAY"
245 DELIVERYPURPOSECATEGORY_JUMP_MINI_APP DeliveryPurposeCategory = "JUMP_MINI_APP"
246)
247
248type FullSendRule struct {
249 TransactionAmountMinimum *int64 `json:"transaction_amount_minimum,omitempty"`
250 SendContent *SendContentCategory `json:"send_content,omitempty"`
251 AwardType *AwardType `json:"award_type,omitempty"`
252 AwardList []AwardBaseInfo `json:"award_list,omitempty"`
253 MerchantOption *SendMerchantOption `json:"merchant_option,omitempty"`
254 MerchantIdList []string `json:"merchant_id_list,omitempty"`
255}
256
257type DeliveryUserCategory string
258
259func (e DeliveryUserCategory) Ptr() *DeliveryUserCategory {
260 return &e
261}
262
263const (
264 DELIVERYUSERCATEGORY_DELIVERY_ALL_PERSON DeliveryUserCategory = "DELIVERY_ALL_PERSON"
265 DELIVERYUSERCATEGORY_DELIVERY_MEMBER_PERSON DeliveryUserCategory = "DELIVERY_MEMBER_PERSON"
266)
267
268type PaymentMode struct {
269 PaymentSceneList []PaymentScene `json:"payment_scene_list,omitempty"`
270}
271
272type PaymentMethodInfo struct {
273 PaymentMethod *PaymentMethodCategory `json:"payment_method,omitempty"`
274 BankAbbreviation *string `json:"bank_abbreviation,omitempty"`
275}
276
277type AvailableTime struct {
278 BeginTime *string `json:"begin_time,omitempty"`
279 EndTime *string `json:"end_time,omitempty"`
280}
281
282type AvailableDayTime struct {
283 BeginDayTime *string `json:"begin_day_time,omitempty"`
284 EndDayTime *string `json:"end_day_time,omitempty"`
285}
286
287type SendContentCategory string
288
289func (e SendContentCategory) Ptr() *SendContentCategory {
290 return &e
291}
292
293const (
294 SENDCONTENTCATEGORY_SINGLE_COUPON SendContentCategory = "SINGLE_COUPON"
295 SENDCONTENTCATEGORY_GIFT_PACKAGE SendContentCategory = "GIFT_PACKAGE"
296)
297
298type AwardBaseInfo struct {
299 StockId *string `json:"stock_id,omitempty"`
300 OriginalImageUrl *string `json:"original_image_url,omitempty"`
301 ThumbnailUrl *string `json:"thumbnail_url,omitempty"`
302}
303
304type SendMerchantOption string
305
306func (e SendMerchantOption) Ptr() *SendMerchantOption {
307 return &e
308}
309
310const (
311 SENDMERCHANTOPTION_IN_SEVICE_COUPON_MERCHANT SendMerchantOption = "IN_SEVICE_COUPON_MERCHANT"
312 SENDMERCHANTOPTION_MANUAL_INPUT_MERCHANT SendMerchantOption = "MANUAL_INPUT_MERCHANT"
313)
314
315type PaymentScene string
316
317func (e PaymentScene) Ptr() *PaymentScene {
318 return &e
319}
320
321const (
322 PAYMENTSCENE_APP_SCENE PaymentScene = "APP_SCENE"
323 PAYMENTSCENE_SWING_CARD_SCENE PaymentScene = "SWING_CARD_SCENE"
324 PAYMENTSCENE_NO_SECRET_SCENE PaymentScene = "NO_SECRET_SCENE"
325 PAYMENTSCENE_MINIAPP_SCENE PaymentScene = "MINIAPP_SCENE"
326 PAYMENTSCENE_FACE_PAY_SCENE PaymentScene = "FACE_PAY_SCENE"
327 PAYMENTSCENE_OTHER_SCENE PaymentScene = "OTHER_SCENE"
328)
329
330type PaymentMethodCategory string
331
332func (e PaymentMethodCategory) Ptr() *PaymentMethodCategory {
333 return &e
334}
335
336const (
337 PAYMENTMETHODCATEGORY_CFT PaymentMethodCategory = "CFT"
338 PAYMENTMETHODCATEGORY_SPECIFIC_BANK_CARD PaymentMethodCategory = "SPECIFIC_BANK_CARD"
339)
340