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();
}
}
Aggregations