
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 "填入 商户号",
16 "填入 商户API证书序列号",
17 "填入 商户API证书私钥文件路径",
18 "填入 微信支付公钥ID",
19 "填入 微信支付公钥文件路径",
20 )
21 if err != nil {
22 fmt.Println(err)
23 return
24 }
25
26 request := &QueryElecsignByNoRequest{
27 TransferBillNo: wxpay_utility.String("1330000071100999991182020050700019480001"),
28 }
29
30 response, err := QueryElecsignByNo(config, request)
31 if err != nil {
32 fmt.Printf("请求失败: %+v\n", err)
33
34 return
35 }
36
37
38 fmt.Printf("请求成功: %+v\n", response)
39}
40
41func QueryElecsignByNo(config *wxpay_utility.MchConfig, request *QueryElecsignByNoRequest) (response *QueryElecsignResponse, err error) {
42 const (
43 host = "https://api.mch.weixin.qq.com"
44 method = "GET"
45 path = "/v3/fund-app/mch-transfer/elecsign/transfer-bill-no/{transfer_bill_no}"
46 )
47
48 reqUrl, err := url.Parse(fmt.Sprintf("%s%s", host, path))
49 if err != nil {
50 return nil, err
51 }
52 reqUrl.Path = strings.Replace(reqUrl.Path, "{transfer_bill_no}", url.PathEscape(*request.TransferBillNo), -1)
53 httpRequest, err := http.NewRequest(method, reqUrl.String(), nil)
54 if err != nil {
55 return nil, err
56 }
57 httpRequest.Header.Set("Accept", "application/json")
58 httpRequest.Header.Set("Wechatpay-Serial", config.WechatPayPublicKeyId())
59 authorization, err := wxpay_utility.BuildAuthorization(config.MchId(), config.CertificateSerialNo(), config.PrivateKey(), method, reqUrl.Path, nil)
60 if err != nil {
61 return nil, err
62 }
63 httpRequest.Header.Set("Authorization", authorization)
64
65 client := &http.Client{}
66 httpResponse, err := client.Do(httpRequest)
67 if err != nil {
68 return nil, err
69 }
70
71 respBody, err := wxpay_utility.ExtractResponseBody(httpResponse)
72 if err != nil {
73 return nil, err
74 }
75
76 if httpResponse.StatusCode >= 200 && httpResponse.StatusCode < 300 {
77
78 err = wxpay_utility.ValidateResponse(
79 config.WechatPayPublicKeyId(),
80 config.WechatPayPublicKey(),
81 &httpResponse.Header,
82 respBody,
83 )
84 if err != nil {
85 return nil, err
86 }
87
88 if err := json.Unmarshal(respBody, response); err != nil {
89 return nil, err
90 }
91
92 return response, nil
93 } else {
94 return nil, &wxpay_utility.ApiException{
95 StatusCode: httpResponse.StatusCode,
96 Header: httpResponse.Header,
97 Body: respBody,
98 }
99 }
100}
101
102type ElecsignHashType string
103
104func (e ElecsignHashType) Ptr() *ElecsignHashType {
105 return &e
106}
107
108const (
109 ELECSIGNHASHTYPE_SHA256 ElecsignHashType = "SHA256"
110 ELECSIGNHASHTYPE_SM3 ElecsignHashType = "SM3"
111)
112
113type QueryElecsignResponse struct {
114 State *ElecsignStatus `json:"state,omitempty"`
115 CreateTime *string `json:"create_time,omitempty"`
116 UpdateTime *string `json:"update_time,omitempty"`
117 HashType *ElecsignHashType `json:"hash_type,omitempty"`
118 HashValue *string `json:"hash_value,omitempty"`
119 DownloadUrl *string `json:"download_url,omitempty"`
120}
121
122type ElecsignStatus string
123
124func (e ElecsignStatus) Ptr() *ElecsignStatus {
125 return &e
126}
127
128const (
129 ELECSIGNSTATUS_GENERATING ElecsignStatus = "GENERATING"
130 ELECSIGNSTATUS_FINISHED ElecsignStatus = "FINISHED"
131 ELECSIGNSTATUS_FAILED ElecsignStatus = "FAILED"
132)
133
134type QueryElecsignByNoRequest struct {
135 TransferBillNo *string `json:"transfer_bill_no,omitempty"`
136}
137
138func (o *QueryElecsignByNoRequest) MarshalJSON() ([]byte, error) {
139 type Alias QueryElecsignByNoRequest
140 a := &struct {
141 TransferBillNo *string `json:"transfer_bill_no,omitempty"`
142 *Alias
143 }{
144
145 TransferBillNo: nil,
146 Alias: (*Alias)(o),
147 }
148 return json.Marshal(a)
149}