
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 := &GetRequest{
27 OutAuthorizationNo: wxpay_utility.String("plfk2020042013"),
28 }
29
30 response, err := Get(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 Get(config *wxpay_utility.MchConfig, request *GetRequest) (response *UserConfirmAuthorizationEntity, err error) {
42 const (
43 host = "https://api.mch.weixin.qq.com"
44 method = "GET"
45 path = "/v3/fund-app/mch-transfer/user-confirm-authorization/out-authorization-no/{out_authorization_no}"
46 )
47
48 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
49 if err != nil {
50 return nil, err
51 }
52 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_authorization_no}", url.PathEscape(*request.OutAuthorizationNo), -1)
53 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
54 if err != nil {
55 return nil, err
56 }
57 httpRequest.Header.Set("Accept", "application/json")
58 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
59 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
60 if err != nil {
61 return nil, err
62 }
63 httpRequest.Header.Set("Authorization", authorization)
64
65 client := &http.Client{}
66 httpResponse, err := client.Do(httpRequest)
67 if err != nil {
68 return nil, err
69 }
70 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
71 if err != nil {
72 return nil, err
73 }
74 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
75
76 err = wxpay_utility.ValidateResponse(
77 config.WechatPayPublicKeyId(),
78 config.WechatPayPublicKey(),
79 &httpResponse.Header,
80 respBody,
81 )
82 if err != nil {
83 return nil, err
84 }
85 response := &UserConfirmAuthorizationEntity{}
86 if err := json.Unmarshal(respBody, response); err != nil {
87 return nil, err
88 }
89
90 return response, nil
91 } else {
92 return nil, wxpay_utility.NewApiException(
93 httpResponse.StatusCode,
94 httpResponse.Header,
95 respBody,
96 )
97 }
98}
99
100type GetRequest struct {
101 OutAuthorizationNo *string `json:"out_authorization_no,omitempty"`
102}
103
104func (o *GetRequest) MarshalJSON() ([]byte, error) {
105 type Alias GetRequest
106 a := &struct {
107 OutAuthorizationNo *string `json:"out_authorization_no,omitempty"`
108 *Alias
109 }{
110
111 OutAuthorizationNo: nil,
112 Alias: (*Alias)(o),
113 }
114 return json.Marshal(a)
115}
116
117type UserConfirmAuthorizationEntity struct {
118 OutAuthorizationNo *string `json:"out_authorization_no,omitempty"`
119 Appid *string `json:"appid,omitempty"`
120 Openid *string `json:"openid,omitempty"`
121 UserDisplayName *string `json:"user_display_name,omitempty"`
122 AuthorizationId *string `json:"authorization_id,omitempty"`
123 State *AuthorizationState `json:"state,omitempty"`
124 AuthorizeTime *string `json:"authorize_time,omitempty"`
125 CloseInfo *UserConfirmAuthorizationCloseInfo `json:"close_info,omitempty"`
126}
127
128type AuthorizationState string
129
130func (e AuthorizationState) Ptr() *AuthorizationState {
131 return &e
132}
133
134const (
135 AUTHORIZATIONSTATE_TAKING_EFFECT AuthorizationState = "TAKING_EFFECT"
136 AUTHORIZATIONSTATE_CLOSED AuthorizationState = "CLOSED"
137)
138
139type UserConfirmAuthorizationCloseInfo struct {
140 CloseTime *string `json:"close_time,omitempty"`
141 CloseReason *CloseReason `json:"close_reason,omitempty"`
142}
143
144type CloseReason string
145
146func (e CloseReason) Ptr() *CloseReason {
147 return &e
148}
149
150const (
151 CLOSEREASON_CLOSE_VIA_MCH_API CloseReason = "CLOSE_VIA_MCH_API"
152 CLOSEREASON_USER_CLOSE CloseReason = "USER_CLOSE"
153)
154