Search in sources :

Example 1 with TransactionContext

use of uk.gov.pay.products.service.transaction.TransactionContext 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)

Aggregations

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 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 BadPaymentRequestException (uk.gov.pay.products.exception.BadPaymentRequestException)1 PaymentCreationException (uk.gov.pay.products.exception.PaymentCreationException)1 PaymentCreatorNotFoundException (uk.gov.pay.products.exception.PaymentCreatorNotFoundException)1 PublicApiResponseErrorException (uk.gov.pay.products.exception.PublicApiResponseErrorException)1 Payment (uk.gov.pay.products.model.Payment)1 PaymentDao (uk.gov.pay.products.persistence.dao.PaymentDao)1 ProductDao (uk.gov.pay.products.persistence.dao.ProductDao)1 PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)1 ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)1