Search in sources :

Example 1 with BaseResponse

use of uk.gov.pay.connector.gateway.model.response.BaseResponse in project pay-connector by alphagov.

the class StripeAuthoriseHandler method authorise.

@Override
public GatewayResponse authorise(CardAuthorisationGatewayRequest request) {
    logger.info("Calling Stripe for authorisation of charge");
    GatewayResponse.GatewayResponseBuilder<BaseResponse> responseBuilder = GatewayResponse.GatewayResponseBuilder.responseBuilder();
    try {
        StripePaymentMethodResponse stripePaymentMethodResponse = createPaymentMethod(request);
        StripePaymentIntentResponse stripePaymentIntentResponse = createPaymentIntent(request, stripePaymentMethodResponse.getId());
        return GatewayResponse.GatewayResponseBuilder.responseBuilder().withResponse(StripeAuthorisationResponse.of(stripePaymentIntentResponse)).build();
    } catch (GatewayException.GatewayErrorException e) {
        if ((e.getStatus().isPresent() && e.getStatus().get() == SC_UNAUTHORIZED) || e.getFamily() == SERVER_ERROR) {
            logger.error("Authorisation failed due to an internal error. Reason: {}. Status code from Stripe: {}.", e.getMessage(), e.getStatus().map(String::valueOf).orElse("no status code"));
            GatewayError gatewayError = gatewayConnectionError("There was an internal server error authorising charge");
            return responseBuilder.withGatewayError(gatewayError).build();
        }
        if (e.getFamily() == CLIENT_ERROR) {
            StripeErrorResponse stripeErrorResponse = jsonObjectMapper.getObject(e.getResponseFromGateway(), StripeErrorResponse.class);
            logger.info("Authorisation failed. Failure code from Stripe: {}, failure message from Stripe: {}. Response code from Stripe: {}", stripeErrorResponse.getError().getCode(), stripeErrorResponse.getError().getMessage(), e.getStatus());
            return responseBuilder.withResponse(StripeAuthorisationFailedResponse.of(stripeErrorResponse)).build();
        }
        logger.info("Unrecognised response status when authorising - status={}, response={}", e.getStatus(), e.getResponseFromGateway());
        throw new RuntimeException("Unrecognised response status when authorising.");
    } catch (GatewayException.GatewayConnectionTimeoutException | GatewayException.GenericGatewayException e) {
        logger.error("GatewayException occurred, error:\n {}", e);
        return responseBuilder.withGatewayError(e.toGatewayError()).build();
    }
}
Also used : GatewayResponse(uk.gov.pay.connector.gateway.model.response.GatewayResponse) StripePaymentMethodResponse(uk.gov.pay.connector.gateway.stripe.response.StripePaymentMethodResponse) StripeErrorResponse(uk.gov.pay.connector.gateway.stripe.json.StripeErrorResponse) BaseResponse(uk.gov.pay.connector.gateway.model.response.BaseResponse) StripePaymentIntentResponse(uk.gov.pay.connector.gateway.stripe.response.StripePaymentIntentResponse) GatewayException(uk.gov.pay.connector.gateway.GatewayException) GatewayError(uk.gov.pay.connector.gateway.model.GatewayError)

Aggregations

GatewayException (uk.gov.pay.connector.gateway.GatewayException)1 GatewayError (uk.gov.pay.connector.gateway.model.GatewayError)1 BaseResponse (uk.gov.pay.connector.gateway.model.response.BaseResponse)1 GatewayResponse (uk.gov.pay.connector.gateway.model.response.GatewayResponse)1 StripeErrorResponse (uk.gov.pay.connector.gateway.stripe.json.StripeErrorResponse)1 StripePaymentIntentResponse (uk.gov.pay.connector.gateway.stripe.response.StripePaymentIntentResponse)1 StripePaymentMethodResponse (uk.gov.pay.connector.gateway.stripe.response.StripePaymentMethodResponse)1