Search in sources :

Example 1 with CAPTURED

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;
}
Also used : CardExpiryDate(uk.gov.service.payments.commons.model.CardExpiryDate) DefaultExternalRefundAvailabilityCalculator(uk.gov.pay.connector.gateway.util.DefaultExternalRefundAvailabilityCalculator) ZonedDateTime(java.time.ZonedDateTime) CREATED(uk.gov.pay.connector.charge.model.domain.ChargeStatus.CREATED) RefundEntity(uk.gov.pay.connector.refund.model.domain.RefundEntity) PAYMENT_NOTIFICATION_CREATED(uk.gov.pay.connector.charge.model.domain.ChargeStatus.PAYMENT_NOTIFICATION_CREATED) SettlementSummary(uk.gov.pay.connector.client.ledger.model.SettlementSummary) FirstDigitsCardNumber(uk.gov.pay.connector.charge.model.FirstDigitsCardNumber) CardBrandLabelEntity(uk.gov.pay.connector.cardtype.model.domain.CardBrandLabelEntity) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) Charge(uk.gov.pay.connector.charge.model.domain.Charge) CAPTURED(uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURED) TransactionState(uk.gov.pay.connector.client.ledger.model.TransactionState) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) CardDetailsEntity(uk.gov.pay.connector.charge.model.CardDetailsEntity) ExternalChargeRefundAvailability(uk.gov.pay.connector.common.model.api.ExternalChargeRefundAvailability) ChargeEventEntity(uk.gov.pay.connector.chargeevent.model.domain.ChargeEventEntity) CardDetails(uk.gov.pay.connector.client.ledger.model.CardDetails) Address(uk.gov.pay.connector.client.ledger.model.Address) LedgerTransaction(uk.gov.pay.connector.client.ledger.model.LedgerTransaction) Optional.ofNullable(java.util.Optional.ofNullable) CAPTURE_SUBMITTED(uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURE_SUBMITTED) LastDigitsCardNumber(uk.gov.pay.connector.charge.model.LastDigitsCardNumber) Source(uk.gov.service.payments.commons.model.Source) Collectors(java.util.stream.Collectors) ThreeDSecure(uk.gov.pay.connector.client.ledger.model.ThreeDSecure) WalletType(uk.gov.pay.connector.wallets.WalletType) List(java.util.List) AuthorisationSummary(uk.gov.pay.connector.client.ledger.model.AuthorisationSummary) Refund(uk.gov.pay.connector.refund.model.domain.Refund) ISO_INSTANT_MILLISECOND_PRECISION(uk.gov.service.payments.commons.model.ApiResponseDateTimeFormatter.ISO_INSTANT_MILLISECOND_PRECISION) UTC(java.time.ZoneOffset.UTC) DateTimeUtils(uk.gov.pay.connector.util.DateTimeUtils) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ChargeStatus(uk.gov.pay.connector.charge.model.domain.ChargeStatus) SupportedLanguage(uk.gov.service.payments.commons.model.SupportedLanguage) Objects.nonNull(java.util.Objects.nonNull) CorporateCardSurchargeCalculator.getTotalAmountFor(uk.gov.pay.connector.charge.util.CorporateCardSurchargeCalculator.getTotalAmountFor) DefaultExternalRefundAvailabilityCalculator(uk.gov.pay.connector.gateway.util.DefaultExternalRefundAvailabilityCalculator) Address(uk.gov.pay.connector.client.ledger.model.Address) CardBrandLabelEntity(uk.gov.pay.connector.cardtype.model.domain.CardBrandLabelEntity) FirstDigitsCardNumber(uk.gov.pay.connector.charge.model.FirstDigitsCardNumber) AuthorisationSummary(uk.gov.pay.connector.client.ledger.model.AuthorisationSummary) LastDigitsCardNumber(uk.gov.pay.connector.charge.model.LastDigitsCardNumber) CardExpiryDate(uk.gov.service.payments.commons.model.CardExpiryDate) GatewayAccountEntity(uk.gov.pay.connector.gatewayaccount.model.GatewayAccountEntity) ThreeDSecure(uk.gov.pay.connector.client.ledger.model.ThreeDSecure) ExternalChargeRefundAvailability(uk.gov.pay.connector.common.model.api.ExternalChargeRefundAvailability) CardDetails(uk.gov.pay.connector.client.ledger.model.CardDetails) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) CardDetailsEntity(uk.gov.pay.connector.charge.model.CardDetailsEntity)

Example 2 with CAPTURED

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));
}
Also used : JsonObject(com.google.gson.JsonObject) ValidatableResponse(io.restassured.response.ValidatableResponse) ZonedDateTime(java.time.ZonedDateTime) RunWith(org.junit.runner.RunWith) JsonParser(com.google.gson.JsonParser) ReceiveMessageResult(com.amazonaws.services.sqs.model.ReceiveMessageResult) CAPTURED(uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURED) Gson(com.google.gson.Gson) ChargingITestBase(uk.gov.pay.connector.it.base.ChargingITestBase) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) PurgeQueueRequest(com.amazonaws.services.sqs.model.PurgeQueueRequest) ReceiveMessageRequest(com.amazonaws.services.sqs.model.ReceiveMessageRequest) DropwizardJUnitRunner(uk.gov.pay.connector.junit.DropwizardJUnitRunner) Before(org.junit.Before) ConfigOverride(uk.gov.pay.connector.junit.ConfigOverride) ImmutableMap(com.google.common.collect.ImmutableMap) HOURS(java.time.temporal.ChronoUnit.HOURS) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ContentType(io.restassured.http.ContentType) Test(org.junit.Test) Message(com.amazonaws.services.sqs.model.Message) Instant(java.time.Instant) ConnectorApp(uk.gov.pay.connector.app.ConnectorApp) List(java.util.List) AmazonSQS(com.amazonaws.services.sqs.AmazonSQS) DropwizardConfig(uk.gov.pay.connector.junit.DropwizardConfig) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) SYSTEM_CANCELLED(uk.gov.pay.connector.charge.model.domain.ChargeStatus.SYSTEM_CANCELLED) AUTHORISATION_SUCCESS(uk.gov.pay.connector.charge.model.domain.ChargeStatus.AUTHORISATION_SUCCESS) ValidatableResponse(io.restassured.response.ValidatableResponse) Message(com.amazonaws.services.sqs.model.Message) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) JsonParser(com.google.gson.JsonParser) Test(org.junit.Test)

Aggregations

ZonedDateTime (java.time.ZonedDateTime)2 List (java.util.List)2 CAPTURED (uk.gov.pay.connector.charge.model.domain.ChargeStatus.CAPTURED)2 AmazonSQS (com.amazonaws.services.sqs.AmazonSQS)1 Message (com.amazonaws.services.sqs.model.Message)1 PurgeQueueRequest (com.amazonaws.services.sqs.model.PurgeQueueRequest)1 ReceiveMessageRequest (com.amazonaws.services.sqs.model.ReceiveMessageRequest)1 ReceiveMessageResult (com.amazonaws.services.sqs.model.ReceiveMessageResult)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Gson (com.google.gson.Gson)1 JsonObject (com.google.gson.JsonObject)1 JsonParser (com.google.gson.JsonParser)1 ContentType (io.restassured.http.ContentType)1 ValidatableResponse (io.restassured.response.ValidatableResponse)1 Instant (java.time.Instant)1 UTC (java.time.ZoneOffset.UTC)1 HOURS (java.time.temporal.ChronoUnit.HOURS)1 Objects.nonNull (java.util.Objects.nonNull)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1