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