bdgtr-cli 0.2 → 0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/bin/bdgtr +26 -5
- data/lib/bdgtr/cli/config.rb +188 -0
- data/lib/bdgtr/cli/config.sample.yml +15 -0
- data/lib/bdgtr/cli/help.rb +168 -0
- data/lib/bdgtr/cli/ledger_sync.rb +102 -0
- data/lib/bdgtr/cli/prompt.rb +47 -0
- data/lib/bdgtr/cli/psd2.rb +530 -0
- data/lib/bdgtr/cli/rows.rb +38 -0
- data/lib/bdgtr/cli/runtime.rb +88 -0
- data/lib/bdgtr/cli/table.rb +458 -0
- data/lib/bdgtr/cli.rb +126 -0
- data/lib/bdgtr/core/binder.rb +79 -0
- data/lib/bdgtr/core/exchange.rb +66 -0
- data/lib/bdgtr/core/models/account.rb +177 -0
- data/lib/bdgtr/core/models/account_requisition.rb +12 -0
- data/lib/bdgtr/core/models/account_sync.rb +12 -0
- data/lib/bdgtr/core/models/import.rb +12 -0
- data/lib/bdgtr/core/models/ledger.rb +10 -0
- data/lib/bdgtr/core/models/requisition.rb +27 -0
- data/lib/bdgtr/core/models/transaction.rb +98 -0
- data/lib/bdgtr/core/models/transaction_category.rb +12 -0
- data/lib/bdgtr/core/record.rb +11 -0
- data/lib/bdgtr/core/schema.rb +290 -0
- data/lib/bdgtr/core/session.rb +76 -0
- data/lib/bdgtr/core/syncable.rb +272 -0
- data/lib/bdgtr/core/transfer.rb +90 -0
- data/lib/bdgtr/core.rb +18 -0
- data/lib/bdgtr/portfolio/Rakefile +20 -0
- data/lib/bdgtr/portfolio/cli/LICENSE +21 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/app.rb +77 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/import.rb +30 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli/spreadsheet.rb +25 -0
- data/lib/bdgtr/portfolio/cli/lib/portfolio_performance_api/cli.rb +52 -0
- data/lib/bdgtr/portfolio/exe/bdgtr +7 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/accounts_csv.rb +67 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/app.rb +104 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/auth.rb +52 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/balances.rb +157 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/cache.rb +46 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_menu.rb +90 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/checkbox_selection.rb +68 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/config.rb +138 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/decryptor.rb +70 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/drive_client.rb +182 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/errors.rb +10 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/file_reader.rb +80 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_import.rb +609 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/fineco_xls.rb +235 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import/fineco.rb +243 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/import.rb +85 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/parser.rb +254 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_source.rb +30 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/portfolio_store.rb +124 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/preview_window.rb +98 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/proto/client_pb.rb +29 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/row_preview.rb +174 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sheets_client.rb +272 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/snapshot.rb +27 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync.rb +247 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/sync_preview.rb +63 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_identity.rb +49 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_sync.rb +948 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/transaction_types.rb +142 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api/version.rb +5 -0
- data/lib/bdgtr/portfolio/lib/portfolio_performance_api.rb +42 -0
- data/lib/bdgtr/portfolio/proto/client.proto +114 -0
- data/lib/bdgtr/psd2/account.rb +24 -0
- data/lib/bdgtr/psd2/account_snapshot.rb +96 -0
- data/lib/bdgtr/psd2/callback.rb +49 -0
- data/lib/bdgtr/psd2/connect.rb +75 -0
- data/lib/bdgtr/psd2/enable_banking.rb +250 -0
- data/lib/bdgtr/psd2/go_cardless.rb +133 -0
- data/lib/bdgtr/psd2/institution.rb +17 -0
- data/lib/bdgtr/psd2/platform.rb +37 -0
- data/lib/bdgtr/psd2/requisition.rb +21 -0
- data/lib/bdgtr/psd2/transaction.rb +29 -0
- data/lib/bdgtr/psd2.rb +25 -0
- data/lib/bdgtr/sync/importer.rb +125 -0
- data/lib/bdgtr/sync/library.rb +18 -0
- data/lib/bdgtr/sync/portfolio.rb +14 -0
- data/lib/bdgtr/sync/projection.rb +38 -0
- data/lib/bdgtr/sync/spreadsheet.rb +23 -0
- data/lib/bdgtr/sync.rb +25 -0
- data/lib/bdgtr/version.rb +1 -1
- metadata +353 -30
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "openssl"
|
|
6
|
+
require "jwt"
|
|
7
|
+
|
|
8
|
+
module Bdgtr
|
|
9
|
+
module Psd2
|
|
10
|
+
# Enable Banking account-information API.
|
|
11
|
+
# https://enablebanking.com/docs/api/reference/#account-information-flow
|
|
12
|
+
class EnableBanking < Platform
|
|
13
|
+
API_URL = "https://api.enablebanking.com"
|
|
14
|
+
|
|
15
|
+
def institutions(country_code)
|
|
16
|
+
payload = request("GET", "/aspsps", query: { country: country_code, service: "AIS" })
|
|
17
|
+
Array(payload["aspsps"]).map do |row|
|
|
18
|
+
Institution.new(id: row["name"], name: row["name"], country: row["country"] || country_code, logo: row["logo"], raw: row)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def start_link(institution_id:, redirect_url:, country: nil, max_historical_days: 90)
|
|
23
|
+
bank = institutions(country).find { |item| item.id == institution_id } if country.present?
|
|
24
|
+
seconds = bank&.raw&.fetch("maximum_consent_validity", nil).to_i
|
|
25
|
+
seconds = 90.days.to_i if seconds <= 0
|
|
26
|
+
valid_until = Time.now.utc + [ seconds, 90.days.to_i ].min
|
|
27
|
+
body = {
|
|
28
|
+
access: { valid_until: valid_until.iso8601 },
|
|
29
|
+
aspsp: { name: institution_id, country: country },
|
|
30
|
+
state: SecureRandom.uuid,
|
|
31
|
+
redirect_url: redirect_url,
|
|
32
|
+
psu_type: "personal",
|
|
33
|
+
language: "en"
|
|
34
|
+
}
|
|
35
|
+
payload = request("POST", "/auth", body: body)
|
|
36
|
+
Requisition.new(
|
|
37
|
+
id: payload["authorization_id"],
|
|
38
|
+
platform: "enablebanking",
|
|
39
|
+
link: payload["url"],
|
|
40
|
+
max_historical_days: max_historical_days,
|
|
41
|
+
access_valid_until: valid_until,
|
|
42
|
+
raw: payload
|
|
43
|
+
)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def finish(callback)
|
|
47
|
+
code = callback[:code].presence || callback["code"].presence
|
|
48
|
+
if code.blank?
|
|
49
|
+
detail = callback["error_description"].presence || callback["error"].presence || "authorization was not completed"
|
|
50
|
+
raise Error, detail
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
payload = request("POST", "/sessions", body: { code: code })
|
|
54
|
+
Requisition.new(
|
|
55
|
+
id: payload["session_id"],
|
|
56
|
+
platform: "enablebanking",
|
|
57
|
+
status: "LN",
|
|
58
|
+
bank_accounts: self.class.bank_accounts_from(payload["accounts"]),
|
|
59
|
+
access_valid_until: time_or_nil(payload.dig("access", "valid_until")),
|
|
60
|
+
max_historical_days: callback[:max_historical_days],
|
|
61
|
+
raw: payload
|
|
62
|
+
)
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def get_requisition_by_id(requisition_id)
|
|
66
|
+
payload = request("GET", "/sessions/#{requisition_id}")
|
|
67
|
+
valid_until = time_or_nil(payload.dig("access", "valid_until"))
|
|
68
|
+
{
|
|
69
|
+
"status" => valid_until.nil? || valid_until > Time.current ? "LN" : "EX",
|
|
70
|
+
"accounts" => Array(payload["accounts"]).filter_map { |account| account["uid"] }
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def get_account(account_id)
|
|
75
|
+
details = request("GET", "/accounts/#{account_id}/details")
|
|
76
|
+
balances = request("GET", "/accounts/#{account_id}/balances")
|
|
77
|
+
iban = details.dig("account_id", "iban")
|
|
78
|
+
iban ||= Array(details["all_account_ids"]).filter_map { |row| row["iban"].presence }.first
|
|
79
|
+
Account.new(
|
|
80
|
+
id: account_id,
|
|
81
|
+
name: details["name"],
|
|
82
|
+
currency: details["currency"],
|
|
83
|
+
institution_id: details.dig("account_servicer", "name"),
|
|
84
|
+
metadata: {
|
|
85
|
+
"id" => account_id,
|
|
86
|
+
"institution_id" => details.dig("account_servicer", "bic_fi"),
|
|
87
|
+
"status" => "READY"
|
|
88
|
+
},
|
|
89
|
+
details: {
|
|
90
|
+
"account" => {
|
|
91
|
+
"name" => details["name"],
|
|
92
|
+
"currency" => details["currency"],
|
|
93
|
+
"iban" => iban,
|
|
94
|
+
"cashAccountType" => details["cash_account_type"],
|
|
95
|
+
"bic" => details.dig("account_servicer", "bic_fi"),
|
|
96
|
+
"product" => details["product"]
|
|
97
|
+
}.compact
|
|
98
|
+
},
|
|
99
|
+
balances: { "balances" => normalized_balances(balances) }
|
|
100
|
+
)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def get_transactions(options = {})
|
|
104
|
+
options = {
|
|
105
|
+
date_from: Date.current - 90,
|
|
106
|
+
date_to: Date.current
|
|
107
|
+
}.merge(options.compact)
|
|
108
|
+
booked = []
|
|
109
|
+
continuation = nil
|
|
110
|
+
loop do
|
|
111
|
+
query = {
|
|
112
|
+
date_from: options[:date_from].strftime("%Y-%m-%d"),
|
|
113
|
+
date_to: options[:date_to].strftime("%Y-%m-%d"),
|
|
114
|
+
transaction_status: "BOOK"
|
|
115
|
+
}
|
|
116
|
+
query[:continuation_key] = continuation if continuation.present?
|
|
117
|
+
page = request("GET", "/accounts/#{options[:requisition_account_id]}/transactions", query: query)
|
|
118
|
+
booked.concat(self.class.transactions_from(page))
|
|
119
|
+
continuation = page["continuation_key"]
|
|
120
|
+
break if continuation.blank?
|
|
121
|
+
end
|
|
122
|
+
{ "transactions" => { "booked" => booked.map(&:to_bank_row) } }
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def self.bank_accounts_from(accounts)
|
|
126
|
+
Array(accounts).filter_map do |account|
|
|
127
|
+
uid = account["uid"]
|
|
128
|
+
next if uid.blank?
|
|
129
|
+
|
|
130
|
+
iban = account.dig("account_id", "iban")
|
|
131
|
+
iban ||= Array(account["all_account_ids"]).filter_map { |row| row["iban"].presence }.first
|
|
132
|
+
{ "id" => uid, "name" => account["name"], "currency" => account["currency"], "iban" => iban }
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def self.transactions_from(page)
|
|
137
|
+
Array(page["transactions"]).map do |row|
|
|
138
|
+
amount = row.dig("transaction_amount", "amount").to_s
|
|
139
|
+
amount = "-#{amount.delete_prefix("-")}" if row["credit_debit_indicator"] == "DBIT" && amount.present?
|
|
140
|
+
description = Array(row["remittance_information"]).map(&:to_s).reject(&:empty?).join(" ")
|
|
141
|
+
description = row["note"].to_s if description.blank?
|
|
142
|
+
Transaction.new(
|
|
143
|
+
id: row["transaction_id"].presence || row["entry_reference"],
|
|
144
|
+
booking_date: row["booking_date"],
|
|
145
|
+
value_date: row["value_date"],
|
|
146
|
+
amount: amount,
|
|
147
|
+
currency: row.dig("transaction_amount", "currency"),
|
|
148
|
+
description: description,
|
|
149
|
+
raw: row
|
|
150
|
+
)
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
def self.credentials
|
|
155
|
+
stored = if defined?(Rails::Application)
|
|
156
|
+
Rails.application.credentials.dig(:enablebanking) || Rails.application.credentials.dig(:enable_banking) || {}
|
|
157
|
+
else
|
|
158
|
+
{}
|
|
159
|
+
end
|
|
160
|
+
configured_key = Bdgtr::Cli::Config.api_scalar("enablebanking") || Bdgtr::Cli::Config.api_scalar("enable_banking")
|
|
161
|
+
{
|
|
162
|
+
app_id: ENV["ENABLEBANKING_APP_ID"].presence || Bdgtr::Cli::Config.api("enablebanking", "app_id") || Bdgtr::Cli::Config.api("enable_banking", "app_id") || credential(stored, :app_id),
|
|
163
|
+
private_key: ENV["ENABLEBANKING_PRIVATE_KEY"].presence || configured_key || Bdgtr::Cli::Config.api("enablebanking", "private_key") || Bdgtr::Cli::Config.api("enable_banking", "private_key") || credential(stored, :private_key)
|
|
164
|
+
}
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def self.credential(stored, key)
|
|
168
|
+
return stored[key] if stored.is_a?(Hash)
|
|
169
|
+
return stored.public_send(key) if stored.respond_to?(key)
|
|
170
|
+
|
|
171
|
+
nil
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
private
|
|
175
|
+
|
|
176
|
+
def request(method, path, query: nil, body: nil)
|
|
177
|
+
uri = URI.join("#{API_URL}/", path.delete_prefix("/"))
|
|
178
|
+
compact = query&.compact
|
|
179
|
+
uri.query = URI.encode_www_form(compact) if compact.present?
|
|
180
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
181
|
+
http.use_ssl = true
|
|
182
|
+
http.open_timeout = 15
|
|
183
|
+
http.read_timeout = 60
|
|
184
|
+
request_class = method == "POST" ? Net::HTTP::Post : Net::HTTP::Get
|
|
185
|
+
message = request_class.new(uri)
|
|
186
|
+
message["Authorization"] = "Bearer #{jwt}"
|
|
187
|
+
message["Accept"] = "application/json"
|
|
188
|
+
if body
|
|
189
|
+
message["Content-Type"] = "application/json"
|
|
190
|
+
message.body = JSON.generate(body)
|
|
191
|
+
end
|
|
192
|
+
response = http.request(message)
|
|
193
|
+
payload = response.body.to_s.empty? ? {} : JSON.parse(response.body)
|
|
194
|
+
return payload if response.is_a?(Net::HTTPSuccess)
|
|
195
|
+
|
|
196
|
+
detail = payload["message"] || payload["detail"] || response.message
|
|
197
|
+
raise Error, "Enable Banking #{response.code}: #{detail}"
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def jwt
|
|
201
|
+
creds = self.class.credentials
|
|
202
|
+
raise Error, "Enable Banking application id is missing" if creds[:app_id].blank?
|
|
203
|
+
raise Error, "Enable Banking private key is missing" if creds[:private_key].blank?
|
|
204
|
+
|
|
205
|
+
key = OpenSSL::PKey::RSA.new(private_key_pem(creds[:private_key]))
|
|
206
|
+
now = Time.now.to_i
|
|
207
|
+
JWT.encode(
|
|
208
|
+
{ iss: "enablebanking.com", aud: "api.enablebanking.com", iat: now, exp: now + 3600 },
|
|
209
|
+
key,
|
|
210
|
+
"RS256",
|
|
211
|
+
{ typ: "JWT", kid: creds[:app_id] }
|
|
212
|
+
)
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def private_key_pem(value)
|
|
216
|
+
text = value.to_s
|
|
217
|
+
return text if text.include?("BEGIN")
|
|
218
|
+
return text unless text.start_with?("~", "/") || text.end_with?(".pem") || text.include?(File::SEPARATOR)
|
|
219
|
+
|
|
220
|
+
path = Bdgtr::Cli::Config.expand_path(text)
|
|
221
|
+
raise Error, "Enable Banking private key was not found at #{path}" unless File.file?(path)
|
|
222
|
+
|
|
223
|
+
File.read(path)
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def normalized_balances(payload)
|
|
227
|
+
rows = Array(payload["balances"]).map do |row|
|
|
228
|
+
type = Bdgtr::Psd2::AccountSnapshot::BALANCE_TYPES.fetch(row["balance_type"].to_s, row["balance_type"])
|
|
229
|
+
{
|
|
230
|
+
"balanceType" => type,
|
|
231
|
+
"balanceAmount" => {
|
|
232
|
+
"amount" => row.dig("balance_amount", "amount"),
|
|
233
|
+
"currency" => row.dig("balance_amount", "currency")
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
end
|
|
237
|
+
preferred = rows.find { |row| row["balanceType"] == "interimAvailable" }
|
|
238
|
+
preferred ? [ preferred, *rows.reject { |row| row.equal?(preferred) } ] : rows
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
def time_or_nil(value)
|
|
242
|
+
return if value.blank?
|
|
243
|
+
|
|
244
|
+
Time.zone.parse(value)
|
|
245
|
+
rescue ArgumentError
|
|
246
|
+
nil
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
end
|
|
250
|
+
end
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "nordigen-ruby"
|
|
4
|
+
|
|
5
|
+
module Bdgtr
|
|
6
|
+
module Psd2
|
|
7
|
+
# GoCardless Bank Account Data, previously Nordigen.
|
|
8
|
+
class GoCardless < Platform
|
|
9
|
+
def initialize(options = {})
|
|
10
|
+
super
|
|
11
|
+
@options = { redirect_url: "https://nordigen.com" }.merge(options)
|
|
12
|
+
secrets = self.class.secrets
|
|
13
|
+
@client = Nordigen::NordigenClient.new(
|
|
14
|
+
secret_id: secrets.fetch(:secret_id),
|
|
15
|
+
secret_key: secrets.fetch(:secret_key)
|
|
16
|
+
)
|
|
17
|
+
http = @client.request
|
|
18
|
+
http.options.open_timeout = 10
|
|
19
|
+
http.options.timeout = 30
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.secrets
|
|
23
|
+
id = Bdgtr::Cli::Config.api("nordigen", "secret_id")
|
|
24
|
+
key = Bdgtr::Cli::Config.api("nordigen", "secret_key")
|
|
25
|
+
return { secret_id: id, secret_key: key } if id && key
|
|
26
|
+
return { secret_id: nil, secret_key: nil } unless defined?(Rails::Application)
|
|
27
|
+
|
|
28
|
+
stored = Rails.application.credentials.nordigen
|
|
29
|
+
{ secret_id: stored.secret_id, secret_key: stored.secret_key }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def auth!
|
|
33
|
+
return @access_token if @access_token
|
|
34
|
+
|
|
35
|
+
token_data = @client.generate_token
|
|
36
|
+
@client.set_token("YOUR_TOKEN")
|
|
37
|
+
access_token = token_data["access"]
|
|
38
|
+
refresh_token = token_data["refresh"]
|
|
39
|
+
@client.exchange_token(refresh_token)
|
|
40
|
+
@access_token = access_token
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def institutions(country_code)
|
|
44
|
+
Array(get_institutions(country_code)).map do |row|
|
|
45
|
+
Institution.new(id: row["id"], name: row["name"], country: country_code, logo: row["logo"], raw: row)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def get_institutions(country_code)
|
|
50
|
+
auth!
|
|
51
|
+
@client.institution.get_institutions(country_code)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def get_institution_by_id(institution_id)
|
|
55
|
+
auth!
|
|
56
|
+
@client.institution.get_institution_by_id(institution_id)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def start_link(institution_id:, redirect_url:, country: nil, max_historical_days: 90)
|
|
60
|
+
@options[:redirect_url] = redirect_url
|
|
61
|
+
init = get_link_for_institution(institution_id, max_historical_days: max_historical_days)
|
|
62
|
+
Requisition.new(
|
|
63
|
+
id: init["id"],
|
|
64
|
+
platform: "gocardless",
|
|
65
|
+
link: init["link"],
|
|
66
|
+
max_historical_days: init["max_historical_days"],
|
|
67
|
+
raw: init
|
|
68
|
+
)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def get_link_for_institution(institution_id, max_historical_days: 90)
|
|
72
|
+
auth!
|
|
73
|
+
capped_days = cap_historical_days(institution_id, max_historical_days)
|
|
74
|
+
init = @client.init_session(
|
|
75
|
+
redirect_url: @options[:redirect_url],
|
|
76
|
+
institution_id: institution_id,
|
|
77
|
+
reference_id: SecureRandom.uuid,
|
|
78
|
+
max_historical_days: capped_days,
|
|
79
|
+
access_valid_for_days: ::Requisition::ACCESS_VALID_FOR_DAYS
|
|
80
|
+
)
|
|
81
|
+
init["max_historical_days"] = capped_days
|
|
82
|
+
init
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def cap_historical_days(institution_id, days)
|
|
86
|
+
requested = days.to_i
|
|
87
|
+
requested = 90 if requested <= 0
|
|
88
|
+
institution = get_institution_by_id(institution_id)
|
|
89
|
+
bank_max = institution.is_a?(Hash) ? institution["transaction_total_days"].to_i : 0
|
|
90
|
+
limit = bank_max.positive? ? [ bank_max, 730 ].min : 730
|
|
91
|
+
requested.clamp(1, limit)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def finish(callback)
|
|
95
|
+
id = callback[:requisition_id].presence || callback["requisition_id"].presence
|
|
96
|
+
raise Error, "GoCardless did not return a requisition id" if id.blank?
|
|
97
|
+
|
|
98
|
+
data = get_requisition_by_id(id)
|
|
99
|
+
Requisition.new(
|
|
100
|
+
id: id,
|
|
101
|
+
platform: "gocardless",
|
|
102
|
+
status: data["status"],
|
|
103
|
+
account_ids: Array(data["accounts"]),
|
|
104
|
+
max_historical_days: callback[:max_historical_days],
|
|
105
|
+
access_valid_until: ::Requisition::ACCESS_VALID_FOR_DAYS.days.from_now,
|
|
106
|
+
raw: data
|
|
107
|
+
)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def get_requisition_by_id(requisition_id)
|
|
111
|
+
auth!
|
|
112
|
+
@client.requisition.get_requisition_by_id(requisition_id)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def get_account(account_id)
|
|
116
|
+
auth!
|
|
117
|
+
@client.account(account_id)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def get_transactions(options = {})
|
|
121
|
+
options = {
|
|
122
|
+
date_from: (Date.current - 90),
|
|
123
|
+
date_to: Date.today
|
|
124
|
+
}.merge(options.compact)
|
|
125
|
+
account = get_account(options[:requisition_account_id])
|
|
126
|
+
account.get_transactions(
|
|
127
|
+
date_from: options[:date_from].strftime("%Y-%m-%d"),
|
|
128
|
+
date_to: options[:date_to].strftime("%Y-%m-%d")
|
|
129
|
+
)
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
class Institution
|
|
6
|
+
attr_reader :id, :name, :country, :logo, :raw
|
|
7
|
+
|
|
8
|
+
def initialize(id:, name:, country:, logo: nil, raw: nil)
|
|
9
|
+
@id = id
|
|
10
|
+
@name = name
|
|
11
|
+
@country = country
|
|
12
|
+
@logo = logo
|
|
13
|
+
@raw = raw
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
# One external bank-sync platform. Subclasses talk to a provider API and
|
|
6
|
+
# return Institution, Requisition, Account, and Transaction.
|
|
7
|
+
class Platform
|
|
8
|
+
def initialize(options = {})
|
|
9
|
+
@options = options
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def institutions(_country_code)
|
|
13
|
+
raise NotImplementedError
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def start_link(institution_id:, redirect_url:, country: nil, max_historical_days: 90)
|
|
17
|
+
raise NotImplementedError
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def finish(_callback)
|
|
21
|
+
raise NotImplementedError
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def get_requisition_by_id(_requisition_id)
|
|
25
|
+
raise NotImplementedError
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def get_account(_account_id)
|
|
29
|
+
raise NotImplementedError
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def get_transactions(_options = {})
|
|
33
|
+
raise NotImplementedError
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
class Requisition
|
|
6
|
+
attr_reader :id, :status, :account_ids, :bank_accounts, :link, :access_valid_until, :max_historical_days, :platform, :raw
|
|
7
|
+
|
|
8
|
+
def initialize(id:, platform:, status: nil, account_ids: [], bank_accounts: nil, link: nil, access_valid_until: nil, max_historical_days: nil, raw: nil)
|
|
9
|
+
@id = id
|
|
10
|
+
@platform = platform
|
|
11
|
+
@status = status
|
|
12
|
+
@bank_accounts = Array(bank_accounts)
|
|
13
|
+
@account_ids = @bank_accounts.filter_map { |account| account["id"] }.presence || Array(account_ids)
|
|
14
|
+
@link = link
|
|
15
|
+
@access_valid_until = access_valid_until
|
|
16
|
+
@max_historical_days = max_historical_days
|
|
17
|
+
@raw = raw
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Psd2
|
|
5
|
+
class Transaction
|
|
6
|
+
attr_reader :id, :booking_date, :value_date, :amount, :currency, :description, :raw
|
|
7
|
+
|
|
8
|
+
def initialize(id:, booking_date:, value_date:, amount:, currency:, description:, raw: nil)
|
|
9
|
+
@id = id
|
|
10
|
+
@booking_date = booking_date
|
|
11
|
+
@value_date = value_date
|
|
12
|
+
@amount = amount
|
|
13
|
+
@currency = currency
|
|
14
|
+
@description = description
|
|
15
|
+
@raw = raw
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def to_bank_row
|
|
19
|
+
{
|
|
20
|
+
"transactionId" => id,
|
|
21
|
+
"bookingDate" => booking_date,
|
|
22
|
+
"valueDate" => value_date,
|
|
23
|
+
"transactionAmount" => { "amount" => amount, "currency" => currency },
|
|
24
|
+
"remittanceInformationUnstructured" => description
|
|
25
|
+
}
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/bdgtr/psd2.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
# Open Banking / PSD2. Platforms talk to a bank API. Requisition, Account,
|
|
5
|
+
# and Transaction are the shared shapes those platforms return.
|
|
6
|
+
module Psd2
|
|
7
|
+
Error = Class.new(StandardError)
|
|
8
|
+
|
|
9
|
+
CLIENTS = {
|
|
10
|
+
"gocardless" => "Bdgtr::Psd2::GoCardless",
|
|
11
|
+
"enablebanking" => "Bdgtr::Psd2::EnableBanking"
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
def self.client(platform, options = {})
|
|
15
|
+
name = CLIENTS[platform.to_s]
|
|
16
|
+
raise Error, "unknown platform #{platform}" if name.blank?
|
|
17
|
+
|
|
18
|
+
name.constantize.new(options)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.platforms
|
|
22
|
+
CLIENTS.keys
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Sync
|
|
5
|
+
# Copies a ledger onto Postgres. A row that already exists is left in place.
|
|
6
|
+
# A missing row is created with the same id. Tags match by name when the id differs.
|
|
7
|
+
class Importer
|
|
8
|
+
def self.call(workspace)
|
|
9
|
+
path = Bdgtr::Cli::Config.ledger_for(workspace)
|
|
10
|
+
raise ArgumentError, "workspace #{workspace.id} has no ledger file" if path.blank?
|
|
11
|
+
raise ArgumentError, "ledger file is missing" unless File.exist?(path)
|
|
12
|
+
|
|
13
|
+
snapshot = Core::Session.open(path) { Core::Transfer.capture(workspace) }
|
|
14
|
+
Account.transaction do
|
|
15
|
+
prepare!(snapshot)
|
|
16
|
+
Core::Transfer.write!(snapshot)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.prepare!(snapshot)
|
|
21
|
+
category_ids = keep_categories!(snapshot)
|
|
22
|
+
transaction_ids = keep_transactions!(snapshot, category_ids)
|
|
23
|
+
tag_ids = keep_tags!(snapshot)
|
|
24
|
+
keep_taggings!(snapshot, tag_ids, transaction_ids)
|
|
25
|
+
snapshot["accounts"] = missing_by_id(Account, snapshot["accounts"])
|
|
26
|
+
snapshot["requisitions"] = missing_by_id(Requisition, snapshot["requisitions"])
|
|
27
|
+
snapshot["account_requisitions"] = missing_by_id(AccountRequisition, snapshot["account_requisitions"])
|
|
28
|
+
snapshot["imports"] = missing_by_id(Import, snapshot["imports"])
|
|
29
|
+
snapshot["account_syncs"] = missing_by_id(AccountSync, snapshot["account_syncs"])
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def self.keep_categories!(snapshot)
|
|
33
|
+
map = {}
|
|
34
|
+
kept = []
|
|
35
|
+
Array(snapshot["transaction_categories"]).each do |attrs|
|
|
36
|
+
existing = TransactionCategory.find_by(id: attrs["id"]) ||
|
|
37
|
+
TransactionCategory.find_by(workspace_id: attrs["workspace_id"], slug: attrs["slug"])
|
|
38
|
+
if existing
|
|
39
|
+
map[attrs["id"]] = existing.id
|
|
40
|
+
else
|
|
41
|
+
map[attrs["id"]] = attrs["id"]
|
|
42
|
+
kept << attrs
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
snapshot["transaction_categories"] = kept
|
|
46
|
+
map
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def self.keep_transactions!(snapshot, category_ids)
|
|
50
|
+
map = {}
|
|
51
|
+
kept = []
|
|
52
|
+
Array(snapshot["transactions"]).each do |attrs|
|
|
53
|
+
category_id = attrs["transaction_category_id"]
|
|
54
|
+
attrs = attrs.merge("transaction_category_id" => category_ids[category_id]) if category_ids.key?(category_id)
|
|
55
|
+
bank_id = attrs["sync_transaction_id"].presence || attrs["transaction_id"].presence
|
|
56
|
+
attrs = attrs.except("transaction_id")
|
|
57
|
+
attrs = attrs.merge("sync_transaction_id" => bank_id) if bank_id.present?
|
|
58
|
+
existing = Transaction.find_by(id: attrs["id"])
|
|
59
|
+
if bank_id.present?
|
|
60
|
+
existing ||= Transaction.find_by(account_id: attrs["account_id"], sync_transaction_id: bank_id)
|
|
61
|
+
end
|
|
62
|
+
if existing
|
|
63
|
+
map[attrs["id"]] = existing.id
|
|
64
|
+
else
|
|
65
|
+
map[attrs["id"]] = attrs["id"]
|
|
66
|
+
kept << attrs
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
snapshot["transactions"] = kept
|
|
70
|
+
map
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def self.keep_tags!(snapshot)
|
|
74
|
+
map = {}
|
|
75
|
+
kept = []
|
|
76
|
+
Array(snapshot["tags"]).each do |attrs|
|
|
77
|
+
existing = ActsAsTaggableOn::Tag.find_by(id: attrs["id"]) ||
|
|
78
|
+
ActsAsTaggableOn::Tag.find_by(name: attrs["name"])
|
|
79
|
+
if existing
|
|
80
|
+
map[attrs["id"]] = existing.id
|
|
81
|
+
else
|
|
82
|
+
map[attrs["id"]] = attrs["id"]
|
|
83
|
+
kept << attrs
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
snapshot["tags"] = kept
|
|
87
|
+
map
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.keep_taggings!(snapshot, tag_ids, transaction_ids)
|
|
91
|
+
snapshot["taggings"] = Array(snapshot["taggings"]).filter_map do |attrs|
|
|
92
|
+
taggable_id = attrs["taggable_id"]
|
|
93
|
+
if attrs["taggable_type"] == "Transaction" && transaction_ids.key?(taggable_id)
|
|
94
|
+
taggable_id = transaction_ids[taggable_id]
|
|
95
|
+
end
|
|
96
|
+
row = attrs.merge(
|
|
97
|
+
"tag_id" => tag_ids.fetch(attrs["tag_id"], attrs["tag_id"]),
|
|
98
|
+
"taggable_id" => taggable_id
|
|
99
|
+
)
|
|
100
|
+
next if tagging_exists?(row)
|
|
101
|
+
|
|
102
|
+
row
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def self.tagging_exists?(attrs)
|
|
107
|
+
ActsAsTaggableOn::Tagging.exists?(id: attrs["id"]) ||
|
|
108
|
+
ActsAsTaggableOn::Tagging.exists?(
|
|
109
|
+
tag_id: attrs["tag_id"],
|
|
110
|
+
taggable_id: attrs["taggable_id"],
|
|
111
|
+
taggable_type: attrs["taggable_type"],
|
|
112
|
+
context: attrs["context"],
|
|
113
|
+
tagger_id: attrs["tagger_id"],
|
|
114
|
+
tagger_type: attrs["tagger_type"]
|
|
115
|
+
)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def self.missing_by_id(model, rows)
|
|
119
|
+
Array(rows).reject { |attrs| model.exists?(id: attrs["id"]) }
|
|
120
|
+
end
|
|
121
|
+
private_class_method :prepare!, :keep_categories!, :keep_transactions!, :keep_tags!,
|
|
122
|
+
:keep_taggings!, :tagging_exists?, :missing_by_id
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "pathname"
|
|
4
|
+
|
|
5
|
+
module Bdgtr
|
|
6
|
+
module Sync
|
|
7
|
+
module Library
|
|
8
|
+
def self.load!
|
|
9
|
+
root = Pathname.new(File.expand_path("../portfolio", __dir__))
|
|
10
|
+
lib = root.join("lib").to_s
|
|
11
|
+
cli = root.join("cli/lib").to_s
|
|
12
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
13
|
+
$LOAD_PATH.unshift(cli) unless $LOAD_PATH.include?(cli)
|
|
14
|
+
ENV["BDGTR_ROOT"] ||= defined?(Rails) ? Rails.root.to_s : File.expand_path("../../..", __dir__)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Bdgtr
|
|
4
|
+
module Sync
|
|
5
|
+
# Read and write a Portfolio Performance .portfolio. The CLI calls this
|
|
6
|
+
# for Fineco imports.
|
|
7
|
+
class Portfolio
|
|
8
|
+
def self.load_library!
|
|
9
|
+
Library.load!
|
|
10
|
+
require "portfolio_performance_api"
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|