Search in sources :

Example 6 with CallContext

use of com.ingenico.connect.gateway.sdk.java.CallContext 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 7 with CallContext

use of com.ingenico.connect.gateway.sdk.java.CallContext in project connect-sdk-java by Ingenico-ePayments.

the class PaymentsClientTest method testCreateIdempotenceError.

/**
 * Tests that a 409 failure response with a duplicate request code and an idempotence key will throw an {@link IdempotenceException}.
 */
@Test
@SuppressWarnings("resource")
public void testCreateIdempotenceError() {
    Client client = Factory.createClient(session);
    String responseBody = getResource("duplicate_request.json");
    whenPost().thenReturn(new Response(409, responseBody, null));
    CreatePaymentRequest body = createRequest();
    CallContext context = new CallContext().withIdempotenceKey("key");
    try {
        client.merchant("merchantId").payments().create(body, context);
        Assert.fail("Expected ApiException");
    } catch (IdempotenceException e) {
        Assert.assertTrue(e.toString().contains(responseBody));
        Assert.assertEquals(context.getIdempotenceKey(), e.getIdempotenceKey());
    }
}
Also used : Response(com.ingenico.connect.gateway.sdk.java.Response) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) 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) IdempotenceException(com.ingenico.connect.gateway.sdk.java.IdempotenceException) Test(org.junit.Test)

Aggregations

CallContext (com.ingenico.connect.gateway.sdk.java.CallContext)7 Client (com.ingenico.connect.gateway.sdk.java.Client)7 CreatePaymentRequest (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest)7 Test (org.junit.Test)7 HashMap (java.util.HashMap)5 HttpHost (org.apache.http.HttpHost)5 CreatePaymentResponse (com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse)4 DeclinedPaymentException (com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException)2 IdempotenceException (com.ingenico.connect.gateway.sdk.java.IdempotenceException)2 Response (com.ingenico.connect.gateway.sdk.java.Response)1 Address (com.ingenico.connect.gateway.sdk.java.domain.definitions.Address)1 AmountOfMoney (com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney)1 Customer (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer)1 Order (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order)1 RedirectPaymentMethodSpecificInput (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.RedirectPaymentMethodSpecificInput)1 RedirectPaymentProduct809SpecificInput (com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.RedirectPaymentProduct809SpecificInput)1