
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
10 "time"
11)
12
13func main() {
14
15 config, err := wxpay_utility.CreateMchConfig(
16 "19xxxxxxxx",
17 "1DDE55AD98Exxxxxxxxxx",
18 "/path/to/apiclient_key.pem",
19 "PUB_KEY_ID_xxxxxxxxxxxxx",
20 "/path/to/wxp_pub.pem",
21 )
22 if err != nil {
23 fmt.Println(err)
24 return
25 }
26
27 request := &QueryByOutRequestNoRequest{
28 ControlType: CONTROLTYPE_PUNISH.Ptr(),
29 OutRequestNo: wxpay_utility.String("OutRequestNo_example"),
30 Mchid: wxpay_utility.String("Mchid_example"),
31 }
32
33 response, err := QueryByOutRequestNo(config, request)
34 if err != nil {
35 fmt.Printf("请求失败: %+v\n", err)
36
37 return
38 }
39
40
41 fmt.Printf("请求成功: %+v\n", response)
42}
43
44func QueryByOutRequestNo(config *wxpay_utility.MchConfig, request *QueryByOutRequestNoRequest) (response *ControlOrdersEntity, err error) {
45 const (
46 host = "https://api.mch.weixin.qq.com"
47 method = "GET"
48 path = "/v3/aggracct-bc/wb-channel/control-orders/{out_request_no}"
49 )
50
51 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
52 if err != nil {
53 return nil, err
54 }
55 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_request_no}", url.PathEscape(*request.OutRequestNo), -1)
56 query := reqUrl.Query()
57 if request.ControlType != nil {
58 query.Add("control_type", fmt.Sprintf("%v", *request.ControlType))
59 }
60 if request.Mchid != nil {
61 query.Add("mchid", *request.Mchid)
62 }
63 reqUrl.RawQuery = query.Encode()
64 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
65 if err != nil {
66 return nil, err
67 }
68 httpRequest.Header.Set("Accept", "application/json")
69 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
70 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
71 if err != nil {
72 return nil, err
73 }
74 httpRequest.Header.Set("Authorization", authorization)
75
76 client := &http.Client{}
77 httpResponse, err := client.Do(httpRequest)
78 if err != nil {
79 return nil, err
80 }
81 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
82 if err != nil {
83 return nil, err
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 response := &ControlOrdersEntity{}
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 QueryByOutRequestNoRequest struct {
112 OutRequestNo *string `json:"out_request_no,omitempty"`
113 Mchid *string `json:"mchid,omitempty"`
114 ControlType *ControlType `json:"control_type,omitempty"`
115}
116
117func (o *QueryByOutRequestNoRequest) MarshalJSON() ([]byte, error) {
118 type Alias QueryByOutRequestNoRequest
119 a := &struct {
120 OutRequestNo *string `json:"out_request_no,omitempty"`
121 Mchid *string `json:"mchid,omitempty"`
122 ControlType *ControlType `json:"control_type,omitempty"`
123 *Alias
124 }{
125
126 OutRequestNo: nil,
127 Mchid: nil,
128 ControlType: nil,
129 Alias: (*Alias)(o),
130 }
131 return json.Marshal(a)
132}
133
134type ControlOrdersEntity struct {
135 ControlScene *ControlType `json:"control_scene,omitempty"`
136 PunishOrder *PunishOrder `json:"punish_order,omitempty"`
137 RecoverOrder *RecoverOrder `json:"recover_order,omitempty"`
138}
139
140type ControlType string
141
142func (e ControlType) Ptr() *ControlType {
143 return &e
144}
145
146const (
147 CONTROLTYPE_PUNISH ControlType = "PUNISH"
148 CONTROLTYPE_RECOVER ControlType = "RECOVER"
149)
150
151type PunishOrder struct {
152 WxpayPunishNo *string `json:"wxpay_punish_no,omitempty"`
153 OutRequestNo *string `json:"out_request_no,omitempty"`
154 Mchid *string `json:"mchid,omitempty"`
155 PunishScene *int64 `json:"punish_scene,omitempty"`
156 PunishRuleVersion *int64 `json:"punish_rule_version,omitempty"`
157 PunishReason *string `json:"punish_reason,omitempty"`
158 PunishStartTime *string `json:"punish_start_time,omitempty"`
159 PunishEndTime *string `json:"punish_end_time,omitempty"`
160 PunishAcceptTime *string `json:"punish_accept_time,omitempty"`
161 PunishFinishTime *string `json:"punish_finish_time,omitempty"`
162 PunishState *PunishState `json:"punish_state,omitempty"`
163 EffectState *EffectState `json:"effect_state,omitempty"`
164 ControlAction []ControlAction `json:"control_action,omitempty"`
165 ErrorMessage *string `json:"error_message,omitempty"`
166 ErrorCode *string `json:"error_code,omitempty"`
167}
168
169type RecoverOrder struct {
170 WxpayRecoverNo *string `json:"wxpay_recover_no,omitempty"`
171 OutRequestNo *string `json:"out_request_no,omitempty"`
172 RelatedWxpayPunishNo *string `json:"related_wxpay_punish_no,omitempty"`
173 Mchid *string `json:"mchid,omitempty"`
174 PunishScene *string `json:"punish_scene,omitempty"`
175 PunishRuleVersion *string `json:"punish_rule_version,omitempty"`
176 RecoverAcceptTime *string `json:"recover_accept_time,omitempty"`
177 RecoverFinishTime *time.Time `json:"recover_finish_time,omitempty"`
178 RecoverState *RecoverState `json:"recover_state,omitempty"`
179 RecoverReason *string `json:"recover_reason,omitempty"`
180 ErrorMessage *string `json:"error_message,omitempty"`
181 ErrorCode *string `json:"error_code,omitempty"`
182}
183
184type PunishState string
185
186func (e PunishState) Ptr() *PunishState {
187 return &e
188}
189
190const (
191 PUNISHSTATE_EXECUTING PunishState = "EXECUTING"
192 PUNISHSTATE_SUCCESS PunishState = "SUCCESS"
193 PUNISHSTATE_FAILED PunishState = "FAILED"
194)
195
196type EffectState string
197
198func (e EffectState) Ptr() *EffectState {
199 return &e
200}
201
202const (
203 EFFECTSTATE_ACTIVE EffectState = "ACTIVE"
204 EFFECTSTATE_RECOVERED EffectState = "RECOVERED"
205)
206
207type ControlAction string
208
209func (e ControlAction) Ptr() *ControlAction {
210 return &e
211}
212
213const (
214 CONTROLACTION_DISABLE_RECEIVE ControlAction = "DISABLE_RECEIVE"
215 CONTROLACTION_DISABLE_WITHDRAWAL ControlAction = "DISABLE_WITHDRAWAL"
216 CONTROLACTION_DISABLE_PAY ControlAction = "DISABLE_PAY"
217 CONTROLACTION_DISABLE_REFUND ControlAction = "DISABLE_REFUND"
218 CONTROLACTION_DISABLE_CLOSE_MERCHANT ControlAction = "DISABLE_CLOSE_MERCHANT"
219)
220
221type RecoverState string
222
223func (e RecoverState) Ptr() *RecoverState {
224 return &e
225}
226
227const (
228 RECOVERSTATE_EXECUTING RecoverState = "EXECUTING"
229 RECOVERSTATE_SUCCESS RecoverState = "SUCCESS"
230 RECOVERSTATE_FAILED RecoverState = "FAILED"
231)
232