Search in sources :

Example 1 with WebhooksEvent

use of com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelper method unmarshal.

// body as String
/**
 * Unmarshals the given body, while also validating it using the given request headers.
 * @return The body unmarshalled as a {@link WebhooksEvent}
 * @throws SignatureValidationException If the body could not be validated successfully.
 * @throws ApiVersionMismatchException If the resulting event has an API version that this version of the SDK does not support.
 */
public WebhooksEvent unmarshal(String body, List<RequestHeader> requestHeaders) {
    validate(body, requestHeaders);
    WebhooksEvent event = marshaller.unmarshal(body, WebhooksEvent.class);
    validateApiVersion(event);
    return event;
}
Also used : WebhooksEvent(com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent)

Example 2 with WebhooksEvent

use of com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelperTest method testUnmarshalStringSuccess.

@Test
public void testUnmarshalStringSuccess() throws IOException {
    WebhooksHelper helper = createHelper();
    InMemorySecretKeyStore.INSTANCE.storeSecretKey(KEY_ID, SECRET_KEY);
    String body = new String(readResource("valid-body"), CHARSET);
    List<RequestHeader> requestHeaders = Arrays.asList(new RequestHeader(SIGNATURE_HEADER, SIGNATURE), new RequestHeader(KEY_ID_HEADER, KEY_ID));
    WebhooksEvent event = helper.unmarshal(body, requestHeaders);
    Assert.assertEquals("v1", event.getApiVersion());
    Assert.assertEquals("8ee793f6-4553-4749-85dc-f2ef095c5ab0", event.getId());
    Assert.assertEquals("2017-02-02T11:24:14.040+0100", event.getCreated());
    Assert.assertEquals("20000", event.getMerchantId());
    Assert.assertEquals("payment.paid", event.getType());
    Assert.assertNull(event.getRefund());
    Assert.assertNull(event.getPayout());
    Assert.assertNull(event.getToken());
    Assert.assertNotNull(event.getPayment());
    Assert.assertEquals("00000200000143570012", event.getPayment().getId());
    Assert.assertNotNull(event.getPayment().getPaymentOutput());
    Assert.assertNotNull(event.getPayment().getPaymentOutput().getAmountOfMoney());
    Assert.assertEquals((Long) 1000L, event.getPayment().getPaymentOutput().getAmountOfMoney().getAmount());
    Assert.assertEquals("EUR", event.getPayment().getPaymentOutput().getAmountOfMoney().getCurrencyCode());
    Assert.assertNotNull(event.getPayment().getPaymentOutput().getReferences());
    Assert.assertEquals("200001681810", event.getPayment().getPaymentOutput().getReferences().getPaymentReference());
    Assert.assertEquals("bankTransfer", event.getPayment().getPaymentOutput().getPaymentMethod());
    Assert.assertNull(event.getPayment().getPaymentOutput().getCardPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getCashPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getDirectDebitPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getInvoicePaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getRedirectPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getSepaDirectDebitPaymentMethodSpecificOutput());
    Assert.assertNotNull(event.getPayment().getPaymentOutput().getBankTransferPaymentMethodSpecificOutput());
    Assert.assertEquals((Integer) 11, event.getPayment().getPaymentOutput().getBankTransferPaymentMethodSpecificOutput().getPaymentProductId());
    Assert.assertEquals("PAID", event.getPayment().getStatus());
    Assert.assertNotNull(event.getPayment().getStatusOutput());
    Assert.assertEquals(false, event.getPayment().getStatusOutput().getIsCancellable());
    Assert.assertEquals("COMPLETED", event.getPayment().getStatusOutput().getStatusCategory());
    Assert.assertEquals((Integer) 1000, event.getPayment().getStatusOutput().getStatusCode());
    Assert.assertEquals("20170202112414", event.getPayment().getStatusOutput().getStatusCodeChangeDateTime());
    Assert.assertEquals(true, event.getPayment().getStatusOutput().getIsAuthorized());
}
Also used : WebhooksEvent(com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) Test(org.junit.Test)

Example 3 with WebhooksEvent

use of com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelperTest method testUnmarshalBytesSuccess.

@Test
public void testUnmarshalBytesSuccess() throws IOException {
    WebhooksHelper helper = createHelper();
    InMemorySecretKeyStore.INSTANCE.storeSecretKey(KEY_ID, SECRET_KEY);
    ByteArrayInputStream bodyStream = new ByteArrayInputStream(readResource("valid-body"));
    List<RequestHeader> requestHeaders = Arrays.asList(new RequestHeader(SIGNATURE_HEADER, SIGNATURE), new RequestHeader(KEY_ID_HEADER, KEY_ID));
    WebhooksEvent event = helper.unmarshal(bodyStream, requestHeaders);
    Assert.assertEquals("v1", event.getApiVersion());
    Assert.assertEquals("8ee793f6-4553-4749-85dc-f2ef095c5ab0", event.getId());
    Assert.assertEquals("2017-02-02T11:24:14.040+0100", event.getCreated());
    Assert.assertEquals("20000", event.getMerchantId());
    Assert.assertEquals("payment.paid", event.getType());
    Assert.assertNull(event.getRefund());
    Assert.assertNull(event.getPayout());
    Assert.assertNull(event.getToken());
    Assert.assertNotNull(event.getPayment());
    Assert.assertEquals("00000200000143570012", event.getPayment().getId());
    Assert.assertNotNull(event.getPayment().getPaymentOutput());
    Assert.assertNotNull(event.getPayment().getPaymentOutput().getAmountOfMoney());
    Assert.assertEquals((Long) 1000L, event.getPayment().getPaymentOutput().getAmountOfMoney().getAmount());
    Assert.assertEquals("EUR", event.getPayment().getPaymentOutput().getAmountOfMoney().getCurrencyCode());
    Assert.assertNotNull(event.getPayment().getPaymentOutput().getReferences());
    Assert.assertEquals("200001681810", event.getPayment().getPaymentOutput().getReferences().getPaymentReference());
    Assert.assertEquals("bankTransfer", event.getPayment().getPaymentOutput().getPaymentMethod());
    Assert.assertNull(event.getPayment().getPaymentOutput().getCardPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getCashPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getDirectDebitPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getInvoicePaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getRedirectPaymentMethodSpecificOutput());
    Assert.assertNull(event.getPayment().getPaymentOutput().getSepaDirectDebitPaymentMethodSpecificOutput());
    Assert.assertNotNull(event.getPayment().getPaymentOutput().getBankTransferPaymentMethodSpecificOutput());
    Assert.assertEquals((Integer) 11, event.getPayment().getPaymentOutput().getBankTransferPaymentMethodSpecificOutput().getPaymentProductId());
    Assert.assertEquals("PAID", event.getPayment().getStatus());
    Assert.assertNotNull(event.getPayment().getStatusOutput());
    Assert.assertEquals(false, event.getPayment().getStatusOutput().getIsCancellable());
    Assert.assertEquals("COMPLETED", event.getPayment().getStatusOutput().getStatusCategory());
    Assert.assertEquals((Integer) 1000, event.getPayment().getStatusOutput().getStatusCode());
    Assert.assertEquals("20170202112414", event.getPayment().getStatusOutput().getStatusCodeChangeDateTime());
    Assert.assertEquals(true, event.getPayment().getStatusOutput().getIsAuthorized());
}
Also used : WebhooksEvent(com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) Test(org.junit.Test)

Example 4 with WebhooksEvent

use of com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelperTest method testUnmarshalApiVersionMismatch.

@Test(expected = ApiVersionMismatchException.class)
public void testUnmarshalApiVersionMismatch() throws IOException {
    Marshaller marshaller = Mockito.mock(Marshaller.class);
    Mockito.when(marshaller.unmarshal(Matchers.anyString(), Matchers.<Class<?>>any())).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            String responseJson = invocation.getArgumentAt(0, String.class);
            Class<?> type = invocation.getArgumentAt(1, Class.class);
            WebhooksEvent event = (WebhooksEvent) DefaultMarshaller.INSTANCE.unmarshal(responseJson, type);
            event.setApiVersion("v0");
            return event;
        }
    });
    WebhooksHelper helper = createHelper(marshaller);
    InMemorySecretKeyStore.INSTANCE.storeSecretKey(KEY_ID, SECRET_KEY);
    String body = new String(readResource("valid-body"), CHARSET);
    List<RequestHeader> requestHeaders = Arrays.asList(new RequestHeader(SIGNATURE_HEADER, SIGNATURE), new RequestHeader(KEY_ID_HEADER, KEY_ID));
    helper.unmarshal(body, requestHeaders);
}
Also used : Marshaller(com.ingenico.connect.gateway.sdk.java.Marshaller) DefaultMarshaller(com.ingenico.connect.gateway.sdk.java.defaultimpl.DefaultMarshaller) WebhooksEvent(com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RequestHeader(com.ingenico.connect.gateway.sdk.java.RequestHeader) Test(org.junit.Test)

Example 5 with WebhooksEvent

use of com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent in project connect-sdk-java by Ingenico-ePayments.

the class WebhooksHelper method unmarshal.

// body as byte array
/**
 * Unmarshals the given body, while also validating it using the given request headers.
 * @return The body unmarshalled as a {@link WebhooksEvent}
 * @throws SignatureValidationException If the body could not be validated successfully.
 * @throws ApiVersionMismatchException If the resulting event has an API version that this version of the SDK does not support.
 */
public WebhooksEvent unmarshal(byte[] body, List<RequestHeader> requestHeaders) {
    validate(body, requestHeaders);
    WebhooksEvent event = marshaller.unmarshal(new String(body, CHARSET), WebhooksEvent.class);
    validateApiVersion(event);
    return event;
}
Also used : WebhooksEvent(com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent)

Aggregations

WebhooksEvent (com.ingenico.connect.gateway.sdk.java.domain.webhooks.WebhooksEvent)5 RequestHeader (com.ingenico.connect.gateway.sdk.java.RequestHeader)3 Test (org.junit.Test)3 Marshaller (com.ingenico.connect.gateway.sdk.java.Marshaller)1 DefaultMarshaller (com.ingenico.connect.gateway.sdk.java.defaultimpl.DefaultMarshaller)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1