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