Debian Codegraph @

segment/hbci_version.go

raw
1 package segment
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/mitch000001/go-hbci/domain"
8 )
9
10 // SupportedHBCIVersions maps integer version codes to HBCIVersions
11 var SupportedHBCIVersions = map[int]HBCIVersion{
12 220: HBCI220,
13 300: FINTS300,
14 }
15
16 // HBCIVersion defines segment constructors for a HBCI version
17 type HBCIVersion struct {
18 version int
19 PinTanEncryptionHeader func(clientSystemId string, keyName domain.KeyName) *EncryptionHeaderSegment
20 RDHEncryptionHeader func(clientSystemId string, keyName domain.KeyName, key []byte) *EncryptionHeaderSegment
21 SignatureHeader func() *SignatureHeaderSegment
22 PinTanSignatureHeader func(controlReference string, clientSystemId string, keyName domain.KeyName) *SignatureHeaderSegment
23 RDHSignatureHeader func(controlReference string, signatureId int, clientSystemId string, keyName domain.KeyName) *SignatureHeaderSegment
24 SignatureEnd func() *SignatureEndSegment
25 SynchronisationRequest func(modus SyncMode) *SynchronisationRequestSegment
26 AccountBalanceRequest func(account domain.AccountConnection, allAccounts bool) AccountBalanceRequest
27 AccountTransactionRequest func(account domain.AccountConnection, allAccounts bool) *AccountTransactionRequestSegment
28 SepaAccountTransactionRequest func(account domain.InternationalAccountConnection, allAccounts bool) *AccountTransactionRequestSegment
29 StatusProtocolRequest func(from, to time.Time, maxEntries int, continuationReference string) StatusProtocolRequest
30 TanProcess4Request func(referencingSegmentID string) *TanRequestSegment
31 }
32
33 // Version returns the HBCI version as integer
34 func (v HBCIVersion) Version() int {
35 return v.version
36 }
37
38 // Builder represents a builder which returns certain builders based on the
39 // provided versions
40 type Builder interface {
41 AccountBalanceRequest(account domain.AccountConnection, allAccounts bool) (AccountBalanceRequest, error)
42 AccountTransactionRequest(account domain.AccountConnection, allAccounts bool) (*AccountTransactionRequestSegment, error)
43 SepaAccountTransactionRequest(account domain.InternationalAccountConnection, allAccounts bool) (*AccountTransactionRequestSegment, error)
44 StatusProtocolRequest(from, to time.Time, maxEntries int, continuationReference string) (StatusProtocolRequest, error)
45 }
46
47 // NewBuilder returns a new Builder which uses the supported segments to
48 // identify which segment to use
49 func NewBuilder(supportedSegments []VersionedSegment) Builder {
50 segments := make(map[string][]int)
51 for _, s := range supportedSegments {
52 segments[s.ID] = append(segments[s.ID], s.Version)
53 }
54 return &builder{segments}
55 }
56
57 type builder struct {
58 supportedSegments map[string][]int
59 }
60
61 func (b *builder) AccountBalanceRequest(account domain.AccountConnection, allAccounts bool) (AccountBalanceRequest, error) {
62 versions, ok := b.supportedSegments["HISALS"]
63 if !ok {
64 return nil, fmt.Errorf("Segment %s not supported", "HKSAL")
65 }
66 request, err := AccountBalanceRequestBuilder(versions)
67 if err != nil {
68 return nil, err
69 }
70 return request(account, allAccounts), nil
71 }
72 func (b *builder) AccountTransactionRequest(account domain.AccountConnection, allAccounts bool) (*AccountTransactionRequestSegment, error) {
73 versions, ok := b.supportedSegments["HIKAZS"]
74 if !ok {
75 return nil, fmt.Errorf("Segment %s not supported", "HKKAZ")
76 }
77 request, err := AccountTransactionRequestBuilder(versions)
78 if err != nil {
79 return nil, err
80 }
81 return request(account, allAccounts), nil
82 }
83 func (b *builder) SepaAccountTransactionRequest(account domain.InternationalAccountConnection, allAccounts bool) (*AccountTransactionRequestSegment, error) {
84 versions, ok := b.supportedSegments["HIKAZS"]
85 if !ok {
86 return nil, fmt.Errorf("Segment %s not supported", "HKKAZ")
87 }
88 request, err := SepaAccountTransactionRequestBuilder(versions)
89 if err != nil {
90 return nil, err
91 }
92 return request(account, allAccounts), nil
93 }
94 func (b *builder) StatusProtocolRequest(from, to time.Time, maxEntries int, continuationReference string) (StatusProtocolRequest, error) {
95 versions, ok := b.supportedSegments["HIPRO"]
96 if !ok {
97 return nil, fmt.Errorf("Segment %s not supported", "HKPRO")
98 }
99 request, err := StatusProtocolRequestBuilder(versions)
100 if err != nil {
101 return nil, err
102 }
103 return request(from, to, maxEntries, continuationReference), nil
104 }