use of uk.gov.pay.products.exception.PublicApiResponseErrorException in project pay-products by alphagov.
the class PaymentCreator method paymentCreation.
private NonTransactionalOperation<TransactionContext, PaymentEntity> paymentCreation(Long priceOverride) {
return context -> {
PaymentEntity paymentEntity = context.get(PaymentEntity.class);
ProductEntity productEntity = paymentEntity.getProductEntity();
String returnUrl = format("%s/%s", productsConfiguration.getProductsUiConfirmUrl(), paymentEntity.getExternalId());
Long paymentPrice = priceOverride != null ? priceOverride : productEntity.getPrice();
boolean isMoto = productEntity.getType() == ProductType.AGENT_INITIATED_MOTO;
Source source = productEntity.getType() == ProductType.AGENT_INITIATED_MOTO ? Source.CARD_AGENT_INITIATED_MOTO : Source.CARD_PAYMENT_LINK;
PaymentRequest paymentRequest = new PaymentRequest(paymentPrice, paymentEntity.getReferenceNumber(), productEntity.getName(), returnUrl, productEntity.getLanguage(), isMoto, productEntity.toProductMetadataMap(), source);
try {
PaymentResponse paymentResponse = publicApiRestClient.createPayment(productEntity.getPayApiToken(), paymentRequest);
paymentEntity.setGovukPaymentId(paymentResponse.getPaymentId());
paymentEntity.setNextUrl(getNextUrl(paymentResponse));
paymentEntity.setStatus(PaymentStatus.SUBMITTED);
paymentEntity.setAmount(paymentResponse.getAmount());
logger.info("Payment creation for product external id {} successful", paymentEntity.getProductEntity().getExternalId(), kv(PAYMENT_EXTERNAL_ID, paymentEntity.getGovukPaymentId()), kv("product_external_id", paymentEntity.getProductEntity().getExternalId()));
if (PanDetector.isSuspectedPan(paymentEntity.getReferenceNumber())) {
logger.warn("Suspected PAN entered by user in reference field", kv(PAYMENT_EXTERNAL_ID, paymentEntity.getGovukPaymentId()), kv(GATEWAY_ACCOUNT_ID, paymentEntity.getGatewayAccountId()), kv("number_of_digits", PanDetector.cleanedReference(paymentEntity.getReferenceNumber()).length()));
}
} catch (PublicApiResponseErrorException e) {
logger.error("Payment creation for product external id {} failed {}", paymentEntity.getProductEntity().getExternalId(), e);
paymentEntity.setStatus(PaymentStatus.ERROR);
}
return paymentEntity;
};
}
use of uk.gov.pay.products.exception.PublicApiResponseErrorException in project pay-products by alphagov.
the class PublicApiRestClient method getPayment.
public Optional<PaymentResponse> getPayment(String apiToken, String paymentId) {
logger.info("Public API client requested finding payment", kv(PAYMENT_EXTERNAL_ID, paymentId));
Response response = client.target(buildAbsoluteUrl(format(PAYMENT_PATH, paymentId))).request().header(AUTHORIZATION, constructBearerToken(apiToken)).get();
if (response.getStatus() == HttpStatus.OK_200) {
PaymentResponse paymentResponse = response.readEntity(PaymentResponse.class);
logger.info("Public API client returned payment found", kv(PAYMENT_EXTERNAL_ID, paymentResponse.getPaymentId()));
return Optional.of(paymentResponse);
}
if (response.getStatus() == HttpStatus.NOT_FOUND_404) {
logger.info("Public API client returned payment not found");
return Optional.empty();
}
PublicApiResponseErrorException publicApiResponseErrorException = new PublicApiResponseErrorException(response);
logger.error("Public API client returned an error - [ {} ]", publicApiResponseErrorException.getMessage());
throw publicApiResponseErrorException;
}
use of uk.gov.pay.products.exception.PublicApiResponseErrorException in project pay-products by alphagov.
the class PublicApiRestClientTest method createPayment_shouldThrowAnExceptionWhenBadRequest.
@Test
public void createPayment_shouldThrowAnExceptionWhenBadRequest() throws PublicApiResponseErrorException {
long amount = 2000;
String reference = "a-reference";
String description = "A Service Description";
String returnUrl = "http://return.url";
String apiToken = "api-token";
SupportedLanguage language = SupportedLanguage.WELSH;
boolean moto = false;
JsonObject expectedPaymentRequestPayload = createPaymentRequestPayload(amount, reference, description, returnUrl, language.toString(), false, null);
JsonObject errorPayload = PublicApiStub.createErrorPayload("a-field", "a-code", "A description");
setupResponseToCreatePaymentRequest(apiToken, expectedPaymentRequestPayload, errorPayload, SC_BAD_REQUEST);
PaymentRequest paymentRequest = new PaymentRequest(amount, reference, description, returnUrl, language, moto, Map.of(), CARD_PAYMENT_LINK);
try {
publicApiRestClient.createPayment(apiToken, paymentRequest);
fail("Expected an PublicApiResponseErrorException to be thrown");
} catch (PublicApiResponseErrorException exception) {
assertThat(exception.getErrorStatus(), is(400));
assertThat(exception.getCode(), is(errorPayload.getString("code")));
assertThat(exception.getDescription(), is(errorPayload.getString("description")));
}
}
use of uk.gov.pay.products.exception.PublicApiResponseErrorException in project pay-products by alphagov.
the class PublicApiRestClientTest method createPayment_shouldThrowAnExceptionWhenUnauthorized.
@Test
public void createPayment_shouldThrowAnExceptionWhenUnauthorized() {
long amount = 2000;
String reference = "a-reference";
String description = "A Service Description";
String returnUrl = "http://return.url";
String apiToken = "invalid-token";
SupportedLanguage language = SupportedLanguage.WELSH;
boolean moto = false;
JsonObject expectedPaymentRequestPayload = createPaymentRequestPayload(amount, reference, description, returnUrl, language.toString(), false, null);
setupResponseToCreatePaymentRequest(apiToken, expectedPaymentRequestPayload, HttpStatus.SC_UNAUTHORIZED);
PaymentRequest paymentRequest = new PaymentRequest(amount, reference, description, returnUrl, language, moto, Map.of(), CARD_PAYMENT_LINK);
try {
publicApiRestClient.createPayment(apiToken, paymentRequest);
fail("Expected an PublicApiResponseErrorException to be thrown");
} catch (PublicApiResponseErrorException exception) {
assertThat(exception.getErrorStatus(), is(401));
}
}
use of uk.gov.pay.products.exception.PublicApiResponseErrorException in project pay-products by alphagov.
the class PublicApiRestClientTest method findPayment_shouldThrowAnException.
@Test
public void findPayment_shouldThrowAnException() {
String paymentId = "hu20sqlact5260q2nanm0q8u93";
String apiToken = "api-token";
JsonObject errorPayload = PublicApiStub.createErrorPayload("a-field", "a-code", "A description");
setupResponseToGetPaymentRequest(paymentId, errorPayload, SC_BAD_REQUEST);
try {
publicApiRestClient.getPayment(apiToken, paymentId);
fail("Expected an PublicApiResponseErrorException to be thrown");
} catch (PublicApiResponseErrorException exception) {
assertThat(exception.getErrorStatus(), is(400));
assertThat(exception.getCode(), is(errorPayload.getString("code")));
assertThat(exception.getDescription(), is(errorPayload.getString("description")));
}
}
Aggregations