
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
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 := &GetQuotaCardPaymentUrlRequest{
27 SpMchid: wxpay_utility.String("12341234"),
28 SubMchid: wxpay_utility.String("43214321"),
29 CardNo: wxpay_utility.String("1068019671702503425"),
30 EmployeeId: wxpay_utility.String("WeBizPay_a968402a-73bb-43e2-9e1a-8371b0ca3d7c"),
31 ApplicationType: APPLICATIONTYPE_H5.Ptr(),
32 }
33
34 response, err := GetQuotaCardPaymentUrl(config, request)
35 if err != nil {
36 fmt.Printf("请求失败: %+v\n", err)
37
38 return
39 }
40
41
42 fmt.Printf("请求成功: %+v\n", response)
43}
44
45func GetQuotaCardPaymentUrl(config *wxpay_utility.MchConfig, request *GetQuotaCardPaymentUrlRequest) (response *CardsGetQuotaCardPaymentUrlResponse, err error) {
46 const (
47 host = "https://api.mch.weixin.qq.com"
48 method = "GET"
49 path = "/v3/webizpay/employees/{employee_id}/quota-cards/{card_no}/payment-url"
50 )
51
52 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
53 if err != nil {
54 return nil, err
55 }
56 reqUrl.Path = strings.Replace(reqUrl.Path, "{card_no}", url.PathEscape(*request.CardNo), -1)
57 reqUrl.Path = strings.Replace(reqUrl.Path, "{employee_id}", url.PathEscape(*request.EmployeeId), -1)
58 query := reqUrl.Query()
59 if request.SpMchid != nil {
60 query.Add("sp_mchid", *request.SpMchid)
61 }
62 if request.SubMchid != nil {
63 query.Add("sub_mchid", *request.SubMchid)
64 }
65 if request.ApplicationType != nil {
66 query.Add("application_type", fmt.Sprintf("%v", *request.ApplicationType))
67 }
68 reqUrl.RawQuery = query.Encode()
69 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
70 if err != nil {
71 return nil, err
72 }
73 httpRequest.Header.Set("Accept", "application/json")
74 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
75 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
76 if err != nil {
77 return nil, err
78 }
79 httpRequest.Header.Set("Authorization", authorization)
80
81 client := &http.Client{}
82 httpResponse, err := client.Do(httpRequest)
83 if err != nil {
84 return nil, err
85 }
86 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
87 if err != nil {
88 return nil, err
89 }
90 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
91
92 err = wxpay_utility.ValidateResponse(
93 config.WechatPayPublicKeyId(),
94 config.WechatPayPublicKey(),
95 &httpResponse.Header,
96 respBody,
97 )
98 if err != nil {
99 return nil, err
100 }
101 response := &CardsGetQuotaCardPaymentUrlResponse{}
102 if err := json.Unmarshal(respBody, response); err != nil {
103 return nil, err
104 }
105
106 return response, nil
107 } else {
108 return nil, wxpay_utility.NewApiException(
109 httpResponse.StatusCode,
110 httpResponse.Header,
111 respBody,
112 )
113 }
114}
115
116type GetQuotaCardPaymentUrlRequest struct {
117 SpMchid *string `json:"sp_mchid,omitempty"`
118 SubMchid *string `json:"sub_mchid,omitempty"`
119 CardNo *string `json:"card_no,omitempty"`
120 EmployeeId *string `json:"employee_id,omitempty"`
121 ApplicationType *ApplicationType `json:"application_type,omitempty"`
122}
123
124func (o *GetQuotaCardPaymentUrlRequest) MarshalJSON() ([]byte, error) {
125 type Alias GetQuotaCardPaymentUrlRequest
126 a := &struct {
127 SpMchid *string `json:"sp_mchid,omitempty"`
128 SubMchid *string `json:"sub_mchid,omitempty"`
129 CardNo *string `json:"card_no,omitempty"`
130 EmployeeId *string `json:"employee_id,omitempty"`
131 ApplicationType *ApplicationType `json:"application_type,omitempty"`
132 *Alias
133 }{
134
135 SpMchid: nil,
136 SubMchid: nil,
137 CardNo: nil,
138 EmployeeId: nil,
139 ApplicationType: nil,
140 Alias: (*Alias)(o),
141 }
142 return json.Marshal(a)
143}
144
145type CardsGetQuotaCardPaymentUrlResponse struct {
146 SpMchid *string `json:"sp_mchid,omitempty"`
147 SubMchid *string `json:"sub_mchid,omitempty"`
148 EmployeeId *string `json:"employee_id,omitempty"`
149 CardNo *string `json:"card_no,omitempty"`
150 ExpireTime *string `json:"expire_time,omitempty"`
151 PaymentUrl *string `json:"payment_url,omitempty"`
152 ApplicationType *ApplicationType `json:"application_type,omitempty"`
153 MpQuery *string `json:"mp_query,omitempty"`
154 MpBusinessType *string `json:"mp_business_type,omitempty"`
155}
156
157type ApplicationType string
158
159func (e ApplicationType) Ptr() *ApplicationType {
160 return &e
161}
162
163const (
164 APPLICATIONTYPE_H5 ApplicationType = "H5"
165 APPLICATIONTYPE_MINIPROGRAM ApplicationType = "MINIPROGRAM"
166)
167