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()));
}
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));
}
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"));
}
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));
}
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));
}
Aggregations