Search in sources :

Example 1 with CreatePaymentRequest

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

the class CreatePaymentExample method example.

@SuppressWarnings("unused")
public void example() throws URISyntaxException, IOException {
    Client client = getClient();
    try {
        Card card = new Card();
        card.setCardNumber("4567350000427977");
        card.setCardholderName("Wile E. Coyote");
        card.setCvv("123");
        card.setExpiryDate("1220");
        CardPaymentMethodSpecificInput cardPaymentMethodSpecificInput = new CardPaymentMethodSpecificInput();
        cardPaymentMethodSpecificInput.setCard(card);
        cardPaymentMethodSpecificInput.setPaymentProductId(1);
        cardPaymentMethodSpecificInput.setSkipAuthentication(false);
        AmountOfMoney amountOfMoney = new AmountOfMoney();
        amountOfMoney.setAmount(2980L);
        amountOfMoney.setCurrencyCode("EUR");
        Address billingAddress = new Address();
        billingAddress.setAdditionalInfo("b");
        billingAddress.setCity("Monument Valley");
        billingAddress.setCountryCode("US");
        billingAddress.setHouseNumber("13");
        billingAddress.setState("Utah");
        billingAddress.setStreet("Desertroad");
        billingAddress.setZip("84536");
        CompanyInformation companyInformation = new CompanyInformation();
        companyInformation.setName("Acme Labs");
        ContactDetails contactDetails = new ContactDetails();
        contactDetails.setEmailAddress("wile.e.coyote@acmelabs.com");
        contactDetails.setEmailMessageType("html");
        contactDetails.setFaxNumber("+1234567891");
        contactDetails.setPhoneNumber("+1234567890");
        PersonalName name = new PersonalName();
        name.setFirstName("Wile");
        name.setSurname("Coyote");
        name.setSurnamePrefix("E.");
        name.setTitle("Mr.");
        PersonalInformation personalInformation = new PersonalInformation();
        personalInformation.setDateOfBirth("19490917");
        personalInformation.setGender("M");
        personalInformation.setName(name);
        PersonalName shippingName = new PersonalName();
        shippingName.setFirstName("Road");
        shippingName.setSurname("Runner");
        shippingName.setTitle("Miss");
        AddressPersonal shippingAddress = new AddressPersonal();
        shippingAddress.setAdditionalInfo("Suite II");
        shippingAddress.setCity("Monument Valley");
        shippingAddress.setCountryCode("US");
        shippingAddress.setHouseNumber("1");
        shippingAddress.setName(shippingName);
        shippingAddress.setState("Utah");
        shippingAddress.setStreet("Desertroad");
        shippingAddress.setZip("84536");
        Customer customer = new Customer();
        customer.setBillingAddress(billingAddress);
        customer.setCompanyInformation(companyInformation);
        customer.setContactDetails(contactDetails);
        customer.setLocale("en_US");
        customer.setMerchantCustomerId("1234");
        customer.setPersonalInformation(personalInformation);
        customer.setShippingAddress(shippingAddress);
        customer.setVatNumber("1234AB5678CD");
        OrderInvoiceData invoiceData = new OrderInvoiceData();
        invoiceData.setInvoiceDate("20140306191500");
        invoiceData.setInvoiceNumber("000000123");
        OrderReferences references = new OrderReferences();
        references.setDescriptor("Fast and Furry-ous");
        references.setInvoiceData(invoiceData);
        references.setMerchantOrderId(123456L);
        references.setMerchantReference("AcmeOrder0001");
        List<LineItem> items = new ArrayList<LineItem>();
        AmountOfMoney item1AmountOfMoney = new AmountOfMoney();
        item1AmountOfMoney.setAmount(2500L);
        item1AmountOfMoney.setCurrencyCode("EUR");
        LineItemInvoiceData item1InvoiceData = new LineItemInvoiceData();
        item1InvoiceData.setDescription("ACME Super Outfit");
        item1InvoiceData.setNrOfItems("1");
        item1InvoiceData.setPricePerItem(2500L);
        LineItem item1 = new LineItem();
        item1.setAmountOfMoney(item1AmountOfMoney);
        item1.setInvoiceData(item1InvoiceData);
        items.add(item1);
        AmountOfMoney item2AmountOfMoney = new AmountOfMoney();
        item2AmountOfMoney.setAmount(480L);
        item2AmountOfMoney.setCurrencyCode("EUR");
        LineItemInvoiceData item2InvoiceData = new LineItemInvoiceData();
        item2InvoiceData.setDescription("Aspirin");
        item2InvoiceData.setNrOfItems("12");
        item2InvoiceData.setPricePerItem(40L);
        LineItem item2 = new LineItem();
        item2.setAmountOfMoney(item2AmountOfMoney);
        item2.setInvoiceData(item2InvoiceData);
        items.add(item2);
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setItems(items);
        Order order = new Order();
        order.setAmountOfMoney(amountOfMoney);
        order.setCustomer(customer);
        order.setReferences(references);
        order.setShoppingCart(shoppingCart);
        CreatePaymentRequest body = new CreatePaymentRequest();
        body.setCardPaymentMethodSpecificInput(cardPaymentMethodSpecificInput);
        body.setOrder(order);
        try {
            CreatePaymentResponse response = client.merchant("merchantId").payments().create(body);
        } catch (DeclinedPaymentException e) {
            handleDeclinedPayment(e.getCreatePaymentResult());
        } catch (ApiException e) {
            handleApiErrors(e.getErrors());
        }
    } finally {
        client.close();
    }
}
Also used : OrderInvoiceData(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.OrderInvoiceData) Order(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Order) OrderReferences(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.OrderReferences) DeclinedPaymentException(com.ingenico.connect.gateway.sdk.java.DeclinedPaymentException) Address(com.ingenico.connect.gateway.sdk.java.domain.definitions.Address) Customer(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.Customer) ArrayList(java.util.ArrayList) AddressPersonal(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.AddressPersonal) LineItem(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.LineItem) CreatePaymentRequest(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentRequest) LineItemInvoiceData(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.LineItemInvoiceData) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) Card(com.ingenico.connect.gateway.sdk.java.domain.definitions.Card) AmountOfMoney(com.ingenico.connect.gateway.sdk.java.domain.definitions.AmountOfMoney) CompanyInformation(com.ingenico.connect.gateway.sdk.java.domain.definitions.CompanyInformation) PersonalInformation(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.PersonalInformation) ContactDetails(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.ContactDetails) ShoppingCart(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.ShoppingCart) PersonalName(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.PersonalName) Client(com.ingenico.connect.gateway.sdk.java.Client) CardPaymentMethodSpecificInput(com.ingenico.connect.gateway.sdk.java.domain.payment.definitions.CardPaymentMethodSpecificInput) ApiException(com.ingenico.connect.gateway.sdk.java.ApiException)

Example 2 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 testIdempotenceDuplicateRequest.

@Test
public void testIdempotenceDuplicateRequest() throws Exception {
    String body = getResource("idempotence_duplicate_failure.json");
    final Long idempotenceRequestTimestamp = System.currentTimeMillis();
    Map<String, String> responseHeaders = new HashMap<String, String>(2);
    responseHeaders.put("Location", "http://localhost/v1/20000/payments/000002000020142549460000100001");
    responseHeaders.put("X-GCS-Idempotence-Request-Timestamp", idempotenceRequestTimestamp.toString());
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 409, 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 IdempotenceException");
    } catch (IdempotenceException e) {
        Assert.assertEquals(409, e.getStatusCode());
        Assert.assertEquals(body, e.getResponseBody());
        Assert.assertEquals(idempotenceKey, e.getIdempotenceKey());
        Assert.assertEquals(idempotenceRequestTimestamp, e.getIdempotenceRequestTimestamp());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertEquals(idempotenceRequestTimestamp, context.getIdempotenceRequestTimestamp());
}
Also used : 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) IdempotenceException(com.ingenico.connect.gateway.sdk.java.IdempotenceException) Test(org.junit.Test)

Example 3 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 testIdempotenceFirstRequest.

@Test
public void testIdempotenceFirstRequest() throws Exception {
    final String body = getResource("idempotence_success.json");
    final String idempotenceKey = UUID.randomUUID().toString();
    Map<String, String> responseHeaders = new HashMap<String, String>(1);
    responseHeaders.put("Location", "http://localhost/v1/20000/payments/000002000020142549460000100001");
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 201, responseHeaders, requestHeaders)).when(requestHandler).handle(Matchers.<HttpRequest>any(), Matchers.<HttpResponse>any(), Matchers.<HttpContext>any());
    serverBootstrap.registerHandler("/v1/20000/payments", requestHandler);
    HttpHost host = start();
    CallContext context = new CallContext().withIdempotenceKey(idempotenceKey);
    Client client = createClient(host);
    try {
        CreatePaymentRequest request = createRequest();
        CreatePaymentResponse response = client.merchant("20000").payments().create(request, context);
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getPayment());
        Assert.assertNotNull(response.getPayment().getId());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertNull(context.getIdempotenceRequestTimestamp());
}
Also used : 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) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Example 4 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 testIdempotenceSecondRequest.

@Test
public void testIdempotenceSecondRequest() throws Exception {
    String body = getResource("idempotence_success.json");
    final Long idempotenceRequestTimestamp = System.currentTimeMillis();
    Map<String, String> responseHeaders = new HashMap<String, String>(2);
    responseHeaders.put("Location", "http://localhost/v1/20000/payments/000002000020142549460000100001");
    responseHeaders.put("X-GCS-Idempotence-Request-Timestamp", idempotenceRequestTimestamp.toString());
    Map<String, String> requestHeaders = new HashMap<String, String>();
    Mockito.doAnswer(setResponse(body, 201, 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();
        CreatePaymentResponse response = client.merchant("20000").payments().create(request, context);
        Assert.assertNotNull(response);
        Assert.assertNotNull(response.getPayment());
        Assert.assertNotNull(response.getPayment().getId());
    } finally {
        client.close();
    }
    Assert.assertEquals(idempotenceKey, requestHeaders.get("X-GCS-Idempotence-Key"));
    Assert.assertEquals(idempotenceKey, context.getIdempotenceKey());
    Assert.assertEquals(idempotenceRequestTimestamp, context.getIdempotenceRequestTimestamp());
}
Also used : 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) CreatePaymentResponse(com.ingenico.connect.gateway.sdk.java.domain.payment.CreatePaymentResponse) CallContext(com.ingenico.connect.gateway.sdk.java.CallContext) Test(org.junit.Test)

Example 5 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 testIdempotenceSecondFailure.

@Test
public void testIdempotenceSecondFailure() throws Exception {
    String body = getResource("idempotence_rejected.json");
    final Long idempotenceRequestTimestamp = System.currentTimeMillis();
    Map<String, String> responseHeaders = new HashMap<String, String>(2);
    responseHeaders.put("X-GCS-Idempotence-Request-Timestamp", idempotenceRequestTimestamp.toString());
    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.assertEquals(idempotenceRequestTimestamp, 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)

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