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