
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 := &QueryElecsignByOutNoRequest{
27 OutBillNo: wxpay_utility.String("plfk2020042013"),
28 }
29
30 response, err := QueryElecsignByOutNo(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 QueryElecsignByOutNo(config *wxpay_utility.MchConfig, request *QueryElecsignByOutNoRequest) (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/out-bill-no/{out_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, "{out_bill_no}", url.PathEscape(*request.OutBillNo), -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 QueryElecsignByOutNoRequest struct {
114 OutBillNo *string `json:"out_bill_no,omitempty"`
115}
116
117func (o *QueryElecsignByOutNoRequest) MarshalJSON() ([]byte, error) {
118 type Alias QueryElecsignByOutNoRequest
119 a := &struct {
120 OutBillNo *string `json:"out_bill_no,omitempty"`
121 *Alias
122 }{
123
124 OutBillNo: nil,
125 Alias: (*Alias)(o),
126 }
127 return json.Marshal(a)
128}
129
130type QueryElecsignResponse struct {
131 State *ElecsignStatus `json:"state,omitempty"`
132 CreateTime *string `json:"create_time,omitempty"`
133 UpdateTime *string `json:"update_time,omitempty"`
134 HashType *ElecsignHashType `json:"hash_type,omitempty"`
135 HashValue *string `json:"hash_value,omitempty"`
136 DownloadUrl *string `json:"download_url,omitempty"`
137}
138
139type ElecsignStatus string
140
141func (e ElecsignStatus) Ptr() *ElecsignStatus {
142 return &e
143}
144
145const (
146 ELECSIGNSTATUS_GENERATING ElecsignStatus = "GENERATING"
147 ELECSIGNSTATUS_FINISHED ElecsignStatus = "FINISHED"
148 ELECSIGNSTATUS_FAILED ElecsignStatus = "FAILED"
149)