
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 := &QueryActiveCardLinkListRequest{
26 BrandId: wxpay_utility.String("123456"),
27 PaymentScene: PAYMENTSCENE_MINI_PROGRAM.Ptr(),
28 PageIndex: wxpay_utility.Int64(1),
29 PageSize: wxpay_utility.Int64(10),
30 }
31
32 response, err := QueryActiveCardLinkList(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 QueryActiveCardLinkList(config *wxpay_utility.MchConfig, request *QueryActiveCardLinkListRequest) (response *QueryActiveCardLinkListResponse, err error) {
44 const (
45 host = "https://api.mch.weixin.qq.com"
46 method = "GET"
47 path = "/v3/brand/card/card-links"
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.BrandId != nil {
56 query.Add("brand_id", *request.BrandId)
57 }
58 if request.PaymentScene != nil {
59 query.Add("payment_scene", fmt.Sprintf("%v", *request.PaymentScene))
60 }
61 if request.PageIndex != nil {
62 query.Add("page_index", fmt.Sprintf("%v", *request.PageIndex))
63 }
64 if request.PageSize != nil {
65 query.Add("page_size", fmt.Sprintf("%v", *request.PageSize))
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 := &QueryActiveCardLinkListResponse{}
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 QueryActiveCardLinkListRequest struct {
116 BrandId *string `json:"brand_id,omitempty"`
117 PageIndex *int64 `json:"page_index,omitempty"`
118 PageSize *int64 `json:"page_size,omitempty"`
119 PaymentScene *PaymentScene `json:"payment_scene,omitempty"`
120}
121
122func (o *QueryActiveCardLinkListRequest) MarshalJSON() ([]byte, error) {
123 type Alias QueryActiveCardLinkListRequest
124 a := &struct {
125 BrandId *string `json:"brand_id,omitempty"`
126 PageIndex *int64 `json:"page_index,omitempty"`
127 PageSize *int64 `json:"page_size,omitempty"`
128 PaymentScene *PaymentScene `json:"payment_scene,omitempty"`
129 *Alias
130 }{
131
132 BrandId: nil,
133 PageIndex: nil,
134 PageSize: nil,
135 PaymentScene: nil,
136 Alias: (*Alias)(o),
137 }
138 return json.Marshal(a)
139}
140
141type QueryActiveCardLinkListResponse struct {
142 BrandId *string `json:"brand_id,omitempty"`
143 TotalNum *int64 `json:"total_num,omitempty"`
144 ActiveLinkList []ActiveCardLink `json:"active_link_list,omitempty"`
145 PageIndex *int64 `json:"page_index,omitempty"`
146 PageSize *int64 `json:"page_size,omitempty"`
147}
148
149type PaymentScene string
150
151func (e PaymentScene) Ptr() *PaymentScene {
152 return &e
153}
154
155const (
156 PAYMENTSCENE_MINI_PROGRAM PaymentScene = "MINI_PROGRAM"
157 PAYMENTSCENE_APP PaymentScene = "APP"
158 PAYMENTSCENE_PAYMENT_SCORE PaymentScene = "PAYMENT_SCORE"
159 PAYMENTSCENE_PAYMENT_CODE PaymentScene = "PAYMENT_CODE"
160)
161
162type ActiveCardLink struct {
163 PaymentScene *PaymentScene `json:"payment_scene,omitempty"`
164 AppidList []string `json:"appid_list,omitempty"`
165 CardLinkMchid *string `json:"card_link_mchid,omitempty"`
166 ServiceId *string `json:"service_id,omitempty"`
167}
168