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