Search in sources :

Example 1 with ErrorIdentifier

use of uk.gov.service.payments.commons.model.ErrorIdentifier in project pay-publicapi by alphagov.

the class AuthorisationRequestExceptionMapper method toResponse.

@Override
public Response toResponse(AuthorisationRequestException exception) {
    int errorStatus;
    ErrorIdentifier errorIdentifier = exception.getErrorIdentifier();
    RequestError requestError;
    switch(errorIdentifier) {
        case CARD_NUMBER_REJECTED:
            errorStatus = PAYMENT_REQUIRED.getStatusCode();
            requestError = aRequestError(AUTHORISATION_CARD_NUMBER_REJECTED_ERROR, exception.getConnectorErrorMessage());
            break;
        case AUTHORISATION_REJECTED:
            errorStatus = PAYMENT_REQUIRED.getStatusCode();
            requestError = aRequestError(AUTHORISATION_REJECTED_ERROR, exception.getConnectorErrorMessage());
            break;
        case AUTHORISATION_ERROR:
        case AUTHORISATION_TIMEOUT:
            errorStatus = INTERNAL_SERVER_ERROR.getStatusCode();
            requestError = aRequestError(AUTHORISATION_ERROR, "There was an error authorising the payment");
            break;
        case ONE_TIME_TOKEN_ALREADY_USED:
            errorStatus = BAD_REQUEST.getStatusCode();
            requestError = aRequestError(AUTHORISATION_ONE_TIME_TOKEN_ALREADY_USED_ERROR, exception.getConnectorErrorMessage());
            break;
        case ONE_TIME_TOKEN_INVALID:
            errorStatus = BAD_REQUEST.getStatusCode();
            requestError = aRequestError(AUTHORISATION_ONE_TIME_TOKEN_INVALID_ERROR, exception.getConnectorErrorMessage());
            break;
        case INVALID_ATTRIBUTE_VALUE:
            errorStatus = SC_UNPROCESSABLE_ENTITY;
            requestError = aRequestError(GENERIC_VALIDATION_EXCEPTION_MESSAGE_FROM_CONNECTOR, exception.getConnectorErrorMessage());
            break;
        default:
            LOGGER.error("Connector invalid response was {}.\n Returning http status {}", exception.getConnectorErrorMessage(), INTERNAL_SERVER_ERROR);
            errorStatus = INTERNAL_SERVER_ERROR.getStatusCode();
            requestError = aRequestError(CREATE_PAYMENT_CONNECTOR_ERROR, exception.getConnectorErrorMessage());
    }
    return Response.status(errorStatus).entity(requestError).build();
}
Also used : RequestError(uk.gov.pay.api.model.RequestError) RequestError.aRequestError(uk.gov.pay.api.model.RequestError.aRequestError) ErrorIdentifier(uk.gov.service.payments.commons.model.ErrorIdentifier)

Example 2 with ErrorIdentifier

use of uk.gov.service.payments.commons.model.ErrorIdentifier in project pay-publicapi by alphagov.

the class AccountAuthenticator method authenticate.

@Override
public Optional<Account> authenticate(String bearerToken) {
    Response response = client.target(publicAuthUrl).request().header(AUTHORIZATION, "Bearer " + bearerToken).accept(MediaType.APPLICATION_JSON).get();
    if (response.getStatus() == OK.getStatusCode()) {
        AuthResponse authResponse = response.readEntity(AuthResponse.class);
        logger.info(format("Successfully authenticated using API key with token_link %s", authResponse.getTokenLink()), kv("token_link", authResponse.getTokenLink()));
        return Optional.of(new Account(authResponse.getAccountId(), authResponse.getTokenType(), authResponse.getTokenLink()));
    } else if (response.getStatus() == UNAUTHORIZED.getStatusCode()) {
        JsonNode unauthorisedResponse = response.readEntity(JsonNode.class);
        ErrorIdentifier errorIdentifier = ErrorIdentifier.valueOf(unauthorisedResponse.get("error_identifier").asText());
        if (errorIdentifier == ErrorIdentifier.AUTH_TOKEN_REVOKED) {
            String tokenLink = unauthorisedResponse.get("token_link").asText();
            logger.warn(format("Attempt to authenticate using revoked API key with token_link %s", tokenLink), kv("token_link", tokenLink));
        } else {
            logger.warn("Attempt to authenticate using invalid API key with valid checksum");
        }
        response.close();
        return Optional.empty();
    } else {
        response.close();
        logger.warn("Unexpected status code " + response.getStatus() + " from auth.");
        throw new ServiceUnavailableException();
    }
}
Also used : Response(javax.ws.rs.core.Response) AuthResponse(uk.gov.pay.api.model.publicauth.AuthResponse) ErrorIdentifier(uk.gov.service.payments.commons.model.ErrorIdentifier) JsonNode(com.fasterxml.jackson.databind.JsonNode) ServiceUnavailableException(javax.ws.rs.ServiceUnavailableException) AuthResponse(uk.gov.pay.api.model.publicauth.AuthResponse)

Example 3 with ErrorIdentifier

use of uk.gov.service.payments.commons.model.ErrorIdentifier in project pay-publicapi by alphagov.

the class CreateChargeExceptionMapper method toResponse.

@Override
public Response toResponse(CreateChargeException exception) {
    RequestError requestError;
    int statusCode = HttpStatus.INTERNAL_SERVER_ERROR_500;
    if (exception.getErrorStatus() == NOT_FOUND.getStatusCode()) {
        if (exception.getErrorIdentifier() == ErrorIdentifier.AGREEMENT_NOT_FOUND) {
            statusCode = HttpStatus.BAD_REQUEST_400;
            requestError = aRequestError("set_up_agreement", CREATE_PAYMENT_AGREEMENT_ID_ERROR);
        } else {
            requestError = aRequestError(CREATE_PAYMENT_ACCOUNT_ERROR);
        }
    } else {
        ErrorIdentifier errorIdentifier = exception.getErrorIdentifier();
        switch(errorIdentifier) {
            case ZERO_AMOUNT_NOT_ALLOWED:
                statusCode = HttpStatus.UNPROCESSABLE_ENTITY_422;
                requestError = aRequestError("amount", CREATE_PAYMENT_VALIDATION_ERROR, "Must be greater than or equal to 1");
                break;
            case MOTO_NOT_ALLOWED:
                statusCode = HttpStatus.UNPROCESSABLE_ENTITY_422;
                requestError = aRequestError(CREATE_PAYMENT_MOTO_NOT_ENABLED);
                break;
            case TELEPHONE_PAYMENT_NOTIFICATIONS_NOT_ALLOWED:
                statusCode = HttpStatus.FORBIDDEN_403;
                requestError = aRequestError(RESOURCE_ACCESS_FORBIDDEN);
                break;
            case ACCOUNT_NOT_LINKED_WITH_PSP:
                statusCode = HttpStatus.FORBIDDEN_403;
                requestError = aRequestError(ACCOUNT_NOT_LINKED_WITH_PSP);
                break;
            case AUTHORISATION_API_NOT_ALLOWED:
                statusCode = HttpStatus.UNPROCESSABLE_ENTITY_422;
                requestError = aRequestError(CREATE_PAYMENT_AUTHORISATION_API_NOT_ENABLED);
                break;
            case MISSING_MANDATORY_ATTRIBUTE:
                statusCode = HttpStatus.BAD_REQUEST_400;
                requestError = aRequestError(GENERIC_MISSING_FIELD_ERROR_MESSAGE_FROM_CONNECTOR, exception.getConnectorErrorMessage());
                break;
            case UNEXPECTED_ATTRIBUTE:
                statusCode = HttpStatus.BAD_REQUEST_400;
                requestError = aRequestError(GENERIC_UNEXPECTED_FIELD_ERROR_MESSAGE_FROM_CONNECTOR, exception.getConnectorErrorMessage());
                break;
            case INVALID_ATTRIBUTE_VALUE:
                statusCode = HttpStatus.UNPROCESSABLE_ENTITY_422;
                requestError = aRequestError(GENERIC_VALIDATION_EXCEPTION_MESSAGE_FROM_CONNECTOR, exception.getConnectorErrorMessage());
                break;
            default:
                requestError = aRequestError(CREATE_PAYMENT_CONNECTOR_ERROR);
                LOGGER.info("Connector invalid response was {}.\n Returning http status {} with error body {}", exception.getMessage(), INTERNAL_SERVER_ERROR, requestError);
        }
    }
    return Response.status(statusCode).entity(requestError).build();
}
Also used : RequestError(uk.gov.pay.api.model.RequestError) RequestError.aRequestError(uk.gov.pay.api.model.RequestError.aRequestError) ErrorIdentifier(uk.gov.service.payments.commons.model.ErrorIdentifier)

Aggregations

ErrorIdentifier (uk.gov.service.payments.commons.model.ErrorIdentifier)3 RequestError (uk.gov.pay.api.model.RequestError)2 RequestError.aRequestError (uk.gov.pay.api.model.RequestError.aRequestError)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ServiceUnavailableException (javax.ws.rs.ServiceUnavailableException)1 Response (javax.ws.rs.core.Response)1 AuthResponse (uk.gov.pay.api.model.publicauth.AuthResponse)1