Class: FinTS::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fints/client.rb

Direct Known Subclasses

PinTanClient

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



13
14
15
# File 'lib/fints/client.rb', line 13

def initialize
  @accounts = []
end

Class Attribute Details

.loggerObject



6
7
8
9
10
# File 'lib/fints/client.rb', line 6

def logger
  @logger ||= Logger.new($stdout).tap do |log|
    log.progname = self.name
  end
end

Instance Method Details

#create_balance_message(dialog, account) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/fints/client.rb', line 73

def create_balance_message(dialog, )
  hversion = dialog.hksalversion

  acc = if [4, 5, 6].include?(hversion)
          [[:accountnumber], [:subaccount], '280', [:blz]].join(':')
        elsif hversion == 7
          [[:iban], [:bic], [:accountnumber], [:subaccount], '280', [:blz]].join(':')
        else
          raise ArgumentError, "Unsupported HKSAL version #{hversion}"
        end

  segment = Segment::HKSAL.new(3, hversion, acc)
  new_message(dialog, [segment])
end

#create_get_holdings_message(dialog, account) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/fints/client.rb', line 175

def create_get_holdings_message(dialog, )
  hversion = dialog.hksalversion

  acc = if [1, 2, 3, 4, 5, 6].include?(hversion)
          [[:accountnumber], [:subaccount], '280', [:blz]].join(':')
        elsif hversion == 7
          [[:iban], [:bic], [:accountnumber], [:subaccount], '280', [:blz]].join(':')
        else
          raise ArgumentError, "Unsupported HKSAL version #{hversion}"
        end

  new_message(dialog, [Segment::HKWPD.new(3, hversion, acc)])
end

#create_statement_message(dialog, account, start_date, end_date, touchdown) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fints/client.rb', line 131

def create_statement_message(dialog, , start_date, end_date, touchdown)
  hversion = dialog.hkkazversion

  acc = if [4, 5, 6].include?(hversion)
          [[:accountnumber], [:subaccount], '280', [:blz]].join(':')
        elsif hversion == 7
          [[:iban], [:bic], [:accountnumber], [:subaccount], '280', [:blz]].join(':')
        else
          raise ArgumentError, "Unsupported HKKAZ version #{hversion}"
        end

  segment = Segment::HKKAZ.new(3, hversion, acc, start_date, end_date, touchdown)
  new_message(dialog, [segment])
end

#get_balance(account) ⇒ Object



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
# File 'lib/fints/client.rb', line 43

def get_balance()
  FinTS::Client.logger.info("Start fetching balance")

  dialog = new_dialog
  dialog.sync
  dialog.init

  msg = create_balance_message(dialog, )
  FinTS::Client.logger.debug("Send message: #{msg}")
  resp = dialog.send_msg(msg)
  dialog.send_end

  # find segment and split up to balance part
  seg = resp.find_segment('HISAL')
  arr = Helper.split_for_data_elements(Helper.split_for_data_groups(seg)[4])

  amount = arr[1].sub(',', '.').to_f
  # 'C' for credit, 'D' for debit
  amount *= -1 if arr[0] == 'D'

  balance = {
    amount: amount,
    currency: arr[2],
    date: Date.parse(arr[3])
  }

  FinTS::Client.logger.debug("Balance: #{balance}")
  balance
end

#get_holdings(account) ⇒ Object



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
# File 'lib/fints/client.rb', line 146

def get_holdings()
  # init dialog
  dialog = self.new_dialog()
  dialog.sync
  dialog.init

  # execute job
  msg = create_get_holdings_message(dialog, )
  FinTS::Client.logger.debug("Sending HKWPD: #{msg}")
  resp = dialog.send_msg(msg)
  FinTS::Client.logger.debug("Got HIWPD response: #{resp}")

  # end dialog
  dialog.send_end

  # find segment and split up to balance part
  seg = resp.find_segment('HIWPD')
  if seg
    mt535_lines = seg.lines
    # The first line contains a FinTS HIWPD header - drop it.
    mt535_lines.delete_at(0)
    mt535 = MT535Miniparser.new
    mt535.parse(mt535_lines)
  else
    FinTS::Client.logger.warn('No HIWPD response segment found - maybe account has no holdings?')
    []
  end
end

#get_sepa_accountsObject



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
# File 'lib/fints/client.rb', line 17

def get_sepa_accounts
  dialog = new_dialog
  dialog.sync
  dialog.init

  msg_spa = new_message(dialog, [Segment::HKSPA.new(3, nil, nil, nil)])
  FinTS::Client.logger.debug("Sending HKSPA: #{msg_spa}")
  resp = dialog.send_msg(msg_spa)
  FinTS::Client.logger.debug("Got HKSPA response: #{resp}")
  dialog.send_end

  accounts = resp.find_segment('HISPA')
  raise SegmentNotFoundError, 'Could not find HISPA segment' if accounts.nil?
  accountlist = accounts.split('+').drop(1)
  @accounts = accountlist.map do |acc|
    arr = acc.split(':')
    {
      iban: arr[1],
      bic: arr[2],
      accountnumber: arr[3],
      subaccount: arr[4],
      blz: arr[6]
    }
  end
end

#get_statement(account, start_date, end_date) ⇒ Object



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
# File 'lib/fints/client.rb', line 88

def get_statement(, start_date, end_date)
  FinTS::Client.logger.info("Start fetching from #{start_date} to #{end_date}")

  dialog = new_dialog
  dialog.sync
  dialog.init

  msg = create_statement_message(dialog, , start_date, end_date, nil)
  FinTS::Client.logger.debug("Send message: #{msg}")
  resp = dialog.send_msg(msg)
  touchdowns = resp.get_touchdowns(msg)
  responses = [resp]
  touchdown_counter = 1

  while touchdowns.include?(Segment::HKKAZ)
    FinTS::Client.logger.info("Fetching more results (#{touchdown_counter})...")
    msg = create_statement_message(dialog, , start_date, end_date, touchdowns[Segment::HKKAZ])
    FinTS::Client.logger.debug("Send message: #{msg}")

    resp = dialog.send_msg(msg)
    responses << resp
    touchdowns = resp.get_touchdowns(msg)

    touchdown_counter += 1
  end

  FinTS::Client.logger.info('Fetching done.')
  re_data = /^[^@]*@([0-9]+?)@(.+)/m
  statement_response = ''
  responses.each do |r|
    seg = r.find_segment('HIKAZ')
    next unless seg
    match = re_data.match(seg)
    next unless match
    statement_response += match[2]
  end
  statement = Helper.mt940_to_array(statement_response)

  FinTS::Client.logger.debug("Statement: #{statement}")
  dialog.send_end
  statement
end