1
1
import Config from "./config" ;
2
+ import { TERMINAL_API_ENDPOINT_TEST } from "./config" ;
3
+
2
4
import HttpURLConnectionClient from "./httpClient/httpURLConnectionClient" ;
3
5
import ClientInterface from "./httpClient/clientInterface" ;
4
6
5
- type ClientParametersOverload =
6
- | { config : Config }
7
- | { config : Config ; httpClient : ClientInterface }
8
- | { username : string ; password : string ; environment : Environment }
9
- | { username : string ; password : string ; environment : Environment ; httpClient : ClientInterface }
10
- | { username : string ; password : string ; environment : Environment ; liveEndpointUrlPrefix : string }
11
- | { username : string ; password : string ; environment : Environment ; liveEndpointUrlPrefix : string ; httpClient : ClientInterface }
12
- | { username : string ; password : string ; environment : Environment ; applicationName : string }
13
- | { username : string ; password : string ; environment : Environment ; applicationName : string ; httpClient : ClientInterface }
14
- | { username : string ; password : string ; environment : Environment ; applicationName : string ; liveEndpointUrlPrefix : string }
15
- | { username : string ; password : string ; environment : Environment ; applicationName : string ; liveEndpointUrlPrefix : string ; httpClient : ClientInterface }
16
- | { apiKey : string ; environment : Environment }
17
- | { apiKey : string ; environment : Environment ; httpClient : ClientInterface }
18
- | { apiKey : string ; environment : Environment ; liveEndpointUrlPrefix : string }
19
- | { apiKey : string ; environment : Environment ; liveEndpointUrlPrefix : string ; httpClient : ClientInterface } ;
20
-
21
- interface ClientParameters {
22
- config ?: Config ;
23
- username ?: string ;
24
- password ?: string ;
25
- environment ?: Environment ;
26
- applicationName ?: string ;
27
- liveEndpointUrlPrefix ?: string ;
28
- apiKey ?: string ;
29
- httpClient ?: ClientInterface ;
30
- }
31
7
8
+ /**
9
+ * Main Adyen API Client class.
10
+ * Handles configuration, authentication, and HTTP client setup for API requests.
11
+ */
32
12
class Client {
13
+ // Static endpoints and API version constants
14
+ // @deprecated : use Config.TERMINAL_API_ENDPOINT_TEST instead
15
+ public static TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com" ;
16
+ // @deprecated : use Config.TERMINAL_API_ENDPOINT_LIVE instead
17
+ public static TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com" ;
18
+ // legacy support for marketPayEndpoint
33
19
public static MARKETPAY_ENDPOINT_TEST = "https://cal-test.adyen.com/cal/services" ;
34
20
public static MARKETPAY_ENDPOINT_LIVE = "https://cal-live.adyen.com/cal/services" ;
35
21
public static MARKETPAY_ACCOUNT_API_VERSION = "v6" ;
36
22
public static MARKETPAY_FUND_API_VERSION = "v6" ;
37
23
public static MARKETPAY_HOP_API_VERSION = "v6" ;
38
24
public static MARKETPAY_NOTIFICATION_API_VERSION = "v5" ;
39
25
public static MARKETPAY_NOTIFICATION_CONFIGURATION_API_VERSION = "v6" ;
40
- public static TERMINAL_API_ENDPOINT_TEST = "https://terminal-api-test.adyen.com" ;
41
- public static TERMINAL_API_ENDPOINT_LIVE = "https://terminal-api-live.adyen.com" ;
26
+
42
27
43
28
private _httpClient ! : ClientInterface ;
44
29
public config : Config ;
45
- public liveEndpointUrlPrefix : string ;
46
-
47
- public constructor ( clientParameters : ClientParametersOverload ) ;
48
- public constructor ( options : ClientParameters ) {
49
- if ( options . config ) {
50
- this . config = options . config ;
51
- } else {
52
- this . config = new Config ( ) ;
30
+
31
+ /**
32
+ * Constructs a new Client instance.
33
+ * @param options - Configuration object
34
+ */
35
+ public constructor ( options : Config , httpClient ?: ClientInterface ) {
36
+
37
+ this . config = options ;
38
+
39
+ if ( ! this . config . environment ) {
40
+ throw new Error ( "Environment must be defined" ) ;
53
41
}
54
- this . liveEndpointUrlPrefix = options . liveEndpointUrlPrefix ?? "" ;
55
-
56
- const environment = options . environment ?? this . config . environment ;
57
- if ( environment ) {
58
- this . setEnvironment ( environment , options . liveEndpointUrlPrefix ) ;
59
- if ( options . username && options . password ) {
60
- this . config . username = options . username ;
61
- this . config . password = options . password ;
62
- }
63
42
64
- if ( options . apiKey ) {
65
- this . config . apiKey = options . apiKey ;
43
+ // set Terminal API endpoints
44
+ if ( this . config . environment === "TEST" ) {
45
+ // one TEST endpoint for all regions
46
+ this . config . terminalApiCloudEndpoint = TERMINAL_API_ENDPOINT_TEST ;
47
+ } else if ( this . config . environment === "LIVE" ) {
48
+ // region-based LIVE endpoints
49
+ if ( this . config . region ) {
50
+ if ( ! Config . isRegionValid ( this . config . region ) ) {
51
+ throw new Error ( `Invalid region provided: ${ this . config . region } ` ) ;
52
+ }
53
+ this . config . terminalApiCloudEndpoint = Config . getTerminalApiEndpoint ( this . config . region ) ;
54
+ } else {
55
+ console . info ( "TerminalAPI Region is not be defined: ignore this if you are not using Terminal API." ) ;
66
56
}
67
57
}
68
58
59
+ if ( this . config . environment === "LIVE" && ! this . config . liveEndpointUrlPrefix ) {
60
+ throw new Error ( "Live endpoint URL prefix must be provided for LIVE environment." ) ;
61
+ }
62
+
63
+ // legacy support for marketPayEndpoint
64
+ if ( this . config . environment === "TEST" ) {
65
+ this . config . marketPayEndpoint = Client . MARKETPAY_ENDPOINT_TEST ;
66
+ } else if ( this . config . environment === "LIVE" ) {
67
+ this . config . marketPayEndpoint = Client . MARKETPAY_ENDPOINT_LIVE ;
68
+ }
69
+
70
+ // Set application name if provided
69
71
if ( options . applicationName ) {
70
72
this . config . applicationName = options . applicationName ;
71
73
}
72
74
73
- if ( options . httpClient ) {
74
- this . _httpClient = options . httpClient ;
75
+ // Set custom HTTP client if provided
76
+ if ( httpClient ) {
77
+ this . _httpClient = httpClient ;
75
78
}
79
+
76
80
}
77
81
82
+ /**
83
+ * Sets the environment and updates endpoints in the config.
84
+ * @param environment - The environment ("TEST" or "LIVE").
85
+ * @param liveEndpointUrlPrefix - Optional live endpoint prefix.
86
+ */
78
87
public setEnvironment ( environment : Environment , liveEndpointUrlPrefix ?: string ) : void {
79
- // ensure environment and liveUrlPrefix is set in config
88
+ // ensure environment is set in config
80
89
this . config . environment = environment ;
81
- this . liveEndpointUrlPrefix = liveEndpointUrlPrefix ?? "" ;
82
90
83
91
if ( environment === "TEST" ) {
84
92
this . config . marketPayEndpoint = Client . MARKETPAY_ENDPOINT_TEST ;
85
- this . config . terminalApiCloudEndpoint = Client . TERMINAL_API_ENDPOINT_TEST ;
86
93
} else if ( environment === "LIVE" ) {
87
94
this . config . marketPayEndpoint = Client . MARKETPAY_ENDPOINT_LIVE ;
88
- this . config . terminalApiCloudEndpoint = Client . TERMINAL_API_ENDPOINT_LIVE ;
89
95
}
90
96
}
91
97
98
+ /**
99
+ * Gets the HTTP client instance, creating a default one if not set.
100
+ */
92
101
public get httpClient ( ) : ClientInterface {
93
102
if ( ! this . _httpClient ) {
94
103
this . _httpClient = new HttpURLConnectionClient ( ) ;
@@ -97,17 +106,30 @@ class Client {
97
106
return this . _httpClient ;
98
107
}
99
108
109
+ /**
110
+ * Sets a custom HTTP client.
111
+ * @param httpClient - The HTTP client to use.
112
+ */
100
113
public set httpClient ( httpClient : ClientInterface ) {
101
114
this . _httpClient = httpClient ;
102
115
}
103
116
117
+ /**
118
+ * Sets the application name in the config.
119
+ * @param applicationName - The application name.
120
+ */
104
121
public setApplicationName ( applicationName : string ) : void {
105
122
this . config . applicationName = applicationName ;
106
123
}
107
124
125
+ /**
126
+ * Sets the connection timeout in milliseconds.
127
+ * @param connectionTimeoutMillis - Timeout in milliseconds.
128
+ */
108
129
public setTimeouts ( connectionTimeoutMillis : number ) : void {
109
130
this . config . connectionTimeoutMillis = connectionTimeoutMillis ;
110
131
}
111
132
}
112
133
113
134
export default Client ;
135
+
0 commit comments