forked from Fintecture/fintecture-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfintecture-client.ts
130 lines (102 loc) · 4.43 KB
/
fintecture-client.ts
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
import { Constants } from './src/utils/Constants';
import { Connect } from './src/Connect';
import { Resources } from './src/Resources';
import { Authentication } from './src/Authentication';
import { PIS } from './src/PIS';
import { AIS } from './src/AIS';
import { Config } from './src/interfaces/ConfigInterface'
/**
* Class responsible to centralize and dispatch all calls to the different Fintecture services
*
* @export
* @class FintectureClient
*/
export class FintectureClient {
config: Config;
connect: Connect;
resources: Resources;
authentication: Authentication;
pis: PIS;
ais: AIS;
/**
* Create an instance of the fintecture client
* @param {Object=} config - configuration parameters
*/
constructor(config: Object) {
this.config = this._validateConfigIntegrity(config);
this.connect = new Connect(this.config);
this.resources = new Resources(this.config);
this.authentication = new Authentication(this.config);
this.pis = new PIS(this.config);
this.ais = new AIS(this.config);
}
async getAccessToken(authCode?: string): Promise<object> {
return this.authentication.accessToken(authCode);
}
async refreshAccessToken(refreshToken: string): Promise<object> {
return this.authentication.refreshToken(refreshToken);
}
async getProviders(options?: object): Promise<object> {
return this.resources.providers(options);
}
async getApplication(): Promise<object> {
return this.resources.application();
}
async getTestAccounts(options?: object): Promise<object> {
return this.resources.testAccounts(options);
}
async getProviderAuthUrl(accessToken: string, providerId: string, redirectUri: string, state?: string): Promise<object> {
return this.ais.authorize(accessToken, providerId, redirectUri, state);
}
async getAccounts(accessToken: string, customerId: string, options?: any): Promise<object> {
return this.ais.getAccounts(accessToken, customerId, options);
}
async getTransactions(accessToken: string, customerId: string, accountId, options?: any): Promise<object> {
return this.ais.getTransactions(accessToken, customerId, accountId, options);
}
async getAccountHolders(accessToken: string, customerId: string, options?: any): Promise<object> {
return this.ais.getAccountHolders(accessToken, customerId, options);
}
async paymentInitiate(accessToken: string, providerId: string, payload: any, redirectUri: string, state?: string): Promise<object> {
return this.pis.initiate(accessToken, providerId, payload, redirectUri, state);
}
async paymentConfirmation(accessToken: string, customerId: string, sessionId: string): Promise<object> {
return this.pis.confirm(accessToken, customerId, sessionId);
}
async getPayment(accessToken: string, customerId: string, sessionId: string): Promise<object> {
return this.pis.getPayments(accessToken, customerId, sessionId);
}
async getPisConnectUrl(accessToken: string, connectConfig: any): Promise<string> {
return this.connect.getPisConnectUrl(accessToken, connectConfig);
}
verifyConnectUrlParameters(queryString): boolean {
return this.connect.verifyUrlParameters(queryString);
}
_validateConfigIntegrity(config) {
if (!config.app_id) {
throw Error('app_id is not configured');
}
if (!config.app_secret) {
throw Error('app_secret is not configured');
}
if (!config.private_key && config.env == Constants.PRODUCTIONENVIRONMENT) {
throw Error('private_key must be a string');
}
if (config.private_key && typeof config.private_key != 'string') {
throw Error('private_key must be a string');
}
if (config.private_key && !(config.private_key.indexOf('-----BEGIN PRIVATE KEY-----') >= 0)) {
throw Error('private_key is in a wrong format');
}
if (config.private_key && !(config.private_key.indexOf('-----END PRIVATE KEY-----') >= 0)) {
throw Error('private_key is in a wrong format');
}
if (config.env && !['sandbox', 'production'].includes(config.env)) {
throw Error('environment is badly configured.');
}
if (!config.env) {
config.env = Constants.DEFAULTENVIRONMENT;
}
return <Config>config;
}
}