This repository was archived by the owner on Feb 10, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 679
/
Copy pathgocardless-service.js
534 lines (475 loc) · 16.4 KB
/
gocardless-service.js
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
import BankFactory from '../bank-factory.js';
import {
RequisitionNotLinked,
AccountNotLinedToRequisition,
InvalidInputDataError,
InvalidGoCardlessTokenError,
AccessDeniedError,
NotFoundError,
ResourceSuspended,
RateLimitError,
UnknownError,
ServiceError,
} from '../errors.js';
import * as nordigenNode from 'nordigen-node';
import * as uuid from 'uuid';
import jwt from 'jws';
import { SecretName, secretsService } from '../../services/secrets-service.js';
const GoCardlessClient = nordigenNode.default;
const clients = new Map();
const getGocardlessClient = () => {
const secrets = {
secretId: secretsService.get(SecretName.gocardless_secretId),
secretKey: secretsService.get(SecretName.gocardless_secretKey),
};
const hash = JSON.stringify(secrets);
if (!clients.has(hash)) {
clients.set(hash, new GoCardlessClient(secrets));
}
return clients.get(hash);
};
export const handleGoCardlessError = (response) => {
switch (response.status_code) {
case 400:
throw new InvalidInputDataError(response);
case 401:
throw new InvalidGoCardlessTokenError(response);
case 403:
throw new AccessDeniedError(response);
case 404:
throw new NotFoundError(response);
case 409:
throw new ResourceSuspended(response);
case 429:
throw new RateLimitError(response);
case 500:
throw new UnknownError(response);
case 503:
throw new ServiceError(response);
default:
return;
}
};
export const goCardlessService = {
/**
* Check if the GoCardless service is configured to be used.
* @returns {boolean}
*/
isConfigured: () => {
return !!(
getGocardlessClient().secretId && getGocardlessClient().secretKey
);
},
/**
*
* @returns {Promise<void>}
*/
setToken: async () => {
const isExpiredJwtToken = (token) => {
const decodedToken = jwt.decode(token);
if (!decodedToken) {
return true;
}
const payload = decodedToken.payload;
const clockTimestamp = Math.floor(Date.now() / 1000);
return clockTimestamp >= payload.exp;
};
if (isExpiredJwtToken(getGocardlessClient().token)) {
// Generate new access token. Token is valid for 24 hours
// Note: access_token is automatically injected to other requests after you successfully obtain it
const tokenData = await client.generateToken();
handleGoCardlessError(tokenData);
}
},
/**
*
* @param requisitionId
* @throws {RequisitionNotLinked} Will throw an error if requisition is not in Linked
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<import('../gocardless-node.types.js').Requisition>}
*/
getLinkedRequisition: async (requisitionId) => {
const requisition = await goCardlessService.getRequisition(requisitionId);
const { status } = requisition;
// Continue only if status of requisition is "LN" what does
// mean that account has been successfully linked to requisition
if (status !== 'LN') {
throw new RequisitionNotLinked({ requisitionStatus: status });
}
return requisition;
},
/**
* Returns requisition and all linked accounts in their Bank format.
* Each account object is extended about details of the institution
* @param requisitionId
* @throws {RequisitionNotLinked} Will throw an error if requisition is not in Linked
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<{requisition: import('../gocardless-node.types.js').Requisition, accounts: Array<import('../gocardless.types.js').NormalizedAccountDetails>}>}
*/
getRequisitionWithAccounts: async (requisitionId) => {
const requisition = await goCardlessService.getLinkedRequisition(
requisitionId,
);
let institutionIdSet = new Set();
const detailedAccounts = await Promise.all(
requisition.accounts.map(async (accountId) => {
const account = await goCardlessService.getDetailedAccount(accountId);
institutionIdSet.add(account.institution_id);
return account;
}),
);
const institutions = await Promise.all(
Array.from(institutionIdSet).map(async (institutionId) => {
return await goCardlessService.getInstitution(institutionId);
}),
);
const extendedAccounts =
await goCardlessService.extendAccountsAboutInstitutions({
accounts: detailedAccounts,
institutions,
});
const normalizedAccounts = extendedAccounts.map((account) => {
const bankAccount = BankFactory(account.institution_id);
return bankAccount.normalizeAccount(account);
});
return { requisition, accounts: normalizedAccounts };
},
/**
*
* @param requisitionId
* @param accountId
* @param startDate
* @param endDate
* @throws {AccountNotLinedToRequisition} Will throw an error if requisition not includes provided account id
* @throws {RequisitionNotLinked} Will throw an error if requisition is not in Linked
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<{iban: string, balances: Array<import('../gocardless-node.types.js').Balance>, institutionId: string, transactions: {booked: Array<import('../gocardless-node.types.js').Transaction>, pending: Array<import('../gocardless-node.types.js').Transaction>, all: Array<import('../gocardless.types.js').TransactionWithBookedStatus>}, startingBalance: number}>}
*/
getTransactionsWithBalance: async (
requisitionId,
accountId,
startDate,
endDate,
) => {
const { institution_id, accounts: accountIds } =
await goCardlessService.getLinkedRequisition(requisitionId);
if (!accountIds.includes(accountId)) {
throw new AccountNotLinedToRequisition(accountId, requisitionId);
}
const [accountMetadata, transactions, accountBalance] = await Promise.all([
goCardlessService.getAccountMetadata(accountId),
goCardlessService.getTransactions({
institutionId: institution_id,
accountId,
startDate,
endDate,
}),
goCardlessService.getBalances(accountId),
]);
const bank = BankFactory(institution_id);
const sortedBookedTransactions = bank.sortTransactions(
transactions.transactions?.booked,
);
const sortedPendingTransactions = bank.sortTransactions(
transactions.transactions?.pending,
);
const allTransactions = sortedBookedTransactions.map((t) => {
return { ...t, booked: true };
});
sortedPendingTransactions.forEach((t) =>
allTransactions.push({ ...t, booked: false }),
);
const sortedAllTransactions = bank.sortTransactions(allTransactions);
const startingBalance = bank.calculateStartingBalance(
sortedBookedTransactions,
accountBalance.balances,
);
return {
iban: accountMetadata.iban,
balances: accountBalance.balances,
institutionId: institution_id,
startingBalance,
transactions: {
booked: sortedBookedTransactions,
pending: sortedPendingTransactions,
all: sortedAllTransactions,
},
};
},
/**
*
* @param {import('../gocardless.types.js').CreateRequisitionParams} params
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<{requisitionId, link}>}
*/
createRequisition: async ({ institutionId, accessValidForDays, host }) => {
await goCardlessService.setToken();
const response = await client.initSession({
redirectUrl: host + '/gocardless/link',
institutionId,
referenceId: uuid.v4(),
accessValidForDays,
maxHistoricalDays: 90,
userLanguage: 'en',
ssn: null,
redirectImmediate: false,
accountSelection: false,
});
handleGoCardlessError(response);
const { link, id: requisitionId } = response;
return {
link,
requisitionId,
};
},
/**
* Deletes requisition by provided ID
* @param requisitionId
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<{summary: string, detail: string}>}
*/
deleteRequisition: async (requisitionId) => {
await goCardlessService.getRequisition(requisitionId);
const response = client.deleteRequisition(requisitionId);
handleGoCardlessError(response);
return response;
},
/**
* Retrieve a requisition by ID
* https://nordigen.com/en/docs/account-information/integration/parameters-and-responses/#/requisitions/requisition%20by%20id
* @param { string } requisitionId
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns { Promise<import('../gocardless-node.types.js').Requisition> }
*/
getRequisition: async (requisitionId) => {
await goCardlessService.setToken();
const response = client.getRequisitionById(requisitionId);
handleGoCardlessError(response);
return response;
},
/**
* Retrieve an detailed account by account id
* @param accountId
* @returns {Promise<import('../gocardless.types.js').DetailedAccount>}
*/
getDetailedAccount: async (accountId) => {
const [detailedAccount, metadataAccount] = await Promise.all([
client.getDetails(accountId),
client.getMetadata(accountId),
]);
handleGoCardlessError(detailedAccount);
handleGoCardlessError(metadataAccount);
return {
...detailedAccount.account,
...metadataAccount,
};
},
/**
* Retrieve account metadata by account id
*
* Unlike getDetailedAccount, this method is not affected by institution rate-limits.
*
* @param accountId
* @returns {Promise<import('../gocardless-node.types.js').GoCardlessAccountMetadata>}
*/
getAccountMetadata: async (accountId) => {
const response = await client.getMetadata(accountId);
handleGoCardlessError(response);
return response;
},
/**
* Retrieve details about all Institutions in a specific country
* @param country
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<Array<import('../gocardless-node.types.js').Institution>>}
*/
getInstitutions: async (country) => {
const response = await client.getInstitutions(country);
handleGoCardlessError(response);
return response;
},
/**
* Retrieve details about a specific Institution
* @param institutionId
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<import('../gocardless-node.types.js').Institution>}
*/
getInstitution: async (institutionId) => {
const response = await client.getInstitutionById(institutionId);
handleGoCardlessError(response);
return response;
},
/**
* Extends provided accounts about details of their institution
* @param {{accounts: Array<import('../gocardless.types.js').DetailedAccount>, institutions: Array<import('../gocardless-node.types.js').Institution>}} params
* @returns {Promise<Array<import('../gocardless.types.js').DetailedAccount&{institution: import('../gocardless-node.types.js').Institution}>>}
*/
extendAccountsAboutInstitutions: async ({ accounts, institutions }) => {
const institutionsById = institutions.reduce((acc, institution) => {
acc[institution.id] = institution;
return acc;
}, {});
return accounts.map((account) => {
const institution = institutionsById[account.institution_id] || null;
return {
...account,
institution,
};
});
},
/**
* Returns account transaction in provided dates
* @param {import('../gocardless.types.js').GetTransactionsParams} params
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<import('../gocardless.types.js').GetTransactionsResponse>}
*/
getTransactions: async ({ institutionId, accountId, startDate, endDate }) => {
const response = await client.getTransactions({
accountId,
dateFrom: startDate,
dateTo: endDate,
});
handleGoCardlessError(response);
const bank = BankFactory(institutionId);
response.transactions.booked = response.transactions.booked
.map((transaction) => bank.normalizeTransaction(transaction, true))
.filter((transaction) => transaction);
response.transactions.pending = response.transactions.pending
.map((transaction) => bank.normalizeTransaction(transaction, false))
.filter((transaction) => transaction);
return response;
},
/**
* Returns account available balances
* @param accountId
* @throws {InvalidInputDataError}
* @throws {InvalidGoCardlessTokenError}
* @throws {AccessDeniedError}
* @throws {NotFoundError}
* @throws {ResourceSuspended}
* @throws {RateLimitError}
* @throws {UnknownError}
* @throws {ServiceError}
* @returns {Promise<import('../gocardless.types.js').GetBalances>}
*/
getBalances: async (accountId) => {
const response = await client.getBalances(accountId);
handleGoCardlessError(response);
return response;
},
};
/**
* All executions of goCardlessClient should be here for testing purposes,
* as the nordigen-node library is not written in a way that is conducive to testing.
* In that way we can mock the `client` const instead of nordigen library
*/
export const client = {
getBalances: async (accountId) =>
await getGocardlessClient().account(accountId).getBalances(),
getTransactions: async ({ accountId, dateFrom, dateTo }) =>
await getGocardlessClient().account(accountId).getTransactions({
dateFrom,
dateTo,
country: undefined,
}),
getInstitutions: async (country) =>
await getGocardlessClient().institution.getInstitutions({ country }),
getInstitutionById: async (institutionId) =>
await getGocardlessClient().institution.getInstitutionById(institutionId),
getDetails: async (accountId) =>
await getGocardlessClient().account(accountId).getDetails(),
getMetadata: async (accountId) =>
await getGocardlessClient().account(accountId).getMetadata(),
getRequisitionById: async (requisitionId) =>
await getGocardlessClient().requisition.getRequisitionById(requisitionId),
deleteRequisition: async (requisitionId) =>
await getGocardlessClient().requisition.deleteRequisition(requisitionId),
initSession: async ({
redirectUrl,
institutionId,
referenceId,
accessValidForDays,
maxHistoricalDays,
userLanguage,
ssn,
redirectImmediate,
accountSelection,
}) =>
await getGocardlessClient().initSession({
redirectUrl,
institutionId,
referenceId,
accessValidForDays,
maxHistoricalDays,
userLanguage,
ssn,
redirectImmediate,
accountSelection,
}),
generateToken: async () => await getGocardlessClient().generateToken(),
exchangeToken: async ({ refreshToken }) =>
await getGocardlessClient().exchangeToken({ refreshToken }),
};