
1package main
2
3import (
4 "demo/wxpay_brand_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9)
10
11func main() {
12
13 config, err := wxpay_brand_utility.CreateBrandConfig(
14 "xxxxxxxx",
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 := &ListDeliveryPlansRequest{
26 PageSize: wxpay_brand_utility.Int64(5),
27 Offset: wxpay_brand_utility.Int64(10),
28 PlanState: PLANSTATE_CREATED.Ptr(),
29 AuditState: PLANAUDITSTATE_AUDIT_INITIAL.Ptr(),
30 PlanId: wxpay_brand_utility.String("12000"),
31 }
32
33 response, err := ListDeliveryPlans(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 ListDeliveryPlans(config *wxpay_brand_utility.BrandConfig, request *ListDeliveryPlansRequest) (response *ListDeliveryPlansResponse, err error) {
45 const (
46 host = "https://api.mch.weixin.qq.com"
47 method = "GET"
48 path = "/brand/marketing/delivery-plan/delivery-plans"
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.PageSize != nil {
57 query.Add("page_size", fmt.Sprintf("%v", *request.PageSize))
58 }
59 if request.Offset != nil {
60 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
61 }
62 if request.PlanState != nil {
63 query.Add("plan_state", fmt.Sprintf("%v", *request.PlanState))
64 }
65 if request.AuditState != nil {
66 query.Add("audit_state", fmt.Sprintf("%v", *request.AuditState))
67 }
68 if request.PlanId != nil {
69 query.Add("plan_id", *request.PlanId)
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_brand_utility.BuildAuthorization(config.BrandId(), 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_brand_utility.ExtractResponseBody(httpResponse)
90 if err != nil {
91 return nil, err
92 }
93 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
94
95 err = wxpay_brand_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 := &ListDeliveryPlansResponse{}
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_brand_utility.NewApiException(
112 httpResponse.StatusCode,
113 httpResponse.Header,
114 respBody,
115 )
116 }
117}
118
119type ListDeliveryPlansRequest struct {
120 PageSize *int64 `json:"page_size,omitempty"`
121 Offset *int64 `json:"offset,omitempty"`
122 PlanId *string `json:"plan_id,omitempty"`
123 PlanState *PlanState `json:"plan_state,omitempty"`
124 AuditState *PlanAuditState `json:"audit_state,omitempty"`
125}
126
127func (o *ListDeliveryPlansRequest) MarshalJSON() ([]byte, error) {
128 type Alias ListDeliveryPlansRequest
129 a := &struct {
130 PageSize *int64 `json:"page_size,omitempty"`
131 Offset *int64 `json:"offset,omitempty"`
132 PlanId *string `json:"plan_id,omitempty"`
133 PlanState *PlanState `json:"plan_state,omitempty"`
134 AuditState *PlanAuditState `json:"audit_state,omitempty"`
135 *Alias
136 }{
137
138 PageSize: nil,
139 Offset: nil,
140 PlanId: nil,
141 PlanState: nil,
142 AuditState: nil,
143 Alias: (*Alias)(o),
144 }
145 return json.Marshal(a)
146}
147
148type ListDeliveryPlansResponse struct {
149 TotalCount *int64 `json:"total_count,omitempty"`
150 PlanList []DeliveryPlan `json:"plan_list,omitempty"`
151}
152
153type PlanState string
154
155func (e PlanState) Ptr() *PlanState {
156 return &e
157}
158
159const (
160 PLANSTATE_CREATED PlanState = "CREATED"
161 PLANSTATE_TERMINATED PlanState = "TERMINATED"
162 PLANSTATE_EXPIRED PlanState = "EXPIRED"
163 PLANSTATE_DELIVERING PlanState = "DELIVERING"
164 PLANSTATE_PAUSED PlanState = "PAUSED"
165)
166
167type PlanAuditState string
168
169func (e PlanAuditState) Ptr() *PlanAuditState {
170 return &e
171}
172
173const (
174 PLANAUDITSTATE_AUDIT_INITIAL PlanAuditState = "AUDIT_INITIAL"
175 PLANAUDITSTATE_AUDIT_PROCESSING PlanAuditState = "AUDIT_PROCESSING"
176 PLANAUDITSTATE_AUDIT_PASSED PlanAuditState = "AUDIT_PASSED"
177 PLANAUDITSTATE_AUDIT_REJECTED PlanAuditState = "AUDIT_REJECTED"
178)
179
180type DeliveryPlan struct {
181 PlanId *string `json:"plan_id,omitempty"`
182 PlanName *string `json:"plan_name,omitempty"`
183 PlanState *PlanState `json:"plan_state,omitempty"`
184 DeliveryStartTime *string `json:"delivery_start_time,omitempty"`
185 DeliveryEndTime *string `json:"delivery_end_time,omitempty"`
186 ProductCouponId *string `json:"product_coupon_id,omitempty"`
187 UsageMode *ProductUsageMode `json:"usage_mode,omitempty"`
188 StockId *string `json:"stock_id,omitempty"`
189 StockBundleId *string `json:"stock_bundle_id,omitempty"`
190 RecommendWord *string `json:"recommend_word,omitempty"`
191 TotalCount *int64 `json:"total_count,omitempty"`
192 UserLimit *int64 `json:"user_limit,omitempty"`
193 DailyLimit *int64 `json:"daily_limit,omitempty"`
194 ReuseCouponConfig *bool `json:"reuse_coupon_config,omitempty"`
195 BrandId *string `json:"brand_id,omitempty"`
196}
197
198type ProductUsageMode string
199
200func (e ProductUsageMode) Ptr() *ProductUsageMode {
201 return &e
202}
203
204const (
205 PRODUCTUSAGEMODE_SINGLE ProductUsageMode = "SINGLE"
206 PRODUCTUSAGEMODE_PROGRESSIVE_BUNDLE ProductUsageMode = "PROGRESSIVE_BUNDLE"
207)
208