
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
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 := &CreateWithdrawRequest{
27 OutRequestNo: wxpay_utility.String("20190611222222222200000000012122"),
28 Amount: wxpay_utility.Int64(1),
29 Remark: wxpay_utility.String("交易提现"),
30 BankMemo: wxpay_utility.String("xx平台提现"),
31 AccountType: WITHDRAWACCOUNTTYPE_BASIC.Ptr(),
32 NotifyUrl: wxpay_utility.String("https://yourapp.com/notify"),
33 }
34
35 response, err := CreateWithdraw(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 CreateWithdraw(config *wxpay_utility.MchConfig, request *CreateWithdrawRequest) (response *CreateWithdrawResponse, err error) {
47 const (
48 host = "https://api.mch.weixin.qq.com"
49 method = "POST"
50 path = "/v3/merchant/fund/withdraw"
51 )
52
53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
54 if err != nil {
55 return nil, err
56 }
57 reqBody, err := json.Marshal(request)
58 if err != nil {
59 return nil, err
60 }
61 httpRequest, err := http.NewRequest(method, reqUrl.String(), bytes.NewReader(reqBody))
62 if err != nil {
63 return nil, err
64 }
65 httpRequest.Header.Set("Accept", "application/json")
66 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
67 httpRequest.Header.Set("Content-Type", "application/json")
68 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), reqBody)
69 if err != nil {
70 return nil, err
71 }
72 httpRequest.Header.Set("Authorization", authorization)
73
74 client := &http.Client{}
75 httpResponse, err := client.Do(httpRequest)
76 if err != nil {
77 return nil, err
78 }
79 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
80 if err != nil {
81 return nil, err
82 }
83 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
84
85 err = wxpay_utility.ValidateResponse(
86 config.WechatPayPublicKeyId(),
87 config.WechatPayPublicKey(),
88 &httpResponse.Header,
89 respBody,
90 )
91 if err != nil {
92 return nil, err
93 }
94 response := &CreateWithdrawResponse{}
95 if err := json.Unmarshal(respBody, response); err != nil {
96 return nil, err
97 }
98
99 return response, nil
100 } else {
101 return nil, wxpay_utility.NewApiException(
102 httpResponse.StatusCode,
103 httpResponse.Header,
104 respBody,
105 )
106 }
107}
108
109type CreateWithdrawRequest struct {
110 OutRequestNo *string `json:"out_request_no,omitempty"`
111 Amount *int64 `json:"amount,omitempty"`
112 Remark *string `json:"remark,omitempty"`
113 BankMemo *string `json:"bank_memo,omitempty"`
114 AccountType *WithdrawAccountType `json:"account_type,omitempty"`
115 NotifyUrl *string `json:"notify_url,omitempty"`
116}
117
118type CreateWithdrawResponse struct {
119 WithdrawId *string `json:"withdraw_id,omitempty"`
120 OutRequestNo *string `json:"out_request_no,omitempty"`
121}
122
123type WithdrawAccountType string
124
125func (e WithdrawAccountType) Ptr() *WithdrawAccountType {
126 return &e
127}
128
129const (
130 WITHDRAWACCOUNTTYPE_BASIC WithdrawAccountType = "BASIC"
131 WITHDRAWACCOUNTTYPE_FEES WithdrawAccountType = "FEES"
132 WITHDRAWACCOUNTTYPE_OPERATION WithdrawAccountType = "OPERATION"
133)
134