
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "time"
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 := &GetApplymentStateRequest{
27 BusinessCode: wxpay_utility.String("1900013511_10000"),
28 ApplymentId: wxpay_utility.String("1111111111"),
29 BrandId: wxpay_utility.String("1234567"),
30 }
31
32 response, err := GetApplymentState(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 GetApplymentState(config *wxpay_utility.MchConfig, request *GetApplymentStateRequest) (response *GetApplymentStateResponse, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "GET"
47 path = "/v3/brand/card/card-configs"
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.BusinessCode != nil {
56 query.Add("business_code", *request.BusinessCode)
57 }
58 if request.ApplymentId != nil {
59 query.Add("applyment_id", *request.ApplymentId)
60 }
61 if request.BrandId != nil {
62 query.Add("brand_id", *request.BrandId)
63 }
64 reqUrl.RawQuery = query.Encode()
65 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
66 if err != nil {
67 return nil, err
68 }
69 httpRequest.Header.Set("Accept", "application/json")
70 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
71 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
72 if err != nil {
73 return nil, err
74 }
75 httpRequest.Header.Set("Authorization", authorization)
76
77 client := &http.Client{}
78 httpResponse, err := client.Do(httpRequest)
79 if err != nil {
80 return nil, err
81 }
82 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
83 if err != nil {
84 return nil, err
85 }
86 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
87
88 err = wxpay_utility.ValidateResponse(
89 config.WechatPayPublicKeyId(),
90 config.WechatPayPublicKey(),
91 &httpResponse.Header,
92 respBody,
93 )
94 if err != nil {
95 return nil, err
96 }
97 response := &GetApplymentStateResponse{}
98 if err := json.Unmarshal(respBody, response); err != nil {
99 return nil, err
100 }
101
102 return response, nil
103 } else {
104 return nil, wxpay_utility.NewApiException(
105 httpResponse.StatusCode,
106 httpResponse.Header,
107 respBody,
108 )
109 }
110}
111
112type GetApplymentStateRequest struct {
113 BusinessCode *string `json:"business_code,omitempty"`
114 ApplymentId *string `json:"applyment_id,omitempty"`
115 BrandId *string `json:"brand_id,omitempty"`
116}
117
118func (o *GetApplymentStateRequest) MarshalJSON() ([]byte, error) {
119 type Alias GetApplymentStateRequest
120 a := &struct {
121 BusinessCode *string `json:"business_code,omitempty"`
122 ApplymentId *string `json:"applyment_id,omitempty"`
123 BrandId *string `json:"brand_id,omitempty"`
124 *Alias
125 }{
126
127 BusinessCode: nil,
128 ApplymentId: nil,
129 BrandId: nil,
130 Alias: (*Alias)(o),
131 }
132 return json.Marshal(a)
133}
134
135type GetApplymentStateResponse struct {
136 BusinessCode *string `json:"business_code,omitempty"`
137 ApplymentId *string `json:"applyment_id,omitempty"`
138 BrandId *string `json:"brand_id,omitempty"`
139 ApplymentState *ApplymentState `json:"applyment_state,omitempty"`
140 ScheduledPublishTime *time.Time `json:"scheduled_publish_time,omitempty"`
141 RejectReason *string `json:"reject_reason,omitempty"`
142 ActualPublishTime *time.Time `json:"actual_publish_time,omitempty"`
143}
144
145type ApplymentState string
146
147func (e ApplymentState) Ptr() *ApplymentState {
148 return &e
149}
150
151const (
152 APPLYMENTSTATE_STATE_UNKNOWN ApplymentState = "STATE_UNKNOWN"
153 APPLYMENTSTATE_DRAFTING ApplymentState = "DRAFTING"
154 APPLYMENTSTATE_AUDITING ApplymentState = "AUDITING"
155 APPLYMENTSTATE_AUDIT_REJECTED ApplymentState = "AUDIT_REJECTED"
156 APPLYMENTSTATE_PENDING_PUBLISH ApplymentState = "PENDING_PUBLISH"
157 APPLYMENTSTATE_PUBLISHED ApplymentState = "PUBLISHED"
158 APPLYMENTSTATE_CANCELED ApplymentState = "CANCELED"
159)
160