Search in sources :

Example 6 with SupportedLanguage

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

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

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

use of uk.gov.service.payments.commons.model.SupportedLanguage in project pay-products by alphagov.

the class PublicApiRestClientTest method createPayment_shouldCreateANewPayment.

@Test
public void createPayment_shouldCreateANewPayment() {
    String paymentId = "hu20sqlact5260q2nanm0q8u93";
    long amount = 2000;
    String reference = "a-reference";
    String description = "A Service Description";
    String returnUrl = "http://return.url";
    String nextUrl = "http://next.url";
    String apiToken = "api-token";
    SupportedLanguage language = SupportedLanguage.WELSH;
    boolean moto = false;
    Map<String, String> metadata = Map.of("key", "value");
    JsonObject expectedPaymentRequestPayload = createPaymentRequestPayload(amount, reference, description, returnUrl, language.toString(), moto, metadata);
    JsonObject paymentResponsePayload = PublicApiStub.createPaymentResponsePayload(paymentId, amount, reference, description, returnUrl, nextUrl, language.toString(), metadata);
    setupResponseToCreatePaymentRequest(apiToken, expectedPaymentRequestPayload, paymentResponsePayload);
    PaymentRequest paymentRequest = new PaymentRequest(amount, reference, description, returnUrl, language, moto, metadata, CARD_PAYMENT_LINK);
    PaymentResponse actualPaymentResponse = publicApiRestClient.createPayment(apiToken, paymentRequest);
    assertThat(actualPaymentResponse, hasAllPaymentProperties(paymentResponsePayload));
}
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) Test(org.junit.Test)

Example 10 with SupportedLanguage

use of uk.gov.service.payments.commons.model.SupportedLanguage in project pay-products by alphagov.

the class PaymentCreatorTest method shouldCreateASuccessfulPayment_withUserDefinedReference_whenReferencePresent.

@Test
public void shouldCreateASuccessfulPayment_withUserDefinedReference_whenReferencePresent() {
    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.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) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

SupportedLanguage (uk.gov.service.payments.commons.model.SupportedLanguage)17 Test (org.junit.Test)12 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 ProductEntity (uk.gov.pay.products.persistence.entity.ProductEntity)8 PaymentEntity (uk.gov.pay.products.persistence.entity.PaymentEntity)7 PaymentRequest (uk.gov.pay.products.client.publicapi.PaymentRequest)6 PaymentResponse (uk.gov.pay.products.client.publicapi.PaymentResponse)5 Payment (uk.gov.pay.products.model.Payment)5 JsonObject (javax.json.JsonObject)4 Service (uk.gov.pay.adminusers.model.Service)3 PublicApiStub.setupResponseToCreatePaymentRequest (uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToCreatePaymentRequest)3 PublicApiStub.setupResponseToGetPaymentRequest (uk.gov.pay.products.stubs.publicapi.PublicApiStub.setupResponseToGetPaymentRequest)3 Test (org.junit.jupiter.api.Test)2 GovUkPayAgreementService (uk.gov.pay.adminusers.service.GovUkPayAgreementService)2 SendLiveAccountCreatedEmailService (uk.gov.pay.adminusers.service.SendLiveAccountCreatedEmailService)2 StripeAgreementService (uk.gov.pay.adminusers.service.StripeAgreementService)2 PublicApiResponseErrorException (uk.gov.pay.products.exception.PublicApiResponseErrorException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Transactional (com.google.inject.persist.Transactional)1 JsonPath (io.restassured.path.json.JsonPath)1