
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 := &ListEcSubjectSubMerchantsRequest{
26 OrganizationType: ORGANIZATIONTYPE_SUBJECT_TYPE_ENTERPRISE.Ptr(),
27 CertNumber: wxpay_utility.String("Iuas%2BxWj7ma0t%2Frcuy6eLqTBVkoxfc1vGVTjl4lqgPKinK24WHVnHao%2BJVp5xAJlvrkGPndf8LHOmroQ9qq44y2rUiPY7fEErV0zMh0vAKAG76M6yqcqQ9A1wOQ8l%2BIL7iQGAnd6SfqRMUsy8AU1nhzDwl7MUn2lBAPyFiR1LtJdc1ASagvSW3CP53iXyaVSkvVianWI6WUzb3xHv7HpEOSpaG3H9bhOjjdkbi8eaCudkbhXgF2f3QKfwO696j5DJTxN56r%2BomxtVfpgvi9B2fQOgDu7pZ%2FoqkkeTSyht%2BL8lL7T14%2FUj5T1m8fV9LOgjk2gGxziLjDgXCiX3jLXNw%3D%3D"),
28 Limit: wxpay_utility.Int64(20),
29 Offset: wxpay_utility.Int64(0),
30 }
31
32 response, err := ListEcSubjectSubMerchants(config, request)
33 if err != nil {
34 fmt.Printf("请求失败: %+v\n", err)
35
36 return
37 }
38
39
40 fmt.Printf("请求成功: %+v\n", response)
41}
42
43func ListEcSubjectSubMerchants(config *wxpay_utility.MchConfig, request *ListEcSubjectSubMerchantsRequest) (response *ListEcSubjectSubMerchantsResponse, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "GET"
47 path = "/v3/ecommerce/subject-sub-merchants"
48 )
49
50 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
51 if err != nil {
52 return nil, err
53 }
54 query := reqUrl.Query()
55 if request.OrganizationType != nil {
56 query.Add("organization_type", fmt.Sprintf("%v", *request.OrganizationType))
57 }
58 if request.CertNumber != nil {
59 query.Add("cert_number", *request.CertNumber)
60 }
61 if request.Limit != nil {
62 query.Add("limit", fmt.Sprintf("%v", *request.Limit))
63 }
64 if request.Offset != nil {
65 query.Add("offset", fmt.Sprintf("%v", *request.Offset))
66 }
67 reqUrl.RawQuery = query.Encode()
68 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Accept", "application/json")
73 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
74 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
75 if err != nil {
76 return nil, err
77 }
78 httpRequest.Header.Set("Authorization", authorization)
79
80 client := &http.Client{}
81 httpResponse, err := client.Do(httpRequest)
82 if err != nil {
83 return nil, err
84 }
85 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
86 if err != nil {
87 return nil, err
88 }
89 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
90
91 err = wxpay_utility.ValidateResponse(
92 config.WechatPayPublicKeyId(),
93 config.WechatPayPublicKey(),
94 &httpResponse.Header,
95 respBody,
96 )
97 if err != nil {
98 return nil, err
99 }
100 response := &ListEcSubjectSubMerchantsResponse{}
101 if err := json.Unmarshal(respBody, response); err != nil {
102 return nil, err
103 }
104
105 return response, nil
106 } else {
107 return nil, wxpay_utility.NewApiException(
108 httpResponse.StatusCode,
109 httpResponse.Header,
110 respBody,
111 )
112 }
113}
114
115type ListEcSubjectSubMerchantsRequest struct {
116 CertNumber *string `json:"cert_number,omitempty"`
117 Limit *int64 `json:"limit,omitempty"`
118 Offset *int64 `json:"offset,omitempty"`
119 OrganizationType *OrganizationType `json:"organization_type,omitempty"`
120}
121
122func (o *ListEcSubjectSubMerchantsRequest) MarshalJSON() ([]byte, error) {
123 type Alias ListEcSubjectSubMerchantsRequest
124 a := &struct {
125 CertNumber *string `json:"cert_number,omitempty"`
126 Limit *int64 `json:"limit,omitempty"`
127 Offset *int64 `json:"offset,omitempty"`
128 OrganizationType *OrganizationType `json:"organization_type,omitempty"`
129 *Alias
130 }{
131
132 CertNumber: nil,
133 Limit: nil,
134 Offset: nil,
135 OrganizationType: nil,
136 Alias: (*Alias)(o),
137 }
138 return json.Marshal(a)
139}
140
141type ListEcSubjectSubMerchantsResponse struct {
142 Data []SubjectSubMerchantItem `json:"data,omitempty"`
143 Offset *int64 `json:"offset,omitempty"`
144 Limit *int64 `json:"limit,omitempty"`
145 TotalCount *int64 `json:"total_count,omitempty"`
146}
147
148type OrganizationType string
149
150func (e OrganizationType) Ptr() *OrganizationType {
151 return &e
152}
153
154const (
155 ORGANIZATIONTYPE_SUBJECT_TYPE_ENTERPRISE OrganizationType = "SUBJECT_TYPE_ENTERPRISE"
156 ORGANIZATIONTYPE_SUBJECT_TYPE_INSTITUTIONS_CLONED OrganizationType = "SUBJECT_TYPE_INSTITUTIONS_CLONED"
157 ORGANIZATIONTYPE_SUBJECT_TYPE_INDIVIDUAL OrganizationType = "SUBJECT_TYPE_INDIVIDUAL"
158 ORGANIZATIONTYPE_SUBJECT_TYPE_OTHERS OrganizationType = "SUBJECT_TYPE_OTHERS"
159 ORGANIZATIONTYPE_SUBJECT_TYPE_GOVERNMENT OrganizationType = "SUBJECT_TYPE_GOVERNMENT"
160 ORGANIZATIONTYPE_SUBJECT_TYPE_MICRO OrganizationType = "SUBJECT_TYPE_MICRO"
161)
162
163type SubjectSubMerchantItem struct {
164 SubMchid *string `json:"sub_mchid,omitempty"`
165 MerchantShortname *string `json:"merchant_shortname,omitempty"`
166 SignTime *string `json:"sign_time,omitempty"`
167}
168