Search in sources :

Example 1 with PaymentEntity

use of uk.gov.pay.products.persistence.entity.PaymentEntity in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateAMotoPaymentWithCardAgentInitiatedMotoSource_whenProductIsAgentInitiatedMoto.

@Test
public void shouldCreateAMotoPaymentWithCardAgentInitiatedMotoSource_whenProductIsAgentInitiatedMoto() {
    PowerMockito.mockStatic(RandomIdGenerator.class);
    int productId = 1;
    String productExternalId = "product-external-id";
    long productPrice = 100L;
    String productName = "name";
    String productReturnUrl = "https://return.url";
    String productApiToken = "api-token";
    String userDefinedReference = "user-defined-reference";
    Integer gatewayAccountId = 1;
    String paymentId = "payment-id";
    String paymentExternalId = "random-external-id";
    String paymentNextUrl = "http://next.url";
    SupportedLanguage language = SupportedLanguage.ENGLISH;
    Long priceOverride = 500L;
    ProductEntity productEntity = createProductEntity(productId, ProductType.AGENT_INITIATED_MOTO, productPrice, productExternalId, productName, "", productApiToken, gatewayAccountId, true, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(priceOverride, userDefinedReference, productName, productReturnUrl + "/" + paymentExternalId, language, true, Map.of(), CARD_AGENT_INITIATED_MOTO);
    PaymentResponse paymentResponse = createPaymentResponse(paymentId, priceOverride, paymentNextUrl, productReturnUrl);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    when(randomUuid()).thenReturn(paymentExternalId);
    when(productsConfiguration.getProductsUiConfirmUrl()).thenReturn(productReturnUrl);
    when(publicApiRestClient.createPayment(argThat(is(productApiToken)), argThat(PaymentRequestMatcher.isSame(expectedPaymentRequest)))).thenReturn(paymentResponse);
    Payment payment = paymentCreator.doCreate(productExternalId, priceOverride, userDefinedReference);
    assertNotNull(payment);
    assertNotNull(payment.getExternalId());
    assertThat(payment.getGovukPaymentId(), is(paymentResponse.getPaymentId()));
    assertThat(payment.getAmount(), is(500L));
    assertThat(payment.getReferenceNumber(), is(userDefinedReference));
    PaymentEntity expectedPaymentEntity = createPaymentEntity(paymentId, userDefinedReference, paymentNextUrl, productEntity, SUBMITTED, priceOverride);
    verify(paymentDao).merge(argThat(PaymentEntityMatcher.isSame(expectedPaymentEntity)));
}
Also used : Payment(uk.gov.pay.products.model.Payment) SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with PaymentEntity

use of uk.gov.pay.products.persistence.entity.PaymentEntity in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateASuccessfulPayment_whenReturnUrlIsPresent.

@Test
public void shouldCreateASuccessfulPayment_whenReturnUrlIsPresent() {
    PowerMockito.mockStatic(RandomIdGenerator.class);
    int productId = 1;
    String productExternalId = "product-external-id";
    String paymentExernalId = "payment-external-id";
    long productPrice = 100L;
    String productName = "name";
    String productReturnUrl = "https://return.url";
    String productApiToken = "api-token";
    String referenceNumber = createRandomReferenceNumber();
    Integer gatewayAccountId = 1;
    String paymentId = "payment-id";
    Long paymentAmount = 50L;
    String paymentNextUrl = "http://next.url";
    String productsUIConfirmUri = "https://products-ui/payment-complete";
    String paymentReturnUrl = format("%s/%s", productsUIConfirmUri, paymentExernalId);
    SupportedLanguage language = SupportedLanguage.WELSH;
    ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, productReturnUrl, productApiToken, gatewayAccountId, false, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(productPrice, referenceNumber, productName, paymentReturnUrl, language, false, Map.of(), CARD_PAYMENT_LINK);
    PaymentResponse paymentResponse = createPaymentResponse(paymentId, paymentAmount, paymentNextUrl, productReturnUrl);
    when(randomUuid()).thenReturn(paymentExernalId);
    when(randomUserFriendlyReference()).thenReturn(referenceNumber);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    when(publicApiRestClient.createPayment(argThat(is(productApiToken)), argThat(PaymentRequestMatcher.isSame(expectedPaymentRequest)))).thenReturn(paymentResponse);
    when(productsConfiguration.getProductsUiConfirmUrl()).thenReturn(productsUIConfirmUri);
    Payment payment = paymentCreator.doCreate(productExternalId, null, null);
    assertNotNull(payment);
    assertNotNull(payment.getExternalId());
    assertThat(payment.getGovukPaymentId(), is(paymentResponse.getPaymentId()));
    assertThat(payment.getNextUrl(), is(paymentResponse.getLinks().getNextUrl().getHref()));
    assertThat(payment.getAmount(), is(paymentResponse.getAmount()));
    assertNotNull(payment.getLinks());
    assertThat(payment.getLinks().size(), is(2));
    assertThat(payment.getLinks().get(0).getMethod(), is("GET"));
    assertThat(payment.getLinks().get(0).getHref(), is(PRODUCT_URL + "/v1/api/payments/" + payment.getExternalId()));
    assertThat(payment.getLinks().get(1).getMethod(), is("GET"));
    assertThat(payment.getLinks().get(1).getHref(), is(paymentResponse.getLinks().getNextUrl().getHref()));
    assertThat(payment.getProductId(), is(productEntity.getId()));
    assertThat(payment.getProductExternalId(), is(productEntity.getExternalId()));
    assertThat(payment.getStatus(), is(SUBMITTED));
    PaymentEntity expectedPaymentEntity = createPaymentEntity(paymentId, paymentNextUrl, productEntity, SUBMITTED, paymentAmount);
    verify(paymentDao).merge(argThat(PaymentEntityMatcher.isSame(expectedPaymentEntity)));
}
Also used : Payment(uk.gov.pay.products.model.Payment) SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with PaymentEntity

use of uk.gov.pay.products.persistence.entity.PaymentEntity in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateAnErrorPayment_whenPublicApiCallFails.

@Test
public void shouldCreateAnErrorPayment_whenPublicApiCallFails() {
    PowerMockito.mockStatic(RandomIdGenerator.class);
    int productId = 1;
    String productExternalId = "product-external-id";
    long productPrice = 100L;
    String productName = "name";
    String productReturnUrl = "https://return.url";
    String productApiToken = "api-token";
    Integer gatewayAccountId = 1;
    String paymentExternalId = "random-external-id";
    String referenceNumber = createRandomReferenceNumber();
    String productsUIConfirmUri = "https://products-ui/payment-complete";
    String paymentReturnUrl = format("%s/%s", productsUIConfirmUri, paymentExternalId);
    SupportedLanguage language = SupportedLanguage.WELSH;
    ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, productReturnUrl, productApiToken, gatewayAccountId, false, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(productPrice, referenceNumber, productName, paymentReturnUrl, language, false, Map.of(), CARD_PAYMENT_LINK);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    when(randomUuid()).thenReturn(paymentExternalId);
    when(randomUserFriendlyReference()).thenReturn(referenceNumber);
    when(productsConfiguration.getProductsUiConfirmUrl()).thenReturn(productsUIConfirmUri);
    when(publicApiRestClient.createPayment(argThat(is(productApiToken)), argThat(PaymentRequestMatcher.isSame(expectedPaymentRequest)))).thenThrow(PublicApiResponseErrorException.class);
    try {
        paymentCreator.doCreate(productExternalId, null, null);
        fail("Expected an PaymentCreationException to be thrown");
    } catch (PaymentCreationException e) {
        assertThat(e.getProductExternalId(), is(productExternalId));
        PaymentEntity expectedPaymentEntity = createPaymentEntity(null, null, productEntity, ERROR, null);
        verify(paymentDao).merge(argThat(PaymentEntityMatcher.isSame(expectedPaymentEntity)));
    }
}
Also used : SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentCreationException(uk.gov.pay.products.exception.PaymentCreationException) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with PaymentEntity

use of uk.gov.pay.products.persistence.entity.PaymentEntity in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateASuccessfulPayment_withOverridePrice_whenPriceOverridePresent.

@Test
public void shouldCreateASuccessfulPayment_withOverridePrice_whenPriceOverridePresent() {
    PowerMockito.mockStatic(RandomIdGenerator.class);
    int productId = 1;
    String productExternalId = "product-external-id";
    long productPrice = 100L;
    String productName = "name";
    String productReturnUrl = "https://return.url";
    String productApiToken = "api-token";
    Integer gatewayAccountId = 1;
    String paymentId = "payment-id";
    String paymentExternalId = "random-external-id";
    String paymentNextUrl = "http://next.url";
    String referenceNumber = createRandomReferenceNumber();
    SupportedLanguage language = SupportedLanguage.WELSH;
    Long priceOverride = 500L;
    ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, "", productApiToken, gatewayAccountId, false, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(priceOverride, referenceNumber, productName, productReturnUrl + "/" + paymentExternalId, language, false, Map.of(), CARD_PAYMENT_LINK);
    PaymentResponse paymentResponse = createPaymentResponse(paymentId, priceOverride, paymentNextUrl, productReturnUrl);
    when(productDao.findByExternalId(productExternalId)).thenReturn(Optional.of(productEntity));
    when(randomUuid()).thenReturn(paymentExternalId);
    when(randomUserFriendlyReference()).thenReturn(referenceNumber);
    when(productsConfiguration.getProductsUiConfirmUrl()).thenReturn(productReturnUrl);
    when(publicApiRestClient.createPayment(argThat(is(productApiToken)), argThat(PaymentRequestMatcher.isSame(expectedPaymentRequest)))).thenReturn(paymentResponse);
    Payment payment = paymentCreator.doCreate(productExternalId, priceOverride, null);
    assertNotNull(payment);
    assertNotNull(payment.getExternalId());
    assertThat(payment.getGovukPaymentId(), is(paymentResponse.getPaymentId()));
    assertThat(payment.getAmount(), is(500L));
    PaymentEntity expectedPaymentEntity = createPaymentEntity(paymentId, paymentNextUrl, productEntity, SUBMITTED, priceOverride);
    verify(paymentDao).merge(argThat(PaymentEntityMatcher.isSame(expectedPaymentEntity)));
}
Also used : Payment(uk.gov.pay.products.model.Payment) SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) PaymentRequest(uk.gov.pay.products.client.publicapi.PaymentRequest) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 5 with PaymentEntity

use of uk.gov.pay.products.persistence.entity.PaymentEntity 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

PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)27 Test (org.junit.Test)23 ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)18 Payment (uk.gov.pay.products.model.Payment)11 PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)7 SupportedLanguage (uk.gov.service.payments.commons.model.SupportedLanguage)7 PaymentEntityFixture.aPaymentEntity (uk.gov.pay.products.fixtures.PaymentEntityFixture.aPaymentEntity)5 Product (uk.gov.pay.products.model.Product)5 ValidatableResponse (io.restassured.response.ValidatableResponse)4 ProductEntityFixture.aProductEntity (uk.gov.pay.products.fixtures.ProductEntityFixture.aProductEntity)4 PublicApiRestClient (uk.gov.pay.products.client.publicapi.PublicApiRestClient)2 PaymentState (uk.gov.pay.products.client.publicapi.model.PaymentState)2 PaymentCreationException (uk.gov.pay.products.exception.PaymentCreationException)2 PaymentDao (uk.gov.pay.products.persistence.dao.PaymentDao)2 PaymentStatus (uk.gov.pay.products.util.PaymentStatus)2 RandomIdGenerator.randomUuid (uk.gov.pay.products.util.RandomIdGenerator.randomUuid)2 Provider (com.google.inject.Provider)1 String.format (java.lang.String.format)1