Compare commits
34 Commits
Author | SHA1 | Date | |
---|---|---|---|
9b7f25c996 | |||
7637fca672 | |||
d7ba62e767 | |||
019197e43c | |||
596f4e5398 | |||
dca6f9ef0b | |||
bdd7419d3d | |||
0d99ee51d5 | |||
0f80623111 | |||
6c02861a79 | |||
a25e758faf | |||
14f2ba0692 | |||
0c391c1fd1 | |||
0c9decce3e | |||
edb3160f35 | |||
0eb51cf3c5 | |||
80373424b4 | |||
d108baf672 | |||
031d140a44 | |||
c96ccf198c | |||
18835fa5ae | |||
c0e26cdc4b | |||
68a3fcb06f | |||
2433b0d7b2 | |||
570a1cd6b2 | |||
9425a85150 | |||
2b902aa31b | |||
7bb93a5edf | |||
4c10678b17 | |||
eb61d1abbf | |||
a0fa17fa1d | |||
f5373c8c8d | |||
ee60d6085c | |||
0ab5f2039c |
@ -100,10 +100,9 @@ codequality:
|
|||||||
only:
|
only:
|
||||||
- tags
|
- tags
|
||||||
script:
|
script:
|
||||||
- npmci command npm install -g tslint typescript
|
- npmci command npm install -g typescript
|
||||||
- npmci npm prepare
|
- npmci npm prepare
|
||||||
- npmci npm install
|
- npmci npm install
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
tags:
|
||||||
- lossless
|
- lossless
|
||||||
- docker
|
- docker
|
||||||
|
25437
package-lock.json
generated
25437
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@ -1,10 +1,11 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/tink",
|
"name": "@mojoio/tink",
|
||||||
"version": "1.0.3",
|
"version": "3.1.0",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "an unofficial api abstraction for tink.com",
|
"description": "an unofficial api abstraction for tink.com",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -12,17 +13,19 @@
|
|||||||
"build": "(tsbuild --web)"
|
"build": "(tsbuild --web)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@gitzone/tsbuild": "^2.1.65",
|
||||||
"@gitzone/tsbundle": "^1.0.78",
|
"@gitzone/tsbundle": "^2.0.7",
|
||||||
"@gitzone/tstest": "^1.0.64",
|
"@gitzone/tstest": "^1.0.73",
|
||||||
"@pushrocks/qenv": "^4.0.10",
|
"@pushrocks/qenv": "^5.0.2",
|
||||||
"@pushrocks/tapbundle": "^4.0.3",
|
"@pushrocks/tapbundle": "^5.0.4",
|
||||||
"@types/node": "^17.0.18",
|
"@types/node": "^18.7.8",
|
||||||
"tslint": "^6.1.3",
|
"tslint": "^6.1.3",
|
||||||
"tslint-config-prettier": "^1.15.0"
|
"tslint-config-prettier": "^1.15.0"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@pushrocks/smartrequest": "^1.1.52"
|
"@pushrocks/smartdelay": "^2.0.13",
|
||||||
|
"@pushrocks/smartrequest": "^2.0.10",
|
||||||
|
"@pushrocks/smarturl": "^3.0.5"
|
||||||
},
|
},
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
|
24
readme.md
24
readme.md
@ -27,6 +27,30 @@ Platform support | [;
|
||||||
|
const tinkUser = await tinkAccount.createTinkUser('<YourOwnUniqueUserId/externalUserId>');
|
||||||
|
const tinkLinkUrl = await tinkUser.getTinkLink('<marketCode like DE>');
|
||||||
|
|
||||||
|
// present the link to your user to connect their bank accounts to the tink platform.
|
||||||
|
|
||||||
|
const tinkProviderConsents = await tinkUser.getProviderConsents();
|
||||||
|
|
||||||
|
for (const providerConsent of tinkProviderConsents) {
|
||||||
|
const bankAccounts = await providerConsent.getBankAccounts();
|
||||||
|
for (const bankAccount of bankAccounts) {
|
||||||
|
const transactions = bankAccount.getTransactions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// additional stuff
|
||||||
|
const existingTinkUser = await tinkAccount.getUser('<YourOwnUniqueUserId/externalUserId>');
|
||||||
|
await existingTinkuser.delete(); // delete the user on the tink platform
|
||||||
|
```
|
||||||
|
|
||||||
## Contribution
|
## Contribution
|
||||||
|
|
||||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
||||||
|
61
test/test.nonci.ts
Normal file
61
test/test.nonci.ts
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
|
||||||
|
import * as qenv from '@pushrocks/qenv';
|
||||||
|
|
||||||
|
const testQenv = new qenv.Qenv('./', './.nogit/');
|
||||||
|
|
||||||
|
import * as tink from '../ts/index.js';
|
||||||
|
|
||||||
|
let tinkTestAccount: tink.TinkAccount;
|
||||||
|
|
||||||
|
tap.test('should create a valid tink account', async () => {
|
||||||
|
tinkTestAccount = new tink.TinkAccount(
|
||||||
|
testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'),
|
||||||
|
testQenv.getEnvVarOnDemand('TINK_CLIENT_SECRET')
|
||||||
|
);
|
||||||
|
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should report tink as healthy', async () => {
|
||||||
|
await expectAsync(tinkTestAccount.getTinkHealthyBoolean()).toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should create a tink user', async (toolsArg) => {
|
||||||
|
await tinkTestAccount.createTinkUser('user_1234_abc');
|
||||||
|
})
|
||||||
|
|
||||||
|
tap.test('should create a valid request', async (toolsArg) => {
|
||||||
|
const tinkuser: tink.TinkUser = await tinkTestAccount.getTinkUser('user_1234_abc');
|
||||||
|
console.log(tinkuser);
|
||||||
|
console.log(await tinkuser.getTinkLinkForMarket()); // defaults to 'DE';
|
||||||
|
console.log(await tinkuser.getProviderConsents());
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('allow tink link to be used', async (toolsArg) => {
|
||||||
|
await toolsArg.delayFor(60000);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('get provider consents', async () => {
|
||||||
|
const tinkuser: tink.TinkUser = await tinkTestAccount.getTinkUser('user_1234_abc');
|
||||||
|
const providerConsents = await tinkuser.getProviderConsents();
|
||||||
|
console.log(providerConsents);
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('get bankaccounts', async (toolsArg) => {
|
||||||
|
const tinkuser: tink.TinkUser = await tinkTestAccount.getTinkUser('user_1234_abc');
|
||||||
|
const bankAccounts = await tinkuser.getAllBankAccounts();
|
||||||
|
console.log(bankAccounts);
|
||||||
|
|
||||||
|
for (const bankAccount of bankAccounts) {
|
||||||
|
const transactions = await bankAccount.getTransactions();
|
||||||
|
console.log(transactions);
|
||||||
|
await toolsArg.delayFor(10000);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should delete existing users', async () => {
|
||||||
|
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
|
||||||
|
const tinkUser = new tink.TinkUser(tinkTestAccount, null, 'user_1234_abc');
|
||||||
|
await tinkUser.delete();
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.start();
|
26
test/test.ts
26
test/test.ts
@ -1,26 +0,0 @@
|
|||||||
import { expect, expectAsync, tap } from '@pushrocks/tapbundle';
|
|
||||||
import * as qenv from '@pushrocks/qenv';
|
|
||||||
|
|
||||||
const testQenv = new qenv.Qenv('./', './.nogit/');
|
|
||||||
|
|
||||||
import * as tink from '../ts/index';
|
|
||||||
|
|
||||||
let tinkTestAccount: tink.TinkAccount;
|
|
||||||
|
|
||||||
tap.test('should create a valid tink account', async () => {
|
|
||||||
tinkTestAccount = new tink.TinkAccount(
|
|
||||||
testQenv.getEnvVarOnDemand('TINK_CLIENT_ID'),
|
|
||||||
testQenv.getEnvVarOnDemand('TINK_CLIENT_SECRET')
|
|
||||||
);
|
|
||||||
expect(tinkTestAccount).toBeInstanceOf(tink.TinkAccount);
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should report tink as healthy', async () => {
|
|
||||||
await expectAsync(tinkTestAccount.getTinkHealthyBoolean()).toBeTrue();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.test('should create a valid request', async () => {
|
|
||||||
await tinkTestAccount.request('', 'POST', '', {});
|
|
||||||
})
|
|
||||||
|
|
||||||
tap.start();
|
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@mojoio/tink',
|
||||||
|
version: '3.1.0',
|
||||||
|
description: 'an unofficial api abstraction for tink.com'
|
||||||
|
}
|
@ -1 +1,3 @@
|
|||||||
export * from './tink.classes.tinkaccount';
|
export * from './tink.classes.tinkaccount.js';
|
||||||
|
export * from './tink.classes.tinkuser.js';
|
||||||
|
export * from './tink.classes.tinkproviderconsent.js';
|
||||||
|
89
ts/tink.classes.bankaccount.ts
Normal file
89
ts/tink.classes.bankaccount.ts
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
import { BankTransaction } from './tink.classes.banktransaction.js';
|
||||||
|
import { TinkUser } from './tink.classes.tinkuser.js';
|
||||||
|
import * as plugins from './tink.plugins.js';
|
||||||
|
|
||||||
|
export interface IBankAccountData {
|
||||||
|
"balances": {
|
||||||
|
"booked": {
|
||||||
|
"amount": {
|
||||||
|
"currencyCode": string,
|
||||||
|
"value": {
|
||||||
|
"scale": string,
|
||||||
|
"unscaledValue": string
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"customerSegment": string,
|
||||||
|
"dates": {
|
||||||
|
"lastRefreshed": string
|
||||||
|
},
|
||||||
|
"financialInstitutionId": string,
|
||||||
|
"id": string,
|
||||||
|
"identifiers": {
|
||||||
|
"iban": {
|
||||||
|
"bban": string,
|
||||||
|
"iban": string,
|
||||||
|
},
|
||||||
|
"pan": {
|
||||||
|
"masked": string
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"name": string,
|
||||||
|
"type": string,
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BankAccount {
|
||||||
|
// STATIC
|
||||||
|
public static async getAccountUserAccessToken(tinkUserArg: TinkUser) {
|
||||||
|
const authorizationCode = await tinkUserArg.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
tinkUserArg.externalUserIdArg,
|
||||||
|
tinkUserArg.tinkAccountRef.clientId,
|
||||||
|
'accounts:read,balances:read,transactions:read,provider-consents:read'
|
||||||
|
);
|
||||||
|
const accessToken = await tinkUserArg.tinkAccountRef.getUserAccessToken(authorizationCode);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getBankAccountsForUser(tinkUserArg: TinkUser) {
|
||||||
|
const userAccessToken = await this.getAccountUserAccessToken(tinkUserArg);
|
||||||
|
const returnBankAccounts: BankAccount[] = []
|
||||||
|
const getBankAccountRecursively = async (nextPageToken?: string) => {
|
||||||
|
const searchParams = new URLSearchParams();
|
||||||
|
searchParams.set('pageSize', '200');
|
||||||
|
if (nextPageToken) {
|
||||||
|
searchParams.set('pageToken', nextPageToken)
|
||||||
|
}
|
||||||
|
const response = await tinkUserArg.tinkAccountRef.request({
|
||||||
|
urlArg: `/data/v2/accounts?${searchParams.toString()}`,
|
||||||
|
accessToken: userAccessToken,
|
||||||
|
methodArg: 'GET',
|
||||||
|
payloadArg: null,
|
||||||
|
});
|
||||||
|
for (const account of response.accounts) {
|
||||||
|
returnBankAccounts.push(new BankAccount(tinkUserArg, account));
|
||||||
|
}
|
||||||
|
if (response.nextPageToken.length > 0) {
|
||||||
|
await getBankAccountRecursively(response.nextPageToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await getBankAccountRecursively();
|
||||||
|
return returnBankAccounts;
|
||||||
|
};
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
tinkUserRef: TinkUser;
|
||||||
|
data: IBankAccountData;
|
||||||
|
constructor(tinkUserRefArg: TinkUser, dataArg: IBankAccountData) {
|
||||||
|
this.tinkUserRef = tinkUserRefArg;
|
||||||
|
this.data = dataArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets the transactions for the bank account
|
||||||
|
*/
|
||||||
|
public async getTransactions() {
|
||||||
|
const transactions = await BankTransaction.getBankTransactions(this);
|
||||||
|
return transactions;
|
||||||
|
}
|
||||||
|
}
|
96
ts/tink.classes.banktransaction.ts
Normal file
96
ts/tink.classes.banktransaction.ts
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
import { BankAccount } from './tink.classes.bankaccount.js';
|
||||||
|
import * as plugins from './tink.plugins.js';
|
||||||
|
|
||||||
|
export interface IBankTransactiondata {
|
||||||
|
accountId:string;
|
||||||
|
amount: {
|
||||||
|
currencyCode: string;
|
||||||
|
value: {
|
||||||
|
scale: string;
|
||||||
|
unscaledValue: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
categories: {
|
||||||
|
pfm: {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
dates: {
|
||||||
|
booked: string;
|
||||||
|
value: string;
|
||||||
|
};
|
||||||
|
descriptions: {
|
||||||
|
display: string;
|
||||||
|
original: string;
|
||||||
|
};
|
||||||
|
id: string;
|
||||||
|
identifiers: {
|
||||||
|
providerTransactionId: string;
|
||||||
|
};
|
||||||
|
merchantInformation: {
|
||||||
|
merchantCategoryCode: 'string';
|
||||||
|
merchantName: 'string';
|
||||||
|
};
|
||||||
|
providerMutability: string;
|
||||||
|
reference: string;
|
||||||
|
status: string;
|
||||||
|
types: {
|
||||||
|
financialInstitutionTypeCode: string;
|
||||||
|
type: string;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export class BankTransaction {
|
||||||
|
// STATIC
|
||||||
|
public static async getTransactionAccessToken(bankAccountArg: BankAccount) {
|
||||||
|
const authorizationCode =
|
||||||
|
await bankAccountArg.tinkUserRef.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
bankAccountArg.tinkUserRef.externalUserIdArg,
|
||||||
|
bankAccountArg.tinkUserRef.tinkAccountRef.clientId,
|
||||||
|
'accounts:read,balances:read,transactions:read,provider-consents:read'
|
||||||
|
);
|
||||||
|
const accessToken = await bankAccountArg.tinkUserRef.tinkAccountRef.getUserAccessToken(
|
||||||
|
authorizationCode
|
||||||
|
);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getBankTransactions(bankAccountArg: BankAccount) {
|
||||||
|
const accessToken = await this.getTransactionAccessToken(bankAccountArg);
|
||||||
|
const pageSize = 100;
|
||||||
|
const returnTransactions: BankTransaction[] = [];
|
||||||
|
const getTransactionsRecursively = async (nextPageToken?: string) => {
|
||||||
|
const searchParams = new URLSearchParams();
|
||||||
|
searchParams.set('accountIdIn', bankAccountArg.data.id);
|
||||||
|
searchParams.set('pageSize', '200');
|
||||||
|
if (nextPageToken) {
|
||||||
|
searchParams.set('pageToken', nextPageToken)
|
||||||
|
}
|
||||||
|
const response = await bankAccountArg.tinkUserRef.tinkAccountRef.request({
|
||||||
|
urlArg: `/data/v2/transactions?${searchParams.toString()}`,
|
||||||
|
accessToken: accessToken,
|
||||||
|
methodArg: 'GET',
|
||||||
|
payloadArg: null,
|
||||||
|
});
|
||||||
|
for (const transaction of response.transactions) {
|
||||||
|
returnTransactions.push(new BankTransaction(bankAccountArg, transaction));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.nextPageToken.length > 0) {
|
||||||
|
await getTransactionsRecursively(response.nextPageToken);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
await getTransactionsRecursively();
|
||||||
|
return returnTransactions;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
bankAccountRef: BankAccount;
|
||||||
|
data: IBankTransactiondata;
|
||||||
|
|
||||||
|
constructor(bankAccountRefArg: BankAccount, dataArg: IBankTransactiondata) {
|
||||||
|
this.bankAccountRef = bankAccountRefArg;
|
||||||
|
this.data = dataArg;
|
||||||
|
}
|
||||||
|
}
|
@ -1,61 +1,178 @@
|
|||||||
import * as plugins from './tink.plugins';
|
import * as plugins from './tink.plugins.js';
|
||||||
|
|
||||||
|
import { TinkUser } from './tink.classes.tinkuser.js';
|
||||||
|
|
||||||
export class TinkAccount {
|
export class TinkAccount {
|
||||||
private clientId: string;
|
public clientId: string;
|
||||||
private clientSecret: string;
|
private _clientSecret: string;
|
||||||
|
|
||||||
|
private _apiBaseUrl: string = 'https://api.tink.com';
|
||||||
|
|
||||||
constructor(clientIdArg: string, clientSecretArg: string) {
|
constructor(clientIdArg: string, clientSecretArg: string) {
|
||||||
this.clientId = clientIdArg;
|
this.clientId = clientIdArg;
|
||||||
this.clientSecret = clientSecretArg;
|
this._clientSecret = clientSecretArg;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getTinkHealthyBoolean (): Promise<boolean> {
|
public async getTinkHealthyBoolean(): Promise<boolean> {
|
||||||
const response = await plugins.smartrequest.request('https://api.tink.com/api/v1/monitoring/healthy', {
|
const response = await plugins.smartrequest.request(
|
||||||
|
'https://api.tink.com/api/v1/monitoring/healthy',
|
||||||
});
|
{
|
||||||
|
keepAlive: false,
|
||||||
|
}
|
||||||
|
);
|
||||||
return response.body === 'ok';
|
return response.body === 'ok';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async getClientAccessTokenForScope(scopeArg: string): Promise<string> {
|
||||||
|
// lets get an accessToken for the request
|
||||||
|
const response = await plugins.smartrequest.postFormDataUrlEncoded(
|
||||||
|
`${this._apiBaseUrl}/api/v1/oauth/token`,
|
||||||
|
{
|
||||||
|
keepAlive: false,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
{
|
||||||
|
key: 'client_id',
|
||||||
|
content: this.clientId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'client_secret',
|
||||||
|
content: this._clientSecret,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'grant_type',
|
||||||
|
content: 'client_credentials',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'scope',
|
||||||
|
content: scopeArg,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
console.log(response.statusCode);
|
||||||
|
console.log(response.body);
|
||||||
|
throw new Error('there was an error aquiring an access token.');
|
||||||
|
}
|
||||||
|
const clientAccessToken = response.body.access_token;
|
||||||
|
return clientAccessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getUserAuthorizationCode(
|
||||||
|
externalUserIdArg: string,
|
||||||
|
actorCLientIdArg: string,
|
||||||
|
scopeArg: string
|
||||||
|
) {
|
||||||
|
const accessToken = await this.getClientAccessTokenForScope('authorization:grant');
|
||||||
|
const response = await plugins.smartrequest.postFormDataUrlEncoded(
|
||||||
|
`${this._apiBaseUrl}/api/v1/oauth/authorization-grant/delegate`,
|
||||||
|
{
|
||||||
|
keepAlive: false,
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${accessToken}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
[
|
||||||
|
{
|
||||||
|
key: 'response_type',
|
||||||
|
content: 'code',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'actor_client_id',
|
||||||
|
content: actorCLientIdArg,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'external_user_id',
|
||||||
|
content: externalUserIdArg,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'id_hint',
|
||||||
|
content: 'Hello there',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'scope',
|
||||||
|
content: scopeArg,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
console.log(response.body);
|
||||||
|
throw new Error('there was an error aquiring an access token.');
|
||||||
|
}
|
||||||
|
const userAuthorizationCode = response.body.code;
|
||||||
|
return userAuthorizationCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getUserAccessToken(authorizationCode: string): Promise<string> {
|
||||||
|
const accessToken = await this.getClientAccessTokenForScope('authorization:grant');
|
||||||
|
const response = await plugins.smartrequest.postFormDataUrlEncoded(
|
||||||
|
`${this._apiBaseUrl}/api/v1/oauth/token`,
|
||||||
|
{
|
||||||
|
keepAlive: false,
|
||||||
|
},
|
||||||
|
[
|
||||||
|
{
|
||||||
|
key: 'code',
|
||||||
|
content: authorizationCode,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'client_id',
|
||||||
|
content: this.clientId,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'client_secret',
|
||||||
|
content: this._clientSecret,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'grant_type',
|
||||||
|
content: 'authorization_code',
|
||||||
|
},
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode !== 200) {
|
||||||
|
console.log(response.body);
|
||||||
|
throw new Error('there was an error aquiring an access token.');
|
||||||
|
}
|
||||||
|
const userAccessToken = response.body.access_token;
|
||||||
|
return userAccessToken;
|
||||||
|
}
|
||||||
|
|
||||||
// the request method for tink respecting platform specific stuff
|
// the request method for tink respecting platform specific stuff
|
||||||
// e.g. certain headers if needed
|
// e.g. certain headers if needed
|
||||||
public async request(urlArg: string, methodArg: 'POST' | 'GET', scopeArg: string , payloadArg: any) {
|
public async request(optionsArg: {
|
||||||
|
urlArg: string;
|
||||||
|
methodArg: 'POST' | 'GET';
|
||||||
|
accessToken: string;
|
||||||
|
payloadArg: any;
|
||||||
|
}) {
|
||||||
// check health
|
// check health
|
||||||
if (!(await this.getTinkHealthyBoolean())) {
|
if (!(await this.getTinkHealthyBoolean())) {
|
||||||
throw new Error('TINK is not healthy tight now. Please try again later.');
|
throw new Error('TINK is not healthy right now. Please try again later.');
|
||||||
} else {
|
} else {
|
||||||
console.log('tink is healthy, continuing...');
|
console.log('tink is healthy, continuing...');
|
||||||
}
|
}
|
||||||
// lets get an accessToken for the request
|
const response = await plugins.smartrequest.request(`${this._apiBaseUrl}${optionsArg.urlArg}`, {
|
||||||
const response = await plugins.smartrequest.postFormData('https://api.tink.com/api/v1/oauth/token', {
|
keepAlive: false,
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'multipart/form-data'
|
Authorization: `Bearer ${optionsArg.accessToken}`,
|
||||||
}
|
'Content-Type': 'application/json',
|
||||||
}, [
|
|
||||||
{
|
|
||||||
name: 'client_id',
|
|
||||||
type: 'string',
|
|
||||||
payload: this.clientId,
|
|
||||||
contentType: 'text/plain'
|
|
||||||
},
|
},
|
||||||
{
|
method: optionsArg.methodArg,
|
||||||
name: 'client_secret',
|
requestBody: JSON.stringify(optionsArg.payloadArg),
|
||||||
type: 'string',
|
});
|
||||||
payload: this.clientSecret,
|
|
||||||
contentType: 'text/plain'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'grant_type',
|
|
||||||
type: 'string',
|
|
||||||
payload: 'client_credentials',
|
|
||||||
contentType: 'text/plain'
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'scope',
|
|
||||||
type: 'string',
|
|
||||||
payload: 'user:create',
|
|
||||||
contentType: 'text/plain'
|
|
||||||
}
|
|
||||||
]);
|
|
||||||
console.log(response.statusCode);
|
console.log(response.statusCode);
|
||||||
console.log(response.body);
|
return response.body;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
public async getTinkUser(externalUserIdArg: string) {
|
||||||
|
const tinkuser = await TinkUser.getTinkUser(this, externalUserIdArg);
|
||||||
|
return tinkuser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async createTinkUser(externalUserIdArg: string) {
|
||||||
|
const tinkuser = await TinkUser.createNewTinkUser(this, externalUserIdArg);
|
||||||
|
return tinkuser;
|
||||||
|
}
|
||||||
}
|
}
|
87
ts/tink.classes.tinkproviderconsent.ts
Normal file
87
ts/tink.classes.tinkproviderconsent.ts
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
import * as plugins from './tink.plugins.js';
|
||||||
|
|
||||||
|
export interface IProviderData {
|
||||||
|
credentialsId: string;
|
||||||
|
providerName: string;
|
||||||
|
status: string;
|
||||||
|
sessionExpiryDate: number;
|
||||||
|
accountIds: string[];
|
||||||
|
statusUpdated: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
import { TinkUser } from './tink.classes.tinkuser.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* a provider consent maps to tinks bank consents
|
||||||
|
*/
|
||||||
|
export class TinkProviderConsent {
|
||||||
|
public static async getProviderUserAccessToken(tinkUserRefArg: TinkUser) {
|
||||||
|
const authorizationCode = await tinkUserRefArg.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
tinkUserRefArg.externalUserIdArg,
|
||||||
|
tinkUserRefArg.tinkAccountRef.clientId,
|
||||||
|
'accounts:read,balances:read,transactions:read,provider-consents:read'
|
||||||
|
);
|
||||||
|
const accessToken = await tinkUserRefArg.tinkAccountRef.getUserAccessToken(authorizationCode);
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
// STATIC
|
||||||
|
public static async getProviderConsentsForUser(tinkUserRefArg: TinkUser) {
|
||||||
|
const returnProviderConsents: TinkProviderConsent[] = [];
|
||||||
|
const accessToken = await this.getProviderUserAccessToken(tinkUserRefArg);
|
||||||
|
const responseData = await tinkUserRefArg.tinkAccountRef.request({
|
||||||
|
urlArg: '/api/v1/provider-consents',
|
||||||
|
accessToken,
|
||||||
|
methodArg: 'GET',
|
||||||
|
payloadArg: null,
|
||||||
|
});
|
||||||
|
// console.log(responseData); // no nextPageToken here?
|
||||||
|
if (responseData.providerConsents) {
|
||||||
|
for (const providerConsentData of responseData.providerConsents) {
|
||||||
|
returnProviderConsents.push(new TinkProviderConsent(tinkUserRefArg, providerConsentData));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return returnProviderConsents;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
tinkUserRef: TinkUser;
|
||||||
|
data: IProviderData;
|
||||||
|
constructor(tinkUserRefArg: TinkUser, dataArg: IProviderData) {
|
||||||
|
this.tinkUserRef;
|
||||||
|
this.data = dataArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* refresh the bank account data from origin
|
||||||
|
*/
|
||||||
|
public async refresh() {
|
||||||
|
const userAccessToken = await TinkProviderConsent.getProviderUserAccessToken(this.tinkUserRef);
|
||||||
|
const response = await this.tinkUserRef.tinkAccountRef.request({
|
||||||
|
accessToken: userAccessToken,
|
||||||
|
methodArg: 'POST',
|
||||||
|
urlArg: `https://api.tink.com/api/v1/credentials/${this.data.credentialsId}/refresh`,
|
||||||
|
payloadArg: null
|
||||||
|
});
|
||||||
|
|
||||||
|
// polling as per documentation for status 'UPDATED';
|
||||||
|
let pollingRounds = 0;
|
||||||
|
let status = null;
|
||||||
|
while(status !== 'UPDATED' && pollingRounds < 10) {
|
||||||
|
await plugins.smartdelay.delayFor(2000);
|
||||||
|
await this.update();
|
||||||
|
status = this.data.status;
|
||||||
|
pollingRounds++;
|
||||||
|
}
|
||||||
|
// think about how to handle errors here
|
||||||
|
}
|
||||||
|
|
||||||
|
public async update() {
|
||||||
|
const providerConsents = await TinkProviderConsent.getProviderConsentsForUser(this.tinkUserRef);
|
||||||
|
for (const providerConsent of providerConsents) {
|
||||||
|
if (providerConsent.data.credentialsId === this.data.credentialsId) {
|
||||||
|
this.data = providerConsent.data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,166 @@
|
|||||||
import * as plugins from './tink.plugins';
|
import * as plugins from './tink.plugins.js';
|
||||||
|
|
||||||
|
import { TinkAccount } from './tink.classes.tinkaccount.js';
|
||||||
|
import { TinkProviderConsent } from './tink.classes.tinkproviderconsent.js';
|
||||||
|
import { BankAccount } from './tink.classes.bankaccount.js';
|
||||||
|
|
||||||
|
export class TinkUser {
|
||||||
|
// STATIC
|
||||||
|
public static async createNewTinkUser(tinkAccountArg: TinkAccount, externalUserIdArg: string) {
|
||||||
|
const accessToken = await tinkAccountArg.getClientAccessTokenForScope('user:create');
|
||||||
|
const responseData: {
|
||||||
|
external_user_id: string;
|
||||||
|
user_id: string;
|
||||||
|
} = await tinkAccountArg.request({
|
||||||
|
urlArg: '/api/v1/user/create',
|
||||||
|
accessToken,
|
||||||
|
methodArg: 'POST',
|
||||||
|
payloadArg: {
|
||||||
|
external_user_id: externalUserIdArg,
|
||||||
|
market: 'DE',
|
||||||
|
locale: 'en_US',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const newTinkUser = await TinkUser.getTinkUser(tinkAccountArg, externalUserIdArg);
|
||||||
|
return newTinkUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static async getTinkUser(tinkAccountArg: TinkAccount, externalUserIdArg: string) {
|
||||||
|
const authorizationCode = await tinkAccountArg.getUserAuthorizationCode(
|
||||||
|
externalUserIdArg,
|
||||||
|
tinkAccountArg.clientId,
|
||||||
|
'user:read'
|
||||||
|
);
|
||||||
|
const accessToken = await tinkAccountArg.getUserAccessToken(authorizationCode);
|
||||||
|
const responseData: {
|
||||||
|
appId: string;
|
||||||
|
created: string;
|
||||||
|
externalUserId: string;
|
||||||
|
flags: string[];
|
||||||
|
id: string;
|
||||||
|
nationalId: string;
|
||||||
|
profile: {
|
||||||
|
// cashbackEnabled: boolean; // deprecated
|
||||||
|
currency: string;
|
||||||
|
locale: string;
|
||||||
|
market: string;
|
||||||
|
notificationSettings: {
|
||||||
|
balance: boolean;
|
||||||
|
budget: boolean;
|
||||||
|
doubleCharge: boolean;
|
||||||
|
einvoices: boolean;
|
||||||
|
fraud: boolean;
|
||||||
|
income: boolean;
|
||||||
|
largeExpense: boolean;
|
||||||
|
leftToSpend: boolean;
|
||||||
|
loanUpdate: boolean;
|
||||||
|
summaryMonthly: boolean;
|
||||||
|
summaryWeekly: boolean;
|
||||||
|
transaction: boolean;
|
||||||
|
unusualAccount: boolean;
|
||||||
|
unusualCategory: boolean;
|
||||||
|
};
|
||||||
|
periodAdjustedDay: 25;
|
||||||
|
periodMode: 'MONTHLY_ADJUSTED' | 'MONTHLY';
|
||||||
|
timeZone: string;
|
||||||
|
};
|
||||||
|
// username: string; // not relevant
|
||||||
|
} = await tinkAccountArg.request({
|
||||||
|
urlArg: '/api/v1/user',
|
||||||
|
accessToken,
|
||||||
|
methodArg: 'GET',
|
||||||
|
payloadArg: null,
|
||||||
|
});
|
||||||
|
const newTinkUser = new TinkUser(tinkAccountArg, responseData.id, responseData.externalUserId);
|
||||||
|
return newTinkUser;
|
||||||
|
}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
public tinkAccountRef: TinkAccount;
|
||||||
|
public tinkUserId: string;
|
||||||
|
public externalUserIdArg: string;
|
||||||
|
|
||||||
|
constructor(tinkAccountrefArg: TinkAccount, tinkUserIdArg: string, externalUserIdArg: string) {
|
||||||
|
this.tinkAccountRef = tinkAccountrefArg;
|
||||||
|
this.tinkUserId = tinkUserIdArg;
|
||||||
|
this.externalUserIdArg = externalUserIdArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* deletes the user
|
||||||
|
*/
|
||||||
|
public async delete() {
|
||||||
|
const authorizationCode = await this.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
this.externalUserIdArg,
|
||||||
|
this.tinkAccountRef.clientId,
|
||||||
|
'user:delete'
|
||||||
|
);
|
||||||
|
const accessToken = await this.tinkAccountRef.getUserAccessToken(authorizationCode);
|
||||||
|
const response = await this.tinkAccountRef.request({
|
||||||
|
methodArg: 'POST',
|
||||||
|
accessToken,
|
||||||
|
payloadArg: {},
|
||||||
|
urlArg: '/api/v1/user/delete',
|
||||||
|
});
|
||||||
|
console.log(`successfully deleted user with externalId ${this.externalUserIdArg}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets a tink link that can be used by a user to connect accounts
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
public async getTinkLinkForMarket(linkOptionsArg: {
|
||||||
|
countryId: string;
|
||||||
|
redirectUrl: string;
|
||||||
|
/**
|
||||||
|
* an optional state that is transported through to the callback
|
||||||
|
*/
|
||||||
|
customState: string;
|
||||||
|
testProviderBool?: boolean;
|
||||||
|
} = {
|
||||||
|
countryId: 'DE',
|
||||||
|
redirectUrl: 'https://console.tink.com/callback',
|
||||||
|
customState: "exampleState",
|
||||||
|
testProviderBool: true
|
||||||
|
}): Promise<string> {
|
||||||
|
if (typeof linkOptionsArg.testProviderBool !== 'boolean') {
|
||||||
|
linkOptionsArg.testProviderBool = false;
|
||||||
|
}
|
||||||
|
const authorizationCode = await this.tinkAccountRef.getUserAuthorizationCode(
|
||||||
|
this.externalUserIdArg,
|
||||||
|
'df05e4b379934cd09963197cc855bfe9', // this is a hardcoded app id for tink link, as recommended by tink.com
|
||||||
|
'authorization:read,authorization:grant,credentials:refresh,credentials:read,credentials:write,providers:read,user:read'
|
||||||
|
);
|
||||||
|
const tinkUrlOptions: {[key: string]: string} = {}
|
||||||
|
tinkUrlOptions['client_id'] = this.tinkAccountRef.clientId;
|
||||||
|
tinkUrlOptions['redirect_uri']= linkOptionsArg.redirectUrl;
|
||||||
|
tinkUrlOptions['authorization_code'] = authorizationCode;
|
||||||
|
tinkUrlOptions['market'] = linkOptionsArg.countryId;
|
||||||
|
if (linkOptionsArg.testProviderBool) {
|
||||||
|
tinkUrlOptions['test'] = 'true';
|
||||||
|
}
|
||||||
|
if (linkOptionsArg.customState) {
|
||||||
|
tinkUrlOptions['state'] = linkOptionsArg.customState;
|
||||||
|
}
|
||||||
|
const url = plugins.smarturl.Smarturl.createFromUrl('https://link.tink.com/1.0/business-transactions/connect-accounts', {
|
||||||
|
searchParams: tinkUrlOptions
|
||||||
|
});
|
||||||
|
const tinkLinkUrl = url.toString();
|
||||||
|
return tinkLinkUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getProviderConsents(): Promise<TinkProviderConsent[]> {
|
||||||
|
const providerConsents: TinkProviderConsent[] =
|
||||||
|
await TinkProviderConsent.getProviderConsentsForUser(this);
|
||||||
|
return providerConsents;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* gets all accounts
|
||||||
|
*/
|
||||||
|
public async getAllBankAccounts() {
|
||||||
|
const bankAccounts = await BankAccount.getBankAccountsForUser(this);
|
||||||
|
return bankAccounts;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// @pushrocks scope
|
// @pushrocks scope
|
||||||
|
import * as smartdelay from '@pushrocks/smartdelay';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
|
import * as smarturl from '@pushrocks/smarturl';
|
||||||
|
|
||||||
export {
|
export { smartdelay, smartrequest, smartpromise, smarturl };
|
||||||
smartrequest
|
|
||||||
}
|
|
||||||
|
10
tsconfig.json
Normal file
10
tsconfig.json
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "ES2022",
|
||||||
|
"moduleResolution": "nodenext",
|
||||||
|
"esModuleInterop": true
|
||||||
|
}
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Reference in New Issue
Block a user