-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNordigenPSD2AccountInfo.cs
378 lines (321 loc) · 10.1 KB
/
NordigenPSD2AccountInfo.cs
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace NordigenPSD2Sharp
{
public class TransactionAmount
{
public string currency { get; set; }
public decimal amount { get; set; }
}
public class TransAccount
{
public string bban { get; set; }
public string iban { get; set; }
}
[DebuggerDisplay("{debtorName} {transactionAmount.amount} EUR {bookingDate} {remittanceInformationUnstructured}")]
public class Transaction
{
public string debtorName { get; set; }
public string creditorName { get; set; }
public TransAccount debtorAccount { get; set; }
public TransAccount creditorAccount { get; set; }
public TransactionAmount transactionAmount { get; set; }
public DateTime valueDate { get; set; }
public DateTime bookingDate { get; set; }
public string remittanceInformationUnstructured { get; set; }
public string[] remittanceInformationUnstructuredArray { get; set; }
}
public class Transactions
{
public Transaction[] booked { get; set; }
public Transaction[] pending { get; set; }
}
class TransactionsMain
{
public Transactions transactions { get; set; }
}
public class Balance
{
public TransactionAmount balanceAmount { get; set; }
public string balanceType { get; set; }
public DateTime lastChangeDateTime { get; set; }
}
public class RequisitionLink
{
public string initiate { get; set; }
}
public class RequisitionStatus
{
public string @short { get; set; }
public string @long { get; set; }
public string description { get; set; }
}
public class Requisition
{
public string id { get; set; }
public string redirect { get; set; }
public string aggreements { get; set; }
public string reference { get; set; }
public string link { get; set; }
public string status { get; set; }
public string[] accounts { get; set; }
}
public class Banks
{
public string id { get; set; }
public string name { get; set; }
public string bic { get; set; }
public int transaction_total_days { get; set; }
public string logo { get; set; }
public string[] countries { get; set; }
}
public class Accounts
{
public string id { get; set; }
public string status { get; set; }
public string agreements { get; set; }
public string[] accounts { get; set; }
public string reference { get; set; }
}
public class AccountDetails
{
public AccountDetails2 account { get; set; }
}
public class AccountDetails2
{
public string resourceId { get; set; }
public string iban { get; set; }
public string currency { get; set; }
public string ownerName { get; set; }
}
public class AgreementReq
{
public int max_historical_days { get; set; }
public int access_valid_for_days { get; set; }
public string[] access_scope { get; set; }
public string institution_id { get; set; }
}
public class Agreement
{
public string id { get; set; }
public DateTime created { get; set; }
public int max_historical_days { get; set; }
public int access_valid_for_days { get; set; }
public string accepted { get; set; }
public string institution_id { get; set; }
}
public class BalancesMain
{
public Balance[] balances { get; set; }
}
public class Token
{
public string access { get; set; }
public int access_expires { get; set; }
public string refresh { get; set; }
public int refresh_expires { get; set; }
}
public class NordigenException: Exception
{
public NordigenException(): base() { }
public NordigenException(string msg) : base(msg) { }
public string summary { get; set; }
public string detail { get; set; }
public int status_code { get; set; }
}
public class RequisitionResult
{
public string id { get; set; }
public DateTime created { get; set; }
public string redirect { get; set; }
public string status { get; set; }
public string institution_id { get; set; }
public string agreement { get; set; }
public string reference { get; set; }
public List<string> accounts { get; set; }
public string user_language { get; set; }
public string link { get; set; }
public string ssn { get; set; }
public bool account_selection { get; set; }
public bool redirect_immediate { get; set; }
}
public class RequisitionResultRoot
{
public int count { get; set; }
public string next { get; set; }
public string previous { get; set; }
public List<RequisitionResult> results { get; set; }
}
public class NordigenPSD2AccountInfo
{
private string _token;
private readonly string burl = "https://ob.nordigen.com/api/v2/";
public NordigenPSD2AccountInfo()
{
_token = "needtosetlater!";
}
public NordigenPSD2AccountInfo(string token)
{
_token = token;
}
public void SetAuth(string token)
{
_token = token;
}
private HttpClient SetupClient()
{
var c = new HttpClient();
var auth = "Bearer "+_token;
c.DefaultRequestHeaders.Add("accept", "application/json");
c.DefaultRequestHeaders.Add("Authorization", auth);
return c;
}
public async Task<T> GetDataAsync<T>(HttpClient c, string url)
{
var res = await c.GetAsync(url);
if (res.IsSuccessStatusCode)
{
return await res.Content.ReadFromJsonAsync<T>();
} else
{
var err = await res.Content.ReadFromJsonAsync<NordigenException>();
throw err;
}
}
public async Task<T> PostDataAsync<T>(HttpClient c, string url, string content)
{
var sc = new StringContent(content, Encoding.UTF8, "application/json");
var res = await c.PostAsync(url, sc);
if (res.IsSuccessStatusCode)
{
return await res.Content.ReadFromJsonAsync<T>();
}
else
{
var errs = await res.Content.ReadAsStringAsync();
var nordExc = JsonSerializer.Deserialize<NordigenException>(errs);
var nexc2 = new NordigenException(errs);
nexc2.status_code = nordExc.status_code;
nexc2.detail = nordExc.detail;
nexc2.summary = nordExc.summary;
throw nexc2;
}
}
public async Task<Transactions> GetTransactionsAsync(string accountId)
{
var transurl = $"{burl}accounts/{accountId}/transactions/";
using (var c = SetupClient())
{
var trans = await GetDataAsync<TransactionsMain>(c, transurl);
return trans.transactions;
}
}
public async Task<List<RequisitionResult>> GetRequisitionsAsync()
{
var transurl = $"{burl}requisitions/?limits=100&offset=1";
using (var c = SetupClient())
{
var reqs = await GetDataAsync<RequisitionResultRoot>(c, transurl);
return reqs.results;
}
}
public async Task<Token> GetToken(string secretid, string secretkey)
{
var transurl = $"{burl}token/new/";
using (var c = new HttpClient())
{
var cs = $"{{ \"secret_id\": \"{secretid}\", \"secret_key\": \"{secretkey}\"}}";
var token = await PostDataAsync<Token>(c, transurl, cs);
return token;
}
}
public async Task<Token> RefreshToken(string refreshToken)
{
var transurl = $"{burl}token/refresh/";
using (var c = new HttpClient())
{
var cs = $"{{ \"refresh\": \"{refreshToken}\" }}";
var token = await PostDataAsync<Token>(c, transurl, cs);
return token;
}
}
public async Task<Balance[]> GetBalancesAsync(string accountId)
{
var transurl = $"{burl}accounts/{accountId}/balances/";
using (var c = SetupClient())
{
var trans = await GetDataAsync<BalancesMain>(c, transurl);
return trans.balances;
}
}
public async Task<AccountDetails> GetAccountDetailsAsync(string accountId)
{
var transurl = $"{burl}accounts/{accountId}/details/";
using (var c = SetupClient())
{
var trans = await GetDataAsync<AccountDetails>(c, transurl);
return trans;
}
}
public async Task<Agreement> CreateAgreementAsync(string institutionId, int maxHistoricalDays, int accessValidForDays, string[] accessScope)
{
var transurl = $"{burl}agreements/enduser/";
using (var c = SetupClient())
{
var reqData = new AgreementReq
{
max_historical_days = maxHistoricalDays,
access_valid_for_days = accessValidForDays,
access_scope = accessScope,
institution_id = institutionId
};
var rString = JsonSerializer.Serialize(reqData);
var req = await PostDataAsync<Agreement>(c, transurl, rString);
return req;
}
}
public async Task<Requisition> CreateRequisitionAsync(string institution_id, string redirect, string reference, string agreement, string user_language)
{
var transurl = $"{burl}requisitions/";
using (var c = SetupClient())
{
var reqs = $"{{ \"redirect\": \"{redirect}\", \"institution_id\": \"{institution_id}\" ";
if (!string.IsNullOrEmpty(reference))
reqs += $", \"reference\": \"{reference}\"";
if (!string.IsNullOrEmpty(agreement))
reqs += $", \"agreement\": \"{agreement}\"";
if (!string.IsNullOrEmpty(user_language))
reqs += $", \"user_language\": \"{user_language}\"";
reqs += "}";
var req = await PostDataAsync<Requisition>(c, transurl, reqs);
return req;
}
}
public async Task<Accounts> GetAccountsAsync(string reqid)
{
var transurl = $"{burl}requisitions/{reqid}/";
using (var c = SetupClient())
{
var accts = await GetDataAsync<Accounts>(c, transurl);
return accts;
}
}
public async Task<Banks[]> GetBanksAsync(string country)
{
var transurl = $"{burl}institutions/?country={country}";
using (var c = SetupClient())
{
var banks = await GetDataAsync<Banks[]>(c, transurl);
return banks;
}
}
}
}