
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
11 "time"
12)
13
14func main() {
15
16 config, err := wxpay_utility.CreateMchConfig(
17 "19xxxxxxxx",
18 "1DDE55AD98Exxxxxxxxxx",
19 "/path/to/apiclient_key.pem",
20 "PUB_KEY_ID_xxxxxxxxxxxxx",
21 "/path/to/wxp_pub.pem",
22 )
23 if err != nil {
24 fmt.Println(err)
25 return
26 }
27
28 request := &RevokeAuthorizationRequest{
29 EmployeeId: wxpay_utility.String("WeBizPay_a968402a-73bb-43e2-9e1a-8371b0ca3d7c"),
30 SpMchid: wxpay_utility.String("12341234"),
31 SubMchid: wxpay_utility.String("43214321"),
32 }
33
34 response, err := RevokeAuthorization(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 RevokeAuthorization(config *wxpay_utility.MchConfig, request *RevokeAuthorizationRequest) (response *UsersRevokeAuthorizationResponse, err error) {
46 const (
47 host = "https://api.mch.weixin.qq.com"
48 method = "POST"
49 path = "/v3/webizpay/employees/{employee_id}/revoke"
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, "{employee_id}", url.PathEscape(*request.EmployeeId), -1)
57 reqBody, err := json.Marshal(request)
58 if err != nil {
59 return nil, err
60 }
61 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
62 if err != nil {
63 return nil, err
64 }
65 httpRequest.Header.Set("Accept", "application/json")
66 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
67 httpRequest.Header.Set("Content-Type", "application/json")
68 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Authorization", authorization)
73
74 client := &http.Client{}
75 httpResponse, err := client.Do(httpRequest)
76 if err != nil {
77 return nil, err
78 }
79 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
80 if err != nil {
81 return nil, err
82 }
83 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
84
85 err = wxpay_utility.ValidateResponse(
86 config.WechatPayPublicKeyId(),
87 config.WechatPayPublicKey(),
88 &httpResponse.Header,
89 respBody,
90 )
91 if err != nil {
92 return nil, err
93 }
94 response := &UsersRevokeAuthorizationResponse{}
95 if err := json.Unmarshal(respBody, response); err != nil {
96 return nil, err
97 }
98
99 return response, nil
100 } else {
101 return nil, wxpay_utility.NewApiException(
102 httpResponse.StatusCode,
103 httpResponse.Header,
104 respBody,
105 )
106 }
107}
108
109type RevokeAuthorizationRequest struct {
110 SpMchid *string `json:"sp_mchid,omitempty"`
111 SubMchid *string `json:"sub_mchid,omitempty"`
112 EmployeeId *string `json:"employee_id,omitempty"`
113}
114
115func (o *RevokeAuthorizationRequest) MarshalJSON() ([]byte, error) {
116 type Alias RevokeAuthorizationRequest
117 a := &struct {
118 EmployeeId *string `json:"employee_id,omitempty"`
119 *Alias
120 }{
121
122 EmployeeId: nil,
123 Alias: (*Alias)(o),
124 }
125 return json.Marshal(a)
126}
127
128type UsersRevokeAuthorizationResponse struct {
129 SpMchid *string `json:"sp_mchid,omitempty"`
130 SubMchid *string `json:"sub_mchid,omitempty"`
131 UserId *string `json:"user_id,omitempty"`
132 AuthorizationState *EmployeeState `json:"authorization_state,omitempty"`
133 AuthorizationRevokedTime *time.Time `json:"authorization_revoked_time,omitempty"`
134 Reason *string `json:"reason,omitempty"`
135}
136
137type EmployeeState string
138
139func (e EmployeeState) Ptr() *EmployeeState {
140 return &e
141}
142
143const (
144 EMPLOYEESTATE_AUTHORIZED EmployeeState = "AUTHORIZED"
145 EMPLOYEESTATE_REVOKED EmployeeState = "REVOKED"
146)
147