Search in sources :

Example 1 with PaymentError

use of uk.gov.pay.api.model.PaymentError in project pay-publicapi by alphagov.

the class RequestJsonParser method validateAndGetSource.

private static Source validateAndGetSource(JsonNode paymentRequest) {
    if (paymentRequest.has(INTERNAL)) {
        JsonNode internalNode = paymentRequest.get(INTERNAL);
        if (internalNode.has(SOURCE_FIELD_NAME)) {
            String errorMessage = "Accepted values are only CARD_PAYMENT_LINK, CARD_AGENT_INITIATED_MOTO";
            PaymentError paymentError = aPaymentError(SOURCE_FIELD_NAME, CREATE_PAYMENT_VALIDATION_ERROR, errorMessage);
            String sourceString = validateSkipNullValueAndGetString(internalNode.get(SOURCE_FIELD_NAME), paymentError);
            try {
                Source source = Source.valueOf(sourceString);
                if (ALLOWED_SOURCES.contains(source)) {
                    return source;
                }
                throw new BadRequestException(paymentError);
            } catch (IllegalArgumentException e) {
                throw new WebApplicationException(Response.status(SC_UNPROCESSABLE_ENTITY).entity(paymentError).build());
            }
        }
    }
    return CARD_API;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) BadRequestException(uk.gov.pay.api.exception.BadRequestException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Source(uk.gov.service.payments.commons.model.Source) PaymentError(uk.gov.pay.api.model.PaymentError) PaymentError.aPaymentError(uk.gov.pay.api.model.PaymentError.aPaymentError)

Example 2 with PaymentError

use of uk.gov.pay.api.model.PaymentError in project pay-publicapi by alphagov.

the class CreateRefundExceptionMapper method toResponse.

@Override
public Response toResponse(CreateRefundException exception) {
    PaymentError paymentError;
    Response.Status status;
    if (exception.getErrorStatus() == NOT_FOUND.getStatusCode()) {
        paymentError = aPaymentError(CREATE_PAYMENT_REFUND_NOT_FOUND_ERROR);
        status = NOT_FOUND;
    } else if (exception.getErrorIdentifier() == ErrorIdentifier.REFUND_NOT_AVAILABLE && exception.hasReason()) {
        paymentError = aPaymentError(CREATE_PAYMENT_REFUND_NOT_AVAILABLE, exception.getReason());
        status = BAD_REQUEST;
    } else if (exception.getErrorIdentifier() == ErrorIdentifier.REFUND_AMOUNT_AVAILABLE_MISMATCH) {
        paymentError = aPaymentError(CREATE_PAYMENT_REFUND_AMOUNT_AVAILABLE_MISMATCH);
        status = PRECONDITION_FAILED;
    } else {
        paymentError = aPaymentError(CREATE_PAYMENT_REFUND_CONNECTOR_ERROR);
        status = INTERNAL_SERVER_ERROR;
    }
    if (status == INTERNAL_SERVER_ERROR) {
        LOGGER.info("Connector invalid response was {}.\n Returning http status {} with error body {}", exception.getMessage(), status, paymentError);
    }
    return Response.status(status).entity(paymentError).build();
}
Also used : Response(javax.ws.rs.core.Response) Status(javax.ws.rs.core.Response.Status) PaymentError.aPaymentError(uk.gov.pay.api.model.PaymentError.aPaymentError) PaymentError(uk.gov.pay.api.model.PaymentError)

Example 3 with PaymentError

use of uk.gov.pay.api.model.PaymentError in project pay-publicapi by alphagov.

the class ViolationExceptionMapper method toResponse.

@Override
public Response toResponse(JerseyViolationException exception) {
    LOGGER.info(exception.getMessage());
    ConstraintViolation<?> firstException = exception.getConstraintViolations().iterator().next();
    String fieldName = getApiFieldName(firstException.getPropertyPath());
    PaymentError paymentError;
    if (firstException.getConstraintDescriptor() != null && firstException.getConstraintDescriptor().getAnnotation() != null && firstException.getConstraintDescriptor().getAnnotation().annotationType() == NotBlank.class || firstException.getConstraintDescriptor().getAnnotation().annotationType() == NotEmpty.class || firstException.getConstraintDescriptor().getAnnotation().annotationType() == NotNull.class) {
        paymentError = PaymentError.aPaymentError(fieldName, CREATE_PAYMENT_MISSING_FIELD_ERROR);
    } else {
        paymentError = PaymentError.aPaymentError(fieldName, CREATE_PAYMENT_VALIDATION_ERROR, firstException.getMessage());
    }
    return Response.status(422).entity(paymentError).build();
}
Also used : NotBlank(org.hibernate.validator.constraints.NotBlank) NotNull(javax.validation.constraints.NotNull) PaymentError(uk.gov.pay.api.model.PaymentError)

Example 4 with PaymentError

use of uk.gov.pay.api.model.PaymentError in project pay-publicapi by alphagov.

the class GetRefundExceptionMapper method toResponse.

@Override
public Response toResponse(GetRefundException exception) {
    PaymentError paymentError;
    Response.Status status;
    if (exception.getErrorStatus() == NOT_FOUND.getStatusCode()) {
        paymentError = aPaymentError(GET_PAYMENT_REFUND_NOT_FOUND_ERROR);
        status = NOT_FOUND;
    } else {
        paymentError = aPaymentError(GET_PAYMENT_REFUND_CONNECTOR_ERROR);
        status = INTERNAL_SERVER_ERROR;
        LOGGER.error("Connector invalid response was {}.\n Returning http status {} with error body {}", exception.getMessage(), status, paymentError);
    }
    return Response.status(status).entity(paymentError).build();
}
Also used : Response(javax.ws.rs.core.Response) PaymentError.aPaymentError(uk.gov.pay.api.model.PaymentError.aPaymentError) PaymentError(uk.gov.pay.api.model.PaymentError)

Example 5 with PaymentError

use of uk.gov.pay.api.model.PaymentError in project pay-publicapi by alphagov.

the class CancelChargeExceptionMapper method toResponse.

@Override
public Response toResponse(CancelChargeException exception) {
    int errorStatus = exception.getErrorStatus();
    PaymentError paymentError;
    Response.Status status;
    if (errorStatus == NOT_FOUND.getStatusCode()) {
        paymentError = aPaymentError(CANCEL_PAYMENT_NOT_FOUND_ERROR);
        status = NOT_FOUND;
    } else if (errorStatus == BAD_REQUEST.getStatusCode()) {
        paymentError = aPaymentError(CANCEL_PAYMENT_CONNECTOR_BAD_REQUEST_ERROR);
        status = BAD_REQUEST;
    } else if (errorStatus == CONFLICT.getStatusCode()) {
        paymentError = aPaymentError(CANCEL_PAYMENT_CONNECTOR_CONFLICT_ERROR);
        status = CONFLICT;
    } else {
        paymentError = aPaymentError(CANCEL_PAYMENT_CONNECTOR_ERROR);
        status = INTERNAL_SERVER_ERROR;
    }
    if (status == INTERNAL_SERVER_ERROR) {
        LOGGER.error("Connector invalid response was {}.\n Returning http status {} with error body {}", exception.getMessage(), status, paymentError);
    }
    return Response.status(status).entity(paymentError).build();
}
Also used : Response(javax.ws.rs.core.Response) Status(javax.ws.rs.core.Response.Status) PaymentError.aPaymentError(uk.gov.pay.api.model.PaymentError.aPaymentError) PaymentError(uk.gov.pay.api.model.PaymentError)

Aggregations

PaymentError (uk.gov.pay.api.model.PaymentError)16 PaymentError.aPaymentError (uk.gov.pay.api.model.PaymentError.aPaymentError)13 Response (javax.ws.rs.core.Response)9 WebApplicationException (javax.ws.rs.WebApplicationException)3 Status (javax.ws.rs.core.Response.Status)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Map (java.util.Map)1 ConstraintViolation (javax.validation.ConstraintViolation)1 NotNull (javax.validation.constraints.NotNull)1 NotBlank (org.hibernate.validator.constraints.NotBlank)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1 BadRequestException (uk.gov.pay.api.exception.BadRequestException)1 ConnectorErrorResponse (uk.gov.pay.api.exception.ConnectorResponseErrorException.ConnectorErrorResponse)1 CreateChargeException (uk.gov.pay.api.exception.CreateChargeException)1 ErrorIdentifier (uk.gov.service.payments.commons.model.ErrorIdentifier)1 Source (uk.gov.service.payments.commons.model.Source)1 ExternalMetadata (uk.gov.service.payments.commons.model.charge.ExternalMetadata)1