
1package main
2
3import (
4 "demo/wxpay_utility"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "net/url"
9 "strings"
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 := &GetBudgetRequest{
27 SponsorMchid: wxpay_utility.String("1900001109"),
28 OutBudgetNo: wxpay_utility.String("budget202506300102"),
29 }
30
31 response, err := GetBudget(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 GetBudget(config *wxpay_utility.MchConfig, request *GetBudgetRequest) (response *BudgetInfo, err error) {
43 const (
44 host = "https://api.mch.weixin.qq.com"
45 method = "GET"
46 path = "/v3/fund-app/mch-transfer/partner/charity-budget/{out_budget_no}"
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, "{out_budget_no}", url.PathEscape(*request.OutBudgetNo), -1)
54 query := reqUrl.Query()
55 query.Add("sponsor_mchid", *request.SponsorMchid)
56 reqUrl.RawQuery = query.Encode()
57 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
58 if err != nil {
59 return nil, err
60 }
61 httpRequest.Header.Set("Accept", "application/json")
62 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
63 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
64 if err != nil {
65 return nil, err
66 }
67 httpRequest.Header.Set("Authorization", authorization)
68
69 client := &http.Client{}
70 httpResponse, err := client.Do(httpRequest)
71 if err != nil {
72 return nil, err
73 }
74
75 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
76 if err != nil {
77 return nil, err
78 }
79
80 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
81
82 err = wxpay_utility.ValidateResponse(
83 config.WechatPayPublicKeyId(),
84 config.WechatPayPublicKey(),
85 &httpResponse.Header,
86 respBody,
87 )
88 if err != nil {
89 return nil, err
90 }
91
92 if err := json.Unmarshal(respBody, response); err != nil {
93 return nil, err
94 }
95
96 return response, nil
97 } else {
98 return nil, wxpay_utility.NewApiException(
99 httpResponse.StatusCode,
100 httpResponse.Header,
101 respBody,
102 )
103 }
104}
105
106type GetBudgetRequest struct {
107 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
108 OutBudgetNo *string `json:"out_budget_no,omitempty"`
109}
110
111func (o *GetBudgetRequest) MarshalJSON() ([]byte, error) {
112 type Alias GetBudgetRequest
113 a := &struct {
114 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
115 OutBudgetNo *string `json:"out_budget_no,omitempty"`
116 *Alias
117 }{
118
119 SponsorMchid: nil,
120 OutBudgetNo: nil,
121 Alias: (*Alias)(o),
122 }
123 return json.Marshal(a)
124}
125
126type BudgetInfo struct {
127 SpMchid *string `json:"sp_mchid,omitempty"`
128 OutBudgetNo *string `json:"out_budget_no,omitempty"`
129 BudgetId *string `json:"budget_id,omitempty"`
130 Amount *int64 `json:"amount,omitempty"`
131 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
132 ActivityName *string `json:"activity_name,omitempty"`
133 ActivityRemark *string `json:"activity_remark,omitempty"`
134 State *BudgetOrderState `json:"state,omitempty"`
135 ConfirmUrl *string `json:"confirm_url,omitempty"`
136 SuperAdminWxidMask *string `json:"super_admin_wxid_mask,omitempty"`
137 RemainAmount *int64 `json:"remain_amount,omitempty"`
138 UnlockRemark *string `json:"unlock_remark,omitempty"`
139 LockedTime *string `json:"locked_time,omitempty"`
140 FinishedTime *string `json:"finished_time,omitempty"`
141 ClosedTime *string `json:"closed_time,omitempty"`
142}
143
144type BudgetOrderState string
145
146func (e BudgetOrderState) Ptr() *BudgetOrderState {
147 return &e
148}
149
150const (
151 BUDGETORDERSTATE_PENDING BudgetOrderState = "PENDING"
152 BUDGETORDERSTATE_LOCKED BudgetOrderState = "LOCKED"
153 BUDGETORDERSTATE_FINISHED BudgetOrderState = "FINISHED"
154 BUDGETORDERSTATE_CLOSED BudgetOrderState = "CLOSED"
155)
156