
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 := &CreateBudgetRequest{
27 OutBudgetNo: wxpay_utility.String("budget202506300102"),
28 SponsorMchid: wxpay_utility.String("1900001109"),
29 SponsorSubjectName: wxpay_utility.String("腾讯控股有限公司"),
30 ActivityName: wxpay_utility.String("公益企业配捐活动"),
31 ActivityRemark: wxpay_utility.String("公益活动开始于9月1日,帮助困难儿童筹款活动"),
32 Amount: wxpay_utility.Int64(20000000),
33 NotifyUrl: wxpay_utility.String("https://www.weixin.qq.com/wxpay/pay.php"),
34 }
35
36 response, err := CreateBudget(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 CreateBudget(config *wxpay_utility.MchConfig, request *CreateBudgetRequest) (response *BudgetInfo, err error) {
48 const (
49 host = "https://api.mch.weixin.qq.com"
50 method = "POST"
51 path = "/v3/fund-app/mch-transfer/partner/charity-budget"
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 := &BudgetInfo{}
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 CreateBudgetRequest struct {
111 OutBudgetNo *string `json:"out_budget_no,omitempty"`
112 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
113 SponsorSubjectName *string `json:"sponsor_subject_name,omitempty"`
114 ActivityName *string `json:"activity_name,omitempty"`
115 ActivityRemark *string `json:"activity_remark,omitempty"`
116 Amount *int64 `json:"amount,omitempty"`
117 NotifyUrl *string `json:"notify_url,omitempty"`
118}
119
120type BudgetInfo struct {
121 SpMchid *string `json:"sp_mchid,omitempty"`
122 OutBudgetNo *string `json:"out_budget_no,omitempty"`
123 BudgetId *string `json:"budget_id,omitempty"`
124 Amount *int64 `json:"amount,omitempty"`
125 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
126 ActivityName *string `json:"activity_name,omitempty"`
127 ActivityRemark *string `json:"activity_remark,omitempty"`
128 State *BudgetOrderState `json:"state,omitempty"`
129 ConfirmUrl *string `json:"confirm_url,omitempty"`
130 RemainAmount *int64 `json:"remain_amount,omitempty"`
131 UnlockRemark *string `json:"unlock_remark,omitempty"`
132 LockedTime *string `json:"locked_time,omitempty"`
133 FinishedTime *string `json:"finished_time,omitempty"`
134 ClosedTime *string `json:"closed_time,omitempty"`
135 UnlockedAmount *int64 `json:"unlocked_amount,omitempty"`
136 ClosedReason *BudgetCloseReason `json:"closed_reason,omitempty"`
137}
138
139type BudgetOrderState string
140
141func (e BudgetOrderState) Ptr() *BudgetOrderState {
142 return &e
143}
144
145const (
146 BUDGETORDERSTATE_PENDING BudgetOrderState = "PENDING"
147 BUDGETORDERSTATE_LOCKED BudgetOrderState = "LOCKED"
148 BUDGETORDERSTATE_FINISHED BudgetOrderState = "FINISHED"
149 BUDGETORDERSTATE_CLOSED BudgetOrderState = "CLOSED"
150 BUDGETORDERSTATE_UNLOCKING BudgetOrderState = "UNLOCKING"
151)
152
153type BudgetCloseReason string
154
155func (e BudgetCloseReason) Ptr() *BudgetCloseReason {
156 return &e
157}
158
159const (
160 BUDGETCLOSEREASON_MCH_OVERDUE_UNCONFIRMED BudgetCloseReason = "MCH_OVERDUE_UNCONFIRMED"
161 BUDGETCLOSEREASON_CLOSE_VIA_MCH_API BudgetCloseReason = "CLOSE_VIA_MCH_API"
162)
163