Search in sources :

Example 6 with Payment

use of uk.gov.pay.products.model.Payment in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateASuccessfulPayment_withUserDefinedReference_whenReferencePresent.

@Test
public void shouldCreateASuccessfulPayment_withUserDefinedReference_whenReferencePresent() {
    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.WELSH;
    Long priceOverride = 500L;
    ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, "", productApiToken, gatewayAccountId, true, language);
    PaymentRequest expectedPaymentRequest = createPaymentRequest(priceOverride, userDefinedReference, 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(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) Test(org.junit.Test)

Example 7 with Payment

use of uk.gov.pay.products.model.Payment in project pay-products by alphagov.

the class PaymentFinderTest method shouldReturnPaymentWithNoExternalStatus_whenFoundByExternalIdAndStatusIsCreated.

@Test
public void shouldReturnPaymentWithNoExternalStatus_whenFoundByExternalIdAndStatusIsCreated() {
    String externalId = randomUuid();
    PaymentEntity paymentEntity = new PaymentEntity();
    ProductEntity productEntity = ProductEntityFixture.aProductEntity().build();
    paymentEntity.setExternalId(externalId);
    paymentEntity.setProductEntity(productEntity);
    paymentEntity.setStatus(PaymentStatus.CREATED);
    paymentEntity.setGovukPaymentId(externalId);
    when(paymentDao.findByExternalId(externalId)).thenReturn(Optional.of(paymentEntity));
    Optional<Payment> optionalPayment = paymentFinder.findByExternalId(externalId);
    assertThat(optionalPayment.isPresent(), is(true));
    Payment payment = optionalPayment.get();
    assertThat(payment.getGovUkStatus(), is(nullValue()));
}
Also used : Payment(uk.gov.pay.products.model.Payment) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) Test(org.junit.Test)

Example 8 with Payment

use of uk.gov.pay.products.model.Payment in project pay-products by alphagov.

the class PaymentFinderTest method shouldReturnAList_whenFoundByProductExternalId.

@Test
public void shouldReturnAList_whenFoundByProductExternalId() {
    String productExternalId = randomUuid();
    String paymentExternalId_1 = randomUuid();
    String paymentExternalId_2 = randomUuid();
    PaymentEntity paymentEntity_1 = new PaymentEntity();
    PaymentEntity paymentEntity_2 = new PaymentEntity();
    ProductEntity productEntity = ProductEntityFixture.aProductEntity().build();
    paymentEntity_1.setExternalId(paymentExternalId_1);
    paymentEntity_1.setProductEntity(productEntity);
    paymentEntity_1.setStatus(PaymentStatus.SUBMITTED);
    paymentEntity_1.setGovukPaymentId(randomUuid());
    paymentEntity_2.setExternalId(paymentExternalId_2);
    paymentEntity_2.setProductEntity(productEntity);
    paymentEntity_2.setStatus(PaymentStatus.CREATED);
    paymentEntity_2.setGovukPaymentId(randomUuid());
    List<PaymentEntity> paymentList = Arrays.asList(paymentEntity_1, paymentEntity_2);
    PaymentResponse paymentResponse = new PaymentResponse();
    paymentResponse.setState(new PaymentState("success", true, "a message", "a code"));
    when(paymentDao.findByProductExternalId(productExternalId)).thenReturn(paymentList);
    when(publicApiRestClient.getPayment(productEntity.getPayApiToken(), paymentEntity_1.getGovukPaymentId())).thenReturn(Optional.of(paymentResponse));
    List<Payment> expectedPaymentList = paymentFinder.findByProductExternalId(productExternalId);
    assertThat(expectedPaymentList.isEmpty(), is(false));
    assertThat(expectedPaymentList.size(), is(2));
    Optional<Payment> payment_1 = expectedPaymentList.stream().filter(payment -> payment.getExternalId().equals(paymentExternalId_1)).findFirst();
    assertThat(payment_1.isPresent(), is(true));
    assertThat(payment_1.get().getGovUkStatus(), is("success"));
    Optional<Payment> payment_2 = expectedPaymentList.stream().filter(payment -> payment.getExternalId().equals(paymentExternalId_2)).findFirst();
    assertThat(payment_2.isPresent(), is(true));
    assertThat(payment_2.get().getGovUkStatus(), is(nullValue()));
}
Also used : PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) PublicApiRestClient(uk.gov.pay.products.client.publicapi.PublicApiRestClient) PaymentDao(uk.gov.pay.products.persistence.dao.PaymentDao) Arrays(java.util.Arrays) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ProductEntityFixture(uk.gov.pay.products.fixtures.ProductEntityFixture) RandomIdGenerator.randomUuid(uk.gov.pay.products.util.RandomIdGenerator.randomUuid) ArrayList(java.util.ArrayList) PaymentState(uk.gov.pay.products.client.publicapi.model.PaymentState) PaymentStatus(uk.gov.pay.products.util.PaymentStatus) List(java.util.List) IsNull.nullValue(org.hamcrest.core.IsNull.nullValue) Is.is(org.hamcrest.core.Is.is) Optional(java.util.Optional) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Before(org.junit.Before) Payment(uk.gov.pay.products.model.Payment) Payment(uk.gov.pay.products.model.Payment) PaymentState(uk.gov.pay.products.client.publicapi.model.PaymentState) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) PaymentResponse(uk.gov.pay.products.client.publicapi.PaymentResponse) Test(org.junit.Test)

Example 9 with Payment

use of uk.gov.pay.products.model.Payment in project pay-products by alphagov.

the class PaymentFinderTest method shouldReturnPaymentWithNoExternalStatus_whenFoundByExternalIdAndStatusIsError.

@Test
public void shouldReturnPaymentWithNoExternalStatus_whenFoundByExternalIdAndStatusIsError() {
    String externalId = randomUuid();
    PaymentEntity paymentEntity = new PaymentEntity();
    ProductEntity productEntity = ProductEntityFixture.aProductEntity().build();
    paymentEntity.setExternalId(externalId);
    paymentEntity.setProductEntity(productEntity);
    paymentEntity.setStatus(PaymentStatus.ERROR);
    paymentEntity.setGovukPaymentId(externalId);
    when(paymentDao.findByExternalId(externalId)).thenReturn(Optional.of(paymentEntity));
    Optional<Payment> optionalPayment = paymentFinder.findByExternalId(externalId);
    assertThat(optionalPayment.isPresent(), is(true));
    Payment payment = optionalPayment.get();
    assertThat(payment.getGovUkStatus(), is(nullValue()));
}
Also used : Payment(uk.gov.pay.products.model.Payment) ProductEntity(uk.gov.pay.products.persistence.entity.ProductEntity) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) Test(org.junit.Test)

Example 10 with Payment

use of uk.gov.pay.products.model.Payment in project pay-products by alphagov.

the class PaymentFinderTest method shouldReturnPayment_whenFoundByExternalId.

@Test
public void shouldReturnPayment_whenFoundByExternalId() {
    String externalId = randomUuid();
    PaymentEntity paymentEntity = new PaymentEntity();
    paymentEntity.setExternalId(externalId);
    when(paymentDao.findByExternalId(externalId)).thenReturn(Optional.of(paymentEntity));
    Optional<Payment> paymentOptional = paymentFinder.findByExternalId(externalId);
    assertThat(paymentOptional.isPresent(), is(true));
    assertThat(paymentOptional.get().getExternalId(), is(externalId));
}
Also used : Payment(uk.gov.pay.products.model.Payment) PaymentEntity(uk.gov.pay.products.persistence.entity.PaymentEntity) Test(org.junit.Test)

Aggregations

Payment (uk.gov.pay.products.model.Payment)13 PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)11 Test (org.junit.Test)10 ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)10 PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)9 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)6 SupportedLanguage (uk.gov.service.payments.commons.model.SupportedLanguage)5 PublicApiRestClient (uk.gov.pay.products.client.publicapi.PublicApiRestClient)2 PaymentState (uk.gov.pay.products.client.publicapi.model.PaymentState)2 PublicApiResponseErrorException (uk.gov.pay.products.exception.PublicApiResponseErrorException)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 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Optional (java.util.Optional)1 Inject (javax.inject.Inject)1