Search in sources :

Example 1 with PublicApiResponseErrorException

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;
    };
}
Also used : PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) StructuredArguments.kv(net.logstash.logback.argument.StructuredArguments.kv) RandomIdGenerator.randomUserFriendlyReference(uk.gov.pay.products.util.RandomIdGenerator.randomUserFriendlyReference) PanDetector(uk.gov.pay.products.validations.PanDetector) PaymentCreatorNotFoundException(uk.gov.pay.products.exception.PaymentCreatorNotFoundException) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) ProductDao(uk.gov.pay.products.persistence.dao.ProductDao) LoggerFactory(org.slf4j.LoggerFactory) RandomIdGenerator.randomUuid(uk.gov.pay.products.util.RandomIdGenerator.randomUuid) Inject(javax.inject.Inject) PaymentStatus(uk.gov.pay.products.util.PaymentStatus) BadPaymentRequestException(uk.gov.pay.products.exception.BadPaymentRequestException) GATEWAY_ACCOUNT_ID(uk.gov.service.payments.logging.LoggingKeys.GATEWAY_ACCOUNT_ID) TransactionalOperation(uk.gov.pay.products.service.transaction.TransactionalOperation) TransactionFlow(uk.gov.pay.products.service.transaction.TransactionFlow) ProductsConfiguration(uk.gov.pay.products.config.ProductsConfiguration) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) ProductType(uk.gov.pay.products.util.ProductType) PAYMENT_EXTERNAL_ID(uk.gov.service.payments.logging.LoggingKeys.PAYMENT_EXTERNAL_ID) PublicApiRestClient(uk.gov.pay.products.client.publicapi.PublicApiRestClient) PaymentDao(uk.gov.pay.products.persistence.dao.PaymentDao) Logger(org.slf4j.Logger) PaymentCreationException(uk.gov.pay.products.exception.PaymentCreationException) TransactionContext(uk.gov.pay.products.service.transaction.TransactionContext) Source(uk.gov.service.payments.commons.model.Source) String.format(java.lang.String.format) Provider(com.google.inject.Provider) NonTransactionalOperation(uk.gov.pay.products.service.transaction.NonTransactionalOperation) PublicApiResponseErrorException(uk.gov.pay.products.exception.PublicApiResponseErrorException) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) Payment(uk.gov.pay.products.model.Payment) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) PublicApiResponseErrorException(uk.gov.pay.products.exception.PublicApiResponseErrorException) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) Source(uk.gov.service.payments.commons.model.Source) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse)

Example 2 with PublicApiResponseErrorException

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;
}
Also used : Response(javax.ws.rs.core.Response) PublicApiResponseErrorException(uk.gov.pay.products.exception.PublicApiResponseErrorException)

Example 3 with 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")));
    }
}
Also used : SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PublicApiStub.setupResponseToCreatePaymentRequest(uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToCreatePaymentRequest) PublicApiStub.setupResponseToGetPaymentRequest(uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToGetPaymentRequest) JsonObject(javax.json.JsonObject) PublicApiResponseErrorException(uk.gov.pay.products.exception.PublicApiResponseErrorException) Test(org.junit.Test)

Example 4 with PublicApiResponseErrorException

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));
    }
}
Also used : SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PublicApiStub.setupResponseToCreatePaymentRequest(uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToCreatePaymentRequest) PublicApiStub.setupResponseToGetPaymentRequest(uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToGetPaymentRequest) JsonObject(javax.json.JsonObject) PublicApiResponseErrorException(uk.gov.pay.products.exception.PublicApiResponseErrorException) Test(org.junit.Test)

Example 5 with PublicApiResponseErrorException

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")));
    }
}
Also used : JsonObject(javax.json.JsonObject) PublicApiResponseErrorException(uk.gov.pay.products.exception.PublicApiResponseErrorException) Test(org.junit.Test)

Aggregations

PublicApiResponseErrorException (uk.gov.pay.products.exception.PublicApiResponseErrorException)7 JsonObject (javax.json.JsonObject)3 Test (org.junit.Test)3 Response (javax.ws.rs.core.Response)2 PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)2 Payment (uk.gov.pay.products.model.Payment)2 PublicApiStub.setupResponseToCreatePaymentRequest (uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToCreatePaymentRequest)2 PublicApiStub.setupResponseToGetPaymentRequest (uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToGetPaymentRequest)2 SupportedLanguage (uk.gov.service.payments.commons.model.SupportedLanguage)2 Provider (com.google.inject.Provider)1 String.format (java.lang.String.format)1 Inject (javax.inject.Inject)1 StructuredArguments.kv (net.logstash.logback.argument.StructuredArguments.kv)1 StringUtils.isEmpty (org.apache.commons.lang3.StringUtils.isEmpty)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)1 PublicApiRestClient (uk.gov.pay.products.client.publicapi.PublicApiRestClient)1 ProductsConfiguration (uk.gov.pay.products.config.ProductsConfiguration)1 BadPaymentRequestException (uk.gov.pay.products.exception.BadPaymentRequestException)1