
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
80 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
81 if err != nil {
82 return nil, err
83 }
84
85 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
86
87 err = wxpay_utility.ValidateResponse(
88 config.WechatPayPublicKeyId(),
89 config.WechatPayPublicKey(),
90 &httpResponse.Header,
91 respBody,
92 )
93 if err != nil {
94 return nil, err
95 }
96
97 if err := json.Unmarshal(respBody, response); err != nil {
98 return nil, err
99 }
100
101 return response, nil
102 } else {
103 return nil, wxpay_utility.NewApiException(
104 httpResponse.StatusCode,
105 httpResponse.Header,
106 respBody,
107 )
108 }
109}
110
111type RevokeAuthorizationRequest struct {
112 SpMchid *string `json:"sp_mchid,omitempty"`
113 SubMchid *string `json:"sub_mchid,omitempty"`
114 EmployeeId *string `json:"employee_id,omitempty"`
115}
116
117func (o *RevokeAuthorizationRequest) MarshalJSON() ([]byte, error) {
118 type Alias RevokeAuthorizationRequest
119 a := &struct {
120 EmployeeId *string `json:"employee_id,omitempty"`
121 *Alias
122 }{
123
124 EmployeeId: nil,
125 Alias: (*Alias)(o),
126 }
127 return json.Marshal(a)
128}
129
130type UsersRevokeAuthorizationResponse struct {
131 SpMchid *string `json:"sp_mchid,omitempty"`
132 SubMchid *string `json:"sub_mchid,omitempty"`
133 UserId *string `json:"user_id,omitempty"`
134 AuthorizationState *EmployeeState `json:"authorization_state,omitempty"`
135 AuthorizationRevokedTime *time.Time `json:"authorization_revoked_time,omitempty"`
136 Reason *string `json:"reason,omitempty"`
137}
138
139type EmployeeState string
140
141func (e EmployeeState) Ptr() *EmployeeState {
142 return &e
143}
144
145const (
146 EMPLOYEESTATE_AUTHORIZED EmployeeState = "AUTHORIZED"
147 EMPLOYEESTATE_REVOKED EmployeeState = "REVOKED"
148)
149