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;
}
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));
}
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));
}
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));
}
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));
}
Aggregations