-
Notifications
You must be signed in to change notification settings - Fork 3
/
invoice.go
136 lines (109 loc) · 3.71 KB
/
invoice.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
package api
import (
"crypto/sha256"
"encoding/base64"
"fmt"
"github.com/alapierre/go-ksef-client/ksef/cipher"
"github.com/alapierre/go-ksef-client/ksef/model"
log "github.com/sirupsen/logrus"
)
type InvoiceService interface {
SendInvoice(content []byte, token string) (*model.SendInvoiceResponse, error)
EncryptAndSend(content []byte, cipher cipher.AesCipher, token string) (*model.SendInvoiceResponse, error)
GetUpo(referenceNumber string) (*model.UpoDTO, error)
GetInvoice(invoiceId, token string) ([]byte, error)
}
type invoice struct {
client Client
}
func NewInvoiceService(client Client) InvoiceService {
return &invoice{client: client}
}
func (i *invoice) EncryptAndSend(content []byte, cipher cipher.AesCipher, token string) (*model.SendInvoiceResponse, error) {
res := &model.SendInvoiceResponse{}
req, err := prepareEncryptedSendInvoiceRequest(content, cipher)
if err != nil {
return nil, err
}
err = i.client.PutJson("/online/Invoice/Send", token, req, res)
return res, err
}
func (i *invoice) SendInvoice(content []byte, token string) (*model.SendInvoiceResponse, error) {
res := &model.SendInvoiceResponse{}
err := i.client.PutJson("/online/Invoice/Send", token, prepareSendInvoiceRequest(content), res)
return res, err
}
// GetUpo get official acknowledgment of sent invoices
func (i *invoice) GetUpo(referenceNumber string) (*model.UpoDTO, error) {
log.Debugf("Getting UPO for referenceNumber: %s", referenceNumber)
res := &model.StatusResponse{}
endpoint := fmt.Sprintf("/common/Status/%s", referenceNumber)
err := i.client.GetJsonNoAuth(endpoint, res)
upo := &model.UpoDTO{
ReferenceNumber: res.ReferenceNumber,
ProcessingCode: res.ProcessingCode,
ProcessingDescription: res.ProcessingDescription,
}
if res.Upo != "" {
upo.Upo, err = base64.StdEncoding.DecodeString(res.Upo)
}
return upo, err
}
// GetInvoice load invoice by KSeF reference number
func (i *invoice) GetInvoice(invoiceId, token string) ([]byte, error) {
log.Debugf("Getting invice by KSeF reference number: %s", invoiceId)
endpoint := fmt.Sprintf("/online/Invoice/Get/%s", invoiceId)
return i.client.Get(endpoint, token)
}
func prepareSendInvoiceRequest(content []byte) *model.SendInvoiceRequest {
digest := sha256.Sum256(content)
digestBase64 := base64.StdEncoding.EncodeToString(digest[:])
contentBase64 := base64.StdEncoding.EncodeToString(content)
return &model.SendInvoiceRequest{
InvoiceHash: model.InvoiceHash{
HashSHA: model.HashSHA{
Algorithm: "SHA-256",
Encoding: "Base64",
Value: digestBase64,
},
FileSize: len(content),
},
InvoicePayload: model.InvoicePayload{
Type: "plain",
InvoiceBody: contentBase64,
},
}
}
func prepareEncryptedSendInvoiceRequest(content []byte, cipher cipher.AesCipher) (*model.SendEncryptedInvoiceRequest, error) {
digest := sha256.Sum256(content)
digestBase64 := base64.StdEncoding.EncodeToString(digest[:])
encrypted, err := cipher.Encrypt(content)
if err != nil {
return nil, err
}
encryptedDigest := sha256.Sum256(encrypted)
encryptedDigestBase64 := base64.StdEncoding.EncodeToString(encryptedDigest[:])
contentBase64 := base64.StdEncoding.EncodeToString(encrypted)
return &model.SendEncryptedInvoiceRequest{
InvoiceHash: model.InvoiceHash{
HashSHA: model.HashSHA{
Algorithm: "SHA-256",
Encoding: "Base64",
Value: digestBase64,
},
FileSize: len(content),
},
InvoicePayload: model.EncryptedInvoicePayload{
Type: "encrypted",
EncryptedInvoiceHash: model.EncryptedInvoiceHash{
HashSHA: model.HashSHA{
Algorithm: "SHA-256",
Encoding: "Base64",
Value: encryptedDigestBase64,
},
FileSize: len(encrypted),
},
EncryptedInvoiceBody: contentBase64,
},
}, nil
}