Search in sources :

Example 1 with Source

use of uk.gov.service.payments.commons.model.Source 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 Source

use of uk.gov.service.payments.commons.model.Source 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 3 with Source

use of uk.gov.service.payments.commons.model.Source in project pay-connector by alphagov.

the class SourceDeserialiserTest method shouldDeserialiseNullToNull.

@Test
void shouldDeserialiseNullToNull() throws IOException {
    given(mockJsonParser.getValueAsString()).willReturn(null);
    Source result = sourceDeserialiser.deserialize(mockJsonParser, mockDeserializationContext);
    assertThat(result, is(nullValue()));
}
Also used : EnumSource(org.junit.jupiter.params.provider.EnumSource) Source(uk.gov.service.payments.commons.model.Source) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with Source

use of uk.gov.service.payments.commons.model.Source in project pay-connector by alphagov.

the class SourceDeserialiserTest method shouldDeserialiseAcceptedSources.

@ParameterizedTest
@EnumSource(value = Source.class, names = { "CARD_API", "CARD_PAYMENT_LINK", "CARD_AGENT_INITIATED_MOTO" })
void shouldDeserialiseAcceptedSources(Source source) throws IOException {
    given(mockJsonParser.getValueAsString()).willReturn(source.name());
    Source result = sourceDeserialiser.deserialize(mockJsonParser, mockDeserializationContext);
    assertThat(result, is(source));
}
Also used : EnumSource(org.junit.jupiter.params.provider.EnumSource) Source(uk.gov.service.payments.commons.model.Source) EnumSource(org.junit.jupiter.params.provider.EnumSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Source (uk.gov.service.payments.commons.model.Source)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 EnumSource (org.junit.jupiter.params.provider.EnumSource)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Provider (com.google.inject.Provider)1 String.format (java.lang.String.format)1 Inject (javax.inject.Inject)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 StructuredArguments.kv (net.logstash.logback.argument.StructuredArguments.kv)1 StringUtils.isEmpty (org.apache.commons.lang3.StringUtils.isEmpty)1 Test (org.junit.jupiter.api.Test)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 BadRequestException (uk.gov.pay.api.exception.BadRequestException)1 PaymentError (uk.gov.pay.api.model.PaymentError)1 PaymentError.aPaymentError (uk.gov.pay.api.model.PaymentError.aPaymentError)1 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)1 PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)1 PublicApiRestClient (uk.gov.pay.products.client.publicapi.PublicApiRestClient)1 ProductsConfiguration (uk.gov.pay.products.config.ProductsConfiguration)1