This repository has been archived by the owner on Jun 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
94 lines (76 loc) · 2.02 KB
/
dispatcher.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
package eet
import (
"bytes"
"crypto/x509"
"encoding/xml"
"net/http"
"time"
"github.com/pkg/errors"
)
type Service string
const (
PlaygroundService Service = "https://pg.eet.cz:443/eet/services/EETServiceSOAP/v3"
ProductionService Service = "https://prod.eet.cz:443/eet/services/EETServiceSOAP/v3"
)
type Regime int
const (
RegularRegime Regime = iota
SimplifiedRegime
)
type Dispatcher struct {
service Service
signer *Signer
certificate *x509.Certificate
testing bool
}
func NewDispatcher(service Service, certPath, password string) (*Dispatcher, error) {
signer, err := NewSigner(certPath, password)
if err != nil {
return nil, errors.Wrap(err, "Failed to create signer")
}
d := Dispatcher{
service: service,
signer: signer,
}
return &d, nil
}
func (d *Dispatcher) SendPayment(receipt Receipt) (*Response, error) {
trzba, err := receipt.Trzba(d.signer)
if err != nil {
return nil, errors.Wrap(err, "Failed to convert Receipt to Trzba")
}
envelope, err := NewSOAPEnvelopeRequest(trzba, d.signer)
if err != nil {
return nil, errors.Wrap(err, "Failed to create SOAPEnvelopeRequest")
}
var buf bytes.Buffer
buf.WriteString(xml.Header)
if err := xml.NewEncoder(&buf).Encode(envelope); err != nil {
return nil, errors.Wrap(err, "Failed to marshal SOAPEnvelopeRequest")
}
client := &http.Client{Timeout: 5 * time.Second}
resp, err := client.Post(string(d.service), "application/xml", &buf)
if resp != nil {
defer func() {
_ = resp.Body.Close()
}()
}
if err != nil {
return nil, errors.Wrap(err, "Failed to send payment")
}
var resEnvelope SOAPEnvelopeResponse
if err := xml.NewDecoder(resp.Body).Decode(&resEnvelope); err != nil {
return nil, errors.Wrap(err, "Failed to xml.Unmarshal SOAPEnvelopeResponse")
}
odpoved := resEnvelope.Body.Odpoved
if odpoved.Chyba != nil {
return nil, odpoved.Chyba
}
response := Response{
DatPrij: odpoved.Hlavicka.DatPrij,
Fik: odpoved.Potvrzeni.Fik,
Bkp: odpoved.Hlavicka.Bkp,
odpoved: odpoved,
}
return &response, nil
}