/
agent.go
462 lines (377 loc) · 13.2 KB
/
agent.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package szamlazzhu
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strconv"
"strings"
"time"
)
// Agent is an authenticated agent that can interact with the szamlazz.hu API endpoint
// https://docs.szamlazz.hu
type Agent struct {
// One of username+password or token is required. Other must be zero value
username string // Authentication username
password string // Authentication password
token string // Authentication token
http *http.Client // Used for storing state between calls
}
// NewUserAgent creates a new Agent instance, based on username authentication
func NewUserAgent(username string, password string) *Agent {
var a Agent
a.username = username
a.password = password
a.init()
return &a
}
// NewAgentUser creates a new Agent instance, based on token authentication
func NewTokenAgent(token string) *Agent {
var a Agent
a.token = token
a.init()
return &a
}
// init performs the actual intialization of an empty Agent
func (a *Agent) init() {
a.http = new(http.Client)
a.http.Timeout = 60 * time.Second
// SzamlaAgent documentation specifically requires clients to handle cookies.
// Using "nil" as PublicSuffixList is safe as long this http client is only used by our API
a.http.Jar, _ = cookiejar.New(nil)
}
// GenerateInvoice creates a new invoice (or other document)
// https://docs.szamlazz.hu/#generating-invoices
func (a *Agent) GenerateInvoice(req Xmlszamla) (Xmlszamlavalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
req.ValaszVerzio = 2
// execute rpc
var rpl Xmlszamlavalasz
err := a.rpcCall(&req, "action-xmlagentxmlfile", &rpl)
if err != nil {
return Xmlszamlavalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlszamlavalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
var stornoRegex = regexp.MustCompile(`^\s*xmlagentresponse=DONE;(.+?)\s*$`)
// ReverseInvoice reverses an existing invoice and returns the reversing document number
// https://docs.szamlazz.hu/#reversing-an-invoice-storno
func (a *Agent) ReverseInvoice(req Xmlszamlast) (string, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl stringContainer
err := a.rpcCall(&req, "action-szamla_agent_st", &rpl)
if err != nil {
return "", err
}
// parse string reply
rplMatch := stornoRegex.FindStringSubmatch(rpl.s)
if rplMatch == nil {
return "", fmt.Errorf("server replied garbage: %q", rpl.s)
}
return rplMatch[1], nil
}
// RegisterCredit updates payment entries for an invoice
// https://docs.szamlazz.hu/#registering-a-credit-entry
func (a *Agent) RegisterCredit(req Xmlszamlakifiz) error {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl stringContainer
err := a.rpcCall(&req, "action-szamla_agent_kifiz", &rpl)
if err != nil {
return err
}
// check string reply
if strings.TrimSpace(rpl.s) != "xmlagentresponse=DONE" {
return fmt.Errorf("server replied garbage: %q", rpl.s)
}
return nil
}
// QueryInvoicePdf downloads the PDF representation of an invoice
// https://docs.szamlazz.hu/#querying-the-invoice-pdf
func (a *Agent) QueryInvoicePdf(req Xmlszamlapdf) (Xmlszamlavalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
req.ValaszVerzio = 2
// execute rpc
var rpl Xmlszamlavalasz
err := a.rpcCall(&req, "action-szamla_agent_pdf", &rpl)
if err != nil {
return Xmlszamlavalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlszamlavalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
// QueryInvoiceXml downloads invoice details
// https://docs.szamlazz.hu/#querying-the-invoice-pdf
func (a *Agent) QueryInvoiceXml(req Xmlszamlaxml) (Szamla, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl Szamla
err := a.rpcCall(&req, "action-szamla_agent_xml", &rpl)
if err != nil {
return Szamla{}, err
}
return rpl, nil
}
// DeleteProforma removes a proforma invoice (díjbekérő) from szamlazz.hu
// https://docs.szamlazz.hu/#deleting-a-pro-forma-invoice
func (a *Agent) DeleteProforma(req Xmlszamladbkdel) (Xmlszamladbkdelvalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl Xmlszamladbkdelvalasz
err := a.rpcCall(&req, "action-szamla_agent_dijbekero_torlese", &rpl)
if err != nil {
return Xmlszamladbkdelvalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlszamladbkdelvalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
// GenerateReceipt creates a new receipt
// https://docs.szamlazz.hu/#generating-a-receipt
func (a *Agent) GenerateReceipt(req Xmlnyugtacreate) (Xmlnyugtavalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl Xmlnyugtavalasz
err := a.rpcCall(&req, "action-szamla_agent_nyugta_create", &rpl)
if err != nil {
return Xmlnyugtavalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlnyugtavalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
// StornoReceipt is an alias of ReverseReceipt for compatibility reasons.
// Deprecated!
func (a *Agent) StornoReceipt(req Xmlnyugtast) (Xmlnyugtavalasz, error) {
return a.ReverseReceipt(req)
}
// ReverseReceipt reverses an existing receipt
// https://docs.szamlazz.hu/#reversing-a-receipt-storno
func (a *Agent) ReverseReceipt(req Xmlnyugtast) (Xmlnyugtavalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl Xmlnyugtavalasz
err := a.rpcCall(&req, "action-szamla_agent_nyugta_storno", &rpl)
if err != nil {
return Xmlnyugtavalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlnyugtavalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
// QueryReceipt downloads an existing receipt
// https://docs.szamlazz.hu/#querying-a-receipt
func (a *Agent) QueryReceipt(req Xmlnyugtaget) (Xmlnyugtavalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl Xmlnyugtavalasz
err := a.rpcCall(&req, "action-szamla_agent_nyugta_get", &rpl)
if err != nil {
return Xmlnyugtavalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlnyugtavalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
// SendReceipt sends an existing receipt via e-mail
// https://docs.szamlazz.hu/#sending-a-receipt
func (a *Agent) SendReceipt(req Xmlnyugtasend) (Xmlnyugtasendvalasz, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl Xmlnyugtasendvalasz
err := a.rpcCall(&req, "action-szamla_agent_nyugta_send", &rpl)
if err != nil {
return Xmlnyugtasendvalasz{}, err
}
// check for xml-level error
if !rpl.Sikeres {
return Xmlnyugtasendvalasz{}, &SzamlazzhuError{rpl.Hibakod, rpl.Hibauzenet}
}
return rpl, nil
}
// QueryTaxpayer requests taxpayer information from NAV via szamlazz.hu
// https://docs.szamlazz.hu/#querying-taxpayers
func (a *Agent) QueryTaxpayer(req Xmltaxpayer) (QueryTaxpayerResponse, error) {
// hardcoded fields
req.Felhasznalo = a.username
req.Jelszo = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl QueryTaxpayerResponse
err := a.rpcCall(&req, "action-szamla_agent_taxpayer", &rpl)
if err != nil {
return QueryTaxpayerResponse{}, err
}
// check for xml-level error
if rpl.ResultFuncCode != "OK" {
return QueryTaxpayerResponse{}, fmt.Errorf("%s %s %s", rpl.ResultFuncCode, rpl.ResultErrorCode, rpl.ResultMessage)
}
return rpl, nil
}
// SupplierStatus describes the state of a supplier account
type SupplierStatus int
const (
Unknown SupplierStatus = iota
NewCompany // New supplier account creation has started
NewCompanyResend // New supplier account creation was already started, e-mail resent
NewConnect // Supplier has existing account, access request sent
NewConnectResend // Supplier has existing account, access was already requested, e-mail resent
)
// agentresponseRegex parses a text-based reply for SupplierAccount calls
var agentresponseRegex = regexp.MustCompile(`^\s*xmlagentresponse=(.+?)\s*$`)
// SupplierAccount creates or requests access to a supplier's szamlazz.hu account
// https://docs.szamlazz.hu/#self-billing
func (a *Agent) SupplierAccount(req Xmlcegmb) (SupplierStatus, error) {
// Possible text responses found by us:
// xmlagentresponse=DONE
// xmlagentresponse=Már létező fiók, nincs fiókgazdája. Fiókgazdai meghívó (megbízott számlakibocsátás) email újraküldve.
// xmlagentresponse=Már létező fiók fiókgazdával. Csatlakozási kérelmet (ASK) küldtünk a fiókgazdának.
// xmlagentresponse=Már létező fiók fiókgazdával. A csatlakozási kérelem emailt (ASK) újraküldtük a fiókgazdának.
// hardcoded fields
req.LoginName = a.username
req.Password = a.password
req.SzamlaAgentKulcs = a.token
// execute rpc
var rpl stringContainer
err := a.rpcCall(&req, "action-agent_ceg_mb", &rpl)
if err != nil {
return Unknown, err
}
// parse string reply
rplMatch := agentresponseRegex.FindStringSubmatch(rpl.s)
if rplMatch == nil {
return Unknown, fmt.Errorf("server replied garbage: %q", rpl.s)
}
switch rplMatch[1] {
case "DONE":
return NewCompany, nil
case "Már létező fiók, nincs fiókgazdája. Fiókgazdai meghívó (megbízott számlakibocsátás) email újraküldve.":
return NewCompanyResend, nil
case "Már létező fiók fiókgazdával. Csatlakozási kérelmet (ASK) küldtünk a fiókgazdának.":
return NewConnect, nil
case "Már létező fiók fiókgazdával. A csatlakozási kérelem emailt (ASK) újraküldtük a fiókgazdának.":
return NewConnectResend, nil
default:
return Unknown, fmt.Errorf("unknown state xmlagentresponse=%s", rplMatch[1])
}
}
// apiEndpoint is the szamlazz.hu API URL
const apiEndpoint = "https://www.szamlazz.hu/szamla/"
// postAsFile will uploads a file as HTTP web form file upload
func (a *Agent) postAsFile(field string, filename string, data []byte) ([]byte, error) {
// Package data into a multipart message and buffer it
var buffer bytes.Buffer
msg := multipart.NewWriter(&buffer)
i, _ := msg.CreateFormFile(field, filename)
i.Write(data)
msg.Close()
// Send the HTTP request to the Szamlazz.hu server
response, err := a.http.Post(apiEndpoint, msg.FormDataContentType(), &buffer)
if err != nil {
return nil, err
}
defer response.Body.Close()
// Szamlazz.hu API always replies 200 OK. Anything else means the request was misrouted or lost.
if response.StatusCode != 200 {
return nil, fmt.Errorf("received HTTP %d %s during file upload", response.StatusCode, response.Status)
}
// Szamlazz.hu may indicate errors via non-standard HTTP custom headers
if ecode := response.Header.Get("szlahu_error_code"); ecode != "" {
c, _ := strconv.Atoi(ecode)
s, _ := url.QueryUnescape(response.Header.Get("szlahu_error"))
return nil, &SzamlazzhuError{c, s}
}
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("failed to read HTTP response %w", err)
}
return body, nil
}
// errRegex matches stack-trace embedded textual error messages from szamlazz.hu
var errRegex = regexp.MustCompile(`(?s)^\s*\[ERR\](.*?)----------`)
// rpcCall performs an RPC request to szamlazz.hu API
// This function takes care of the encoding/decoding of message structs, as well as submitting the encoded data
// Note that the request should, the reply _must_ be a pointer for correct behavior
func (a *Agent) rpcCall(request Message, field string, reply Message) error {
// Convert incoming data to XML representation
datareq, err := xml.Marshal(request)
if err != nil {
return fmt.Errorf("failed to marshal RPC message: %w", err)
}
// Communicate with szamlazz.hu
dataresp, err := a.postAsFile(field, "agent.xml", datareq)
if err != nil {
return err
}
// szamlazz.hu may throw a stacktrace for any request, we can find these errors via regex
if szlaerr := errRegex.FindSubmatch(dataresp); szlaerr != nil {
return &SzamlazzhuError{-1, string(szlaerr[1])}
}
// some endpoints return string instead of XML, use stringContainer to encapsulate them
if str, ok := reply.(*stringContainer); ok {
str.s = string(dataresp)
return nil
}
// attempt to understand the xml reply
err = xml.Unmarshal(dataresp, reply)
if err != nil {
// some endpoints return xmlszamlavalasz upon error instead of the contracted XSD schema
var xmlerror Xmlszamlavalasz
if errDecode := xml.Unmarshal(dataresp, &xmlerror); errDecode == nil && !xmlerror.Sikeres {
return &SzamlazzhuError{xmlerror.Hibakod, xmlerror.Hibauzenet}
}
return fmt.Errorf("failed to unmarshal RPC reply: %w", err)
}
return nil
}