-
Notifications
You must be signed in to change notification settings - Fork 8
/
fintecture-client.ts
156 lines (121 loc) · 5.94 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
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
import { AIS } from './src/Ais';
import { Authentication } from './src/Authentication';
import { IFintectureConfig } from './src/interfaces/ConfigInterface'
import { IPisConnect, IAisConnect } from './src/interfaces/connect/ConnectInterface'
import { Constants, environment } from './src/utils/Constants';
import { Connect } from './src/Connect';
import { PIS } from './src/Pis';
import { Resources } from './src/Resources';
import { Validation } from './src/Validation';
/**
* Class responsible to centralize and dispatch all calls to the different Fintecture services
*
* @export
* @class FintectureClient
*/
export class FintectureClient {
public validation: Validation;
private config: IFintectureConfig;
private connect: Connect;
private resources: Resources;
private authentication: Authentication;
private pis: PIS;
private 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);
this.validation = new Validation(this.config);
}
public async getAccessToken(authCode?: string, scopes?: string): Promise<object> {
return this.authentication.accessToken(authCode, scopes);
}
public async refreshAccessToken(refreshToken: string): Promise<object> {
return this.authentication.refreshToken(refreshToken);
}
public async getProviders(search?: object): Promise<object> {
return this.resources.providers(search);
}
public async getApplication(): Promise<object> {
return this.resources.application();
}
public async getTestAccounts(search?: object): Promise<object> {
return this.resources.testAccounts(search);
}
public async getRedirectAuthUrl(accessToken: string, providerId: string, redirectUri: string, state?: string): Promise<object> {
return this.ais.authorize(accessToken, providerId, redirectUri, state);
}
public async getDecoupledAuthUrl(accessToken: string, providerId: string, psuId: string, psuIpAddress: string, redirectUri: string): Promise<object> {
return this.ais.authorize(accessToken, providerId, redirectUri, null, Constants.DECOUPLEDMODEL, psuId, psuIpAddress);
}
public async getDecoupledAuthStatus(accessToken: string, providerId: string, pollingId: string): Promise<object> {
return this.ais.decoupled(accessToken, providerId, pollingId);
}
public async getAccounts(accessToken: string, customerId: string, search?: any, headers?: any): Promise<object> {
return this.ais.getAccounts(accessToken, customerId, search, headers);
}
public async getTransactions(accessToken: string, customerId: string, accountId, search?: any, headers?: any, paginationUrl?: string): Promise<object> {
return this.ais.getTransactions(accessToken, customerId, accountId, search, headers, paginationUrl);
}
public async getAccountHolders(accessToken: string, customerId: string, search?: any, headers?: any): Promise<object> {
return this.ais.getAccountHolders(accessToken, customerId, search, headers);
}
public async preparePayment(accessToken: string, payload: any): Promise<object> {
return this.pis.prepare(accessToken, payload);
}
public async paymentInitiate(accessToken: string, providerId: string, payload: any, redirectUri: string, state?: string): Promise<object> {
return this.pis.initiate(accessToken, providerId, payload, redirectUri, state);
}
public async paymentConfirmation(accessToken: string, customerId: string, sessionId: string): Promise<object> {
return this.pis.confirm(accessToken, customerId, sessionId);
}
public async getPayments(accessToken: string, sessionId: string, queryParameters?: object): Promise<object> {
return this.pis.getPayments(accessToken, sessionId, queryParameters);
}
public async initiateRefund(accessToken: string, sessionId: string) {
return this.pis.initiateRefund(accessToken, sessionId);
}
public async getPisConnect(accessToken: string, connectConfig: any): Promise<IPisConnect> {
return this.connect.getPisConnect(accessToken, connectConfig);
}
public async getAisConnect(accessToken: string, connectConfig: any): Promise<IAisConnect> {
return this.connect.getAisConnect(accessToken, connectConfig);
}
public deleteCustomer(accessToken: string, customerId: string): Promise<object> {
return this.ais.deleteCustomer(accessToken, customerId);
}
private _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) {
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 && !Object.keys(environment).includes(config.env)) {
throw Error(`environment is badly configured. Environments are ${Object.keys(environment).join(", ")}.`);
}
if (!config.env) {
config.env = Constants.DEFAULTENVIRONMENT;
}
return config as IFintectureConfig;
}
}