Source:
private Pair<Integer, Integer> expireChargesOrPotentiallyForceTransitionState(List<ChargeEntity> charges) { AtomicInteger expireCancelled = new AtomicInteger(); AtomicInteger expireCancelFailed = new AtomicInteger(); charges.forEach(chargeEntity -> { Optional<ChargeStatus> gatewayStatus = queryService.getMappedGatewayStatus(chargeEntity); gatewayStatus.ifPresentOrElse(status -> { if (status.toExternal().isFinished()) { logger.info(format("Expiring charge skipped as charge is in a terminal state on the gateway " + "provider. Attempting to force state on charge to [%s]", status.getValue()), kv(PAYMENT_EXTERNAL_ID, chargeEntity.getExternalId()), kv(GATEWAY_ACCOUNT_ID, chargeEntity.getGatewayAccount().getId()), kv(PROVIDER, chargeEntity.getGatewayAccount().getGatewayName())); try { chargeService.forceTransitionChargeState(chargeEntity, status); expireCancelFailed.getAndIncrement(); } catch (InvalidForceStateTransitionException e) { logger.error(format("Cannot expire charge as it is in a terminal state of [%s] with the " + "gateway provider. The charge's state could not be transitioned to [%s].", status.getValue(), status.getValue()), kv(PAYMENT_EXTERNAL_ID, chargeEntity.getExternalId())); expireCancelFailed.getAndIncrement(); } } else { ChargeEntity expiredCharge = expireChargeWithGatewayCleanup(chargeEntity); if (EXPIRED.getValue().equals(expiredCharge.getStatus())) { expireCancelled.getAndIncrement(); } else if (EXPIRE_CANCEL_FAILED.getValue().equals(expiredCharge.getStatus())) { expireCancelFailed.getAndIncrement(); } } }, () -> { logger.error(format("Gateway status does not map to any charge " + "status in %s, expiring without cancelling on the gateway.", ChargeStatus.class.getCanonicalName()), kv(PAYMENT_EXTERNAL_ID, chargeEntity.getExternalId())); chargeService.transitionChargeState(chargeEntity.getExternalId(), EXPIRED); expireCancelled.getAndIncrement(); }); }); return Pair.of( expireCancelled.intValue(), expireCancelFailed.intValue() ); }

Target:
private Pair<Integer, Integer> expireChargesOrPotentiallyForceTransitionState(List<ChargeEntity> charges) { AtomicInteger expireCancelled = new AtomicInteger(); AtomicInteger expireCancelFailed = new AtomicInteger(); charges.forEach(chargeEntity -> { Optional<ChargeStatus> gatewayStatus = queryService.getMappedGatewayStatus(chargeEntity); gatewayStatus.ifPresentOrElse(status -> { if (status.toExternal().isFinished()) { logger.info(format("Expiring charge skipped as charge is in a terminal state on the gateway " + "provider. Attempting to force state on charge to [%s]", status.getValue()), kv(PAYMENT_EXTERNAL_ID, chargeEntity.getExternalId()), kv(GATEWAY_ACCOUNT_ID, chargeEntity.getGatewayAccount().getId()), kv(PROVIDER, chargeEntity.getGatewayAccount().getGatewayName())); try { chargeService.forceTransitionChargeState(chargeEntity, status); expireCancelFailed.getAndIncrement(); } catch (InvalidForceStateTransitionException e) { logger.error(format("Cannot expire charge as it is in a terminal state of [%s] with " + "the gateway provider and it is not possible to transition the charge " + "into this state. Current state: [%s]", status.getValue(), chargeEntity.getStatus()), kv(PAYMENT_EXTERNAL_ID, chargeEntity.getExternalId())); expireCancelFailed.getAndIncrement(); } } else { ChargeEntity expiredCharge = expireChargeWithGatewayCleanup(chargeEntity); if (EXPIRED.getValue().equals(expiredCharge.getStatus())) { expireCancelled.getAndIncrement(); } else if (EXPIRE_CANCEL_FAILED.getValue().equals(expiredCharge.getStatus())) { expireCancelFailed.getAndIncrement(); } } }, () -> { logger.error(format("Gateway status does not map to any charge " + "status in %s, expiring without cancelling on the gateway.", ChargeStatus.class.getCanonicalName()), kv(PAYMENT_EXTERNAL_ID, chargeEntity.getExternalId())); chargeService.transitionChargeState(chargeEntity.getExternalId(), EXPIRED); expireCancelled.getAndIncrement(); }); }); return Pair.of( expireCancelled.intValue(), expireCancelFailed.intValue() ); }