|
5 | 5 | import com.adyen.constants.ApiConstants; |
6 | 6 | import com.adyen.model.clouddevice.*; |
7 | 7 | import com.adyen.model.clouddevice.CloudDeviceApiAsyncResponse; |
8 | | -import com.adyen.security.clouddevice.EncryptionCredentialDetails; |
9 | | -import com.adyen.security.clouddevice.NexoSecurityException; |
10 | | -import com.adyen.security.clouddevice.NexoSecurityManager; |
11 | 8 | import com.adyen.service.exception.ApiException; |
12 | 9 | import com.adyen.service.resource.Resource; |
13 | | -import com.fasterxml.jackson.databind.JsonNode; |
14 | | -import com.fasterxml.jackson.databind.ObjectMapper; |
15 | 10 | import java.io.IOException; |
16 | 11 | import java.util.HashMap; |
17 | 12 | import java.util.Map; |
18 | 13 |
|
| 14 | +/** Service of the Cloud Device API */ |
19 | 15 | public class CloudDeviceApi extends Service { |
20 | 16 |
|
21 | 17 | public static final String API_VERSION = "1"; |
@@ -216,189 +212,4 @@ public DeviceStatusResponse getDeviceStatus(String merchantAccount, String devic |
216 | 212 |
|
217 | 213 | return DeviceStatusResponse.fromJson(response); |
218 | 214 | } |
219 | | - |
220 | | - /** |
221 | | - * Send a synchronous encrypted request. |
222 | | - * |
223 | | - * @param merchantAccount The unique identifier of the merchant account |
224 | | - * @param deviceId The unique identifier of the device that you send this request to (must match |
225 | | - * POIID in the MessageHeader). |
226 | | - * @param cloudDeviceApiRequest The request to send. |
227 | | - * @return instance of CloudDeviceApiResponse |
228 | | - * @throws ApiException when an error occurs |
229 | | - * @throws IOException when an I/O error occurs |
230 | | - * @throws NexoSecurityException when encryption or decryption fails |
231 | | - */ |
232 | | - public CloudDeviceApiResponse syncEncrypted( |
233 | | - String merchantAccount, |
234 | | - String deviceId, |
235 | | - CloudDeviceApiRequest cloudDeviceApiRequest, |
236 | | - EncryptionCredentialDetails encryptionCredentialDetails) |
237 | | - throws ApiException, IOException, NexoSecurityException { |
238 | | - |
239 | | - NexoSecurityManager nexoSecurityManager = new NexoSecurityManager(encryptionCredentialDetails); |
240 | | - |
241 | | - // Add path params |
242 | | - Map<String, String> pathParams = new HashMap<>(); |
243 | | - |
244 | | - if (merchantAccount == null) { |
245 | | - throw new IllegalArgumentException("Please provide the merchantAccount path parameter"); |
246 | | - } |
247 | | - pathParams.put("merchantAccount", merchantAccount); |
248 | | - |
249 | | - if (deviceId == null) { |
250 | | - throw new IllegalArgumentException("Please provide the deviceId path parameter"); |
251 | | - } |
252 | | - pathParams.put("deviceId", deviceId); |
253 | | - |
254 | | - if (cloudDeviceApiRequest.getSaleToPOIRequest() == null |
255 | | - || cloudDeviceApiRequest.getSaleToPOIRequest().getMessageHeader() == null) { |
256 | | - throw new IllegalArgumentException( |
257 | | - "cloudDeviceApiRequest must contain a SaleToPOIRequest with a MessageHeader"); |
258 | | - } |
259 | | - cloudDeviceApiRequest.getSaleToPOIRequest().getMessageHeader().setPOIID(deviceId); |
260 | | - |
261 | | - // encrypt payload |
262 | | - SaleToPOISecuredMessage saleToPOISecuredRequest = |
263 | | - nexoSecurityManager.encrypt( |
264 | | - cloudDeviceApiRequest.toJson(), |
265 | | - cloudDeviceApiRequest.getSaleToPOIRequest().getMessageHeader()); |
266 | | - |
267 | | - CloudDeviceApiSecuredRequest cloudDeviceApiSecuredRequest = new CloudDeviceApiSecuredRequest(); |
268 | | - cloudDeviceApiSecuredRequest.setSaleToPOIRequest(saleToPOISecuredRequest); |
269 | | - |
270 | | - String encryptedJson = cloudDeviceApiSecuredRequest.toJson(); |
271 | | - |
272 | | - // perform API call |
273 | | - Resource resource = |
274 | | - new Resource( |
275 | | - this, this.baseURL + "/merchants/{merchantAccount}/devices/{deviceId}/sync", null); |
276 | | - String response = |
277 | | - resource.request(encryptedJson, null, ApiConstants.HttpMethod.POST, pathParams); |
278 | | - |
279 | | - CloudDeviceApiSecuredResponse cloudDeviceApiSecuredResponse = |
280 | | - CloudDeviceApiSecuredResponse.fromJson(response); |
281 | | - |
282 | | - String jsonDecryptedResponse = |
283 | | - nexoSecurityManager.decrypt(cloudDeviceApiSecuredResponse.getSaleToPOIResponse()); |
284 | | - |
285 | | - return CloudDeviceApiResponse.fromJson(jsonDecryptedResponse); |
286 | | - } |
287 | | - |
288 | | - /** |
289 | | - * Send an asynchronous encrypted request. |
290 | | - * |
291 | | - * @param merchantAccount The unique identifier of the merchant account |
292 | | - * @param deviceId The unique identifier of the device that you send this request to (must match |
293 | | - * POIID in the MessageHeader). |
294 | | - * @param cloudDeviceApiRequest The request to send. |
295 | | - * @return "ok" on success |
296 | | - * @throws ApiException when an error occurs |
297 | | - * @throws IOException when an I/O error occurs |
298 | | - * @throws NexoSecurityException when encryption or decryption fails |
299 | | - */ |
300 | | - public String asyncEncrypted( |
301 | | - String merchantAccount, |
302 | | - String deviceId, |
303 | | - CloudDeviceApiRequest cloudDeviceApiRequest, |
304 | | - EncryptionCredentialDetails encryptionCredentialDetails) |
305 | | - throws ApiException, IOException, NexoSecurityException { |
306 | | - |
307 | | - NexoSecurityManager nexoSecurityManager = new NexoSecurityManager(encryptionCredentialDetails); |
308 | | - |
309 | | - // Add path params |
310 | | - Map<String, String> pathParams = new HashMap<>(); |
311 | | - |
312 | | - if (merchantAccount == null) { |
313 | | - throw new IllegalArgumentException("Please provide the merchantAccount path parameter"); |
314 | | - } |
315 | | - pathParams.put("merchantAccount", merchantAccount); |
316 | | - |
317 | | - if (deviceId == null) { |
318 | | - throw new IllegalArgumentException("Please provide the deviceId path parameter"); |
319 | | - } |
320 | | - pathParams.put("deviceId", deviceId); |
321 | | - |
322 | | - if (cloudDeviceApiRequest.getSaleToPOIRequest() == null |
323 | | - || cloudDeviceApiRequest.getSaleToPOIRequest().getMessageHeader() == null) { |
324 | | - throw new IllegalArgumentException( |
325 | | - "cloudDeviceApiRequest must contain a SaleToPOIRequest with a MessageHeader"); |
326 | | - } |
327 | | - cloudDeviceApiRequest.getSaleToPOIRequest().getMessageHeader().setPOIID(deviceId); |
328 | | - |
329 | | - // encrypt payload |
330 | | - SaleToPOISecuredMessage saleToPOISecuredRequest = |
331 | | - nexoSecurityManager.encrypt( |
332 | | - cloudDeviceApiRequest.toJson(), |
333 | | - cloudDeviceApiRequest.getSaleToPOIRequest().getMessageHeader()); |
334 | | - |
335 | | - CloudDeviceApiSecuredRequest cloudDeviceApiSecuredRequest = new CloudDeviceApiSecuredRequest(); |
336 | | - cloudDeviceApiSecuredRequest.setSaleToPOIRequest(saleToPOISecuredRequest); |
337 | | - |
338 | | - String encryptedJson = cloudDeviceApiSecuredRequest.toJson(); |
339 | | - |
340 | | - // perform API call |
341 | | - Resource resource = |
342 | | - new Resource( |
343 | | - this, this.baseURL + "/merchants/{merchantAccount}/devices/{deviceId}/async", null); |
344 | | - |
345 | | - // async responses are decrypted |
346 | | - String response = |
347 | | - resource.request(encryptedJson, null, ApiConstants.HttpMethod.POST, pathParams); |
348 | | - |
349 | | - return response; |
350 | | - } |
351 | | - |
352 | | - /** |
353 | | - * Decrypt an event notification |
354 | | - * |
355 | | - * @param payload Event notification in JSON string format: it can be SaleToPOIResponse (async |
356 | | - * response) or SaleToPOIRequest (event notification) |
357 | | - * @param encryptionCredentialDetails The details of the encryption credential used for decrypting |
358 | | - * the payload (nexoBlob) |
359 | | - * @return the decrypted payload |
360 | | - * @throws NexoSecurityException when decryption fails |
361 | | - */ |
362 | | - public String decryptNotification( |
363 | | - String payload, EncryptionCredentialDetails encryptionCredentialDetails) |
364 | | - throws NexoSecurityException { |
365 | | - |
366 | | - try { |
367 | | - NexoSecurityManager nexoSecurityManager = |
368 | | - new NexoSecurityManager(encryptionCredentialDetails); |
369 | | - |
370 | | - ObjectMapper objectMapper = new ObjectMapper(); |
371 | | - JsonNode jsonNode; |
372 | | - |
373 | | - try { |
374 | | - jsonNode = objectMapper.readTree(payload); |
375 | | - } catch (Exception e) { |
376 | | - throw new NexoSecurityException("Invalid payload"); |
377 | | - } |
378 | | - |
379 | | - String decryptedMessage; |
380 | | - if (jsonNode.has("SaleToPOIResponse")) { |
381 | | - // async response received |
382 | | - CloudDeviceApiSecuredResponse cloudDeviceApiSecuredResponse = |
383 | | - CloudDeviceApiSecuredResponse.fromJson(payload); |
384 | | - decryptedMessage = |
385 | | - nexoSecurityManager.decrypt(cloudDeviceApiSecuredResponse.getSaleToPOIResponse()); |
386 | | - } else if (jsonNode.has("SaleToPOIRequest")) { |
387 | | - CloudDeviceApiSecuredRequest cloudDeviceApiSecuredRequest = |
388 | | - CloudDeviceApiSecuredRequest.fromJson(payload); |
389 | | - decryptedMessage = |
390 | | - nexoSecurityManager.decrypt(cloudDeviceApiSecuredRequest.getSaleToPOIRequest()); |
391 | | - } else { |
392 | | - throw new NexoSecurityException( |
393 | | - "Unexpected payload without SaleToPOIResponse or SaleToPOIRequest"); |
394 | | - } |
395 | | - |
396 | | - return decryptedMessage; |
397 | | - |
398 | | - } catch (NexoSecurityException e) { |
399 | | - throw e; |
400 | | - } catch (Exception e) { |
401 | | - throw new NexoSecurityException(e.getMessage(), e); |
402 | | - } |
403 | | - } |
404 | 215 | } |
0 commit comments