Search in sources :

Example 1 with Supplemental

use of uk.gov.pay.connector.charge.model.telephone.Supplemental in project pay-connector by alphagov.

the class ChargeService method populateResponseBuilderWith.

private ChargeResponse.ChargeResponseBuilder populateResponseBuilderWith(AbstractChargeResponseBuilder<ChargeResponse.ChargeResponseBuilder, ChargeResponse> responseBuilder, ChargeEntity chargeEntity) {
    PersistedCard persistedCard = null;
    if (chargeEntity.getCardDetails() != null) {
        persistedCard = chargeEntity.getCardDetails().toCard();
    }
    ChargeResponse.ChargeResponseBuilder builderOfResponse = responseBuilder.withAmount(chargeEntity.getAmount()).withReference(chargeEntity.getReference()).withDescription(chargeEntity.getDescription()).withProviderId(chargeEntity.getGatewayTransactionId()).withCardDetails(persistedCard).withEmail(chargeEntity.getEmail()).withChargeId(chargeEntity.getExternalId());
    chargeEntity.getExternalMetadata().ifPresent(externalMetadata -> {
        final PaymentOutcome paymentOutcome = new PaymentOutcome(externalMetadata.getMetadata().get("status").toString());
        ExternalTransactionState state;
        if (externalMetadata.getMetadata().get("status").toString().equals("success")) {
            state = new ExternalTransactionState(externalMetadata.getMetadata().get("status").toString(), true);
        } else {
            String message = Stream.of(ExternalChargeState.values()).filter(chargeState -> chargeState.getCode() != null).collect(Collectors.toMap(ExternalChargeState::getCode, ExternalChargeState::getMessage)).get(externalMetadata.getMetadata().get("code").toString());
            state = new ExternalTransactionState(externalMetadata.getMetadata().get("status").toString(), true, externalMetadata.getMetadata().get("code").toString(), message);
            paymentOutcome.setCode(externalMetadata.getMetadata().get("code").toString());
        }
        if (externalMetadata.getMetadata().get("error_code") != null || externalMetadata.getMetadata().get("error_message") != null) {
            paymentOutcome.setSupplemental(new Supplemental((String) externalMetadata.getMetadata().get("error_code"), (String) externalMetadata.getMetadata().get("error_message")));
        }
        if (externalMetadata.getMetadata().get("authorised_date") != null) {
            builderOfResponse.withAuthorisedDate(ZonedDateTime.parse(((String) externalMetadata.getMetadata().get("authorised_date"))).toInstant());
        }
        if (externalMetadata.getMetadata().get("created_date") != null) {
            builderOfResponse.withCreatedDate(ZonedDateTime.parse(((String) externalMetadata.getMetadata().get("created_date"))).toInstant());
        }
        builderOfResponse.withProcessorId((String) externalMetadata.getMetadata().get("processor_id")).withAuthCode((String) externalMetadata.getMetadata().get("auth_code")).withTelephoneNumber((String) externalMetadata.getMetadata().get("telephone_number")).withState(state).withPaymentOutcome(paymentOutcome);
    });
    return builderOfResponse;
}
Also used : PersistedCard(uk.gov.pay.connector.charge.model.domain.PersistedCard) Supplemental(uk.gov.pay.connector.charge.model.telephone.Supplemental) PaymentOutcome(uk.gov.pay.connector.charge.model.telephone.PaymentOutcome) ExternalChargeState(uk.gov.pay.connector.common.model.api.ExternalChargeState) ExternalTransactionState(uk.gov.pay.connector.common.model.api.ExternalTransactionState) ChargeStatus.fromString(uk.gov.pay.connector.charge.model.domain.ChargeStatus.fromString) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse)

Example 2 with Supplemental

use of uk.gov.pay.connector.charge.model.telephone.Supplemental in project pay-connector by alphagov.

the class PaymentOutcomeValidatorTest method failsValidationForInvalidErrorCode.

@Test
public void failsValidationForInvalidErrorCode() {
    PaymentOutcome paymentOutcome = new PaymentOutcome("failed", "error", new Supplemental("ECKOH01234", "textual message describing error code"));
    TelephoneChargeCreateRequest telephoneChargeCreateRequest = telephoneRequestBuilder.withPaymentOutcome(paymentOutcome).build();
    Set<ConstraintViolation<TelephoneChargeCreateRequest>> constraintViolations = validator.validate(telephoneChargeCreateRequest);
    assertThat(constraintViolations.size(), isNumber(1));
    assertThat(constraintViolations.iterator().next().getMessage(), is("Field [payment_outcome] must include a valid status and error code"));
}
Also used : Supplemental(uk.gov.pay.connector.charge.model.telephone.Supplemental) TelephoneChargeCreateRequest(uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest) PaymentOutcome(uk.gov.pay.connector.charge.model.telephone.PaymentOutcome) ConstraintViolation(javax.validation.ConstraintViolation) Test(org.junit.Test)

Example 3 with Supplemental

use of uk.gov.pay.connector.charge.model.telephone.Supplemental in project pay-connector by alphagov.

the class ChargeServiceCreateTest method shouldCreateATelephoneChargeForFailureCodeOfP0050.

@Test
public void shouldCreateATelephoneChargeForFailureCodeOfP0050() {
    when(mockGatewayAccountCredentialsService.getCurrentOrActiveCredential(gatewayAccount)).thenReturn(gatewayAccountCredentialsEntity);
    Supplemental supplemental = new Supplemental("ECKOH01234", "textual message describing error code");
    PaymentOutcome paymentOutcome = new PaymentOutcome("failed", "P0050", supplemental);
    Map<String, Object> metadata = Map.of("created_date", "2018-02-21T16:04:25Z", "authorised_date", "2018-02-21T16:05:33Z", "processor_id", "1PROC", "auth_code", "666", "telephone_number", "+447700900796", "status", "failed", "code", "P0050", "error_code", "ECKOH01234", "error_message", "textual message describing error code");
    TelephoneChargeCreateRequest telephoneChargeCreateRequest = telephoneRequestBuilder.withPaymentOutcome(paymentOutcome).withCardExpiry(null).build();
    populateChargeEntity();
    service.createFromTelephonePaymentNotification(telephoneChargeCreateRequest, gatewayAccount);
    verify(mockedChargeDao).persist(chargeEntityArgumentCaptor.capture());
    ChargeEntity createdChargeEntity = chargeEntityArgumentCaptor.getValue();
    assertThat(createdChargeEntity.getId(), is(CHARGE_ENTITY_ID));
    assertThat(createdChargeEntity.getGatewayAccount().getId(), is(GATEWAY_ACCOUNT_ID));
    assertThat(createdChargeEntity.getExternalId(), is(EXTERNAL_CHARGE_ID[0]));
    assertThat(createdChargeEntity.getGatewayAccount().getGatewayName(), is("sandbox"));
    assertThat(createdChargeEntity.getPaymentProvider(), is("sandbox"));
    assertThat(createdChargeEntity.getAmount(), is(100L));
    assertThat(createdChargeEntity.getReference(), is(ServicePaymentReference.of("Some reference")));
    assertThat(createdChargeEntity.getDescription(), is("Some description"));
    assertThat(createdChargeEntity.getStatus(), is("AUTHORISATION ERROR"));
    assertThat(createdChargeEntity.getEmail(), is("jane.doe@example.com"));
    assertThat(ZonedDateTime.ofInstant(createdChargeEntity.getCreatedDate(), ZoneOffset.UTC), is(ZonedDateTimeMatchers.within(3, ChronoUnit.SECONDS, now(ZoneOffset.UTC))));
    assertThat(createdChargeEntity.getCardDetails().getLastDigitsCardNumber().toString(), is("1234"));
    assertThat(createdChargeEntity.getCardDetails().getFirstDigitsCardNumber().toString(), is("123456"));
    assertThat(createdChargeEntity.getCardDetails().getCardHolderName(), is("Jane Doe"));
    assertThat(createdChargeEntity.getCardDetails().getExpiryDate(), is(nullValue()));
    assertThat(createdChargeEntity.getCardDetails().getCardBrand(), is("visa"));
    assertThat(createdChargeEntity.getGatewayTransactionId(), is("1PROV"));
    assertThat(createdChargeEntity.getExternalMetadata().get().getMetadata(), equalTo(metadata));
    assertThat(createdChargeEntity.getLanguage(), is(SupportedLanguage.ENGLISH));
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Supplemental(uk.gov.pay.connector.charge.model.telephone.Supplemental) TelephoneChargeCreateRequest(uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest) PaymentOutcome(uk.gov.pay.connector.charge.model.telephone.PaymentOutcome) Test(org.junit.Test)

Example 4 with Supplemental

use of uk.gov.pay.connector.charge.model.telephone.Supplemental in project pay-connector by alphagov.

the class ChargeServiceCreateTest method shouldCreateATelephoneChargeAndNotTruncateMetaDataOf50Characters.

@Test
public void shouldCreateATelephoneChargeAndNotTruncateMetaDataOf50Characters() {
    when(mockGatewayAccountCredentialsService.getCurrentOrActiveCredential(gatewayAccount)).thenReturn(gatewayAccountCredentialsEntity);
    String stringOf50 = StringUtils.repeat("*", 50);
    Supplemental supplemental = new Supplemental(stringOf50, stringOf50);
    PaymentOutcome paymentOutcome = new PaymentOutcome("failed", "P0050", supplemental);
    Map<String, Object> metadata = Map.of("created_date", "2018-02-21T16:04:25Z", "authorised_date", "2018-02-21T16:05:33Z", "processor_id", stringOf50, "auth_code", stringOf50, "telephone_number", stringOf50, "status", "failed", "code", "P0050", "error_code", stringOf50, "error_message", stringOf50);
    TelephoneChargeCreateRequest telephoneChargeCreateRequest = telephoneRequestBuilder.withPaymentOutcome(paymentOutcome).withProcessorId(stringOf50).withAuthCode(stringOf50).withTelephoneNumber(stringOf50).build();
    populateChargeEntity();
    service.createFromTelephonePaymentNotification(telephoneChargeCreateRequest, gatewayAccount);
    verify(mockedChargeDao).persist(chargeEntityArgumentCaptor.capture());
    ChargeEntity createdChargeEntity = chargeEntityArgumentCaptor.getValue();
    assertThat(createdChargeEntity.getId(), is(CHARGE_ENTITY_ID));
    assertThat(createdChargeEntity.getGatewayAccount().getId(), is(GATEWAY_ACCOUNT_ID));
    assertThat(createdChargeEntity.getExternalId(), is(EXTERNAL_CHARGE_ID[0]));
    assertThat(createdChargeEntity.getGatewayAccount().getGatewayName(), is("sandbox"));
    assertThat(createdChargeEntity.getPaymentProvider(), is("sandbox"));
    assertThat(createdChargeEntity.getAmount(), is(100L));
    assertThat(createdChargeEntity.getReference(), is(ServicePaymentReference.of("Some reference")));
    assertThat(createdChargeEntity.getDescription(), is("Some description"));
    assertThat(createdChargeEntity.getStatus(), is("AUTHORISATION ERROR"));
    assertThat(createdChargeEntity.getEmail(), is("jane.doe@example.com"));
    assertThat(ZonedDateTime.ofInstant(createdChargeEntity.getCreatedDate(), ZoneOffset.UTC), is(ZonedDateTimeMatchers.within(3, ChronoUnit.SECONDS, ZonedDateTime.now(ZoneOffset.UTC))));
    assertThat(createdChargeEntity.getCardDetails().getLastDigitsCardNumber().toString(), is("1234"));
    assertThat(createdChargeEntity.getCardDetails().getFirstDigitsCardNumber().toString(), is("123456"));
    assertThat(createdChargeEntity.getCardDetails().getCardHolderName(), is("Jane Doe"));
    assertThat(createdChargeEntity.getCardDetails().getExpiryDate().toString(), is("01/19"));
    assertThat(createdChargeEntity.getCardDetails().getCardBrand(), is("visa"));
    assertThat(createdChargeEntity.getGatewayTransactionId(), is("1PROV"));
    assertThat(createdChargeEntity.getExternalMetadata().get().getMetadata(), equalTo(metadata));
    assertThat(createdChargeEntity.getLanguage(), is(SupportedLanguage.ENGLISH));
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Supplemental(uk.gov.pay.connector.charge.model.telephone.Supplemental) TelephoneChargeCreateRequest(uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest) PaymentOutcome(uk.gov.pay.connector.charge.model.telephone.PaymentOutcome) Test(org.junit.Test)

Example 5 with Supplemental

use of uk.gov.pay.connector.charge.model.telephone.Supplemental in project pay-connector by alphagov.

the class ChargeServiceCreateTest method shouldCreateATelephoneChargeAndTruncateMetaDataOver50Characters.

@Test
public void shouldCreateATelephoneChargeAndTruncateMetaDataOver50Characters() {
    when(mockGatewayAccountCredentialsService.getCurrentOrActiveCredential(gatewayAccount)).thenReturn(gatewayAccountCredentialsEntity);
    String stringGreaterThan50 = StringUtils.repeat("*", 51);
    String stringOf50 = StringUtils.repeat("*", 50);
    Supplemental supplemental = new Supplemental(stringGreaterThan50, stringGreaterThan50);
    PaymentOutcome paymentOutcome = new PaymentOutcome("failed", "P0050", supplemental);
    Map<String, Object> metadata = Map.of("created_date", "2018-02-21T16:04:25Z", "authorised_date", "2018-02-21T16:05:33Z", "processor_id", stringOf50, "auth_code", stringOf50, "telephone_number", stringOf50, "status", "failed", "code", "P0050", "error_code", stringOf50, "error_message", stringOf50);
    TelephoneChargeCreateRequest telephoneChargeCreateRequest = telephoneRequestBuilder.withPaymentOutcome(paymentOutcome).withProcessorId(stringGreaterThan50).withAuthCode(stringGreaterThan50).withTelephoneNumber(stringGreaterThan50).build();
    populateChargeEntity();
    service.createFromTelephonePaymentNotification(telephoneChargeCreateRequest, gatewayAccount);
    verify(mockedChargeDao).persist(chargeEntityArgumentCaptor.capture());
    ChargeEntity createdChargeEntity = chargeEntityArgumentCaptor.getValue();
    assertThat(createdChargeEntity.getId(), is(CHARGE_ENTITY_ID));
    assertThat(createdChargeEntity.getGatewayAccount().getId(), is(GATEWAY_ACCOUNT_ID));
    assertThat(createdChargeEntity.getExternalId(), is(EXTERNAL_CHARGE_ID[0]));
    assertThat(createdChargeEntity.getGatewayAccount().getGatewayName(), is("sandbox"));
    assertThat(createdChargeEntity.getPaymentProvider(), is("sandbox"));
    assertThat(createdChargeEntity.getAmount(), is(100L));
    assertThat(createdChargeEntity.getReference(), is(ServicePaymentReference.of("Some reference")));
    assertThat(createdChargeEntity.getDescription(), is("Some description"));
    assertThat(createdChargeEntity.getStatus(), is("AUTHORISATION ERROR"));
    assertThat(createdChargeEntity.getEmail(), is("jane.doe@example.com"));
    assertThat(ZonedDateTime.ofInstant(createdChargeEntity.getCreatedDate(), ZoneOffset.UTC), is(ZonedDateTimeMatchers.within(3, ChronoUnit.SECONDS, ZonedDateTime.now(ZoneOffset.UTC))));
    assertThat(createdChargeEntity.getCardDetails().getLastDigitsCardNumber().toString(), is("1234"));
    assertThat(createdChargeEntity.getCardDetails().getFirstDigitsCardNumber().toString(), is("123456"));
    assertThat(createdChargeEntity.getCardDetails().getCardHolderName(), is("Jane Doe"));
    assertThat(createdChargeEntity.getCardDetails().getExpiryDate().toString(), is("01/19"));
    assertThat(createdChargeEntity.getCardDetails().getCardBrand(), is("visa"));
    assertThat(createdChargeEntity.getGatewayTransactionId(), is("1PROV"));
    assertThat(createdChargeEntity.getExternalMetadata().get().getMetadata(), equalTo(metadata));
    assertThat(createdChargeEntity.getLanguage(), is(SupportedLanguage.ENGLISH));
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) Supplemental(uk.gov.pay.connector.charge.model.telephone.Supplemental) TelephoneChargeCreateRequest(uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest) PaymentOutcome(uk.gov.pay.connector.charge.model.telephone.PaymentOutcome) Test(org.junit.Test)

Aggregations

PaymentOutcome (uk.gov.pay.connector.charge.model.telephone.PaymentOutcome)9 Supplemental (uk.gov.pay.connector.charge.model.telephone.Supplemental)9 Test (org.junit.Test)8 TelephoneChargeCreateRequest (uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest)8 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)4 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)4 ConstraintViolation (javax.validation.ConstraintViolation)3 ChargeResponse (uk.gov.pay.connector.charge.model.ChargeResponse)2 ChargeStatus.fromString (uk.gov.pay.connector.charge.model.domain.ChargeStatus.fromString)1 PersistedCard (uk.gov.pay.connector.charge.model.domain.PersistedCard)1 ExternalChargeState (uk.gov.pay.connector.common.model.api.ExternalChargeState)1 ExternalTransactionState (uk.gov.pay.connector.common.model.api.ExternalTransactionState)1