
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 := &ListPartnershipsRequest{
26 Limit: wxpay_utility.Int64(5),
27 Offset: wxpay_utility.Int64(10),
28 AuthorizedData: &AuthorizedDataFilter{
29 BusinessType: AUTHBUSINESSTYPE_FAVOR_STOCK.Ptr(),
30 StockId: wxpay_utility.String("2433405"),
31 },
32 Partner: &PartnerInfo{
33 Type: PARTNERTYPE_APPID.Ptr(),
34 Appid: wxpay_utility.String("wx4e1916a585d1f4e9"),
35 MerchantId: wxpay_utility.String("2480029552"),
36 },
37 }
38
39 response, err := ListPartnerships(config, request)
40 if err != nil {
41 fmt.Printf("请求失败: %+v\n", err)
42
43 return
44 }
45
46
47 fmt.Printf("请求成功: %+v\n", response)
48}
49
50func ListPartnerships(config *wxpay_utility.MchConfig, request *ListPartnershipsRequest) (response *ListPartnershipsResponse, err error) {
51 const (
52 host = "https://api.mch.weixin.qq.com"
53 method = "GET"
54 path = "/v3/marketing/partnerships"
55 )
56
57 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
58 if err != nil {
59 return nil, err
60 }
61 query := reqUrl.Query()
62 if request.Limit != nil {
63 query.Add("limit", fmt.Sprintf("%v", *request.Limit))
64 }
65 if request.Offset != nil {
66 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
67 }
68 if request.Partner != nil {
69 jsonStr, err := json.Marshal(*request.Partner)
70 if err != nil {
71 return nil, fmt.Errorf("failed to marshal partner: %v", err)
72 }
73 query.Add("partner", string(jsonStr))
74 }
75 if request.AuthorizedData != nil {
76 jsonStr, err := json.Marshal(*request.AuthorizedData)
77 if err != nil {
78 return nil, fmt.Errorf("failed to marshal authorized_data: %v", err)
79 }
80 query.Add("authorized_data", string(jsonStr))
81 }
82 reqUrl.RawQuery = query.Encode()
83 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
84 if err != nil {
85 return nil, err
86 }
87 httpRequest.Header.Set("Accept", "application/json")
88 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
89 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
90 if err != nil {
91 return nil, err
92 }
93 httpRequest.Header.Set("Authorization", authorization)
94
95 client := &http.Client{}
96 httpResponse, err := client.Do(httpRequest)
97 if err != nil {
98 return nil, err
99 }
100 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
101 if err != nil {
102 return nil, err
103 }
104 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
105
106 err = wxpay_utility.ValidateResponse(
107 config.WechatPayPublicKeyId(),
108 config.WechatPayPublicKey(),
109 &httpResponse.Header,
110 respBody,
111 )
112 if err != nil {
113 return nil, err
114 }
115 response := &ListPartnershipsResponse{}
116 if err := json.Unmarshal(respBody, response); err != nil {
117 return nil, err
118 }
119
120 return response, nil
121 } else {
122 return nil, wxpay_utility.NewApiException(
123 httpResponse.StatusCode,
124 httpResponse.Header,
125 respBody,
126 )
127 }
128}
129
130type ListPartnershipsRequest struct {
131 Limit *int64 `json:"limit,omitempty"`
132 Offset *int64 `json:"offset,omitempty"`
133 Partner *PartnerInfo `json:"partner,omitempty"`
134 AuthorizedData *AuthorizedDataFilter `json:"authorized_data,omitempty"`
135}
136
137func (o *ListPartnershipsRequest) MarshalJSON() ([]byte, error) {
138 type Alias ListPartnershipsRequest
139 a := &struct {
140 Limit *int64 `json:"limit,omitempty"`
141 Offset *int64 `json:"offset,omitempty"`
142 Partner *PartnerInfo `json:"partner,omitempty"`
143 AuthorizedData *AuthorizedDataFilter `json:"authorized_data,omitempty"`
144 *Alias
145 }{
146
147 Limit: nil,
148 Offset: nil,
149 Partner: nil,
150 AuthorizedData: nil,
151 Alias: (*Alias)(o),
152 }
153 return json.Marshal(a)
154}
155
156type ListPartnershipsResponse struct {
157 Data []PartnershipsEntity `json:"data,omitempty"`
158 Offset *int64 `json:"offset,omitempty"`
159 Limit *int64 `json:"limit,omitempty"`
160 TotalCount *int64 `json:"total_count,omitempty"`
161}
162
163type PartnerInfo struct {
164 Type *PartnerType `json:"type,omitempty"`
165 Appid *string `json:"appid,omitempty"`
166 MerchantId *string `json:"merchant_id,omitempty"`
167}
168
169type AuthorizedDataFilter struct {
170 BusinessType *AuthBusinessType `json:"business_type,omitempty"`
171 StockId *string `json:"stock_id,omitempty"`
172}
173
174type PartnershipsEntity struct {
175 Partner *PartnerInfo `json:"partner,omitempty"`
176 AuthorizedData *AuthorizedData `json:"authorized_data,omitempty"`
177 BuildTime *string `json:"build_time,omitempty"`
178 TerminateTime *string `json:"terminate_time,omitempty"`
179 CreateTime *string `json:"create_time,omitempty"`
180 UpdateTime *string `json:"update_time,omitempty"`
181}
182
183type PartnerType string
184
185func (e PartnerType) Ptr() *PartnerType {
186 return &e
187}
188
189const (
190 PARTNERTYPE_APPID PartnerType = "APPID"
191 PARTNERTYPE_MERCHANT PartnerType = "MERCHANT"
192)
193
194type AuthBusinessType string
195
196func (e AuthBusinessType) Ptr() *AuthBusinessType {
197 return &e
198}
199
200const (
201 AUTHBUSINESSTYPE_FAVOR_STOCK AuthBusinessType = "FAVOR_STOCK"
202 AUTHBUSINESSTYPE_BUSIFAVOR_STOCK AuthBusinessType = "BUSIFAVOR_STOCK"
203)
204
205type AuthorizedData struct {
206 BusinessType *AuthBusinessType `json:"business_type,omitempty"`
207 StockId *string `json:"stock_id,omitempty"`
208}
209