Search in sources :

Example 6 with CreatePaymentRequest

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

the class DefaultConnectionIdempotenceTest method testIdempotenceFirstFailure.

@Test
public void testIdempotenceFirstFailure() throws Exception {
    String body = getResource("idempotence_rejected.json");
    Map<String, String> responseHeaders = new HashMap<String, String>();
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 402, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    final String idempotenceKey = UUID.randomUUID().toString();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        client.merchant("20000").payments().create(request, context);
        Assert.fail("Expected DeclinedPaymentException");
    } catch (DeclinedPaymentException e) {
        Assert.assertEquals(402, e.getStatusCode());
        Assert.assertEquals(body, e.getResponseBody());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertNull(context.getIdempotenceRequestTimestamp());
}
Also used : DeclinedPaymentException(com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Example 7 with CreatePaymentRequest

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

the class IdempotenceTest method test.

/**
 * Smoke test for idempotence.
 */
@Test
public void test() throws URISyntaxException, IOException {
    CreatePaymentRequest body = new CreatePaymentRequest();
    Order order = new Order();
    AmountOfMoney amountOfMoney = new AmountOfMoney();
    amountOfMoney.setCurrencyCode("EUR");
    amountOfMoney.setAmount(100L);
    order.setAmountOfMoney(amountOfMoney);
    Customer customer = new Customer();
    customer.setLocale("en");
    Address billingAddress = new Address();
    billingAddress.setCountryCode("NL");
    customer.setBillingAddress(billingAddress);
    order.setCustomer(customer);
    body.setOrder(order);
    RedirectPaymentMethodSpecificInput paymentMethodSpecificInput = new RedirectPaymentMethodSpecificInput();
    paymentMethodSpecificInput.setReturnUrl("http://example.com/");
    paymentMethodSpecificInput.setPaymentProductId(809);
    RedirectPaymentProduct809SpecificInput paymentProductSpecificInput = new RedirectPaymentProduct809SpecificInput();
    paymentProductSpecificInput.setIssuerId("INGBNL2A");
    paymentMethodSpecificInput.setPaymentProduct809SpecificInput(paymentProductSpecificInput);
    body.setRedirectPaymentMethodSpecificInput(paymentMethodSpecificInput);
    String idempotenceKey = UUID.randomUUID().toString();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = getClient();
    try {
        CreatePaymentResponse response = client.merchant("20000").payments().create(body, context);
        String paymentId = response.getPayment().getId();
        Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
        Assert.assertNull(context.getIdempotenceRequestTimestamp());
        response = client.merchant("20000").payments().create(body, context);
        Assert.assertEquals(paymentId, response.getPayment().getId());
        Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
        Assert.assertNotNull(context.getIdempotenceRequestTimestamp());
    } finally {
        client.close();
    }
}
Also used : Order(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order) RedirectPaymentMethodSpecificInput(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.RedirectPaymentMethodSpecificInput) Address(com.ingenico.connect.gateway.sdk.java.domain.definitions.Address) Customer(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) RedirectPaymentProduct809SpecificInput(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.RedirectPaymentProduct809SpecificInput) Client(com.ingenico.connect.gateway.sdk.java.Client) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) AmountOfMoney(com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney) Test(org.junit.Test)

Example 8 with CreatePaymentRequest

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

the class DefaultConnectionLoggerTest method testCreatePayment.

@Test
public void testCreatePayment() throws Exception {
    // POST with a success (201) response
    serverBootstrap.registerHandler("/v1/1234/payments", requestHandler);
    HttpHost host = start();
    Client client = createClient(host);
    TestLogger logger = new TestLogger();
    client.enableLogging(logger);
    setupRequestHandler(setCreatedJsonResponse("createPayment.json", "http://localhost/v1/1234/payments/000000123410000595980000100001"));
    try {
        AmountOfMoney amountOfMoney = new AmountOfMoney();
        amountOfMoney.setCurrencyCode("CAD");
        amountOfMoney.setAmount(2345L);
        Address billingAddress = new Address();
        billingAddress.setCountryCode("CA");
        Customer customer = new Customer();
        customer.setBillingAddress(billingAddress);
        Order order = new Order();
        order.setAmountOfMoney(amountOfMoney);
        order.setCustomer(customer);
        Card card = new Card();
        card.setCvv("123");
        card.setCardNumber("1234567890123456");
        card.setExpiryDate("1220");
        CardPaymentMethodSpecificInput paymentMethodSpecificInput = new CardPaymentMethodSpecificInput();
        paymentMethodSpecificInput.setPaymentProductId(1);
        paymentMethodSpecificInput.setCard(card);
        CreatePaymentRequest request = new CreatePaymentRequest();
        request.setOrder(order);
        request.setCardPaymentMethodSpecificInput(paymentMethodSpecificInput);
        CreatePaymentResponse response = client.merchant("1234").payments().create(request);
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getPayment());
        Assert.assertNotNull(response.getPayment().getId());
    } finally {
        client.close();
    }
    Assert.assertEquals(2, logger.entries.size());
    TestLoggerEntry requestEntry = logger.entries.get(0);
    Assert.assertNotNull(requestEntry.message);
    Assert.assertNull(requestEntry.thrown);
    TestLoggerEntry responseEntry = logger.entries.get(1);
    Assert.assertNotNull(responseEntry.message);
    Assert.assertNull(responseEntry.thrown);
    assertRequestAndResponse(requestEntry.message, responseEntry.message, "createPayment");
}
Also used : Order(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order) Address(com.ingenico.connect.gateway.sdk.java.domain.definitions.Address) Customer(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) CardPaymentMethodSpecificInput(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.CardPaymentMethodSpecificInput) AmountOfMoney(com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney) Card(com.ingenico.connect.gateway.sdk.java.domain.definitions.Card) Test(org.junit.Test)

Example 9 with CreatePaymentRequest

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

the class DefaultConnectionLoggerTest method testCreatePaymentInvalidCardNumber.

@Test
public void testCreatePaymentInvalidCardNumber() throws Exception {
    // an exception is thrown after logging the response
    serverBootstrap.registerHandler("/v1/1234/payments", requestHandler);
    HttpHost host = start();
    Client client = createClient(host);
    TestLogger logger = new TestLogger();
    client.enableLogging(logger);
    setupRequestHandler(setJsonResponse("createPayment.failure.invalidCardNumber.json", 400));
    try {
        AmountOfMoney amountOfMoney = new AmountOfMoney();
        amountOfMoney.setCurrencyCode("CAD");
        amountOfMoney.setAmount(2345L);
        Address billingAddress = new Address();
        billingAddress.setCountryCode("CA");
        Customer customer = new Customer();
        customer.setBillingAddress(billingAddress);
        Order order = new Order();
        order.setAmountOfMoney(amountOfMoney);
        order.setCustomer(customer);
        Card card = new Card();
        card.setCvv("123");
        card.setCardNumber("1234567890123456");
        card.setExpiryDate("1220");
        CardPaymentMethodSpecificInput paymentMethodSpecificInput = new CardPaymentMethodSpecificInput();
        paymentMethodSpecificInput.setPaymentProductId(1);
        paymentMethodSpecificInput.setCard(card);
        CreatePaymentRequest request = new CreatePaymentRequest();
        request.setOrder(order);
        request.setCardPaymentMethodSpecificInput(paymentMethodSpecificInput);
        client.merchant("1234").payments().create(request);
        Assert.fail("expected ValidationException");
    } catch (ValidationException e) {
    // expected
    } finally {
        client.close();
    }
    Assert.assertEquals(2, logger.entries.size());
    TestLoggerEntry requestEntry = logger.entries.get(0);
    Assert.assertNotNull(requestEntry.message);
    Assert.assertNull(requestEntry.thrown);
    TestLoggerEntry responseEntry = logger.entries.get(1);
    Assert.assertNotNull(responseEntry.message);
    Assert.assertNull(responseEntry.thrown);
    assertRequestAndResponse(requestEntry.message, responseEntry.message, "createPayment.failure.invalidCardNumber");
}
Also used : Order(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order) ValidationException(com.ingenico.connect.gateway.sdk.java.ValidationException) Address(com.ingenico.connect.gateway.sdk.java.domain.definitions.Address) Customer(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer) HttpHost(org.apache.http.HttpHost) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) CardPaymentMethodSpecificInput(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.CardPaymentMethodSpecificInput) AmountOfMoney(com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney) Card(com.ingenico.connect.gateway.sdk.java.domain.definitions.Card) Test(org.junit.Test)

Example 10 with CreatePaymentRequest

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

the class PaymentsClientTest method testCreateInvalidRequest.

/**
 * Tests that a 400 failure response without a payment result will throw a {@link ValidationException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateInvalidRequest() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("invalid_request.json");
    whenPost().thenReturn(new Response(400, responseBody, null));
    CreatePaymentRequest body = createRequest();
    try {
        client.merchant("merchantId").payments().create(body);
        Assert.fail("Expected ValidationException");
    } catch (ValidationException e) {
        Assert.assertTrue(e.toString().contains(responseBody));
    }
}
Also used : Response(com.ingenico.connect.gateway.sdk.java.Response) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) ValidationException(com.ingenico.connect.gateway.sdk.java.ValidationException) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) Client(com.ingenico.connect.gateway.sdk.java.Client) Test(org.junit.Test)

Aggregations

CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)24 Client (com.ingenico.connect.gateway.sdk.java.Client)19 Test (org.junit.Test)18 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)15 Response (com.ingenico.connect.gateway.sdk.java.Response)9 Address (com.ingenico.connect.gateway.sdk.java.domain.definitions.Address)8 AmountOfMoney (com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney)8 Customer (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer)8 Order (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order)8 HttpHost (org.apache.http.HttpHost)8 CallContext (com.ingenico.connect.gateway.sdk.java.CallContext)7 Card (com.ingenico.connect.gateway.sdk.java.domain.definitions.Card)6 CardPaymentMethodSpecificInput (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.CardPaymentMethodSpecificInput)6 DeclinedPaymentException (com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException)5 HashMap (java.util.HashMap)5 ResponseHeader (com.ingenico.connect.gateway.sdk.java.ResponseHeader)3 ApiException (com.ingenico.connect.gateway.sdk.java.ApiException)2 IdempotenceException (com.ingenico.connect.gateway.sdk.java.IdempotenceException)2 ResponseException (com.ingenico.connect.gateway.sdk.java.ResponseException)2 ValidationException (com.ingenico.connect.gateway.sdk.java.ValidationException)2