
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 := &BatchGetUserJointDeliveryConfigRequest{
26 IdentifierType: USERIDENTIFIERTYPE_USER_IDENTIFIER_TYPE_OPENID.Ptr(),
27 UserIdentifiers: []string{"oUasdgxuAJO_M2pxb1Q9zNjWeS6o"},
28 }
29
30 response, err := BatchGetUserJointDeliveryConfig(config, request)
31 if err != nil {
32 fmt.Printf("请求失败: %+v\n", err)
33
34 return
35 }
36
37
38 fmt.Printf("请求成功: %+v\n", response)
39}
40
41func BatchGetUserJointDeliveryConfig(config *wxpay_brand_utility.BrandConfig, request *BatchGetUserJointDeliveryConfigRequest) (response *BatchGetUserJointDeliveryConfigResponse, err error) {
42 const (
43 host = "https://api.mch.weixin.qq.com"
44 method = "GET"
45 path = "/brand/marketing/joint-delivery/users/delivery-configs/batch-get"
46 )
47
48 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
49 if err != nil {
50 return nil, err
51 }
52 query := reqUrl.Query()
53 if request.IdentifierType != nil {
54 query.Add("identifier_type", fmt.Sprintf("%v", *request.IdentifierType))
55 }
56 if request.UserIdentifiers != nil {
57 query.Add("user_identifiers", fmt.Sprintf("%v", *request.UserIdentifiers))
58 }
59 reqUrl.RawQuery = query.Encode()
60 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
61 if err != nil {
62 return nil, err
63 }
64 httpRequest.Header.Set("Accept", "application/json")
65 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
66 authorization, err := wxpay_brand_utility.BuildAuthorization(config.BrandId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
67 if err != nil {
68 return nil, err
69 }
70 httpRequest.Header.Set("Authorization", authorization)
71
72 client := &http.Client{}
73 httpResponse, err := client.Do(httpRequest)
74 if err != nil {
75 return nil, err
76 }
77 respBody, err := wxpay_brand_utility.ExtractResponseBody(httpResponse)
78 if err != nil {
79 return nil, err
80 }
81 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
82
83 err = wxpay_brand_utility.ValidateResponse(
84 config.WechatPayPublicKeyId(),
85 config.WechatPayPublicKey(),
86 &httpResponse.Header,
87 respBody,
88 )
89 if err != nil {
90 return nil, err
91 }
92 response := &BatchGetUserJointDeliveryConfigResponse{}
93 if err := json.Unmarshal(respBody, response); err != nil {
94 return nil, err
95 }
96
97 return response, nil
98 } else {
99 return nil, wxpay_brand_utility.NewApiException(
100 httpResponse.StatusCode,
101 httpResponse.Header,
102 respBody,
103 )
104 }
105}
106
107type BatchGetUserJointDeliveryConfigRequest struct {
108 UserIdentifiers []string `json:"user_identifiers,omitempty"`
109 IdentifierType *UserIdentifierType `json:"identifier_type,omitempty"`
110}
111
112func (o *BatchGetUserJointDeliveryConfigRequest) MarshalJSON() ([]byte, error) {
113 type Alias BatchGetUserJointDeliveryConfigRequest
114 a := &struct {
115 UserIdentifiers []string `json:"user_identifiers,omitempty"`
116 IdentifierType *UserIdentifierType `json:"identifier_type,omitempty"`
117 *Alias
118 }{
119
120 UserIdentifiers: nil,
121 IdentifierType: nil,
122 Alias: (*Alias)(o),
123 }
124 return json.Marshal(a)
125}
126
127type BatchGetUserJointDeliveryConfigResponse struct {
128 UserConfigs []UserJointDeliveryConfigResponse `json:"user_configs,omitempty"`
129}
130
131type UserIdentifierType string
132
133func (e UserIdentifierType) Ptr() *UserIdentifierType {
134 return &e
135}
136
137const (
138 USERIDENTIFIERTYPE_USER_IDENTIFIER_TYPE_OPENID UserIdentifierType = "USER_IDENTIFIER_TYPE_OPENID"
139 USERIDENTIFIERTYPE_USER_IDENTIFIER_TYPE_PHONE_MD5 UserIdentifierType = "USER_IDENTIFIER_TYPE_PHONE_MD5"
140)
141
142type UserJointDeliveryConfigResponse struct {
143 UserIdentifier *string `json:"user_identifier,omitempty"`
144 PlanIds []string `json:"plan_ids,omitempty"`
145 UpdateTime *string `json:"update_time,omitempty"`
146 ErrorCode *string `json:"error_code,omitempty"`
147 ErrorMessage *string `json:"error_message,omitempty"`
148}
149