Search in sources :

Example 1 with ExternalMetadata

use of uk.gov.service.payments.commons.model.charge.ExternalMetadata in project pay-publicapi by alphagov.

the class CreatePaymentServiceTest method testCreatePayment.

@Test
@PactVerification({ "connector" })
@Pacts(pacts = { "publicapi-connector-create-payment" })
public void testCreatePayment() {
    Map<String, Object> metadata = Map.of("ledger_code", 123, "fund_code", "ISIN122038", "cancellable", false);
    var requestPayload = CreateCardPaymentRequestBuilder.builder().amount(100).returnUrl("https://somewhere.gov.uk/rainbow/1").reference("a reference").description("a description").metadata(new ExternalMetadata(metadata)).delayedCapture(Boolean.TRUE).moto(Boolean.TRUE).language(SupportedLanguage.WELSH).email("joe.bogs@example.org").cardholderName("J. Bogs").addressLine1("address line 1").addressLine2("address line 2").city("address city").postcode("AB1 CD2").country("GB").build();
    PaymentWithAllLinks paymentResponse = createPaymentService.create(account, requestPayload);
    CardPayment payment = (CardPayment) paymentResponse.getPayment();
    assertThat(payment.getPaymentId(), is("ch_ab2341da231434l"));
    assertThat(payment.getAmount(), is(100L));
    assertThat(payment.getReference(), is("a reference"));
    assertThat(payment.getDescription(), is("a description"));
    assertThat(payment.getState(), is(new PaymentState("created", false)));
    assertThat(payment.getReturnUrl().get(), is("https://somewhere.gov.uk/rainbow/1"));
    assertThat(payment.getPaymentProvider(), is("Sandbox"));
    assertThat(payment.getCreatedDate(), is("2016-01-01T12:00:00Z"));
    assertThat(paymentResponse.getLinks().getSelf(), is(new Link("http://publicapi.test.localhost/v1/payments/ch_ab2341da231434l", "GET")));
    assertThat(paymentResponse.getLinks().getNextUrl(), is(new Link("http://frontend_connector/charge/token_1234567asdf", "GET")));
    PostLink expectedLink = new PostLink("http://frontend_connector/charge/", "POST", "application/x-www-form-urlencoded", Collections.singletonMap("chargeTokenId", "token_1234567asdf"));
    assertThat(paymentResponse.getLinks().getNextUrlPost(), is(expectedLink));
    assertThat(payment.getMetadata().getMetadata(), is(metadata));
    assertThat(payment.getDelayedCapture(), is(true));
    assertThat(payment.getLanguage(), is(SupportedLanguage.WELSH));
    assertThat(payment.getEmail().isPresent(), is(true));
    assertThat(payment.getEmail().get(), is("joe.bogs@example.org"));
    assertThat(payment.getCardDetails().isPresent(), is(true));
    assertThat(payment.getCardDetails().get().getCardHolderName(), is("J. Bogs"));
    assertThat(payment.getCardDetails().get().getBillingAddress().isPresent(), is(true));
    Address billingAddress = payment.getCardDetails().get().getBillingAddress().get();
    assertThat(billingAddress.getLine1(), is("address line 1"));
    assertThat(billingAddress.getLine2(), is("address line 2"));
    assertThat(billingAddress.getCity(), is("address city"));
    assertThat(billingAddress.getPostcode(), is("AB1 CD2"));
    assertThat(billingAddress.getCountry(), is("GB"));
}
Also used : CardPayment(uk.gov.pay.api.model.CardPayment) Address(uk.gov.pay.api.model.Address) PaymentWithAllLinks(uk.gov.pay.api.model.links.PaymentWithAllLinks) PaymentState(uk.gov.pay.api.model.PaymentState) ExternalMetadata(uk.gov.service.payments.commons.model.charge.ExternalMetadata) Link(uk.gov.pay.api.model.links.Link) PostLink(uk.gov.pay.api.model.links.PostLink) PostLink(uk.gov.pay.api.model.links.PostLink) PactVerification(au.com.dius.pact.consumer.PactVerification) Test(org.junit.Test) Pacts(uk.gov.service.payments.commons.testing.pact.consumers.Pacts)

Example 2 with ExternalMetadata

use of uk.gov.service.payments.commons.model.charge.ExternalMetadata in project pay-connector by alphagov.

the class ChargeDaoIT method shouldCreateANewChargeWithExternalMetadata.

@Test
public void shouldCreateANewChargeWithExternalMetadata() {
    ExternalMetadata expectedExternalMetadata = new ExternalMetadata(Map.of("key1", "String1", "key2", 123, "key3", true));
    ChargeEntity chargeEntity = aValidChargeEntity().withGatewayAccountEntity(gatewayAccount).withGatewayAccountCredentialsEntity(gatewayAccountCredentialsEntity).withExternalMetadata(expectedExternalMetadata).build();
    chargeDao.persist(chargeEntity);
    chargeDao.forceRefresh(chargeEntity);
    Optional<ChargeEntity> charge = chargeDao.findById(chargeEntity.getId());
    assertThat(charge.get().getExternalMetadata().get().getMetadata(), equalTo(expectedExternalMetadata.getMetadata()));
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ExternalMetadata(uk.gov.service.payments.commons.model.charge.ExternalMetadata) Test(org.junit.Test)

Example 3 with ExternalMetadata

use of uk.gov.service.payments.commons.model.charge.ExternalMetadata in project pay-connector by alphagov.

the class ChargeDaoIT method shouldNotSaveChargeWithInvalidExternalMetadata.

@Test
public void shouldNotSaveChargeWithInvalidExternalMetadata() {
    var metaDataWithAnObject = new ExternalMetadata(Map.of("key_1", Map.of("key_1_a", "some value")));
    ChargeEntity chargeEntity = aValidChargeEntity().withExternalMetadata(metaDataWithAnObject).build();
    try {
        chargeDao.persist(chargeEntity);
        fail("Persist should throw a ConstraintViolationException");
    } catch (ConstraintViolationException ex) {
        assertThat(ex.getConstraintViolations().size(), is(1));
        assertThat(ex.getConstraintViolations().iterator().next().getMessage(), is("Field [metadata] values must be of type String, Boolean or Number"));
    }
}
Also used : ChargeEntityFixture.aValidChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity) ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ConstraintViolationException(javax.validation.ConstraintViolationException) ExternalMetadata(uk.gov.service.payments.commons.model.charge.ExternalMetadata) Test(org.junit.Test)

Example 4 with ExternalMetadata

use of uk.gov.service.payments.commons.model.charge.ExternalMetadata in project pay-connector by alphagov.

the class ChargeService method storeExtraFieldsInMetaData.

private ExternalMetadata storeExtraFieldsInMetaData(TelephoneChargeCreateRequest telephoneChargeRequest) {
    HashMap<String, Object> telephoneJSON = new HashMap<>();
    String processorId = telephoneChargeRequest.getProcessorId();
    telephoneJSON.put("processor_id", checkAndGetTruncatedValue(processorId, "processor_id", processorId));
    telephoneJSON.put("status", telephoneChargeRequest.getPaymentOutcome().getStatus());
    telephoneChargeRequest.getCreatedDate().ifPresent(createdDate -> telephoneJSON.put("created_date", createdDate));
    telephoneChargeRequest.getAuthorisedDate().ifPresent(authorisedDate -> telephoneJSON.put("authorised_date", authorisedDate));
    telephoneChargeRequest.getAuthCode().ifPresent(authCode -> telephoneJSON.put("auth_code", checkAndGetTruncatedValue(processorId, "auth_code", authCode)));
    telephoneChargeRequest.getTelephoneNumber().ifPresent(telephoneNumber -> telephoneJSON.put("telephone_number", checkAndGetTruncatedValue(processorId, "telephone_number", telephoneNumber)));
    telephoneChargeRequest.getPaymentOutcome().getCode().ifPresent(code -> telephoneJSON.put("code", code));
    telephoneChargeRequest.getPaymentOutcome().getSupplemental().ifPresent(supplemental -> {
        supplemental.getErrorCode().ifPresent(errorCode -> telephoneJSON.put("error_code", checkAndGetTruncatedValue(processorId, "error_code", errorCode)));
        supplemental.getErrorMessage().ifPresent(errorMessage -> telephoneJSON.put("error_message", checkAndGetTruncatedValue(processorId, "error_message", errorMessage)));
    });
    return new ExternalMetadata(telephoneJSON);
}
Also used : HashMap(java.util.HashMap) ChargeStatus.fromString(uk.gov.pay.connector.charge.model.domain.ChargeStatus.fromString) ExternalMetadata(uk.gov.service.payments.commons.model.charge.ExternalMetadata)

Example 5 with ExternalMetadata

use of uk.gov.service.payments.commons.model.charge.ExternalMetadata in project pay-connector by alphagov.

the class PaymentNotificationCreatedTest method whenAllTheDataIsAvailable.

@Test
void whenAllTheDataIsAvailable() throws JsonProcessingException {
    ExternalMetadata externalMetadata = new ExternalMetadata(Map.of("processor_id", "processorID", "auth_code", "012345", "telephone_number", "+447700900796"));
    chargeEntityFixture.withExternalMetadata(externalMetadata).withCardLabelEntity("Visa", "visa");
    ChargeEventEntity chargeEvent = mock(ChargeEventEntity.class);
    ChargeEntity chargeEntity = chargeEntityFixture.build();
    when(chargeEvent.getChargeEntity()).thenReturn(chargeEntity);
    String actual = PaymentNotificationCreated.from(chargeEvent).toJsonString();
    assertThat(actual, hasJsonPath("$.event_type", equalTo("PAYMENT_NOTIFICATION_CREATED")));
    assertThat(actual, hasJsonPath("$.resource_type", equalTo("payment")));
    assertThat(actual, hasJsonPath("$.resource_external_id", equalTo(chargeEntity.getExternalId())));
    assertThat(actual, hasJsonPath("$.event_details.amount", equalTo(100)));
    assertThat(actual, hasJsonPath("$.event_details.live", equalTo(false)));
    assertThat(actual, hasJsonPath("$.event_details.description", equalTo("This is a description")));
    assertThat(actual, hasJsonPath("$.event_details.email", equalTo("test@email.invalid")));
    assertThat(actual, hasJsonPath("$.event_details.card_brand", equalTo("visa")));
    assertThat(actual, hasJsonPath("$.event_details.card_brand_label", equalTo("Visa")));
    assertThat(actual, hasJsonPath("$.event_details.gateway_transaction_id", equalTo(providerId)));
    assertThat(actual, hasJsonPath("$.event_details.first_digits_card_number", equalTo("424242")));
    assertThat(actual, hasJsonPath("$.event_details.last_digits_card_number", equalTo("4242")));
    assertThat(actual, hasJsonPath("$.event_details.cardholder_name", equalTo("Mr Test")));
    assertThat(actual, hasJsonPath("$.event_details.expiry_date", equalTo("12/99")));
    assertThat(actual, hasJsonPath("$.event_details.payment_provider", equalTo("sandbox")));
    assertThat(actual, hasJsonPath("$.event_details.source", equalTo("CARD_EXTERNAL_TELEPHONE")));
    assertThat(actual, hasJsonPath("$.event_details.external_metadata.processor_id", equalTo("processorID")));
    assertThat(actual, hasJsonPath("$.event_details.external_metadata.auth_code", equalTo("012345")));
    assertThat(actual, hasJsonPath("$.event_details.external_metadata.telephone_number", equalTo("+447700900796")));
    assertThat(actual, hasJsonPath("$.event_details.credential_external_id", equalTo(chargeEntity.getGatewayAccountCredentialsEntity().getExternalId())));
    assertThat(actual, hasJsonPath("$.event_details.gateway_account_id", equalTo(chargeEntity.getGatewayAccount().getId().intValue())));
}
Also used : ChargeEntity(uk.gov.pay.connector.charge.model.domain.ChargeEntity) ChargeEventEntity(uk.gov.pay.connector.chargeevent.model.domain.ChargeEventEntity) ExternalMetadata(uk.gov.service.payments.commons.model.charge.ExternalMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

ExternalMetadata (uk.gov.service.payments.commons.model.charge.ExternalMetadata)13 Test (org.junit.Test)7 ChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntity)6 ChargeEntityFixture.aValidChargeEntity (uk.gov.pay.connector.charge.model.domain.ChargeEntityFixture.aValidChargeEntity)5 PactVerifyProvider (au.com.dius.pact.provider.PactVerifyProvider)2 TelephoneChargeCreateRequest (uk.gov.pay.connector.charge.model.telephone.TelephoneChargeCreateRequest)2 ChargeEventEntity (uk.gov.pay.connector.chargeevent.model.domain.ChargeEventEntity)2 PactVerification (au.com.dius.pact.consumer.PactVerification)1 State (au.com.dius.pact.provider.junit.State)1 JsonObject (com.google.gson.JsonObject)1 ValidatableResponse (io.restassured.response.ValidatableResponse)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ConstraintViolation (javax.validation.ConstraintViolation)1 ConstraintViolationException (javax.validation.ConstraintViolationException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Test (org.junit.jupiter.api.Test)1 Address (uk.gov.pay.api.model.Address)1 CardPayment (uk.gov.pay.api.model.CardPayment)1