
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
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 := &ListDeliveryPlansRequest{
27 BrandId: wxpay_utility.String("40016"),
28 PageSize: wxpay_utility.Int64(5),
29 Offset: wxpay_utility.Int64(10),
30 PlanState: PLANSTATE_CREATED.Ptr(),
31 AuditState: PLANAUDITSTATE_AUDIT_INITIAL.Ptr(),
32 PlanId: wxpay_utility.String("12000"),
33 }
34
35 response, err := ListDeliveryPlans(config, request)
36 if err != nil {
37 fmt.Printf("请求失败: %+v\n", err)
38
39 return
40 }
41
42
43 fmt.Printf("请求成功: %+v\n", response)
44}
45
46func ListDeliveryPlans(config *wxpay_utility.MchConfig, request *ListDeliveryPlansRequest) (response *ListDeliveryPlansResponse, err error) {
47 const (
48 host = "https://api.mch.weixin.qq.com"
49 method = "GET"
50 path = "/v3/marketing/partner/delivery-plan/delivery-plans/{brand_id}/delivery-plans"
51 )
52
53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
54 if err != nil {
55 return nil, err
56 }
57 reqUrl.Path = strings.Replace(reqUrl.Path, "{brand_id}", url.PathEscape(*request.BrandId), -1)
58 query := reqUrl.Query()
59 if request.PageSize != nil {
60 query.Add("page_size", fmt.Sprintf("%v", *request.PageSize))
61 }
62 if request.Offset != nil {
63 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
64 }
65 if request.PlanState != nil {
66 query.Add("plan_state", fmt.Sprintf("%v", *request.PlanState))
67 }
68 if request.AuditState != nil {
69 query.Add("audit_state", fmt.Sprintf("%v", *request.AuditState))
70 }
71 if request.PlanId != nil {
72 query.Add("plan_id", *request.PlanId)
73 }
74 reqUrl.RawQuery = query.Encode()
75 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
76 if err != nil {
77 return nil, err
78 }
79 httpRequest.Header.Set("Accept", "application/json")
80 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
81 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
82 if err != nil {
83 return nil, err
84 }
85 httpRequest.Header.Set("Authorization", authorization)
86
87 client := &http.Client{}
88 httpResponse, err := client.Do(httpRequest)
89 if err != nil {
90 return nil, err
91 }
92 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
93 if err != nil {
94 return nil, err
95 }
96 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
97
98 err = wxpay_utility.ValidateResponse(
99 config.WechatPayPublicKeyId(),
100 config.WechatPayPublicKey(),
101 &httpResponse.Header,
102 respBody,
103 )
104 if err != nil {
105 return nil, err
106 }
107 response := &ListDeliveryPlansResponse{}
108 if err := json.Unmarshal(respBody, response); err != nil {
109 return nil, err
110 }
111
112 return response, nil
113 } else {
114 return nil, wxpay_utility.NewApiException(
115 httpResponse.StatusCode,
116 httpResponse.Header,
117 respBody,
118 )
119 }
120}
121
122type ListDeliveryPlansRequest struct {
123 BrandId *string `json:"brand_id,omitempty"`
124 PageSize *int64 `json:"page_size,omitempty"`
125 Offset *int64 `json:"offset,omitempty"`
126 PlanId *string `json:"plan_id,omitempty"`
127 PlanState *PlanState `json:"plan_state,omitempty"`
128 AuditState *PlanAuditState `json:"audit_state,omitempty"`
129}
130
131func (o *ListDeliveryPlansRequest) MarshalJSON() ([]byte, error) {
132 type Alias ListDeliveryPlansRequest
133 a := &struct {
134 BrandId *string `json:"brand_id,omitempty"`
135 PageSize *int64 `json:"page_size,omitempty"`
136 Offset *int64 `json:"offset,omitempty"`
137 PlanId *string `json:"plan_id,omitempty"`
138 PlanState *PlanState `json:"plan_state,omitempty"`
139 AuditState *PlanAuditState `json:"audit_state,omitempty"`
140 *Alias
141 }{
142
143 BrandId: nil,
144 PageSize: nil,
145 Offset: nil,
146 PlanId: nil,
147 PlanState: nil,
148 AuditState: nil,
149 Alias: (*Alias)(o),
150 }
151 return json.Marshal(a)
152}
153
154type ListDeliveryPlansResponse struct {
155 TotalCount *int64 `json:"total_count,omitempty"`
156 PlanList []DeliveryPlan `json:"plan_list,omitempty"`
157}
158
159type PlanState string
160
161func (e PlanState) Ptr() *PlanState {
162 return &e
163}
164
165const (
166 PLANSTATE_CREATED PlanState = "CREATED"
167 PLANSTATE_TERMINATED PlanState = "TERMINATED"
168 PLANSTATE_EXPIRED PlanState = "EXPIRED"
169 PLANSTATE_DELIVERING PlanState = "DELIVERING"
170 PLANSTATE_PAUSED PlanState = "PAUSED"
171)
172
173type PlanAuditState string
174
175func (e PlanAuditState) Ptr() *PlanAuditState {
176 return &e
177}
178
179const (
180 PLANAUDITSTATE_AUDIT_INITIAL PlanAuditState = "AUDIT_INITIAL"
181 PLANAUDITSTATE_AUDIT_PROCESSING PlanAuditState = "AUDIT_PROCESSING"
182 PLANAUDITSTATE_AUDIT_PASSED PlanAuditState = "AUDIT_PASSED"
183 PLANAUDITSTATE_AUDIT_REJECTED PlanAuditState = "AUDIT_REJECTED"
184)
185
186type DeliveryPlan struct {
187 PlanId *string `json:"plan_id,omitempty"`
188 PlanName *string `json:"plan_name,omitempty"`
189 PlanState *PlanState `json:"plan_state,omitempty"`
190 DeliveryStartTime *string `json:"delivery_start_time,omitempty"`
191 DeliveryEndTime *string `json:"delivery_end_time,omitempty"`
192 ProductCouponId *string `json:"product_coupon_id,omitempty"`
193 UsageMode *ProductUsageMode `json:"usage_mode,omitempty"`
194 StockId *string `json:"stock_id,omitempty"`
195 StockBundleId *string `json:"stock_bundle_id,omitempty"`
196 RecommendWord *string `json:"recommend_word,omitempty"`
197 BrandId *string `json:"brand_id,omitempty"`
198 TotalCount *int64 `json:"total_count,omitempty"`
199 UserLimit *int64 `json:"user_limit,omitempty"`
200 DailyLimit *int64 `json:"daily_limit,omitempty"`
201 ReuseCouponConfig *bool `json:"reuse_coupon_config,omitempty"`
202}
203
204type ProductUsageMode string
205
206func (e ProductUsageMode) Ptr() *ProductUsageMode {
207 return &e
208}
209
210const (
211 PRODUCTUSAGEMODE_SINGLE ProductUsageMode = "SINGLE"
212 PRODUCTUSAGEMODE_PROGRESSIVE_BUNDLE ProductUsageMode = "PROGRESSIVE_BUNDLE"
213)
214