-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
320 lines (301 loc) · 11 KB
/
client.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
package client
import (
"fmt"
"strings"
"time"
"github.com/mitch000001/go-hbci/bankinfo"
"github.com/mitch000001/go-hbci/dialog"
"github.com/mitch000001/go-hbci/domain"
"github.com/mitch000001/go-hbci/element"
"github.com/mitch000001/go-hbci/message"
"github.com/mitch000001/go-hbci/segment"
"github.com/mitch000001/go-hbci/transport"
)
// Config defines the basic configuration needed for a Client to work.
type Config struct {
BankID string `json:"bank_id"`
AccountID string `json:"account_id"`
PIN string `json:"pin"`
URL string `json:"url"`
HBCIVersion int `json:"hbci_version"`
Transport transport.Transport
}
func (c Config) hbciVersion() (segment.HBCIVersion, error) {
version, ok := segment.SupportedHBCIVersions[c.HBCIVersion]
if !ok {
return version, fmt.Errorf("Unsupported HBCI version. Supported versions are %v", domain.SupportedHBCIVersions)
}
return version, nil
}
// New creates a new HBCI client. It returns an error if the provided
// HBCI-Version of the config is not supported or if there is no entry in the
// bank institute database for the provided BankID.
//
// If the provided Config does not provide a URL or a HBCI-Version it will be
// looked up in the bankinfo database.
func New(config Config) (*Client, error) {
bankID := domain.BankID{
CountryCode: 280,
ID: config.BankID,
}
bankInfo := bankinfo.FindByBankID(config.BankID)
var (
url string
hbciVersion segment.HBCIVersion
)
if config.URL != "" {
url = config.URL
} else {
url = bankInfo.URL
}
if config.HBCIVersion > 0 {
version, err := config.hbciVersion()
if err != nil {
return nil, err
}
hbciVersion = version
} else {
version, ok := segment.SupportedHBCIVersions[bankInfo.HbciVersion()]
if !ok {
return nil, fmt.Errorf("Unsupported HBCI version. Supported versions are %v", domain.SupportedHBCIVersions)
}
hbciVersion = version
}
dcfg := dialog.Config{
BankID: bankID,
HBCIURL: url,
UserID: config.AccountID,
HBCIVersion: hbciVersion,
Transport: config.Transport,
}
d := dialog.NewPinTanDialog(dcfg)
d.SetPin(config.PIN)
client := &Client{
config: config,
hbciVersion: hbciVersion,
pinTanDialog: d,
}
return client, nil
}
// Client is the main entrypoint to perform high level HBCI requests.
//
// Its methods reflect possible actions and abstract the lower level dialog
// methods.
type Client struct {
config Config
hbciVersion segment.HBCIVersion
pinTanDialog *dialog.PinTanDialog
}
func (c *Client) init() error {
if c.pinTanDialog.BankParameterDataVersion() == 0 {
_, err := c.pinTanDialog.SyncClientSystemID()
if err != nil {
return fmt.Errorf("Error while fetching accounts: %v", err)
}
}
return nil
}
// Accounts return the basic account information for the provided client config.
func (c *Client) Accounts() ([]domain.AccountInformation, error) {
if err := c.init(); err != nil {
return nil, err
}
return c.pinTanDialog.Accounts, nil
}
// AccountTransactions return all transactions for the provided timeframe.
// If allAccouts is true, it will fetch all transactions associated with the
// proviced account. For the initial request no continuationReference is
// needed, as this method will be called recursivly if the server sends one.
func (c *Client) AccountTransactions(account domain.AccountConnection, timeframe domain.Timeframe, allAccounts bool, continuationReference string) ([]domain.AccountTransaction, error) {
if err := c.init(); err != nil {
return nil, err
}
builder := segment.NewBuilder(c.pinTanDialog.SupportedSegments())
accountTransactionRequest, err := builder.AccountTransactionRequest(account, allAccounts)
if err != nil {
return nil, err
}
accountTransactionRequest.SetTransactionRange(timeframe)
if continuationReference != "" {
accountTransactionRequest.SetContinuationReference(continuationReference)
}
decryptedMessage, err := c.pinTanDialog.SendMessage(message.NewHBCIMessage(c.hbciVersion, accountTransactionRequest))
if err != nil {
return nil, err
}
acknowledgements := decryptedMessage.Acknowledgements()
for _, ack := range acknowledgements {
if ack.Code == element.AcknowledgementAdditionalInformation {
fmt.Printf("Additional information: %+v\n", ack)
}
}
var accountTransactions []domain.AccountTransaction
accountTransactionResponses := decryptedMessage.FindSegments("HIKAZ")
if accountTransactionResponses != nil {
type response struct {
transactions []domain.AccountTransaction
err error
}
resFn := func(tr []domain.AccountTransaction, err error) response {
return response{tr, err}
}
responses := make(chan response, len(accountTransactionResponses))
for _, unmarshaledSegment := range accountTransactionResponses {
seg := unmarshaledSegment.(segment.AccountTransactionResponse)
accountTransactions = append(accountTransactions, seg.Transactions()...)
if seg != nil {
go func() {
responses <- resFn(c.AccountTransactions(account, timeframe, allAccounts, continuationReference))
}()
} else {
responses <- resFn([]domain.AccountTransaction{}, nil)
}
}
var errs []string
for {
if len(responses) == 0 {
break
}
res := <-responses
accountTransactions = append(accountTransactions, res.transactions...)
if res.err != nil {
errs = append(errs, fmt.Sprintf("%T:%v", res.err, res.err))
}
}
if len(errs) != 0 {
return nil, fmt.Errorf("Got errors: %s", strings.Join(errs, "\t"))
}
} else {
return nil, fmt.Errorf("Malformed response: expected HIKAZ segment")
}
return accountTransactions, nil
}
// SepaAccountTransactions return all transactions for the provided timeframe.
// If allAccouts is true, it will fetch all transactions associated with the
// provided account. For the initial request no continuationReference is
// needed, as this method will be called recursivly if the server sends one.
func (c *Client) SepaAccountTransactions(account domain.InternationalAccountConnection, timeframe domain.Timeframe, allAccounts bool, continuationReference string) ([]domain.AccountTransaction, error) {
if err := c.init(); err != nil {
return nil, err
}
accountTransactionRequest := c.hbciVersion.SepaAccountTransactionRequest(account, allAccounts)
accountTransactionRequest.SetTransactionRange(timeframe)
if continuationReference != "" {
accountTransactionRequest.SetContinuationReference(continuationReference)
}
decryptedMessage, err := c.pinTanDialog.SendMessage(message.NewHBCIMessage(c.hbciVersion, accountTransactionRequest))
if err != nil {
return nil, err
}
acknowledgements := decryptedMessage.Acknowledgements()
for _, ack := range acknowledgements {
if ack.Code == element.AcknowledgementAdditionalInformation {
fmt.Printf("Additional information: %+v\n", ack)
}
}
var accountTransactions []domain.AccountTransaction
accountTransactionResponses := decryptedMessage.FindSegments("HIKAZ")
if accountTransactionResponses != nil {
for _, unmarshaledSegment := range accountTransactionResponses {
seg := unmarshaledSegment.(segment.AccountTransactionResponse)
accountTransactions = append(accountTransactions, seg.Transactions()...)
}
} else {
return nil, fmt.Errorf("Malformed response: expected HIKAZ segment")
}
return accountTransactions, nil
}
// AccountInformation will print all information attached to the provided
// account. If allAccounts is true it will fetch also the information
// associated with the account.
func (c *Client) AccountInformation(account domain.AccountConnection, allAccounts bool) error {
if err := c.init(); err != nil {
return err
}
accountInformationRequest := segment.NewAccountInformationRequestSegmentV1(account, allAccounts)
decryptedMessage, err := c.pinTanDialog.SendMessage(message.NewHBCIMessage(c.hbciVersion, accountInformationRequest))
if err != nil {
return err
}
accountInfoResponse := decryptedMessage.FindMarshaledSegment("HIKIF")
if accountInfoResponse != nil {
fmt.Printf("Account Info: %s\n", accountInfoResponse)
return nil
}
return fmt.Errorf("Malformed response: expected HIKIF segment")
}
// AccountBalances retrieves the balance for the provided account.
// If allAccounts is true it will fetch also the balances for all accounts
// associated with the account.
func (c *Client) AccountBalances(account domain.AccountConnection, allAccounts bool) ([]domain.AccountBalance, error) {
if err := c.init(); err != nil {
return nil, err
}
builder := segment.NewBuilder(c.pinTanDialog.SupportedSegments())
accountBalanceRequest, err := builder.AccountBalanceRequest(account, allAccounts)
if err != nil {
return nil, err
}
decryptedMessage, err := c.pinTanDialog.SendMessage(
message.NewHBCIMessage(c.hbciVersion, accountBalanceRequest),
)
if err != nil {
return nil, err
}
var balances []domain.AccountBalance
balanceResponses := decryptedMessage.FindMarshaledSegments("HISAL")
if balanceResponses != nil {
for _, marshaledSegment := range balanceResponses {
balanceSegment := &segment.AccountBalanceResponseSegment{}
err = balanceSegment.UnmarshalHBCI(marshaledSegment)
if err != nil {
return nil, fmt.Errorf("Error while parsing account balance: %v", err)
}
balances = append(balances, balanceSegment.AccountBalance())
}
} else {
return nil, fmt.Errorf("Malformed response: expected HISAL segment")
}
return balances, nil
}
// Status returns information about open jobs to fetch from the institute.
// If a continuationReference is present, the status information attached to it
// will be fetched.
func (c *Client) Status(from, to time.Time, maxEntries int, continuationReference string) ([]domain.StatusAcknowledgement, error) {
if err := c.init(); err != nil {
return nil, err
}
builder := segment.NewBuilder(c.pinTanDialog.SupportedSegments())
statusRequest, err := builder.StatusProtocolRequest(from, to, maxEntries, continuationReference)
if err != nil {
return nil, err
}
bankMessage, err := c.pinTanDialog.SendMessage(message.NewHBCIMessage(c.hbciVersion, statusRequest))
if err != nil {
return nil, err
}
var statusAcknowledgements []domain.StatusAcknowledgement
statusResponses := bankMessage.FindSegments("HIPRO")
if statusResponses != nil {
for _, seg := range statusResponses {
statusResponse := seg.(segment.StatusProtocolResponse)
statusAcknowledgements = append(statusAcknowledgements, statusResponse.Status())
}
}
return statusAcknowledgements, nil
}
// AnonymousClient wraps a Client and allows anonymous requests to bank
// institutes. Examples for those jobs are stock exchange news.
type AnonymousClient struct {
*Client
}
// CommunicationAccess returns data used to make calls to a given institute.
// Not yet properly implemented, therefore only the raw data are returned.
func (a *AnonymousClient) CommunicationAccess(from, to domain.BankID, maxEntries int) ([]byte, error) {
commRequest := segment.NewCommunicationAccessRequestSegment(from, to, maxEntries, "")
decryptedMessage, err := a.pinTanDialog.SendAnonymousMessage(message.NewHBCIMessage(a.hbciVersion, commRequest))
if err != nil {
return nil, err
}
return []byte(fmt.Sprintf("%+#v", decryptedMessage)), nil
}