
1package main
2
3import (
4 "bytes"
5 "demo/wxpay_utility"
6 "encoding/json"
7 "fmt"
8 "net/http"
9 "net/url"
10 "strings"
11)
12
13func main() {
14
15 config, err := wxpay_utility.CreateMchConfig(
16 "19xxxxxxxx",
17 "1DDE55AD98Exxxxxxxxxx",
18 "/path/to/apiclient_key.pem",
19 "PUB_KEY_ID_xxxxxxxxxxxxx",
20 "/path/to/wxp_pub.pem",
21 )
22 if err != nil {
23 fmt.Println(err)
24 return
25 }
26
27 request := &ApplyElectronicReceiptRequest{
28 OutBudgetNo: wxpay_utility.String("F1752898344298958610"),
29 StartDate: wxpay_utility.String("2025-09-01"),
30 EndDate: wxpay_utility.String("2025-09-30"),
31 SponsorMchid: wxpay_utility.String("1900102208"),
32 ReceiverMchid: wxpay_utility.String("1900102209"),
33 }
34
35 response, err := ApplyElectronicReceipt(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 ApplyElectronicReceipt(config *wxpay_utility.MchConfig, request *ApplyElectronicReceiptRequest) (response *BudgetElectronicReceipt, err error) {
47 const (
48 host = "https://api.mch.weixin.qq.com"
49 method = "POST"
50 path = "/v3/fund-app/mch-transfer/partner/budget/{out_budget_no}/electronic-receipts"
51 )
52
53 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
54 if err != nil {
55 return nil, err
56 }
57 reqUrl.Path = strings.Replace(reqUrl.Path, "{out_budget_no}", url.PathEscape(*request.OutBudgetNo), -1)
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 := &BudgetElectronicReceipt{}
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 ApplyElectronicReceiptRequest struct {
111 OutBudgetNo *string `json:"out_budget_no,omitempty"`
112 StartDate *string `json:"start_date,omitempty"`
113 EndDate *string `json:"end_date,omitempty"`
114 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
115 ReceiverMchid *string `json:"receiver_mchid,omitempty"`
116}
117
118func (o *ApplyElectronicReceiptRequest) MarshalJSON() ([]byte, error) {
119 type Alias ApplyElectronicReceiptRequest
120 a := &struct {
121 OutBudgetNo *string `json:"out_budget_no,omitempty"`
122 *Alias
123 }{
124
125 OutBudgetNo: nil,
126 Alias: (*Alias)(o),
127 }
128 return json.Marshal(a)
129}
130
131type BudgetElectronicReceipt struct {
132 OutBudgetNo *string `json:"out_budget_no,omitempty"`
133 StartDate *string `json:"start_date,omitempty"`
134 EndDate *string `json:"end_date,omitempty"`
135 SponsorMchid *string `json:"sponsor_mchid,omitempty"`
136 ReceiverMchid *string `json:"receiver_mchid,omitempty"`
137 ReceiptState *ReceiptState `json:"receipt_state,omitempty"`
138 AcceptId *string `json:"accept_id,omitempty"`
139 HashType *HashType `json:"hash_type,omitempty"`
140 HashValue *string `json:"hash_value,omitempty"`
141 DownloadUrl *string `json:"download_url,omitempty"`
142 FailReason *string `json:"fail_reason,omitempty"`
143}
144
145type ReceiptState string
146
147func (e ReceiptState) Ptr() *ReceiptState {
148 return &e
149}
150
151const (
152 RECEIPTSTATE_ACCEPTED ReceiptState = "ACCEPTED"
153 RECEIPTSTATE_FINISHED ReceiptState = "FINISHED"
154 RECEIPTSTATE_FAILED ReceiptState = "FAILED"
155)
156
157type HashType string
158
159func (e HashType) Ptr() *HashType {
160 return &e
161}
162
163const (
164 HASHTYPE_SM3 HashType = "SM3"
165)
166