Search in sources :

Example 1 with GatewayAccountEntity

use of uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity in project pay-connector by alphagov.

the class WorldpayNotificationService method handleNotificationFor.

@Transactional
public boolean handleNotificationFor(String ipAddress, String payload) {
    if (isNotificationRejectedFromIpAddress(ipAddress)) {
        logger.error("{} notification received from ip '{}' which is not in domain '{}'", PAYMENT_GATEWAY_NAME, ipAddress, notificationDomain());
        return false;
    }
    WorldpayNotification notification;
    try {
        logger.info("Parsing {} notification", PAYMENT_GATEWAY_NAME);
        logger.debug("Payload: {}", payload);
        notification = XMLUnmarshaller.unmarshall(payload, WorldpayNotification.class);
        logger.info("Parsed {} notification: {}", PAYMENT_GATEWAY_NAME, notification);
    } catch (XMLUnmarshallerException e) {
        logger.error("{} notification parsing failed: {}", PAYMENT_GATEWAY_NAME, e);
        return true;
    }
    if (isIgnored(notification)) {
        logger.info("{} notification {} ignored", PAYMENT_GATEWAY_NAME, notification);
        return true;
    }
    if (isTransactionIdBlank(notification)) {
        logger.warn("{} notification {} failed verification because it has no transaction ID", PAYMENT_GATEWAY_NAME, notification);
        return true;
    }
    Optional<Charge> maybeCharge = chargeService.findByProviderAndTransactionIdFromDbOrLedger(PAYMENT_GATEWAY_NAME, notification.getTransactionId());
    if (maybeCharge.isEmpty()) {
        // will be blocked until the notification being retried is successful or removed from Worldpay's queue
        if (gatewayAccountService.isATelephonePaymentNotificationAccount(notification.getMerchantCode())) {
            logger.info("{} notification {} for telephone payments gateway account could not be evaluated (associated charge entity not found)", PAYMENT_GATEWAY_NAME, notification);
            return false;
        }
        logger.info("{} notification {} could not be evaluated (associated charge entity not found)", PAYMENT_GATEWAY_NAME, notification);
        return true;
    }
    Charge charge = maybeCharge.get();
    Optional<GatewayAccountEntity> mayBeGatewayAccountEntity = gatewayAccountService.getGatewayAccount(charge.getGatewayAccountId());
    if (mayBeGatewayAccountEntity.isEmpty()) {
        logger.error("{} notification {} could not be processed (associated gateway account [{}] not found for charge [{}] {}, {})", PAYMENT_GATEWAY_NAME, notification, charge.getGatewayAccountId(), charge.getExternalId(), kv(PAYMENT_EXTERNAL_ID, charge.getExternalId()), kv(GATEWAY_ACCOUNT_ID, charge.getGatewayAccountId()));
        return false;
    }
    GatewayAccountEntity gatewayAccountEntity = mayBeGatewayAccountEntity.get();
    if (isCaptureNotification(notification)) {
        if (charge.isHistoric()) {
            chargeNotificationProcessor.processCaptureNotificationForExpungedCharge(gatewayAccountEntity, notification.getTransactionId(), charge, CAPTURED);
            return true;
        }
        chargeNotificationProcessor.invoke(notification.getTransactionId(), charge, CAPTURED, notification.getGatewayEventDate());
    } else if (isRefundNotification(notification)) {
        refundNotificationProcessor.invoke(PaymentGatewayName.WORLDPAY, newRefundStatus(notification), gatewayAccountEntity, notification.getReference(), notification.getTransactionId(), charge);
    } else if (isErrorNotification(notification)) {
        if (gatewayAccountEntity.isLive()) {
            logger.error("{} error notification received for live account {}", PAYMENT_GATEWAY_NAME, notification);
        } else {
            logger.info("{} error notification received for test account {}", PAYMENT_GATEWAY_NAME, notification);
        }
    } else {
        logger.error("{} notification {} unknown", PAYMENT_GATEWAY_NAME, notification);
    }
    return true;
}
Also used : Charge(uk.gov.pay.connector.charge.model.domain.Charge) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) XMLUnmarshallerException(uk.gov.pay.connector.gateway.util.XMLUnmarshallerException) Transactional(com.google.inject.persist.Transactional)

Example 2 with GatewayAccountEntity

use of uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity in project pay-connector by alphagov.

the class GatewayAccountObjectConverter method createEntityFrom.

public static GatewayAccountEntity createEntityFrom(GatewayAccountRequest gatewayAccountRequest) {
    GatewayAccountEntity gatewayAccountEntity = new GatewayAccountEntity(GatewayAccountType.fromString(gatewayAccountRequest.getProviderAccountType()));
    gatewayAccountEntity.setExternalId(randomUuid());
    gatewayAccountEntity.setServiceName(gatewayAccountRequest.getServiceName());
    gatewayAccountEntity.setServiceId(gatewayAccountRequest.getServiceId());
    gatewayAccountEntity.setDescription(gatewayAccountRequest.getDescription());
    gatewayAccountEntity.setAnalyticsId(gatewayAccountRequest.getAnalyticsId());
    gatewayAccountEntity.setRequires3ds(gatewayAccountRequest.getRequires3ds());
    gatewayAccountEntity.setIntegrationVersion3ds(DEFAULT_INTEGRATION_VERSION_3_DS);
    gatewayAccountEntity.addNotification(EmailNotificationType.PAYMENT_CONFIRMED, new EmailNotificationEntity(gatewayAccountEntity));
    gatewayAccountEntity.addNotification(EmailNotificationType.REFUND_ISSUED, new EmailNotificationEntity(gatewayAccountEntity));
    return gatewayAccountEntity;
}
Also used : EmailNotificationEntity(uk.gov.pay.connector.usernotification.model.domain.EmailNotificationEntity) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity)

Example 3 with GatewayAccountEntity

use of uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity in project pay-connector by alphagov.

the class GatewayAccountCredentialsService method updateGatewayAccountCredentials.

@Transactional
public GatewayAccountCredentials updateGatewayAccountCredentials(GatewayAccountCredentialsEntity gatewayAccountCredentialsEntity, Iterable<JsonPatchRequest> updateRequests) {
    for (JsonPatchRequest updateRequest : updateRequests) {
        if (JsonPatchOp.REPLACE == updateRequest.getOp()) {
            updateGatewayAccountCredentialField(updateRequest, gatewayAccountCredentialsEntity);
        }
    }
    gatewayAccountCredentialsDao.merge(gatewayAccountCredentialsEntity);
    GatewayAccountEntity gatewayAccountEntity = gatewayAccountCredentialsEntity.getGatewayAccountEntity();
    LOGGER.info("Updated credentials for gateway account [id={}]", gatewayAccountEntity.getId(), kv(GATEWAY_ACCOUNT_ID, gatewayAccountEntity.getId()), kv(GATEWAY_ACCOUNT_TYPE, gatewayAccountEntity.getType()), kv(PROVIDER, gatewayAccountCredentialsEntity.getPaymentProvider()), kv("state", gatewayAccountCredentialsEntity.getState()), kv(USER_EXTERNAL_ID, gatewayAccountCredentialsEntity.getLastUpdatedByUserExternalId()));
    return new GatewayAccountCredentials(gatewayAccountCredentialsEntity);
}
Also used : JsonPatchRequest(uk.gov.service.payments.commons.model.jsonpatch.JsonPatchRequest) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) GatewayAccountCredentials(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountCredentials) Transactional(com.google.inject.persist.Transactional)

Example 4 with GatewayAccountEntity

use of uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity in project pay-connector by alphagov.

the class SmartpayPaymentProviderTest method shouldFailRequestAuthorisationIfCredentialsAreNotCorrect.

@Test
public void shouldFailRequestAuthorisationIfCredentialsAreNotCorrect() throws Exception {
    PaymentProvider paymentProvider = getSmartpayPaymentProvider();
    GatewayAccountEntity accountWithInvalidCredentials = new GatewayAccountEntity();
    accountWithInvalidCredentials.setId(11L);
    accountWithInvalidCredentials.setGatewayAccountCredentials(List.of(aGatewayAccountCredentialsEntity().withCredentials(Map.of("merchant_id", "MerchantAccount", "username", "wrong-username", "password", "wrong-password")).withGatewayAccountEntity(accountWithInvalidCredentials).withPaymentProvider(SMARTPAY.getName()).withState(ACTIVE).build()));
    accountWithInvalidCredentials.setType(TEST);
    chargeEntity.setGatewayAccount(accountWithInvalidCredentials);
    CardAuthorisationGatewayRequest request = getCardAuthorisationRequest(chargeEntity);
    GatewayResponse<BaseAuthoriseResponse> response = paymentProvider.authorise(request);
    assertFalse(response.isSuccessful());
    assertNotNull(response.getGatewayError());
}
Also used : PaymentProvider(uk.gov.pay.connector.gateway.PaymentProvider) SmartpayPaymentProvider(uk.gov.pay.connector.gateway.smartpay.SmartpayPaymentProvider) BaseAuthoriseResponse(uk.gov.pay.connector.gateway.model.response.BaseAuthoriseResponse) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) CardAuthorisationGatewayRequest(uk.gov.pay.connector.gateway.model.request.CardAuthorisationGatewayRequest) Test(org.junit.Test)

Example 5 with GatewayAccountEntity

use of uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity 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)

Aggregations

GatewayAccountEntity (uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity)101 GatewayAccountCredentialsEntity (uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity)49 GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity (uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity)39 GatewayAccountEntityFixture.aGatewayAccountEntity (uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntityFixture.aGatewayAccountEntity)37 Test (org.junit.jupiter.api.Test)36 Test (org.junit.Test)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)23 Charge (uk.gov.pay.connector.charge.model.domain.Charge)20 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)12 Before (org.junit.Before)9 ChargeQueryGatewayRequest (uk.gov.pay.connector.gateway.ChargeQueryGatewayRequest)6 BaseAuthoriseResponse (uk.gov.pay.connector.gateway.model.response.BaseAuthoriseResponse)6 Map (java.util.Map)5 Optional (java.util.Optional)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 ChargeQueryResponse (uk.gov.pay.connector.gateway.ChargeQueryResponse)5 EmailNotificationEntity (uk.gov.pay.connector.usernotification.model.domain.EmailNotificationEntity)5 Environment (io.dropwizard.setup.Environment)4