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