
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 := &QuerySpecialReductionRequest{
27 OutRequestNo: wxpay_utility.String("htozr8denqegbt6tk6u10iymo7yww5pn"),
28 Mchid: wxpay_utility.String("1900016681"),
29 BalAccountNo: wxpay_utility.String("8609cb22e1774a50a930e414cc71eca06121bcd266335cda230d24a7886a8d9f"),
30 CardNo: wxpay_utility.String("CardNo_example"),
31 SgnNo: wxpay_utility.String("SgnNo_example"),
32 ReqScene: REQUESTSCENE_SJT_CONVENIENT_SETTLEMENT_SERVICE_MERCHANT.Ptr(),
33 }
34
35 response, err := QuerySpecialReduction(config, request)
36 if err != nil {
37 fmt.Printf("请求失败: %+v\n", err)
38
39 return
40 }
41
42
43 fmt.Printf("请求成功: %+v\n", response)
44}
45
46func QuerySpecialReduction(config *wxpay_utility.MchConfig, request *QuerySpecialReductionRequest) (response *SpecialReduction, err error) {
47 const (
48 host = "https://api.mch.weixin.qq.com"
49 method = "GET"
50 path = "/v3/aggracct-bc/wb-channel/special-reduction/{out_request_no}"
51 )
52
53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
54 if err != nil {
55 return nil, err
56 }
57 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_request_no}", url.PathEscape(*request.OutRequestNo), -1)
58 query := reqUrl.Query()
59 if request.Mchid != nil {
60 query.Add("mchid", *request.Mchid)
61 }
62 if request.BalAccountNo != nil {
63 query.Add("bal_account_no", *request.BalAccountNo)
64 }
65 if request.CardNo != nil {
66 query.Add("card_no", *request.CardNo)
67 }
68 if request.SgnNo != nil {
69 query.Add("sgn_no", *request.SgnNo)
70 }
71 if request.ReqScene != nil {
72 query.Add("req_scene", fmt.Sprintf("%v", *request.ReqScene))
73 }
74 reqUrl.RawQuery = query.Encode()
75 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
76 if err != nil {
77 return nil, err
78 }
79 httpRequest.Header.Set("Accept", "application/json")
80 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
81 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
82 if err != nil {
83 return nil, err
84 }
85 httpRequest.Header.Set("Authorization", authorization)
86
87 client := &http.Client{}
88 httpResponse, err := client.Do(httpRequest)
89 if err != nil {
90 return nil, err
91 }
92 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
93 if err != nil {
94 return nil, err
95 }
96 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
97
98 err = wxpay_utility.ValidateResponse(
99 config.WechatPayPublicKeyId(),
100 config.WechatPayPublicKey(),
101 &httpResponse.Header,
102 respBody,
103 )
104 if err != nil {
105 return nil, err
106 }
107 response := &SpecialReduction{}
108 if err := json.Unmarshal(respBody, response); err != nil {
109 return nil, err
110 }
111
112 return response, nil
113 } else {
114 return nil, wxpay_utility.NewApiException(
115 httpResponse.StatusCode,
116 httpResponse.Header,
117 respBody,
118 )
119 }
120}
121
122type QuerySpecialReductionRequest struct {
123 OutRequestNo *string `json:"out_request_no,omitempty"`
124 Mchid *string `json:"mchid,omitempty"`
125 BalAccountNo *string `json:"bal_account_no,omitempty"`
126 CardNo *string `json:"card_no,omitempty"`
127 SgnNo *string `json:"sgn_no,omitempty"`
128 ReqScene *RequestScene `json:"req_scene,omitempty"`
129}
130
131func (o *QuerySpecialReductionRequest) MarshalJSON() ([]byte, error) {
132 type Alias QuerySpecialReductionRequest
133 a := &struct {
134 OutRequestNo *string `json:"out_request_no,omitempty"`
135 Mchid *string `json:"mchid,omitempty"`
136 BalAccountNo *string `json:"bal_account_no,omitempty"`
137 CardNo *string `json:"card_no,omitempty"`
138 SgnNo *string `json:"sgn_no,omitempty"`
139 ReqScene *RequestScene `json:"req_scene,omitempty"`
140 *Alias
141 }{
142
143 OutRequestNo: nil,
144 Mchid: nil,
145 BalAccountNo: nil,
146 CardNo: nil,
147 SgnNo: nil,
148 ReqScene: nil,
149 Alias: (*Alias)(o),
150 }
151 return json.Marshal(a)
152}
153
154type SpecialReduction struct {
155 Mchid *string `json:"mchid,omitempty"`
156 BalAccountNo *string `json:"bal_account_no,omitempty"`
157 OutRequestNo *string `json:"out_request_no,omitempty"`
158 ReductionType *SpecialReductionType `json:"reduction_type,omitempty"`
159 Amount *int64 `json:"amount,omitempty"`
160 State *SpecialReductionState `json:"state,omitempty"`
161 Reason *string `json:"reason,omitempty"`
162 QuotaChangeNo *string `json:"quota_change_no,omitempty"`
163 SuccessTime *string `json:"success_time,omitempty"`
164 FailTime *string `json:"fail_time,omitempty"`
165 FailReason *SpecialReductionFailReason `json:"fail_reason,omitempty"`
166 CardNo *string `json:"card_no,omitempty"`
167 SgnNo *string `json:"sgn_no,omitempty"`
168}
169
170type RequestScene string
171
172func (e RequestScene) Ptr() *RequestScene {
173 return &e
174}
175
176const (
177 REQUESTSCENE_SJT_CONVENIENT_SETTLEMENT_SERVICE_MERCHANT RequestScene = "SJT_CONVENIENT_SETTLEMENT_SERVICE_MERCHANT"
178 REQUESTSCENE_SJT_MICRO_MERCHANT RequestScene = "SJT_MICRO_MERCHANT"
179 REQUESTSCENE_WX_SHOP_CONVENIENT_SETTLEMENT_SERVICE_MERCHANT RequestScene = "WX_SHOP_CONVENIENT_SETTLEMENT_SERVICE_MERCHANT"
180)
181
182type SpecialReductionType string
183
184func (e SpecialReductionType) Ptr() *SpecialReductionType {
185 return &e
186}
187
188const (
189 SPECIALREDUCTIONTYPE_REDUCTION_TYPE_UNKNOWN SpecialReductionType = "REDUCTION_TYPE_UNKNOWN"
190 SPECIALREDUCTIONTYPE_FINANCIAL_PENALTY SpecialReductionType = "FINANCIAL_PENALTY"
191 SPECIALREDUCTIONTYPE_JUDICIAL_DEDUCTION SpecialReductionType = "JUDICIAL_DEDUCTION"
192)
193
194type SpecialReductionState string
195
196func (e SpecialReductionState) Ptr() *SpecialReductionState {
197 return &e
198}
199
200const (
201 SPECIALREDUCTIONSTATE_STATE_UNKNOWN SpecialReductionState = "STATE_UNKNOWN"
202 SPECIALREDUCTIONSTATE_PROCESSING SpecialReductionState = "PROCESSING"
203 SPECIALREDUCTIONSTATE_SUCCESS SpecialReductionState = "SUCCESS"
204 SPECIALREDUCTIONSTATE_FAILED SpecialReductionState = "FAILED"
205)
206
207type SpecialReductionFailReason string
208
209func (e SpecialReductionFailReason) Ptr() *SpecialReductionFailReason {
210 return &e
211}
212
213const (
214 SPECIALREDUCTIONFAILREASON_FAIL_REASON_UNKNOWN SpecialReductionFailReason = "FAIL_REASON_UNKNOWN"
215 SPECIALREDUCTIONFAILREASON_BALANCE_NOT_ENOUGH SpecialReductionFailReason = "BALANCE_NOT_ENOUGH"
216 SPECIALREDUCTIONFAILREASON_RECEIPT_NOT_EXIST SpecialReductionFailReason = "RECEIPT_NOT_EXIST"
217 SPECIALREDUCTIONFAILREASON_TIME_OUT_CLOSED SpecialReductionFailReason = "TIME_OUT_CLOSED"
218)
219