Search in sources :

Example 1 with ChargeResponse

use of uk.gov.pay.connector.charge.model.ChargeResponse 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 ChargeResponse

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

the class ChargeServiceFindTest method shouldNotFindCharge.

@Test
public void shouldNotFindCharge() {
    PaymentOutcome paymentOutcome = new PaymentOutcome("success");
    TelephoneChargeCreateRequest telephoneChargeCreateRequest = telephoneRequestBuilder.withProviderId("new").withPaymentOutcome(paymentOutcome).build();
    Optional<ChargeResponse> telephoneChargeResponse = service.findCharge(1234L, telephoneChargeCreateRequest);
    ArgumentCaptor<String> gatewayTransactionIdArgumentCaptor = forClass(String.class);
    verify(mockedChargeDao).findByGatewayTransactionIdAndAccount(anyLong(), gatewayTransactionIdArgumentCaptor.capture());
    String providerId = gatewayTransactionIdArgumentCaptor.getValue();
    assertThat(providerId, is("new"));
    assertThat(telephoneChargeResponse.isPresent(), is(false));
}
Also used : TelephoneChargeCreateRequest(uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest) PaymentOutcome(uk.gov.pay.connector.charge.model.telephone.PaymentOutcome) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) Test(org.junit.Test)

Example 3 with ChargeResponse

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

the class ChargeServiceFindTest method shouldFindChargeForChargeId_withCorporateSurcharge.

@Test
public void shouldFindChargeForChargeId_withCorporateSurcharge() {
    Long chargeId = 101L;
    Long totalAmount = 1250L;
    ChargeEntity newCharge = aValidChargeEntity().withId(chargeId).withGatewayAccountEntity(gatewayAccount).withStatus(AUTHORISATION_READY).withAmount(1000L).withCorporateSurcharge(250L).build();
    String externalId = newCharge.getExternalId();
    doAnswer(invocation -> fromUri(SERVICE_HOST)).when(this.mockedUriInfo).getBaseUriBuilder();
    when(mockedLinksConfig.getFrontendUrl()).thenReturn("http://frontend.test");
    when(mockedProviders.byName(any(PaymentGatewayName.class))).thenReturn(mockedPaymentProvider);
    when(mockedPaymentProvider.getExternalChargeRefundAvailability(any(Charge.class), any(List.class))).thenReturn(EXTERNAL_AVAILABLE);
    when(mockedChargeDao.findByExternalIdAndGatewayAccount(externalId, GATEWAY_ACCOUNT_ID)).thenReturn(Optional.ofNullable(newCharge));
    Optional<ChargeResponse> chargeResponseForAccount = service.findChargeForAccount(externalId, GATEWAY_ACCOUNT_ID, mockedUriInfo);
    verify(mockedTokenDao).persist(tokenEntityArgumentCaptor.capture());
    assertThat(chargeResponseForAccount.isPresent(), is(true));
    final ChargeResponse chargeResponse = chargeResponseForAccount.get();
    assertThat(chargeResponse.getCorporateCardSurcharge(), is(250L));
    assertThat(chargeResponse.getTotalAmount(), is(totalAmount));
    assertThat(chargeResponse.getRefundSummary().getAmountAvailable(), is(totalAmount));
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Charge(uk.gov.pay.connector.charge.model.domain.Charge) List(java.util.List) PaymentGatewayName(uk.gov.pay.connector.gateway.PaymentGatewayName) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) Test(org.junit.Test)

Example 4 with ChargeResponse

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

the class ChargeServiceFindTest method shouldFindChargeForChargeIdAndAccountIdWithNextUrlWhenChargeStatusIs.

@Test
@Parameters({ "CREATED", "ENTERING_CARD_DETAILS", "AUTHORISATION_READY" })
public void shouldFindChargeForChargeIdAndAccountIdWithNextUrlWhenChargeStatusIs(String status) throws Exception {
    Long chargeId = 101L;
    ChargeEntity newCharge = aValidChargeEntity().withId(chargeId).withGatewayAccountEntity(gatewayAccount).withStatus(ChargeStatus.valueOf(status)).withWalletType(WalletType.APPLE_PAY).build();
    String externalId = newCharge.getExternalId();
    doAnswer(invocation -> fromUri(SERVICE_HOST)).when(this.mockedUriInfo).getBaseUriBuilder();
    when(mockedLinksConfig.getFrontendUrl()).thenReturn("http://frontend.test");
    when(mockedProviders.byName(any(PaymentGatewayName.class))).thenReturn(mockedPaymentProvider);
    when(mockedPaymentProvider.getExternalChargeRefundAvailability(any(Charge.class), any(List.class))).thenReturn(EXTERNAL_AVAILABLE);
    when(mockedChargeDao.findByExternalIdAndGatewayAccount(externalId, GATEWAY_ACCOUNT_ID)).thenReturn(Optional.ofNullable(newCharge));
    Optional<ChargeResponse> chargeResponseForAccount = service.findChargeForAccount(externalId, GATEWAY_ACCOUNT_ID, mockedUriInfo);
    verify(mockedTokenDao).persist(tokenEntityArgumentCaptor.capture());
    TokenEntity tokenEntity = tokenEntityArgumentCaptor.getValue();
    assertThat(tokenEntity.getChargeEntity().getId(), is(newCharge.getId()));
    assertThat(tokenEntity.getToken(), is(notNullValue()));
    ChargeResponse.ChargeResponseBuilder chargeResponseWithoutCorporateCardSurcharge = chargeResponseBuilderOf(newCharge);
    chargeResponseWithoutCorporateCardSurcharge.withWalletType(WalletType.APPLE_PAY);
    chargeResponseWithoutCorporateCardSurcharge.withLink("self", GET, new URI(SERVICE_HOST + "/v1/api/accounts/10/charges/" + externalId));
    chargeResponseWithoutCorporateCardSurcharge.withLink("refunds", GET, new URI(SERVICE_HOST + "/v1/api/accounts/10/charges/" + externalId + "/refunds"));
    chargeResponseWithoutCorporateCardSurcharge.withLink("next_url", GET, new URI("http://frontend.test/secure/" + tokenEntity.getToken()));
    chargeResponseWithoutCorporateCardSurcharge.withLink("next_url_post", POST, new URI("http://frontend.test/secure"), "application/x-www-form-urlencoded", new HashMap<String, Object>() {

        {
            put("chargeTokenId", tokenEntity.getToken());
        }
    });
    assertThat(chargeResponseForAccount.isPresent(), is(true));
    final ChargeResponse chargeResponse = chargeResponseForAccount.get();
    assertThat(chargeResponse.getCorporateCardSurcharge(), is(nullValue()));
    assertThat(chargeResponse.getTotalAmount(), is(nullValue()));
    assertThat(chargeResponse, is(chargeResponseWithoutCorporateCardSurcharge.build()));
    assertThat(chargeResponse.getWalletType(), is(WalletType.APPLE_PAY));
}
Also used : Charge(uk.gov.pay.connector.charge.model.domain.Charge) URI(java.net.URI) ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TokenEntity(uk.gov.pay.connector.token.model.domain.TokenEntity) List(java.util.List) PaymentGatewayName(uk.gov.pay.connector.gateway.PaymentGatewayName) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 5 with ChargeResponse

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

the class ChargeServiceFindTest method shouldFindChargeForChargeId_withFee.

@Test
public void shouldFindChargeForChargeId_withFee() {
    Long chargeId = 101L;
    Long amount = 1000L;
    Long fee = 100L;
    ChargeEntity charge = aValidChargeEntity().withId(chargeId).withGatewayAccountEntity(gatewayAccount).withStatus(CAPTURED).withAmount(amount).withFee(Fee.of(null, fee)).build();
    String externalId = charge.getExternalId();
    doAnswer(invocation -> fromUri(SERVICE_HOST)).when(this.mockedUriInfo).getBaseUriBuilder();
    when(mockedLinksConfig.getFrontendUrl()).thenReturn("http://frontend.test");
    when(mockedProviders.byName(any(PaymentGatewayName.class))).thenReturn(mockedPaymentProvider);
    when(mockedPaymentProvider.getExternalChargeRefundAvailability(any(Charge.class), any(List.class))).thenReturn(EXTERNAL_AVAILABLE);
    when(mockedChargeDao.findByExternalIdAndGatewayAccount(externalId, GATEWAY_ACCOUNT_ID)).thenReturn(Optional.ofNullable(charge));
    Optional<ChargeResponse> chargeResponseForAccount = service.findChargeForAccount(externalId, GATEWAY_ACCOUNT_ID, mockedUriInfo);
    assertThat(chargeResponseForAccount.isPresent(), is(true));
    final ChargeResponse chargeResponse = chargeResponseForAccount.get();
    assertThat(chargeResponse.getNetAmount(), is(amount - fee));
    assertThat(chargeResponse.getAmount(), is(amount));
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) Charge(uk.gov.pay.connector.charge.model.domain.Charge) List(java.util.List) PaymentGatewayName(uk.gov.pay.connector.gateway.PaymentGatewayName) ChargeResponse(uk.gov.pay.connector.charge.model.ChargeResponse) Test(org.junit.Test)

Aggregations

ChargeResponse (uk.gov.pay.connector.charge.model.ChargeResponse)11 Test (org.junit.Test)9 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)7 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)7 List (java.util.List)5 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)5 Charge (uk.gov.pay.connector.charge.model.domain.Charge)5 PaymentOutcome (uk.gov.pay.connector.charge.model.telephone.PaymentOutcome)5 PaymentGatewayName (uk.gov.pay.connector.gateway.PaymentGatewayName)5 URI (java.net.URI)4 TelephoneChargeCreateRequest (uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest)4 Supplemental (uk.gov.pay.connector.charge.model.telephone.Supplemental)2 TokenEntity (uk.gov.pay.connector.token.model.domain.TokenEntity)2 Parameters (junitparams.Parameters)1 CardDetailsEntity (uk.gov.pay.connector.charge.model.CardDetailsEntity)1 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 ExternalMetadata (uk.gov.service.payments.commons.model.charge.ExternalMetadata)1