diff --git a/src/main/java/it/pagopa/swclient/mil/idpay/ErrorCode.java b/src/main/java/it/pagopa/swclient/mil/idpay/ErrorCode.java index 02ef0c4..d1169c0 100644 --- a/src/main/java/it/pagopa/swclient/mil/idpay/ErrorCode.java +++ b/src/main/java/it/pagopa/swclient/mil/idpay/ErrorCode.java @@ -44,7 +44,7 @@ public final class ErrorCode { public static final String ERROR_CALLING_IDPAY_REST_SERVICES = MODULE_ID + "000050"; public static final String ERROR_NOT_FOUND_IDPAY_REST_SERVICES = MODULE_ID + "000051"; public static final String ERROR_CALLING_AUTHORIZE_REST_SERVICES = MODULE_ID + "000052"; - public static final String ERROR_IDPAY_WRONG_AUTH_CODE = MODULE_ID + "000053"; + public static final String ERROR_IDPAY_PAYMENT_INVALID_PINBLOCK = MODULE_ID + "000053"; public static final String ERROR_IDPAY_UNKNOWN_ERROR_CODE = MODULE_ID + "000054"; public static final String ERROR_RETRIEVING_SECOND_FACTOR = MODULE_ID + "000055"; public static final String ERROR_PIN_CIE_CURRENT_USER = MODULE_ID + "000056"; @@ -95,7 +95,6 @@ public final class ErrorCode { public static final String ERROR_RETRIEVING_KEY_PAIR_DESCR = "Azure KV key pair retrieving error"; public static final String ERROR_CALLING_AUTHORIZE_REST_SERVICES_DESCR = "Error calling IdPay rest service with authorize transaction"; public static final String ERROR_RETRIEVING_PUBLIC_KEY_IDPAY_DESCR = "IDPay public key retrieving error"; - public static final String ERROR_IDPAY_WRONG_AUTH_CODE_DESCR = "IDPay responds with WRONG_AUTH_CODE"; public static final String ERROR_IDPAY_UNKNOWN_ERROR_DESCR = "IDPay responds with an unknown error 500"; public static final String ERROR_ENCRYPTING_SESSION_KEY_DESCR = "Error occurs during encrypting session key"; public static final String ERROR_RETRIEVING_SECOND_FACTOR_DESCR = "Error retrieving second factor"; @@ -126,7 +125,6 @@ public final class ErrorCode { public static final String ERROR_RETRIEVING_KEY_PAIR_MSG = "[" + ERROR_RETRIEVING_KEY_PAIR + "] " + ERROR_RETRIEVING_KEY_PAIR_DESCR; public static final String ERROR_CALLING_AUTHORIZE_REST_SERVICES_MSG = "[" + ERROR_CALLING_AUTHORIZE_REST_SERVICES + "] " + ERROR_CALLING_AUTHORIZE_REST_SERVICES_DESCR; public static final String ERROR_RETRIEVING_PUBLIC_KEY_IDPAY_MSG = "[" + ERROR_RETRIEVING_PUBLIC_KEY_IDPAY + "] " + ERROR_RETRIEVING_PUBLIC_KEY_IDPAY_DESCR; - public static final String ERROR_IDPAY_WRONG_AUTH_CODE_MSG = "[" + ERROR_IDPAY_WRONG_AUTH_CODE + "] " + ERROR_IDPAY_WRONG_AUTH_CODE_DESCR; public static final String ERROR_IDPAY_UNKNOWN_ERROR_MSG = "[" + ERROR_IDPAY_UNKNOWN_ERROR_CODE + "] " + ERROR_IDPAY_UNKNOWN_ERROR_DESCR; public static final String ERROR_ENCRYPTING_SESSION_KEY_MSG = "[" + ERROR_ENCRYPTING_SESSION_KEY + "] " + ERROR_ENCRYPTING_SESSION_KEY_DESCR; public static final String ERROR_RETRIEVING_SECOND_FACTOR_MSG = "[" + ERROR_RETRIEVING_SECOND_FACTOR + "] " + ERROR_RETRIEVING_SECOND_FACTOR_DESCR; diff --git a/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthMessageType.java b/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthMessageType.java deleted file mode 100644 index 4d9d803..0000000 --- a/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthMessageType.java +++ /dev/null @@ -1,31 +0,0 @@ -package it.pagopa.swclient.mil.idpay.bean; - -public enum AuthMessageType { - PAYMENT_NOT_FOUND_OR_EXPIRED, - PAYMENT_TRANSACTION_EXPIRED, - PAYMENT_INITIATIVE_NOT_FOUND, - PAYMENT_INITIATIVE_INVALID_DATE, - PAYMENT_INITIATIVE_NOT_DISCOUNT, - PAYMENT_ALREADY_AUTHORIZED, - PAYMENT_BUDGET_EXHAUSTED, - PAYMENT_GENERIC_REJECTED, - PAYMENT_TOO_MANY_REQUESTS, - PAYMENT_GENERIC_ERROR, - PAYMENT_USER_SUSPENDED, - PAYMENT_INVALID_PINBLOCK, - PAYMENT_IDPAYCODE_NOT_FOUND, - PAYMENT_USER_NOT_ONBOARDED, - PAYMENT_USER_UNSUBSCRIBED, - PAYMENT_ALREADY_ASSIGNED, - PAYMENT_NOT_ALLOWED_FOR_TRX_STATUS, - PAYMENT_NOT_ALLOWED_MISMATCHED_MERCHANT, - PAYMENT_USER_NOT_ASSOCIATED, - PAYMENT_DELETE_NOT_ALLOWED_FOR_TRX_STATUS, - PAYMENT_UNRELATE_NOT_ALLOWED_FOR_TRX_STATUS, - PAYMENT_AMOUNT_NOT_VALID, - PAYMENT_MERCHANT_NOT_ONBOARDED, - PAYMENT_INVALID_REQUEST, - PAYMENT_STATUS_NOT_VALID, - PAYMENT_TRANSACTION_VERSION_MISMATCH, - PAYMENT_TRANSACTION_VERSION_PENDING -} diff --git a/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthTransactionResponseWrong.java b/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthTransactionResponseWrong.java index 6404588..38628a4 100644 --- a/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthTransactionResponseWrong.java +++ b/src/main/java/it/pagopa/swclient/mil/idpay/bean/AuthTransactionResponseWrong.java @@ -9,6 +9,6 @@ @RegisterForReflection public class AuthTransactionResponseWrong { - private AuthMessageType code; + private String code; private String message; } diff --git a/src/main/java/it/pagopa/swclient/mil/idpay/service/TransactionsService.java b/src/main/java/it/pagopa/swclient/mil/idpay/service/TransactionsService.java index ce5117d..e014f7a 100644 --- a/src/main/java/it/pagopa/swclient/mil/idpay/service/TransactionsService.java +++ b/src/main/java/it/pagopa/swclient/mil/idpay/service/TransactionsService.java @@ -25,10 +25,7 @@ import it.pagopa.swclient.mil.idpay.dao.IdpayTransactionEntity; import it.pagopa.swclient.mil.idpay.dao.IdpayTransactionRepository; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.ws.rs.BadRequestException; -import jakarta.ws.rs.InternalServerErrorException; -import jakarta.ws.rs.NotFoundException; -import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.*; import jakarta.ws.rs.core.Response; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.eclipse.microprofile.rest.client.inject.RestClient; @@ -655,15 +652,25 @@ private InternalServerErrorException errorRetrieveResult(Throwable t) { private Uni authorize(IdpayTransactionEntity dbData, PinBlockDTO pinBlock) { return idPayRestService.authorize(dbData.idpayTransaction.getMerchantId(), dbData.idpayTransaction.getAcquirerId(), dbData.idpayTransaction.getIdpayTransactionId(), pinBlock) .onFailure().transform(Unchecked.function(t -> { - - // Error 500 while trying to authorize transaction Log.errorf(t, "TransactionsService -> authorizeTransaction: error response while authorizing transaction."); - Errors errors = new Errors(List.of(ErrorCode.ERROR_CALLING_AUTHORIZE_REST_SERVICES), List.of(ErrorCode.ERROR_CALLING_AUTHORIZE_REST_SERVICES_MSG)); - return new InternalServerErrorException(Response - .status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(errors) - .build()); + // Error 400 if idpay returns a 403 + if (t instanceof ClientWebApplicationException webEx && webEx.getResponse().getStatus() == 403) { + Errors errors = new Errors(List.of(ErrorCode.ERROR_IDPAY_PAYMENT_INVALID_PINBLOCK), List.of(ErrorCode.ERROR_IDPAY_PAYMENT_INVALID_PINBLOCK)); + return new BadRequestException(Response + .status(Response.Status.BAD_REQUEST) + .entity(errors) + .build()); + } else { + + // Error 500 returned for every other error received from idpay + Errors errors = new Errors(List.of(ErrorCode.ERROR_CALLING_AUTHORIZE_REST_SERVICES), List.of(ErrorCode.ERROR_CALLING_AUTHORIZE_REST_SERVICES_MSG)); + + return new InternalServerErrorException(Response + .status(Response.Status.INTERNAL_SERVER_ERROR) + .entity(errors) + .build()); + } })) .chain(finalResult -> { if (finalResult.getAuthTransactionResponseOk() != null) { @@ -675,16 +682,6 @@ private Uni authorize(IdpayTransactionEntity dbData, PinBlockDTO pinBl return updateAuthorizeTransactionStatus(dbData) .onItem() .transform(result -> result); - } else if (finalResult.getAuthTransactionResponseWrong() != null) { - - // If IDPay responds with WRONG_AUTH_CODE, send BAD_REQUEST to client - Log.errorf("TransactionsService -> authorizeTransaction: error IDPay responds with WRONG_AUTH_CODE for transaction: [%s]", dbData.transactionId); - Errors errors = new Errors(List.of(ErrorCode.ERROR_IDPAY_WRONG_AUTH_CODE), List.of(ErrorCode.ERROR_IDPAY_WRONG_AUTH_CODE_MSG)); - - return Uni.createFrom().item((Response - .status(Response.Status.BAD_REQUEST) - .entity(errors) - .build())); } else { // If any other from IDPay, send INTERNAL_SERVER_ERROR to client diff --git a/src/test/java/it/pagopa/swclient/mil/idpay/TransactionResourceAuthorizationTest.java b/src/test/java/it/pagopa/swclient/mil/idpay/TransactionResourceAuthorizationTest.java index c5a853d..6430d64 100644 --- a/src/test/java/it/pagopa/swclient/mil/idpay/TransactionResourceAuthorizationTest.java +++ b/src/test/java/it/pagopa/swclient/mil/idpay/TransactionResourceAuthorizationTest.java @@ -383,15 +383,8 @@ void authorizeTransactionTest_KOAuthorize400() { Mockito.when(idPayRestService.retrieveIdpayPublicKey(Mockito.any(String.class))) .thenReturn(Uni.createFrom().item(publicKeyIDPay)); - authTransactionResponse.setAuthTransactionResponseOk(null); - authTransactionResponse.setAuthTransactionResponseWrong(AuthTransactionResponseWrong - .builder() - .code(AuthMessageType.PAYMENT_INVALID_PINBLOCK) - .message("Wrong Authorization Code") - .build()); - Mockito.when(idPayRestService.authorize(Mockito.any(String.class), Mockito.any(String.class), Mockito.any(String.class), Mockito.any(PinBlockDTO.class))) - .thenReturn(Uni.createFrom().item(authTransactionResponse)); + .thenReturn(Uni.createFrom().failure(new ClientWebApplicationException(403))); Response response = given() @@ -406,14 +399,11 @@ void authorizeTransactionTest_KOAuthorize400() { .extract() .response(); - authTransactionResponse.setAuthTransactionResponseWrong(null); - authTransactionResponse = TransactionsTestData.getAuthTransactionResponse(); - Assertions.assertEquals(400, response.statusCode()); Assertions.assertEquals(1, response.jsonPath().getList("errors").size()); Assertions.assertEquals(1, response.jsonPath().getList("descriptions").size()); - Assertions.assertTrue(response.jsonPath().getList("errors").contains(ErrorCode.ERROR_IDPAY_WRONG_AUTH_CODE)); + Assertions.assertTrue(response.jsonPath().getList("errors").contains(ErrorCode.ERROR_IDPAY_PAYMENT_INVALID_PINBLOCK)); } @Test