-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclient.ts
112 lines (96 loc) · 4.7 KB
/
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
import Config from "./config";
import HttpURLConnectionClient from "./httpClient/httpURLConnectionClient";
import ClientInterface from "./httpClient/clientInterface";
type ClientParametersOverload =
| { config: Config }
| { config: Config; httpClient: ClientInterface }
| { username: string; password: string; environment: Environment}
| { username: string; password: string; environment: Environment; httpClient: ClientInterface }
| { username: string; password: string; environment: Environment; liveEndpointUrlPrefix: string }
| { username: string; password: string; environment: Environment; liveEndpointUrlPrefix: string; httpClient: ClientInterface }
| { username: string; password: string; environment: Environment; applicationName: string }
| { username: string; password: string; environment: Environment; applicationName: string; httpClient: ClientInterface }
| { username: string; password: string; environment: Environment; applicationName: string; liveEndpointUrlPrefix: string }
| { username: string; password: string; environment: Environment; applicationName: string; liveEndpointUrlPrefix: string; httpClient: ClientInterface }
| { apiKey: string; environment: Environment }
| { apiKey: string; environment: Environment; httpClient: ClientInterface }
| { apiKey: string; environment: Environment; liveEndpointUrlPrefix: string }
| { apiKey: string; environment: Environment; liveEndpointUrlPrefix: string; httpClient: ClientInterface };
interface ClientParameters {
config?: Config;
username?: string;
password?: string;
environment?: Environment;
applicationName?: string;
liveEndpointUrlPrefix?: string;
apiKey?: string;
httpClient?: ClientInterface;
}
class Client {
public static MARKETPAY_ENDPOINT_TEST = "https://cal-test.adyen.com/cal/services";
public static MARKETPAY_ENDPOINT_LIVE = "https://cal-live.adyen.com/cal/services";
public static MARKETPAY_ACCOUNT_API_VERSION = "v6";
public static MARKETPAY_FUND_API_VERSION = "v6";
public static MARKETPAY_HOP_API_VERSION = "v6";
public static MARKETPAY_NOTIFICATION_API_VERSION = "v5";
public static MARKETPAY_NOTIFICATION_CONFIGURATION_API_VERSION = "v6";
public static TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com";
public static TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com";
private _httpClient!: ClientInterface;
public config: Config;
public liveEndpointUrlPrefix: string;
public constructor(clientParameters: ClientParametersOverload);
public constructor(options: ClientParameters) {
if (options.config) {
this.config = options.config;
} else {
this.config = new Config();
}
this.liveEndpointUrlPrefix = options.liveEndpointUrlPrefix ?? "";
const environment = options.environment ?? this.config.environment;
if (environment) {
this.setEnvironment(environment, options.liveEndpointUrlPrefix);
if (options.username && options.password) {
this.config.username = options.username;
this.config.password = options.password;
if(options.applicationName) {
this.config.applicationName = options.applicationName;
}
}
if (options.apiKey) {
this.config.apiKey = options.apiKey;
}
}
if (options.httpClient) {
this._httpClient = options.httpClient;
}
}
public setEnvironment(environment: Environment, liveEndpointUrlPrefix?: string): void {
// ensure environment and liveUrlPrefix is set in config
this.config.environment = environment;
this.liveEndpointUrlPrefix = liveEndpointUrlPrefix ?? "";
if (environment === "TEST") {
this.config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_TEST;
this.config.terminalApiCloudEndpoint = Client.TERMINAL_API_ENDPOINT_TEST;
} else if (environment === "LIVE") {
this.config.marketPayEndpoint = Client.MARKETPAY_ENDPOINT_LIVE;
this.config.terminalApiCloudEndpoint = Client.TERMINAL_API_ENDPOINT_LIVE;
}
}
public get httpClient(): ClientInterface {
if (!this._httpClient) {
this._httpClient = new HttpURLConnectionClient();
}
return this._httpClient;
}
public set httpClient(httpClient: ClientInterface) {
this._httpClient = httpClient;
}
public setApplicationName(applicationName: string): void {
this.config.applicationName = applicationName;
}
public setTimeouts(connectionTimeoutMillis: number): void {
this.config.connectionTimeoutMillis = connectionTimeoutMillis;
}
}
export default Client;