Search in sources :

Example 1 with GatewayAccountCredentialsEntity

use of uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity in project pay-connector by alphagov.

the class GatewayAccountSwitchPaymentProviderService method switchPaymentProviderForAccount.

@Transactional
public void switchPaymentProviderForAccount(GatewayAccountEntity gatewayAccountEntity, GatewayAccountSwitchPaymentProviderRequest request) {
    List<GatewayAccountCredentialsEntity> credentials = gatewayAccountEntity.getGatewayAccountCredentials();
    if (credentials.size() < 2) {
        throw new BadRequestException("Account has no credential to switch to/from");
    }
    var activeCredentialEntity = credentials.stream().filter(credential -> ACTIVE.equals(credential.getState())).findFirst().orElseThrow(() -> new BadRequestException("Account credential with ACTIVE state not found."));
    var switchToCredentialsEntity = credentials.stream().filter(credential -> request.getGACredentialExternalId().equals(credential.getExternalId())).findFirst().orElseThrow(() -> new NotFoundException(format("Account credential with id [%s] not found.", request.getGACredentialExternalId())));
    if (!switchToCredentialsEntity.getState().equals(VERIFIED_WITH_LIVE_PAYMENT)) {
        throw new BadRequestException(format("Credential with id [%s] is not in correct state.", request.getGACredentialExternalId()));
    }
    switchToCredentialsEntity.setLastUpdatedByUserExternalId(request.getUserExternalId());
    switchToCredentialsEntity.setActiveStartDate(Instant.now());
    switchToCredentialsEntity.setState(ACTIVE);
    activeCredentialEntity.setState(RETIRED);
    activeCredentialEntity.setLastUpdatedByUserExternalId(request.getUserExternalId());
    activeCredentialEntity.setActiveEndDate(Instant.now());
    gatewayAccountEntity.setProviderSwitchEnabled(false);
    gatewayAccountEntity.setIntegrationVersion3ds(switchToCredentialsEntity.getPaymentProvider().equalsIgnoreCase("worldpay") ? 1 : 2);
    gatewayAccountCredentialsDao.merge(switchToCredentialsEntity);
    gatewayAccountCredentialsDao.merge(activeCredentialEntity);
    gatewayAccountDao.merge(gatewayAccountEntity);
    LOGGER.info("Gateway account [id={}] switched to new payment provider", gatewayAccountEntity.getId(), kv(GATEWAY_ACCOUNT_ID, gatewayAccountEntity.getId()), kv(GATEWAY_ACCOUNT_TYPE, gatewayAccountEntity.getType()), kv(PROVIDER, activeCredentialEntity.getPaymentProvider()), kv("new_payment_provider", switchToCredentialsEntity.getPaymentProvider()), kv(USER_EXTERNAL_ID, request.getUserExternalId()));
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException) GatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity) Transactional(com.google.inject.persist.Transactional)

Example 2 with GatewayAccountCredentialsEntity

use of uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity in project pay-connector by alphagov.

the class ChargeDaoCardDetailsIT method persist_shouldStoreNullCardTypeDetails.

@Test
public void persist_shouldStoreNullCardTypeDetails() {
    GatewayAccountEntity testAccount = new GatewayAccountEntity(GatewayAccountType.TEST);
    testAccount.setExternalId(randomUuid());
    gatewayAccountDao.persist(testAccount);
    GatewayAccountCredentialsEntity credentialsEntity = aGatewayAccountCredentialsEntity().withCredentials(Map.of("username", "theUsername", "password", "thePassword", "merchant_id", "theMerchantCode")).withGatewayAccountEntity(testAccount).withPaymentProvider(SANDBOX.getName()).withState(ACTIVE).withExternalId(randomUuid()).build();
    gatewayAccountCredentialsDao.persist(credentialsEntity);
    Address billingAddress = AddressFixture.anAddress().build();
    ChargeEntity chargeEntity = ChargeEntityFixture.aValidChargeEntity().withGatewayAccountCredentialsEntity(credentialsEntity).build();
    CardDetailsEntity cardDetailsEntity = new CardDetailsEntity(FirstDigitsCardNumber.of("123456"), LastDigitsCardNumber.of("1258"), "Mr. Pay Mc Payment", CardExpiryDate.valueOf("03/09"), "VISA", null, new AddressEntity(billingAddress));
    chargeEntity.setCardDetails(cardDetailsEntity);
    chargeDao.persist(chargeEntity);
    Map<String, Object> cardDetailsSaved = databaseTestHelper.getChargeCardDetails(chargeEntity.getId());
    assertThat(cardDetailsSaved, hasEntry("card_type", null));
}
Also used : ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Address(uk.gov.pay.connector.common.model.domain.Address) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) AddressEntity(uk.gov.pay.connector.charge.model.AddressEntity) GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity) GatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity) CardDetailsEntity(uk.gov.pay.connector.charge.model.CardDetailsEntity) Test(org.junit.Test)

Example 3 with GatewayAccountCredentialsEntity

use of uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity in project pay-connector by alphagov.

the class ChargeDaoCardDetailsIT method persist_shouldStoreCardDetails.

@Test
public void persist_shouldStoreCardDetails() {
    GatewayAccountEntity testAccount = new GatewayAccountEntity(GatewayAccountType.TEST);
    testAccount.setExternalId(randomUuid());
    gatewayAccountDao.persist(testAccount);
    GatewayAccountCredentialsEntity credentialsEntity = aGatewayAccountCredentialsEntity().withCredentials(Map.of()).withGatewayAccountEntity(testAccount).withPaymentProvider(SANDBOX.getName()).withState(ACTIVE).build();
    credentialsEntity.setExternalId(randomUuid());
    gatewayAccountCredentialsDao.persist(credentialsEntity);
    Address billingAddress = AddressFixture.anAddress().build();
    ChargeEntity chargeEntity = ChargeEntityFixture.aValidChargeEntity().withGatewayAccountCredentialsEntity(credentialsEntity).build();
    CardDetailsEntity cardDetailsEntity = new CardDetailsEntity(FirstDigitsCardNumber.of("123456"), LastDigitsCardNumber.of("1258"), "Mr. Pay Mc Payment", CardExpiryDate.valueOf("03/09"), "VISA", CardType.DEBIT, new AddressEntity(billingAddress));
    chargeEntity.setCardDetails(cardDetailsEntity);
    chargeDao.persist(chargeEntity);
    Map<String, Object> cardDetailsSaved = databaseTestHelper.getChargeCardDetails(chargeEntity.getId());
    assertThat(cardDetailsSaved, hasEntry("card_type", "DEBIT"));
}
Also used : ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Address(uk.gov.pay.connector.common.model.domain.Address) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) AddressEntity(uk.gov.pay.connector.charge.model.AddressEntity) GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity) GatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity) CardDetailsEntity(uk.gov.pay.connector.charge.model.CardDetailsEntity) Test(org.junit.Test)

Example 4 with GatewayAccountCredentialsEntity

use of uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity in project pay-connector by alphagov.

the class GatewayAccountCredentialsServiceTest method createCredentialsForStripeAndWithOutCredentialsShouldCreateRecordWithCreatedState.

@Test
void createCredentialsForStripeAndWithOutCredentialsShouldCreateRecordWithCreatedState() {
    GatewayAccountEntity gatewayAccountEntity = aGatewayAccountEntity().withType(LIVE).build();
    ArgumentCaptor<GatewayAccountCredentialsEntity> argumentCaptor = ArgumentCaptor.forClass(GatewayAccountCredentialsEntity.class);
    gatewayAccountCredentialsService.createGatewayAccountCredentials(gatewayAccountEntity, "stripe", Map.of());
    verify(mockGatewayAccountCredentialsDao).persist(argumentCaptor.capture());
    GatewayAccountCredentialsEntity gatewayAccountCredentialsEntity = argumentCaptor.getValue();
    assertThat(gatewayAccountCredentialsEntity.getPaymentProvider(), is("stripe"));
    assertThat(gatewayAccountCredentialsEntity.getGatewayAccountEntity(), is(gatewayAccountEntity));
    assertThat(gatewayAccountCredentialsEntity.getState(), is(CREATED));
    assertThat(gatewayAccountCredentialsEntity.getCredentials().isEmpty(), is(true));
}
Also used : GatewayAccountEntityFixture.aGatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntityFixture.aGatewayAccountEntity) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) GatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity) GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with GatewayAccountCredentialsEntity

use of uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity in project pay-connector by alphagov.

the class GatewayAccountCredentialsServiceTest method shouldNotChangeState_whenCredentialsNotInCreatedState.

@Test
void shouldNotChangeState_whenCredentialsNotInCreatedState() {
    GatewayAccountEntity gatewayAccountEntity = aGatewayAccountEntity().build();
    GatewayAccountCredentialsEntity credentialsEntity = aGatewayAccountCredentialsEntity().withGatewayAccountEntity(gatewayAccountEntity).withState(VERIFIED_WITH_LIVE_PAYMENT).build();
    gatewayAccountEntity.setGatewayAccountCredentials(List.of(credentialsEntity));
    JsonPatchRequest patchRequest = JsonPatchRequest.from(objectMapper.valueToTree(Map.of("path", "credentials", "op", "replace", "value", Map.of("merchant_id", "new-merchant-id"))));
    gatewayAccountCredentialsService.updateGatewayAccountCredentials(credentialsEntity, Collections.singletonList(patchRequest));
    assertThat(credentialsEntity.getState(), is(VERIFIED_WITH_LIVE_PAYMENT));
}
Also used : JsonPatchRequest(uk.gov.service.payments.commons.model.jsonpatch.JsonPatchRequest) GatewayAccountEntityFixture.aGatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntityFixture.aGatewayAccountEntity) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) GatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity) GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

GatewayAccountCredentialsEntity (uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity)71 GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity (uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity)56 Test (org.junit.jupiter.api.Test)49 GatewayAccountEntity (uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity)49 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)30 GatewayAccountEntityFixture.aGatewayAccountEntity (uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntityFixture.aGatewayAccountEntity)30 Charge (uk.gov.pay.connector.charge.model.domain.Charge)15 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)10 Test (org.junit.Test)6 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)6 Transactional (com.google.inject.persist.Transactional)5 ChargeEntityFixture.defaultGatewayAccountCredentialsEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.defaultGatewayAccountCredentialsEntity)5 ChargeEntityFixture.defaultGatewayAccountEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.defaultGatewayAccountEntity)5 ChargeQueryGatewayRequest (uk.gov.pay.connector.gateway.ChargeQueryGatewayRequest)5 ChargeQueryResponse (uk.gov.pay.connector.gateway.ChargeQueryResponse)4 JsonPatchRequest (uk.gov.service.payments.commons.model.jsonpatch.JsonPatchRequest)4 CardDetailsEntity (uk.gov.pay.connector.charge.model.CardDetailsEntity)3 ChargeStatus (uk.gov.pay.connector.charge.model.domain.ChargeStatus)3 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)2 ValueSource (org.junit.jupiter.params.provider.ValueSource)2