Skip to content

Commit 20e3963

Browse files
authored
Client setup: remove old code and promote best practises (#1525)
* Add javadoc and tests * Minor edit in README * Convert field to local var * Javadoc and tidy up * Improve test to follow best practises * Update README to follow best practises * Update README * Update README * Improve code and indentation * Minor edit
1 parent d6eb3ea commit 20e3963

File tree

6 files changed

+270
-166
lines changed

6 files changed

+270
-166
lines changed

README.md

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ Alternatively, you can download the [release on GitHub](https://github.com/Adyen
8282

8383
### General use with API key
8484

85-
Every API the library supports is represented by a service object. The name of the service matching the corresponding
86-
API is listed in the [Supported API versions](#supported-api-versions) section of this document.
85+
For every API, one or more corresponding service classes can be found in the folder with the same name.
86+
Check the [Supported API versions](#supported-api-versions).
87+
88+
**Note**: For requests on `live` environment, you must define the [Live URL Prefix](https://docs.adyen.com/development-resources/live-endpoints#live-url-prefix) in the Client object:
8789

8890
~~~~ java
8991
// Import the required classes
@@ -92,8 +94,13 @@ import com.adyen.enums.Environment;
9294
import com.adyen.service.checkout.PaymentsApi;
9395
import com.adyen.model.checkout.*;
9496

95-
// Setup Client and Service
96-
Client client = new Client("Your X-API-KEY", Environment.TEST);
97+
// Setup Client using Config object
98+
Config config = new Config()
99+
.environment(Environment.LIVE)
100+
.liveEndpointUrlPrefix("myCompany")
101+
.apiKey(apiKey);
102+
Client client = new Client(config);
103+
97104
PaymentsApi paymentsApi = new PaymentsApi(client);
98105

99106
// Create PaymentRequest
@@ -120,29 +127,18 @@ PaymentResponse paymentResponse = paymentsApi.payments(paymentRequest);
120127

121128
~~~~
122129

123-
### General use with API key for live environment
124-
For requests on live environment, you need to pass the [Live URL Prefix](https://docs.adyen.com/development-resources/live-endpoints#live-url-prefix) to the Client object:
125-
~~~~ java
126-
// Import the required classes
127-
import com.adyen.Client;
128-
import com.adyen.enums.Environment;
129-
import com.adyen.service.checkout.ModificationsApi
130-
131-
// Setup Client and Service
132-
Client client = new Client("Your X-API-KEY", Environment.LIVE, "Your live URL prefix");
133-
ModificationsApi modificationsApi = new ModificationsApi(client);
134-
135-
...
136-
~~~~
137130
### General use with basic auth
138131
~~~~ java
139132
// Import the required classes
140133
import com.adyen.Client;
141134
import com.adyen.enums.Environment;
142135
import com.adyen.service.checkout.PaymentLinksApi
143136

144-
// Setup Client and Service
145-
Client client = new Client("Your username", "Your password", Environment.LIVE, "Your live URL prefix", "Your application name");
137+
// Setup Client and Service passing prefix
138+
Client client = new Client("Your username", "Your password", Environment.LIVE, "mycompany123");
139+
// Or setup Client and Service passing prefix and application name
140+
//Client client = new Client("Your username", "Your password", Environment.LIVE, "mycompany123", "Your application name");
141+
146142
PaymentLinksApi paymentLinksApi = new PaymentLinksApi(client);
147143

148144
...
@@ -156,6 +152,23 @@ import com.adyen.model.checkout.PaymentRequest;
156152
// Deserialize using built-in function
157153
PaymentRequest paymentRequest = PaymentRequest.fromJson("YOUR_JSON_STRING");
158154
~~~~
155+
### Error handling
156+
157+
Use a try-catch block to handle API errors. Catch the `ApiException` to inspect the response and handle specific cases:
158+
~~~~
159+
try {
160+
service.getPaymentLink("1234");
161+
} catch (ApiException e) {
162+
// Obtain response
163+
int statusCode = e.getStatusCode();
164+
String responseBody = e.getResponseBody();
165+
// Check ApiError object
166+
ApiError apiError = e.getError();
167+
String errorCode = apiError.getErrorCode();
168+
List<String> invalidFields = apiError.getInvalidFields();
169+
....
170+
}
171+
~~~~
159172
### Using notification webhooks parser
160173
~~~~ java
161174
// Import the required classes
@@ -295,27 +308,9 @@ Client client = new Client(sslContext, apiKey);
295308
// Use the client
296309
~~~~
297310

298-
### Classic Platforms Error Handling
299311

300-
When requests fail, the library throws exceptions. For Classic AFP endpoints like [Create Account Holder](https://docs.adyen.com/api-explorer/Account/6/post/createAccountHolder), you can decode further details from the exception:
301-
302-
```java
303-
Client client = new Client("Your YOUR_API_KEY", Environment.TEST);
304-
ClassicPlatformAccountApi api = new ClassicPlatformAccountApi(client);
305-
CreateAccountHolderRequest request = new CreateAccountHolderRequest();
306-
307-
try {
308-
api.createAccountHolder(request);
309-
} catch (ApiException e) {
310-
CreateAccountHolderResponse error = CreateAccountHolderResponse.fromJson(e.getResponseBody());
311-
// inspect the error
312-
System.out.println(e.getStatusCode());
313-
System.out.println(error.getInvalidFields());
314-
}
315-
```
316-
317-
## Using the Cloud Terminal API Integration
318-
In order to submit In-Person requests with [Terminal API over Cloud](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/cloud/) you need to initialize the client in a similar way as the steps listed above for Ecommerce transactions, but make sure to include `TerminalCloudAPI`:
312+
## Using the Cloud Terminal API
313+
For In-Person Payments integrations with the [Cloud Terminal API](https://docs.adyen.com/point-of-sale/design-your-integration/choose-your-architecture/cloud/), you must initialise the Client **setting the closest** [Region](https://docs.adyen.com/point-of-sale/design-your-integration/terminal-api/#cloud):
319314
``` java
320315
// Step 1: Import the required classes
321316
import com.adyen.Client;
@@ -325,13 +320,11 @@ import com.adyen.model.nexo.*;
325320
import com.adyen.model.terminal.*;
326321

327322
// Step 2: Initialize the client object
328-
Client client = new Client("Your YOUR_API_KEY", Environment.TEST);
329-
330-
// for LIVE environment use
331-
// Config config = new Config();
332-
// config.setEnvironment(Environment.LIVE);
333-
// config.setTerminalApiRegion(Region.EU);
334-
// Client client = new Client(config);
323+
Config config = new Config()
324+
.environment(Environment.LIVE)
325+
.terminalApiRegion(Region.EU)
326+
.apiKey(apiKey);
327+
Client client = new Client(config);
335328

336329
// Step 3: Initialize the API object
337330
TerminalCloudAPI terminalCloudApi = new TerminalCloudAPI(client);
@@ -567,9 +560,7 @@ TerminalAPIRequest terminalAPIPaymentRequest = new TerminalAPIRequest();
567560
TerminalAPIResponse terminalAPIResponse = terminalLocalAPI.request(terminalAPIRequest);
568561
```
569562

570-
571563
### Example integrations
572-
573564
For a closer look at how our Java library works, you can clone one of our example integrations:
574565
* [Java Spring Boot example integration](https://github.com/adyen-examples/adyen-java-spring-online-payments).
575566
* [Kotlin Spring Boot example integration](https://github.com/adyen-examples/adyen-kotlin-spring-online-payments).
@@ -580,20 +571,15 @@ These include commented code, highlighting key features and concepts, and exampl
580571
We value your input! Help us enhance our API Libraries and improve the integration experience by providing your feedback. Please take a moment to fill out [our feedback form](https://forms.gle/A4EERrR6CWgKWe5r9) to share your thoughts, suggestions or ideas.
581572

582573
## Contributing
583-
584-
585-
We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements.
586-
574+
We encourage you to contribute to this repository, so everyone can benefit from new features, bug fixes, and any other improvements.
587575

588576
Have a look at our [contributing guidelines](CONTRIBUTING.md) to find out how to raise a pull request.
589-
590-
577+
591578
## Support
592579
If you have a feature request, or spotted a bug or a technical problem, [create an issue here](https://github.com/Adyen/adyen-java-api-library/issues/new/choose).
593580

594-
For other questions, [contact our Support Team](https://www.adyen.help/hc/en-us/requests/new?ticket_form_id=39.1.1705420).
595-
596-
581+
For other questions, [contact our Support Team](https://www.adyen.help/hc/en-us/requests/new?ticket_form_id=39.0.0705420).
582+
597583
## Licence
598584
This repository is available under the [MIT license](https://github.com/Adyen/adyen-java-api-library/blob/main/LICENSE).
599585

src/main/java/com/adyen/Client.java

Lines changed: 46 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ public class Client {
1818
public static final String TERMINAL_API_ENDPOINT_APSE =
1919
"https://terminal-api-live-apse.adyen.com";
2020

21+
/** Create Client instance (empty config) */
2122
public Client() {
2223
this.config = new Config();
2324
}
2425

26+
/**
27+
* Create Client instance with the given configuration
28+
*
29+
* @param config Configuration
30+
*/
2531
public Client(Config config) {
2632
this.config = config;
2733
this.setEnvironment(config.environment, config.liveEndpointUrlPrefix);
2834
}
2935

30-
public Client(String username, String password, Environment environment, String applicationName) {
31-
this(username, password, environment, null, applicationName);
32-
}
33-
3436
/**
3537
* Use this constructor to create client for client certificate authentication along with API key.
3638
* Note: Client certificate authentication is only applicable for PAL and Checkout services in
@@ -44,6 +46,28 @@ public Client(SSLContext sslContext, String apiKey) {
4446
this.config.setSSLContext(sslContext);
4547
}
4648

49+
/**
50+
* Create Client instance
51+
*
52+
* @param username HTTP basic username
53+
* @param password HTTP basic password
54+
* @param environment Environment (Test or Live)
55+
* @param liveEndpointUrlPrefix Prefix required for Live integrations
56+
*/
57+
public Client(
58+
String username, String password, Environment environment, String liveEndpointUrlPrefix) {
59+
this(username, password, environment, liveEndpointUrlPrefix, null);
60+
}
61+
62+
/**
63+
* Create Client instance
64+
*
65+
* @param username HTTP basic username
66+
* @param password HTTP basic password
67+
* @param environment Environment (Test or Live)
68+
* @param liveEndpointUrlPrefix Prefix required for Live integrations
69+
* @param applicationName Application name (additional name/tag passed in HTTP requests)
70+
*/
4771
public Client(
4872
String username,
4973
String password,
@@ -58,99 +82,33 @@ public Client(
5882
}
5983

6084
/**
61-
* @param username your merchant account Username
62-
* @param password your merchant accont Password
63-
* @param environment This defines the payment environment live or test
64-
* @param connectionTimeoutMillis Provide the time to time out
65-
* @deprecated As of library version 1.6.1, timeouts should be set by {@link #setTimeouts(int
66-
* connectionTimeoutMillis, int readTimeoutMillis)} or directly by {@link
67-
* com.adyen.Config#setConnectionTimeoutMillis(int connectionTimeoutMillis)}.
68-
*/
69-
@Deprecated
70-
public Client(
71-
String username, String password, Environment environment, int connectionTimeoutMillis) {
72-
this(username, password, environment, null);
73-
this.config.setConnectionTimeoutMillis(connectionTimeoutMillis);
74-
}
75-
76-
/**
77-
* @param username your merchant account Username
78-
* @param password your merchant accont Password
79-
* @param environment This defines the payment environment live or test
80-
* @param connectionTimeoutMillis Provide the time to time out
81-
* @param liveEndpointUrlPrefix provide the merchant specific url
82-
* @deprecated As of library version 1.6.1, timeouts should be set by {@link #setTimeouts(int
83-
* connectionTimeoutMillis, int readTimeoutMillis)} or directly by {@link
84-
* com.adyen.Config#setConnectionTimeoutMillis(int connectionTimeoutMillis)}.
85+
* Create Client instance
86+
*
87+
* @param apiKey API Key
88+
* @param environment Environment (Test or Live)
8589
*/
86-
@Deprecated
87-
public Client(
88-
String username,
89-
String password,
90-
Environment environment,
91-
int connectionTimeoutMillis,
92-
String liveEndpointUrlPrefix) {
93-
this(username, password, environment, liveEndpointUrlPrefix, null);
94-
this.config.setConnectionTimeoutMillis(connectionTimeoutMillis);
95-
}
96-
9790
public Client(String apiKey, Environment environment) {
9891
this(apiKey, environment, null);
9992
}
10093

94+
/**
95+
* Create Client instance
96+
*
97+
* @param apiKey API Key
98+
* @param environment Environment (Test or Live)
99+
* @param liveEndpointUrlPrefix Prefix required for the live integrations
100+
*/
101101
public Client(String apiKey, Environment environment, String liveEndpointUrlPrefix) {
102102
this.config = new Config();
103103
this.config.setApiKey(apiKey);
104104
this.setEnvironment(environment, liveEndpointUrlPrefix);
105105
}
106106

107107
/**
108-
* @param apiKey Defines the api key that can be retrieved by back office
109-
* @param environment This defines the payment environment live or test
110-
* @param connectionTimeoutMillis Provide the time to time out
111-
* @deprecated As of library version 1.6.1, timeouts should be set by {@link #setTimeouts(int
112-
* connectionTimeoutMillis, int readTimeoutMillis)} or directly by {@link
113-
* com.adyen.Config#setConnectionTimeoutMillis(int connectionTimeoutMillis)}.
114-
*/
115-
@Deprecated
116-
public Client(String apiKey, Environment environment, int connectionTimeoutMillis) {
117-
this(apiKey, environment);
118-
this.config.setConnectionTimeoutMillis(connectionTimeoutMillis);
119-
}
120-
121-
/**
122-
* @param apiKey Defines the api key that can be retrieved by back office
123-
* @param environment This defines the payment environment live or test
124-
* @param connectionTimeoutMillis Provide the time to time out
125-
* @param liveEndpointUrlPrefix provide the merchant specific url
126-
* @deprecated As of library version 1.6.1, timeouts should be set by {@link #setTimeouts(int
127-
* connectionTimeoutMillis, int readTimeoutMillis)} or directly by {@link
128-
* com.adyen.Config#setConnectionTimeoutMillis(int connectionTimeoutMillis)}.
129-
*/
130-
@Deprecated
131-
public Client(
132-
String apiKey,
133-
Environment environment,
134-
int connectionTimeoutMillis,
135-
String liveEndpointUrlPrefix) {
136-
this(apiKey, environment, liveEndpointUrlPrefix);
137-
this.config.setConnectionTimeoutMillis(connectionTimeoutMillis);
138-
}
139-
140-
/**
141-
* @param environment This defines the payment environment live or test
142-
* @deprecated As of library version 1.5.4, replaced by {@link #setEnvironment(Environment
143-
* environment, String liveEndpointUrlPrefix)}.
144-
*/
145-
@Deprecated
146-
public void setEnvironment(Environment environment) {
147-
this.setEnvironment(environment, null);
148-
}
149-
150-
/**
151-
* @param environment This defines the payment environment live or test
152-
* @param liveEndpointUrlPrefix Provide the unique live url prefix from the "API URLs and
153-
* Response" menu in the Adyen Customer Area
108+
* Set Environment, together with the live endpoint url prefix.
109+
*
110+
* @param environment Environment (Test or Live)
111+
* @param liveEndpointUrlPrefix The unique live url prefix (required for live integrations)
154112
*/
155113
public void setEnvironment(Environment environment, String liveEndpointUrlPrefix) {
156114
config.setEnvironment(environment);
@@ -161,8 +119,11 @@ public void setEnvironment(Environment environment, String liveEndpointUrlPrefix
161119
}
162120

163121
/**
122+
* Retrieve the Terminal Cloud endpoint based on Region and Environment
123+
*
164124
* @param region The region for which the endpoint is requested. If null or the region is not
165125
* found, defaults to default EU endpoint.
126+
* @param environment Environment (Test or Live)
166127
*/
167128
public String retrieveCloudEndpoint(Region region, Environment environment) {
168129
// Check the environment for TEST and get the endpoint

src/main/java/com/adyen/Config.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66
import javax.net.ssl.SSLContext;
77

88
public class Config {
9+
// API key authentication
10+
protected String apiKey;
11+
// Basic authentication
912
protected String username;
1013
protected String password;
14+
// Environment: Test or Live
1115
protected Environment environment;
1216

13-
/** Application name: used as HTTP client User-Agent */
17+
// Application name: used as HTTP client User-Agent
1418
protected String applicationName;
1519

16-
protected String apiKey;
20+
// HTTP Client options
1721
protected int connectionTimeoutMillis = 60 * 1000; // default 60 sec
1822
protected int readTimeoutMillis = 60 * 1000; // default 60 sec
1923
protected int connectionRequestTimeoutMillis = 60 * 1000; // default 60 sec
2024
protected int defaultKeepAliveMillis = 60 * 1000; // default 60 sec
2125
protected Boolean protocolUpgradeEnabled;
2226

23-
// Terminal API Specific
27+
// Terminal API configuration
2428
protected String terminalApiCloudEndpoint;
2529
protected String terminalApiLocalEndpoint;
2630
protected String liveEndpointUrlPrefix;

0 commit comments

Comments
 (0)