
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 := &GetReceiptRequest{
28 ReceiptId: wxpay_utility.String("0100011742874700562078230000"),
29 }
30
31 response, err := GetReceipt(config, request)
32 if err != nil {
33 fmt.Printf("请求失败: %+v\n", err)
34
35 return
36 }
37
38
39 fmt.Printf("请求成功: %+v\n", response)
40}
41
42func GetReceipt(config *wxpay_utility.MchConfig, request *GetReceiptRequest) (response *AbnormalFundReceipt, err error) {
43 const (
44 host = "https://api.mch.weixin.qq.com"
45 method = "GET"
46 path = "/v3/abnormal-fund-processing/receipts/{receipt_id}"
47 )
48
49 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
50 if err != nil {
51 return nil, err
52 }
53 reqUrl.Path = strings.Replace(reqUrl.Path, "{receipt_id}", url.PathEscape(*request.ReceiptId), -1)
54 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
55 if err != nil {
56 return nil, err
57 }
58 httpRequest.Header.Set("Accept", "application/json")
59 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
60 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
61 if err != nil {
62 return nil, err
63 }
64 httpRequest.Header.Set("Authorization", authorization)
65
66 client := &http.Client{}
67 httpResponse, err := client.Do(httpRequest)
68 if err != nil {
69 return nil, err
70 }
71 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
72 if err != nil {
73 return nil, err
74 }
75 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
76
77 err = wxpay_utility.ValidateResponse(
78 config.WechatPayPublicKeyId(),
79 config.WechatPayPublicKey(),
80 &httpResponse.Header,
81 respBody,
82 )
83 if err != nil {
84 return nil, err
85 }
86 response := &AbnormalFundReceipt{}
87 if err := json.Unmarshal(respBody, response); err != nil {
88 return nil, err
89 }
90
91 return response, nil
92 } else {
93 return nil, wxpay_utility.NewApiException(
94 httpResponse.StatusCode,
95 httpResponse.Header,
96 respBody,
97 )
98 }
99}
100
101type GetReceiptRequest struct {
102 ReceiptId *string `json:"receipt_id,omitempty"`
103}
104
105func (o *GetReceiptRequest) MarshalJSON() ([]byte, error) {
106 type Alias GetReceiptRequest
107 a := &struct {
108 ReceiptId *string `json:"receipt_id,omitempty"`
109 *Alias
110 }{
111
112 ReceiptId: nil,
113 Alias: (*Alias)(o),
114 }
115 return json.Marshal(a)
116}
117
118type AbnormalFundReceipt struct {
119 ProductName *string `json:"product_name,omitempty"`
120 ReceiptId *string `json:"receipt_id,omitempty"`
121 TransferAmount *Amount `json:"transfer_amount,omitempty"`
122 ReceiptState *ReceiptState `json:"receipt_state,omitempty"`
123 CreateTime *time.Time `json:"create_time,omitempty"`
124 LastUpdateTime *time.Time `json:"last_update_time,omitempty"`
125 Instruction *Instruction `json:"instruction,omitempty"`
126}
127
128type Amount struct {
129 Total *int64 `json:"total,omitempty"`
130 Currency *Currency `json:"currency,omitempty"`
131}
132
133type ReceiptState string
134
135func (e ReceiptState) Ptr() *ReceiptState {
136 return &e
137}
138
139const (
140 RECEIPTSTATE_RECEIPT_STATE_PENDING ReceiptState = "RECEIPT_STATE_PENDING"
141 RECEIPTSTATE_RECEIPT_STATE_PROGRESS ReceiptState = "RECEIPT_STATE_PROGRESS"
142 RECEIPTSTATE_RECEIPT_STATE_COMPLETED ReceiptState = "RECEIPT_STATE_COMPLETED"
143)
144
145type Instruction struct {
146 OutInstructionNo *string `json:"out_instruction_no,omitempty"`
147 Commander *Commander `json:"commander,omitempty"`
148 TransferMode *TransferModeType `json:"transfer_mode,omitempty"`
149 Receiver *Receiver `json:"receiver,omitempty"`
150 InstructionState *InstructionState `json:"instruction_state,omitempty"`
151 CreateTime *time.Time `json:"create_time,omitempty"`
152 SuccessTime *time.Time `json:"success_time,omitempty"`
153 NotifyUrl *string `json:"notify_url,omitempty"`
154 InstructionInstanceId *string `json:"instruction_instance_id,omitempty"`
155}
156
157type Currency string
158
159func (e Currency) Ptr() *Currency {
160 return &e
161}
162
163const (
164 CURRENCY_CNY Currency = "CNY"
165)
166
167type Commander struct {
168 Operator *Operator `json:"operator,omitempty"`
169 Mchid *string `json:"mchid,omitempty"`
170}
171
172type TransferModeType string
173
174func (e TransferModeType) Ptr() *TransferModeType {
175 return &e
176}
177
178const (
179 TRANSFERMODETYPE_TRANSFER_TO_ORIGINAL_RECEIVE_USER TransferModeType = "TRANSFER_TO_ORIGINAL_RECEIVE_USER"
180 TRANSFERMODETYPE_TRANSFER_TO_ORIGINAL_RECEIVE_MERCHANT TransferModeType = "TRANSFER_TO_ORIGINAL_RECEIVE_MERCHANT"
181 TRANSFERMODETYPE_TRANSFER_TO_SPECIFIED_RECEIVE_MERCHANT TransferModeType = "TRANSFER_TO_SPECIFIED_RECEIVE_MERCHANT"
182)
183
184type Receiver struct {
185 AccountType *string `json:"account_type,omitempty"`
186 Openid *string `json:"openid,omitempty"`
187 Appid *string `json:"appid,omitempty"`
188 Mchid *string `json:"mchid,omitempty"`
189}
190
191type InstructionState string
192
193func (e InstructionState) Ptr() *InstructionState {
194 return &e
195}
196
197const (
198 INSTRUCTIONSTATE_INSTRUCTION_STATE_PENDING InstructionState = "INSTRUCTION_STATE_PENDING"
199 INSTRUCTIONSTATE_INSTRUCTION_STATE_IN_PROGRESS InstructionState = "INSTRUCTION_STATE_IN_PROGRESS"
200 INSTRUCTIONSTATE_INSTRUCTION_STATE_CLOSED InstructionState = "INSTRUCTION_STATE_CLOSED"
201 INSTRUCTIONSTATE_INSTRUCTION_STATE_SUCCESS InstructionState = "INSTRUCTION_STATE_SUCCESS"
202)
203
204type Operator string
205
206func (e Operator) Ptr() *Operator {
207 return &e
208}
209
210const (
211 OPERATOR_MERCHANT Operator = "MERCHANT"
212)
213