
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 := &QuerySingleElectronicReceiptRequest{
27 SubMchid: wxpay_utility.String("1900001109"),
28 OutBillNo: wxpay_utility.String("plfk2020042013"),
29 }
30
31 response, err := QuerySingleElectronicReceipt(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 QuerySingleElectronicReceipt(config *wxpay_utility.MchConfig, request *QuerySingleElectronicReceiptRequest) (response *SingleElectronicReceipt, err error) {
43 const (
44 host = "https://api.mch.weixin.qq.com"
45 method = "GET"
46 path = "/v3/fund-app/mch-transfer/partner/electronic-receipts/{out_bill_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_bill_no}", url.PathEscape(*request.OutBillNo), -1)
54 query := reqUrl.Query()
55 if request.SubMchid != nil {
56 query.Add("sub_mchid", *request.SubMchid)
57 }
58 reqUrl.RawQuery = query.Encode()
59 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
60 if err != nil {
61 return nil, err
62 }
63 httpRequest.Header.Set("Accept", "application/json")
64 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
65 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.RequestURI(), nil)
66 if err != nil {
67 return nil, err
68 }
69 httpRequest.Header.Set("Authorization", authorization)
70
71 client := &http.Client{}
72 httpResponse, err := client.Do(httpRequest)
73 if err != nil {
74 return nil, err
75 }
76 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
77 if err != nil {
78 return nil, err
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 response := &SingleElectronicReceipt{}
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 QuerySingleElectronicReceiptRequest struct {
107 SubMchid *string `json:"sub_mchid,omitempty"`
108 OutBillNo *string `json:"out_bill_no,omitempty"`
109}
110
111func (o *QuerySingleElectronicReceiptRequest) MarshalJSON() ([]byte, error) {
112 type Alias QuerySingleElectronicReceiptRequest
113 a := &struct {
114 SubMchid *string `json:"sub_mchid,omitempty"`
115 OutBillNo *string `json:"out_bill_no,omitempty"`
116 *Alias
117 }{
118
119 SubMchid: nil,
120 OutBillNo: nil,
121 Alias: (*Alias)(o),
122 }
123 return json.Marshal(a)
124}
125
126type SingleElectronicReceipt struct {
127 SubMchid *string `json:"sub_mchid,omitempty"`
128 OutBillNo *string `json:"out_bill_no,omitempty"`
129 State *TransferSingleReceiptState `json:"state,omitempty"`
130 CreateTime *string `json:"create_time,omitempty"`
131 UpdateTime *string `json:"update_time,omitempty"`
132 HashType *HashType `json:"hash_type,omitempty"`
133 HashValue *string `json:"hash_value,omitempty"`
134 DownloadUrl *string `json:"download_url,omitempty"`
135}
136
137type TransferSingleReceiptState string
138
139func (e TransferSingleReceiptState) Ptr() *TransferSingleReceiptState {
140 return &e
141}
142
143const (
144 TRANSFERSINGLERECEIPTSTATE_GENERATING TransferSingleReceiptState = "GENERATING"
145 TRANSFERSINGLERECEIPTSTATE_FINISHED TransferSingleReceiptState = "FINISHED"
146)
147
148type HashType string
149
150func (e HashType) Ptr() *HashType {
151 return &e
152}
153
154const (
155 HASHTYPE_SM3 HashType = "SM3"
156)
157