Search in sources :

Example 1 with TokenEntity

use of uk.gov.pay.connector.token.model.domain.TokenEntity in project pay-connector by alphagov.

the class ChargeServiceFindTest method shouldFindChargeForChargeIdAndAccountIdWithNextUrlWhenChargeStatusIs.

@Test
@Parameters({ "CREATED", "ENTERING_CARD_DETAILS", "AUTHORISATION_READY" })
public void shouldFindChargeForChargeIdAndAccountIdWithNextUrlWhenChargeStatusIs(String status) throws Exception {
    Long chargeId = 101L;
    ChargeEntity newCharge = aValidChargeEntity().withId(chargeId).withGatewayAccountEntity(gatewayAccount).withStatus(ChargeStatus.valueOf(status)).withWalletType(WalletType.APPLE_PAY).build();
    String externalId = newCharge.getExternalId();
    doAnswer(invocation -> fromUri(SERVICE_HOST)).when(this.mockedUriInfo).getBaseUriBuilder();
    when(mockedLinksConfig.getFrontendUrl()).thenReturn("http://frontend.test");
    when(mockedProviders.byName(any(PaymentGatewayName.class))).thenReturn(mockedPaymentProvider);
    when(mockedPaymentProvider.getExternalChargeRefundAvailability(any(Charge.class), any(List.class))).thenReturn(EXTERNAL_AVAILABLE);
    when(mockedChargeDao.findByExternalIdAndGatewayAccount(externalId, GATEWAY_ACCOUNT_ID)).thenReturn(Optional.ofNullable(newCharge));
    Optional<ChargeResponse> chargeResponseForAccount = service.findChargeForAccount(externalId, GATEWAY_ACCOUNT_ID, mockedUriInfo);
    verify(mockedTokenDao).persist(tokenEntityArgumentCaptor.capture());
    TokenEntity tokenEntity = tokenEntityArgumentCaptor.getValue();
    assertThat(tokenEntity.getChargeEntity().getId(), is(newCharge.getId()));
    assertThat(tokenEntity.getToken(), is(notNullValue()));
    ChargeResponse.ChargeResponseBuilder chargeResponseWithoutCorporateCardSurcharge = chargeResponseBuilderOf(newCharge);
    chargeResponseWithoutCorporateCardSurcharge.withWalletType(WalletType.APPLE_PAY);
    chargeResponseWithoutCorporateCardSurcharge.withLink("self", GET, new URI(SERVICE_HOST + "/v1/api/accounts/10/charges/" + externalId));
    chargeResponseWithoutCorporateCardSurcharge.withLink("refunds", GET, new URI(SERVICE_HOST + "/v1/api/accounts/10/charges/" + externalId + "/refunds"));
    chargeResponseWithoutCorporateCardSurcharge.withLink("next_url", GET, new URI("http://frontend.test/secure/" + tokenEntity.getToken()));
    chargeResponseWithoutCorporateCardSurcharge.withLink("next_url_post", POST, new URI("http://frontend.test/secure"), "application/x-www-form-urlencoded", new HashMap<String, Object>() {

        {
            put("chargeTokenId", tokenEntity.getToken());
        }
    });
    assertThat(chargeResponseForAccount.isPresent(), is(true));
    final ChargeResponse chargeResponse = chargeResponseForAccount.get();
    assertThat(chargeResponse.getCorporateCardSurcharge(), is(nullValue()));
    assertThat(chargeResponse.getTotalAmount(), is(nullValue()));
    assertThat(chargeResponse, is(chargeResponseWithoutCorporateCardSurcharge.build()));
    assertThat(chargeResponse.getWalletType(), is(WalletType.APPLE_PAY));
}
Also used : Charge(uk.gov.pay.connector.charge.model.domain.Charge) URI(java.net.URI) ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TokenEntity(uk.gov.pay.connector.token.model.domain.TokenEntity) List(java.util.List) PaymentGatewayName(uk.gov.pay.connector.gateway.PaymentGatewayName) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 2 with TokenEntity

use of uk.gov.pay.connector.token.model.domain.TokenEntity in project pay-connector by alphagov.

the class CardResource method authorise.

@POST
@Path("/v1/api/charges/authorise")
@Produces(APPLICATION_JSON)
public Response authorise(@Valid @NotNull AuthoriseRequest authoriseRequest) {
    // Fields are removed from the MDC when the API responds in LoggingMDCResponseFilter
    MDC.put(SECURE_TOKEN, authoriseRequest.getOneTimeToken());
    authoriseRequest.validate();
    TokenEntity tokenEntity = tokenService.validateAndMarkTokenAsUsedForMotoApi(authoriseRequest.getOneTimeToken());
    MDCUtils.addChargeAndGatewayAccountDetailsToMDC(tokenEntity.getChargeEntity());
    CardInformation cardInformation = motoApiCardNumberValidationService.validateCardNumber(tokenEntity.getChargeEntity(), authoriseRequest.getCardNumber());
    AuthorisationResponse response = cardAuthoriseService.doAuthoriseMotoApi(tokenEntity.getChargeEntity(), cardInformation, authoriseRequest);
    return handleAuthResponseForMotoApi(response);
}
Also used : AuthorisationResponse(uk.gov.pay.connector.paymentprocessor.api.AuthorisationResponse) Gateway3DSAuthorisationResponse(uk.gov.pay.connector.gateway.model.response.Gateway3DSAuthorisationResponse) TokenEntity(uk.gov.pay.connector.token.model.domain.TokenEntity) CardInformation(uk.gov.pay.connector.client.cardid.model.CardInformation) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 3 with TokenEntity

use of uk.gov.pay.connector.token.model.domain.TokenEntity in project pay-connector by alphagov.

the class TokenDaoJpaIT method findByTokenId_shouldFindUnusedToken.

@Test
public void findByTokenId_shouldFindUnusedToken() {
    String tokenId = "qwerty";
    databaseTestHelper.addToken(defaultTestCharge.getChargeId(), tokenId);
    TokenEntity entity = tokenDao.findByTokenId(tokenId).get();
    assertThat(entity.getId(), is(notNullValue()));
    assertThat(entity.getChargeEntity().getId(), is(defaultTestCharge.getChargeId()));
    assertThat(entity.getToken(), is(tokenId));
    assertThat(entity.isUsed(), is(false));
}
Also used : TokenEntity(uk.gov.pay.connector.token.model.domain.TokenEntity) Test(org.junit.Test)

Example 4 with TokenEntity

use of uk.gov.pay.connector.token.model.domain.TokenEntity in project pay-connector by alphagov.

the class TokenDaoJpaIT method findByTokenId_shouldFindUsedToken.

@Test
public void findByTokenId_shouldFindUsedToken() {
    String tokenId = "qwerty";
    databaseTestHelper.addToken(defaultTestCharge.getChargeId(), tokenId, true);
    TokenEntity entity = tokenDao.findByTokenId(tokenId).get();
    assertThat(entity.getId(), is(notNullValue()));
    assertThat(entity.getChargeEntity().getId(), is(defaultTestCharge.getChargeId()));
    assertThat(entity.getToken(), is(tokenId));
    assertThat(entity.isUsed(), is(true));
}
Also used : TokenEntity(uk.gov.pay.connector.token.model.domain.TokenEntity) Test(org.junit.Test)

Example 5 with TokenEntity

use of uk.gov.pay.connector.token.model.domain.TokenEntity in project pay-connector by alphagov.

the class TokenDaoJpaIT method deleteByCutOffDate_shouldDeleteOlderTokens.

@Test
public void deleteByCutOffDate_shouldDeleteOlderTokens() {
    ZonedDateTime today = ZonedDateTime.now(ZoneId.of("UTC"));
    ChargeEntityFixture chargeEntityFixture = new ChargeEntityFixture();
    ChargeEntity chargeTestEntity = chargeEntityFixture.build();
    chargeTestEntity.setId(defaultTestCharge.getChargeId());
    TokenEntity presentDayToken = TokenEntity.generateNewTokenFor(chargeTestEntity);
    presentDayToken.setCreatedDate(today);
    presentDayToken.setToken("present-day-token");
    tokenDao.persist(presentDayToken);
    TokenEntity olderThanExpiryThreshold = TokenEntity.generateNewTokenFor(chargeTestEntity);
    olderThanExpiryThreshold.setCreatedDate(today.minusDays(8));
    olderThanExpiryThreshold.setToken("old-token");
    tokenDao.persist(olderThanExpiryThreshold);
    final ZonedDateTime expiryThreshold = today.minusDays(7);
    tokenDao.deleteTokensOlderThanSpecifiedDate(expiryThreshold);
    assertThat(tokenDao.findByTokenId("old-token"), isEmpty());
    assertThat(tokenDao.findByTokenId("present-day-token"), isPresent());
}
Also used : ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ZonedDateTime(java.time.ZonedDateTime) TokenEntity(uk.gov.pay.connector.token.model.domain.TokenEntity) ChargeEntityFixture(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture) Test(org.junit.Test)

Aggregations

TokenEntity (uk.gov.pay.connector.token.model.domain.TokenEntity)17 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)9 Test (org.junit.jupiter.api.Test)7 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)7 ChargeResponse (uk.gov.pay.connector.charge.model.ChargeResponse)6 Charge (uk.gov.pay.connector.charge.model.domain.Charge)6 PaymentGatewayName (uk.gov.pay.connector.gateway.PaymentGatewayName)6 URI (java.net.URI)5 Test (org.junit.Test)5 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 ChargeEntityFixture (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture)2 ZonedDateTime (java.time.ZonedDateTime)1 HashMap (java.util.HashMap)1 List (java.util.List)1 GET (javax.ws.rs.HttpMethod.GET)1 POST (javax.ws.rs.HttpMethod.POST)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1