Search in sources :

Example 1 with Charge

use of uk.gov.pay.connector.charge.model.domain.Charge 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 Charge

use of uk.gov.pay.connector.charge.model.domain.Charge in project pay-connector by alphagov.

the class GatewayAccountCredentialsServiceTest method shouldReturnGatewayAccountCredentialForMatchedCredentialExternalId.

@Test
void shouldReturnGatewayAccountCredentialForMatchedCredentialExternalId() {
    GatewayAccountCredentialsEntity aGatewayAccountCredentialsEntityOne = GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity().build();
    GatewayAccountCredentialsEntity aGatewayAccountCredentialsEntityTwo = GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity().build();
    GatewayAccountEntity gatewayAccountEntity = aGatewayAccountEntity().withGatewayAccountCredentials(List.of(aGatewayAccountCredentialsEntityOne, aGatewayAccountCredentialsEntityTwo)).build();
    Charge charge = Charge.from(ChargeEntityFixture.aValidChargeEntity().withGatewayAccountCredentialsEntity(aGatewayAccountCredentialsEntityOne).build());
    String expectedCredentialsExternalId = aGatewayAccountCredentialsEntityOne.getExternalId();
    Optional<GatewayAccountCredentialsEntity> gatewayAccountCredentialsEntity = gatewayAccountCredentialsService.findCredentialFromCharge(charge, gatewayAccountEntity);
    assertThat(gatewayAccountCredentialsEntity.isPresent(), is(true));
    assertThat(gatewayAccountCredentialsEntity.get().getExternalId(), is(expectedCredentialsExternalId));
}
Also used : GatewayAccountEntityFixture.aGatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntityFixture.aGatewayAccountEntity) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) Charge(uk.gov.pay.connector.charge.model.domain.Charge) 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 3 with Charge

use of uk.gov.pay.connector.charge.model.domain.Charge in project pay-connector by alphagov.

the class EpdqPaymentProvider method queryPaymentStatus.

public ChargeQueryResponse queryPaymentStatus(ChargeQueryGatewayRequest chargeQueryGatewayRequest) throws GatewayException {
    URI url = URI.create(String.format("%s/%s", gatewayUrlMap.get(chargeQueryGatewayRequest.getGatewayAccount().getType()), ROUTE_FOR_QUERY_ORDER));
    GatewayClient.Response response = authoriseClient.postRequestFor(url, EPDQ, chargeQueryGatewayRequest.getGatewayAccount().getType(), buildQueryOrderRequestFor(chargeQueryGatewayRequest), getGatewayAccountCredentialsAsAuthHeader(chargeQueryGatewayRequest.getGatewayCredentials()));
    GatewayResponse<EpdqQueryResponse> epdqGatewayResponse = getUninterpretedEpdqGatewayResponse(response, EpdqQueryResponse.class);
    return epdqGatewayResponse.getBaseResponse().map(epdqQueryResponse -> {
        ChargeStatus mappedStatus = EpdqStatusMapper.map(epdqQueryResponse.getStatus());
        return new ChargeQueryResponse(mappedStatus, epdqQueryResponse);
    }).orElseThrow(() -> new WebApplicationException(String.format("Unable to query charge %s - an error occurred: %s", chargeQueryGatewayRequest.getChargeExternalId(), epdqGatewayResponse)));
}
Also used : EpdqPayloadDefinitionForCancelOrder(uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForCancelOrder) CREDENTIALS_SHA_IN_PASSPHRASE(uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_SHA_IN_PASSPHRASE) LoggerFactory(org.slf4j.LoggerFactory) AUTHORISE(uk.gov.pay.connector.gateway.GatewayOperation.AUTHORISE) GatewayOrder(uk.gov.pay.connector.gateway.GatewayOrder) CREDENTIALS_MERCHANT_ID(uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_MERCHANT_ID) PaymentProvider(uk.gov.pay.connector.gateway.PaymentProvider) Map(java.util.Map) ChargeQueryGatewayRequest(uk.gov.pay.connector.gateway.ChargeQueryGatewayRequest) EpdqCancelResponse(uk.gov.pay.connector.gateway.epdq.model.response.EpdqCancelResponse) URI(java.net.URI) RefundGatewayRequest(uk.gov.pay.connector.gateway.model.request.RefundGatewayRequest) EPDQ(uk.gov.pay.connector.gateway.PaymentGatewayName.EPDQ) BaseResponse(uk.gov.pay.connector.gateway.model.response.BaseResponse) Gateway3DSAuthorisationResponse(uk.gov.pay.connector.gateway.model.response.Gateway3DSAuthorisationResponse) ERROR(uk.gov.pay.connector.gateway.model.response.BaseAuthoriseResponse.AuthoriseStatus.ERROR) ChargeQueryResponse(uk.gov.pay.connector.gateway.ChargeQueryResponse) String.format(java.lang.String.format) List(java.util.List) EpdqQueryResponse(uk.gov.pay.connector.gateway.epdq.model.response.EpdqQueryResponse) BaseAuthoriseResponse(uk.gov.pay.connector.gateway.model.response.BaseAuthoriseResponse) CANCEL(uk.gov.pay.connector.gateway.GatewayOperation.CANCEL) WebApplicationException(javax.ws.rs.WebApplicationException) Optional(java.util.Optional) ChargeStatus(uk.gov.pay.connector.charge.model.domain.ChargeStatus) EpdqAuthorisationResponse(uk.gov.pay.connector.gateway.epdq.model.response.EpdqAuthorisationResponse) CAPTURE(uk.gov.pay.connector.gateway.GatewayOperation.CAPTURE) GatewayResponseUnmarshaller.unmarshallResponse(uk.gov.pay.connector.gateway.GatewayResponseUnmarshaller.unmarshallResponse) GatewayRefundResponse(uk.gov.pay.connector.gateway.model.response.GatewayRefundResponse) EpdqPayloadDefinitionForQueryOrder(uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForQueryOrder) GatewayClient(uk.gov.pay.connector.gateway.GatewayClient) Inject(javax.inject.Inject) Charge(uk.gov.pay.connector.charge.model.domain.Charge) Auth3dsResponseGatewayRequest(uk.gov.pay.connector.gateway.model.request.Auth3dsResponseGatewayRequest) CaptureGatewayRequest(uk.gov.pay.connector.gateway.model.request.CaptureGatewayRequest) GenericGatewayException(uk.gov.pay.connector.gateway.GatewayException.GenericGatewayException) ExternalChargeRefundAvailability(uk.gov.pay.connector.common.model.api.ExternalChargeRefundAvailability) WalletAuthorisationGatewayRequest(uk.gov.pay.connector.wallets.WalletAuthorisationGatewayRequest) CREDENTIALS_PASSWORD(uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_PASSWORD) Environment(io.dropwizard.setup.Environment) PaymentGatewayName(uk.gov.pay.connector.gateway.PaymentGatewayName) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) ExternalRefundAvailabilityCalculator(uk.gov.pay.connector.gateway.util.ExternalRefundAvailabilityCalculator) EpdqPayloadDefinitionForNew3dsOrder(uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNew3dsOrder) AuthUtil.getGatewayAccountCredentialsAsAuthHeader(uk.gov.pay.connector.gateway.util.AuthUtil.getGatewayAccountCredentialsAsAuthHeader) BaseCancelResponse(uk.gov.pay.connector.gateway.model.response.BaseCancelResponse) CaptureResponse(uk.gov.pay.connector.gateway.CaptureResponse) REFUND(uk.gov.pay.connector.gateway.GatewayOperation.REFUND) ConnectorConfiguration(uk.gov.pay.connector.app.ConnectorConfiguration) EpdqExternalRefundAvailabilityCalculator(uk.gov.pay.connector.gateway.util.EpdqExternalRefundAvailabilityCalculator) CREDENTIALS_USERNAME(uk.gov.pay.connector.gatewayaccount.model.GatewayAccount.CREDENTIALS_USERNAME) CardAuthorisationGatewayRequest(uk.gov.pay.connector.gateway.model.request.CardAuthorisationGatewayRequest) Auth3dsResultOutcome(uk.gov.pay.connector.gateway.model.Auth3dsResult.Auth3dsResultOutcome) GatewayErrorException(uk.gov.pay.connector.gateway.GatewayException.GatewayErrorException) Refund(uk.gov.pay.connector.refund.model.domain.Refund) CancelGatewayRequest(uk.gov.pay.connector.gateway.model.request.CancelGatewayRequest) GatewayResponse(uk.gov.pay.connector.gateway.model.response.GatewayResponse) GatewayClientFactory(uk.gov.pay.connector.gateway.GatewayClientFactory) EpdqPayloadDefinitionForNew3ds2Order(uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNew3ds2Order) Clock(java.time.Clock) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) GatewayException(uk.gov.pay.connector.gateway.GatewayException) AuthCardDetails(uk.gov.pay.connector.gateway.model.AuthCardDetails) EpdqPayloadDefinitionForNewOrder(uk.gov.pay.connector.gateway.epdq.payload.EpdqPayloadDefinitionForNewOrder) ChargeQueryResponse(uk.gov.pay.connector.gateway.ChargeQueryResponse) EpdqQueryResponse(uk.gov.pay.connector.gateway.epdq.model.response.EpdqQueryResponse) GatewayClient(uk.gov.pay.connector.gateway.GatewayClient) ChargeStatus(uk.gov.pay.connector.charge.model.domain.ChargeStatus) WebApplicationException(javax.ws.rs.WebApplicationException) URI(java.net.URI)

Example 4 with Charge

use of uk.gov.pay.connector.charge.model.domain.Charge in project pay-connector by alphagov.

the class DiscrepancyServiceTest method aChargeShouldBeCancellable_whenPayAndGatewayStatusesAllowItAndIsOlderThan2Days.

@Test
void aChargeShouldBeCancellable_whenPayAndGatewayStatusesAllowItAndIsOlderThan2Days() throws GatewayException {
    ChargeEntity chargeEntity = ChargeEntityFixture.aValidChargeEntity().withCreatedDate(Instant.now().minus(Duration.ofDays(3))).withStatus(EXPIRED).build();
    Charge charge = Charge.from(chargeEntity);
    ChargeQueryResponse chargeQueryResponse = new ChargeQueryResponse(AUTHORISATION_SUCCESS, mockGatewayResponse);
    when(chargeService.findCharge(chargeEntity.getExternalId())).thenReturn(Optional.of(charge));
    when(chargeService.findChargeByExternalId(chargeEntity.getExternalId())).thenReturn(chargeEntity);
    when(queryService.getChargeGatewayStatus(charge, chargeEntity.getGatewayAccount())).thenReturn(chargeQueryResponse);
    when(gatewayAccountService.getGatewayAccount(charge.getGatewayAccountId())).thenReturn(Optional.of(chargeEntity.getGatewayAccount()));
    when(expiryService.forceCancelWithGateway(chargeEntity)).thenReturn(true);
    discrepancyService.resolveDiscrepancies(Collections.singletonList(chargeEntity.getExternalId()));
    verify(expiryService).forceCancelWithGateway(chargeEntity);
}
Also used : ChargeQueryResponse(uk.gov.pay.connector.gateway.ChargeQueryResponse) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Charge(uk.gov.pay.connector.charge.model.domain.Charge) Test(org.junit.jupiter.api.Test)

Example 5 with Charge

use of uk.gov.pay.connector.charge.model.domain.Charge in project pay-connector by alphagov.

the class QueryServiceTest method returnSuccessfulGatewayResponse.

@Test
void returnSuccessfulGatewayResponse() throws Exception {
    ChargeEntity chargeEntity = aValidChargeEntity().withGatewayAccountEntity(defaultGatewayAccountEntity()).build();
    Charge charge = Charge.from(chargeEntity);
    GatewayAccountEntity gatewayAccountEntity = chargeEntity.getGatewayAccount();
    GatewayAccountCredentialsEntity gatewayAccountCredentialsEntity = chargeEntity.getGatewayAccountCredentialsEntity();
    ChargeQueryResponse response = new ChargeQueryResponse(AUTHORISATION_3DS_REQUIRED, mockGatewayResponse);
    when(paymentProvider.queryPaymentStatus(any(ChargeQueryGatewayRequest.class))).thenReturn(response);
    when(gatewayAccountCredentialsService.findCredentialFromCharge(charge, gatewayAccountEntity)).thenReturn(Optional.of(gatewayAccountCredentialsEntity));
    assertThat(queryService.getChargeGatewayStatus(chargeEntity), is(response));
}
Also used : ChargeQueryGatewayRequest(uk.gov.pay.connector.gateway.ChargeQueryGatewayRequest) ChargeQueryResponse(uk.gov.pay.connector.gateway.ChargeQueryResponse) ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Charge(uk.gov.pay.connector.charge.model.domain.Charge) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) ChargeEntityFixture.defaultGatewayAccountEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.defaultGatewayAccountEntity) ChargeEntityFixture.defaultGatewayAccountCredentialsEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.defaultGatewayAccountCredentialsEntity) GatewayAccountCredentialsEntity(uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity) Test(org.junit.jupiter.api.Test)

Aggregations

Charge (uk.gov.pay.connector.charge.model.domain.Charge)56 Test (org.junit.jupiter.api.Test)30 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)22 GatewayAccountEntity (uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity)18 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)15 GatewayAccountCredentialsEntity (uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntity)14 RefundRequest (uk.gov.pay.connector.refund.model.RefundRequest)13 RefundEntity (uk.gov.pay.connector.refund.model.domain.RefundEntity)13 RefundEntityFixture.aValidRefundEntity (uk.gov.pay.connector.model.domain.RefundEntityFixture.aValidRefundEntity)12 Test (org.junit.Test)10 LedgerTransaction (uk.gov.pay.connector.client.ledger.model.LedgerTransaction)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)7 ChargeStatus (uk.gov.pay.connector.charge.model.domain.ChargeStatus)7 ChargeQueryGatewayRequest (uk.gov.pay.connector.gateway.ChargeQueryGatewayRequest)7 ChargeQueryResponse (uk.gov.pay.connector.gateway.ChargeQueryResponse)7 GatewayAccountEntityFixture.aGatewayAccountEntity (uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntityFixture.aGatewayAccountEntity)7 GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity (uk.gov.pay.connector.gatewayaccountcredentials.model.GatewayAccountCredentialsEntityFixture.aGatewayAccountCredentialsEntity)7 Refund (uk.gov.pay.connector.refund.model.domain.Refund)7 ChargeRefundResponse (uk.gov.pay.connector.refund.service.ChargeRefundResponse)7