Skip to content

Commit

Permalink
Added error PAYMENT_INVALID_PINBLOCK to authorize
Browse files Browse the repository at this point in the history
  • Loading branch information
RiccardoGiuliani committed Feb 16, 2024
1 parent 29916ee commit 267b9f8
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 68 deletions.
4 changes: 1 addition & 3 deletions src/main/java/it/pagopa/swclient/mil/idpay/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
@RegisterForReflection
public class AuthTransactionResponseWrong {

private AuthMessageType code;
private String code;
private String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -655,15 +652,25 @@ private InternalServerErrorException errorRetrieveResult(Throwable t) {
private Uni<Response> 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) {
Expand All @@ -675,16 +682,6 @@ private Uni<Response> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 267b9f8

Please sign in to comment.