use of uk.gov.pay.products.client.publicapi.PaymentRequest 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)));
}
use of uk.gov.pay.products.client.publicapi.PaymentRequest in project pay-products by alphagov.
the class PaymentCreatorTest method shouldCreateASuccessfulPayment_whenReturnUrlIsNotPresent.
@Test
public void shouldCreateASuccessfulPayment_whenReturnUrlIsNotPresent() {
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";
Long paymentAmount = 50L;
String paymentNextUrl = "http://next.url";
String referenceNumber = createRandomReferenceNumber();
SupportedLanguage language = SupportedLanguage.WELSH;
ProductEntity productEntity = createProductEntity(productId, productPrice, productExternalId, productName, "", productApiToken, gatewayAccountId, false, language);
PaymentRequest expectedPaymentRequest = createPaymentRequest(productPrice, referenceNumber, productName, productReturnUrl + "/" + paymentExternalId, language, false, Map.of(), CARD_PAYMENT_LINK);
PaymentResponse paymentResponse = createPaymentResponse(paymentId, paymentAmount, 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, 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)));
}
Aggregations