-
Notifications
You must be signed in to change notification settings - Fork 10
/
x11.go
187 lines (168 loc) · 4.87 KB
/
x11.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package webmoney
import (
"bytes"
"encoding/xml"
"errors"
"fmt"
"github.com/paulrosania/go-charset/charset"
_ "github.com/paulrosania/go-charset/data"
"io"
"net/http"
"os"
"strings"
)
var (
ErrAttestatRecalled = errors.New("attestat recalled")
ErrAttestatNotFound = errors.New("attestat not found")
)
type RequestX11 struct {
XMLName xml.Name `xml:"request"`
Wmid string `xml:"wmid"`
PassportWmid string `xml:"passportwmid"`
Sign string `xml:"sign"`
Dict string `xml:"params>dict"`
Info string `xml:"params>info"`
Mode string `xml:"params>mode"`
}
type ResponseX11 struct {
XMLName xml.Name `xml:"response"`
Retval int `xml:"retval,attr"`
CertInfo CertInfo `xml:"certinfo"`
}
type CertInfo struct {
XMLName xml.Name `xml:"certinfo"`
Attestat Attestat `xml:"attestat>row"`
Wmid string `xml:"wmid,attr"`
Wmids []WmidP `xml:"wmids>row"`
UserInfo UserInfo `xml:"userinfo>value>row"`
}
type WmidP struct {
XMLName xml.Name `xml:"row"`
Wmid string `xml:"wmid,attr"`
Info string `xml:"info,attr"`
Nickname string `xml:"nickname,attr"`
Datereg string `xml:"datereg,attr"`
Ctype string `xml:"ctype,attr"`
Companyname string `xml:"companyname,attr"`
Companyid string `xml:"companyid,attr"`
}
type UserInfo struct {
XMLName xml.Name `xml:"row"`
Nickname string `xml:"nickname,attr"`
Country string `xml:"country,attr"`
City string `xml:"city,attr"`
Zipcode string `xml:"zipcode,attr"`
Adres string `xml:"adres,attr"`
Fname string `xml:"fname,attr"`
Iname string `xml:"iname,attr"`
Oname string `xml:"oname,attr"`
Pnomer string `xml:"pnomer,attr"`
Pdate string `xml:"pdate,attr"`
Pdateend string `xml:"pdateend,attr"`
Pcountry string `xml:"pcountry,attr"`
Pbywhom string `xml:"pbywhom,attr"`
Inn string `xml:"inn,attr"`
Bplace string `xml:"bplace,attr"`
Bday string `xml:"bday,attr"`
Bmonth string `xml:"bmonth,attr"`
Byear string `xml:"byear,attr"`
Icq string `xml:"icq,attr"`
Email string `xml:"email,attr"`
Web string `xml:"web,attr"`
Phone string `xml:"phone,attr"`
CapOwner string `xml:"cap_owner,attr"`
Pasdoc string `xml:"pasdoc,attr"`
Regdoc string `xml:"regdoc,attr"`
Inndoc string `xml:"inndoc,attr"`
Photoid string `xml:"phoneid,attr"`
Jabberid string `xml:"jabberid,attr"`
Sex string `xml:"sex,attr"`
Infoopen string `xml:"infoopen,attr"`
}
type Attestat struct {
TID string `xml:"tid,attr"`
Recalled string `xml:"recalled,attr"`
Datecrt string `xml:"datecrt,attr"`
Dateupd string `xml:"dateupd,attr"`
Regnikname string `xml:"regnickname,attr"`
Regwmid string `xml:"regwmid,attr"`
Status string `xml:"status,attr"`
Notary string `xml:"notary,attr"`
}
func IssetWmid(wmid string) (bool, error) {
if result, err := GetInfoWmid(wmid); result.CertInfo.Wmid == "" || err != nil {
return false, err
}
return true, nil
}
func GetInfoWmid(wmid string) (ResponseX11, error) {
v := RequestX11{PassportWmid: wmid}
v1 := ResponseX11{}
out, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
return v1, err
}
apiPassportDomain := "apipassport.web.money"
if v, ok := os.LookupEnv("API_PASSPORT_DOMAIN"); ok {
apiPassportDomain = v
}
resp, err := http.Post(fmt.Sprintf("https://%s/asp/XMLGetWMPassport.asp", apiPassportDomain), "text/xml", strings.NewReader(string(out)))
if err != nil {
return v1, err
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return v1, err
}
r := bytes.NewReader(body)
dec := xml.NewDecoder(r)
dec.CharsetReader = charset.NewReader
err = dec.Decode(&v1)
if err != nil {
return ResponseX11{}, err
}
if v1.CertInfo.Attestat.Recalled == "1" {
return v1, ErrAttestatRecalled
}
if v1.CertInfo.Attestat.TID != "" {
return v1, nil
} else {
return v1, ErrAttestatNotFound
}
}
func WMPassportByWmid(wmid string) (*ResponseX11, error) {
v := RequestX11{PassportWmid: wmid}
out, err := xml.MarshalIndent(v, " ", " ")
if err != nil {
return nil, err
}
apiPassportDomain := "apipassport.web.money"
if v, ok := os.LookupEnv("API_PASSPORT_DOMAIN"); ok {
apiPassportDomain = v
}
resp, err := http.Post(fmt.Sprintf("https://%s/asp/XMLGetWMPassport.asp", apiPassportDomain), "text/xml", strings.NewReader(string(out)))
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
return nil, err
}
r := bytes.NewReader(body)
dec := xml.NewDecoder(r)
dec.CharsetReader = charset.NewReader
v1 := &ResponseX11{}
err = dec.Decode(v1)
if err != nil {
return nil, err
}
return v1, nil
}
func (w *WmClient) GetInfoWmid(wmid string) (ResponseX11, error) {
return GetInfoWmid(wmid)
}
func (w *WmClient) IssetWmid(wmid string) (bool, error) {
return IssetWmid(wmid)
}