
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 := &QueryParkingLotRequest{
26 OutParkingLotId: wxpay_utility.String("lot_sz_tencent_001"),
27 WxParkingLotId: wxpay_utility.String("21232735744117624001123604298240"),
28 }
29
30 response, err := QueryParkingLot(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 QueryParkingLot(config *wxpay_utility.MchConfig, request *QueryParkingLotRequest) (response *QueryParkingLotResponse, err error) {
42 const (
43 host = "https://api.mch.weixin.qq.com"
44 method = "GET"
45 path = "/v3/parking/reminders/parking-lot"
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.OutParkingLotId != nil {
54 query.Add("out_parking_lot_id", *request.OutParkingLotId)
55 }
56 if request.WxParkingLotId != nil {
57 query.Add("wx_parking_lot_id", *request.WxParkingLotId)
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_utility.BuildAuthorization(config.MchId(), 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_utility.ExtractResponseBody(httpResponse)
78 if err != nil {
79 return nil, err
80 }
81 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
82
83 err = wxpay_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 := &QueryParkingLotResponse{}
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_utility.NewApiException(
100 httpResponse.StatusCode,
101 httpResponse.Header,
102 respBody,
103 )
104 }
105}
106
107type QueryParkingLotRequest struct {
108 OutParkingLotId *string `json:"out_parking_lot_id,omitempty"`
109 WxParkingLotId *string `json:"wx_parking_lot_id,omitempty"`
110}
111
112func (o *QueryParkingLotRequest) MarshalJSON() ([]byte, error) {
113 type Alias QueryParkingLotRequest
114 a := &struct {
115 OutParkingLotId *string `json:"out_parking_lot_id,omitempty"`
116 WxParkingLotId *string `json:"wx_parking_lot_id,omitempty"`
117 *Alias
118 }{
119
120 OutParkingLotId: nil,
121 WxParkingLotId: nil,
122 Alias: (*Alias)(o),
123 }
124 return json.Marshal(a)
125}
126
127type QueryParkingLotResponse struct {
128 Code *string `json:"code,omitempty"`
129 Message *string `json:"message,omitempty"`
130 WxParkingLotId *string `json:"wx_parking_lot_id,omitempty"`
131 OutParkingLotId *string `json:"out_parking_lot_id,omitempty"`
132 ParkingLotName *string `json:"parking_lot_name,omitempty"`
133 ParkingLotAddress *string `json:"parking_lot_address,omitempty"`
134 PaymentMiniProgAppid *string `json:"payment_mini_prog_appid,omitempty"`
135 PaymentMiniProgPath *string `json:"payment_mini_prog_path,omitempty"`
136 ParkingOrderMiniProgAppid *string `json:"parking_order_mini_prog_appid,omitempty"`
137 ParkingOrderMiniProgPath *string `json:"parking_order_mini_prog_path,omitempty"`
138 EnabledState *ParkingLotEnabledState `json:"enabled_state,omitempty"`
139 Reason *string `json:"reason,omitempty"`
140}
141
142type ParkingLotEnabledState string
143
144func (e ParkingLotEnabledState) Ptr() *ParkingLotEnabledState {
145 return &e
146}
147
148const (
149 PARKINGLOTENABLEDSTATE_ENABLED_STATE_UNKNOWN ParkingLotEnabledState = "ENABLED_STATE_UNKNOWN"
150 PARKINGLOTENABLEDSTATE_ENABLED_STATE_NOT_ENABLED ParkingLotEnabledState = "ENABLED_STATE_NOT_ENABLED"
151 PARKINGLOTENABLEDSTATE_ENABLED_STATE_ENABLED ParkingLotEnabledState = "ENABLED_STATE_ENABLED"
152 PARKINGLOTENABLEDSTATE_ENABLED_STATE_OFFLINE ParkingLotEnabledState = "ENABLED_STATE_OFFLINE"
153)
154