use of uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURED in project pay-connector by alphagov.
the class LedgerTransactionFixture method from.
public static LedgerTransactionFixture from(ChargeEntity chargeEntity, List<RefundEntity> refundsList) {
LedgerTransactionFixture ledgerTransactionFixture = aValidLedgerTransaction().withCreatedDate(chargeEntity.getCreatedDate().atZone(UTC)).withStatus(ChargeStatus.fromString(chargeEntity.getStatus()).toExternal().getStatusV2()).withExternalId(chargeEntity.getExternalId()).withAmount(chargeEntity.getAmount()).withDescription(chargeEntity.getDescription()).withReference(chargeEntity.getReference().toString()).withLanguage(chargeEntity.getLanguage()).withEmail(chargeEntity.getEmail()).withReturnUrl(chargeEntity.getReturnUrl()).withGatewayTransactionId(chargeEntity.getGatewayTransactionId()).withCredentialExternalId(chargeEntity.getGatewayAccountCredentialsEntity().getExternalId()).withDelayedCapture(chargeEntity.isDelayedCapture()).withSource(chargeEntity.getSource()).withMoto(chargeEntity.isMoto()).withFee(chargeEntity.getFeeAmount().orElse(null)).withCorporateCardSurcharge(chargeEntity.getCorporateSurcharge().orElse(null)).withWalletType(chargeEntity.getWalletType()).withTotalAmount(getTotalAmountFor(chargeEntity)).withNetAmount(chargeEntity.getNetAmount().orElse(null));
ledgerTransactionFixture.withCreatedDate(getEventDate(chargeEntity.getEvents(), List.of(CREATED, PAYMENT_NOTIFICATION_CREATED)));
if (chargeEntity.getGatewayAccount() != null) {
GatewayAccountEntity gatewayAccount = chargeEntity.getGatewayAccount();
ledgerTransactionFixture.withPaymentProvider(chargeEntity.getPaymentProvider());
ledgerTransactionFixture.withGatewayAccountId(gatewayAccount.getId());
ledgerTransactionFixture.isLive(gatewayAccount.isLive());
}
if (nonNull(chargeEntity.getCardDetails())) {
CardDetailsEntity chargeEntityCardDetails = chargeEntity.getCardDetails();
Address ledgerAddress = chargeEntityCardDetails.getBillingAddress().map(addressEntity -> {
return new Address(addressEntity.getLine1(), addressEntity.getLine2(), addressEntity.getPostcode(), addressEntity.getCity(), addressEntity.getCounty(), addressEntity.getCountry());
}).orElse(null);
CardDetails cardDetails = new CardDetails(chargeEntityCardDetails.getCardHolderName(), ledgerAddress, chargeEntityCardDetails.getCardTypeDetails().map(CardBrandLabelEntity::getLabel).orElse(null), ofNullable(chargeEntityCardDetails.getLastDigitsCardNumber()).map(LastDigitsCardNumber::toString).orElse(null), ofNullable(chargeEntityCardDetails.getFirstDigitsCardNumber()).map(FirstDigitsCardNumber::toString).orElse(null), ofNullable(chargeEntityCardDetails.getExpiryDate()).map(CardExpiryDate::toString).orElse(null), ofNullable(chargeEntityCardDetails.getCardType()).map(cardType -> cardType.toString().toLowerCase()).orElse(null));
ledgerTransactionFixture.withCardDetails(cardDetails);
}
ledgerTransactionFixture.withCapturedDate(getEventDate(chargeEntity.getEvents(), List.of(CAPTURED)));
ledgerTransactionFixture.withCaptureSubmittedDate(getEventDate(chargeEntity.getEvents(), List.of(CAPTURE_SUBMITTED)));
ChargeResponse.RefundSummary refundSummary = new ChargeResponse.RefundSummary();
ExternalChargeRefundAvailability refundAvailability;
if (refundsList != null) {
refundAvailability = new DefaultExternalRefundAvailabilityCalculator().calculate(Charge.from(chargeEntity), refundsList.stream().map(Refund::from).collect(Collectors.toList()));
} else {
refundAvailability = new DefaultExternalRefundAvailabilityCalculator().calculate(Charge.from(chargeEntity), List.of());
}
refundSummary.setStatus(refundAvailability.getStatus());
ledgerTransactionFixture.withRefundSummary(refundSummary);
if (chargeEntity.get3dsRequiredDetails() != null) {
AuthorisationSummary authorisationSummary = new AuthorisationSummary();
ThreeDSecure threeDSecure = new ThreeDSecure();
threeDSecure.setRequired(true);
threeDSecure.setVersion(chargeEntity.get3dsRequiredDetails().getThreeDsVersion());
authorisationSummary.setThreeDSecure(threeDSecure);
ledgerTransactionFixture.withAuthorisationSummary(authorisationSummary);
}
return ledgerTransactionFixture;
}
use of uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURED in project pay-connector by alphagov.
the class StateTransitionsIT method shouldEmitCorrectRefundEvents.
@Test
public void shouldEmitCorrectRefundEvents() throws Exception {
String chargeId = addCharge(CAPTURED, "ref", Instant.now().minus(1, HOURS), "transaction-id-transition-it");
Long refundAmount = 50L;
Long refundAmountAvailable = AMOUNT;
ImmutableMap<String, Long> refundData = ImmutableMap.of("amount", refundAmount, "refund_amount_available", refundAmountAvailable);
String refundPayload = new Gson().toJson(refundData);
ValidatableResponse response = givenSetup().body(refundPayload).accept(ContentType.JSON).contentType(ContentType.JSON).post("/v1/api/accounts/{accountId}/charges/{chargeId}/refunds".replace("{accountId}", accountId).replace("{chargeId}", chargeId)).then().statusCode(202);
Thread.sleep(500L);
String refundId = response.extract().response().jsonPath().get("refund_id");
List<Message> messages = readMessagesFromEventQueue();
assertThat(messages.size(), is(4));
JsonObject message1 = messages.stream().map(m -> new JsonParser().parse(m.getBody()).getAsJsonObject()).filter(m -> "REFUND_CREATED_BY_SERVICE".equals(m.get("event_type").getAsString())).findFirst().get();
assertThat(message1.get("event_type").getAsString(), is("REFUND_CREATED_BY_SERVICE"));
assertThat(message1.get("resource_external_id").getAsString(), is(refundId));
assertThat(message1.get("service_id").getAsString(), is("external-service-id"));
assertThat(message1.get("live").getAsBoolean(), is(false));
assertThat(message1.get("parent_resource_external_id").getAsString(), is(chargeId));
assertThat(message1.get("event_details").getAsJsonObject().get("amount").getAsInt(), is(50));
JsonObject message2 = messages.stream().map(m -> new JsonParser().parse(m.getBody()).getAsJsonObject()).filter(m -> "REFUND_AVAILABILITY_UPDATED".equals(m.get("event_type").getAsString())).findFirst().get();
assertThat(message2.get("event_type").getAsString(), is("REFUND_AVAILABILITY_UPDATED"));
assertThat(message2.get("service_id").getAsString(), is("external-service-id"));
assertThat(message2.get("live").getAsBoolean(), is(false));
assertThat(message2.get("resource_external_id").getAsString(), is(chargeId));
assertThat(message2.get("event_details").getAsJsonObject().get("refund_amount_available").getAsInt(), is(6184));
assertThat(message2.get("event_details").getAsJsonObject().get("refund_amount_refunded").getAsInt(), is(50));
assertThat(message2.get("event_details").getAsJsonObject().get("refund_status").getAsString(), is("available"));
JsonObject message3 = messages.stream().map(m -> new JsonParser().parse(m.getBody()).getAsJsonObject()).filter(m -> "REFUND_SUBMITTED".equals(m.get("event_type").getAsString())).findFirst().get();
assertThat(message3.get("event_type").getAsString(), is("REFUND_SUBMITTED"));
assertThat(message3.get("resource_external_id").getAsString(), is(refundId));
assertThat(message3.get("service_id").getAsString(), is("external-service-id"));
assertThat(message3.get("live").getAsBoolean(), is(false));
JsonObject message4 = messages.stream().map(m -> new JsonParser().parse(m.getBody()).getAsJsonObject()).filter(m -> "REFUND_SUCCEEDED".equals(m.get("event_type").getAsString())).findFirst().get();
assertThat(message4.get("event_type").getAsString(), is("REFUND_SUCCEEDED"));
assertThat(message4.get("resource_external_id").getAsString(), is(refundId));
assertThat(message4.get("event_details").getAsJsonObject().get("gateway_transaction_id").getAsString(), is(notNullValue()));
assertThat(message4.get("service_id").getAsString(), is("external-service-id"));
assertThat(message4.get("live").getAsBoolean(), is(false));
}
Aggregations