From 3d538d148dc56f49eab6fc1fefe1005ef6e1df68 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Wed, 27 Sep 2017 11:10:41 -0300 Subject: [PATCH 01/11] feat(Order): added method to get an order list added method to get an order list --- src/main/java/br/com/moip/api/OrderAPI.java | 53 ++++++++++++++- .../java/br/com/moip/api/filter/Filters.java | 47 +++++++++++++ .../br/com/moip/api/filter/Pagination.java | 5 ++ .../com/moip/response/OrderListResponse.java | 45 +++++++++++++ .../br/com/moip/util/QueryStringFactory.java | 62 +++++++++++++++++ .../java/br/com/moip/api/OrderAPITest.java | 18 +++++ .../br/com/moip/api/filter/FiltersTest.java | 67 +++++++++++++++++++ .../com/moip/util/QueryStringFactoryTest.java | 59 ++++++++++++++++ src/test/resources/plays/orders/list.yaml | 15 +++++ 9 files changed, 369 insertions(+), 2 deletions(-) create mode 100644 src/main/java/br/com/moip/api/filter/Filters.java create mode 100644 src/main/java/br/com/moip/response/OrderListResponse.java create mode 100644 src/main/java/br/com/moip/util/QueryStringFactory.java create mode 100644 src/test/java/br/com/moip/api/filter/FiltersTest.java create mode 100644 src/test/java/br/com/moip/util/QueryStringFactoryTest.java create mode 100644 src/test/resources/plays/orders/list.yaml diff --git a/src/main/java/br/com/moip/api/OrderAPI.java b/src/main/java/br/com/moip/api/OrderAPI.java index d8e32b7..d69d44f 100644 --- a/src/main/java/br/com/moip/api/OrderAPI.java +++ b/src/main/java/br/com/moip/api/OrderAPI.java @@ -1,22 +1,71 @@ package br.com.moip.api; import br.com.moip.Client; +import br.com.moip.api.filter.Filters; +import br.com.moip.api.filter.Pagination; import br.com.moip.request.OrderRequest; import br.com.moip.resource.Order; +import br.com.moip.response.OrderListResponse; +import br.com.moip.util.QueryStringFactory; + +import java.util.HashMap; +import java.util.Map; public class OrderAPI { private final Client client; + private static final String PATH = "/v2/orders"; + public OrderAPI(final Client client) { this.client = client; } public Order create(final OrderRequest order) { - return client.post("/v2/orders", order, Order.class); + return client.post(PATH, order, Order.class); } public Order get(final String id) { - return client.get("/v2/orders/" + id, Order.class); + return client.get(String.format("%s/%s", PATH, id), Order.class); + } + + public OrderListResponse list() { + return client.get(PATH, OrderListResponse.class); + } + + public OrderListResponse list(final Pagination pagination) { + String path = new QueryStringFactory(PATH, pagination, null, null).generate(); + + return client.get(path, OrderListResponse.class); + } + + public OrderListResponse list(final Filters filters) { + return client.get(new QueryStringFactory(PATH, null, filters, null).generate(), OrderListResponse.class); + } + + public OrderListResponse list(final String qParam) { + return client.get(new QueryStringFactory(PATH, null, null, hashParams(qParam)).generate(), OrderListResponse.class); + } + + public OrderListResponse list(final Pagination pagination, final Filters filters) { + return client.get(new QueryStringFactory(PATH, pagination, filters, null).generate(), OrderListResponse.class); + } + + public OrderListResponse list(final Pagination pagination, final String qParam) { + return client.get(new QueryStringFactory(PATH, pagination, null, hashParams(qParam)).generate(), OrderListResponse.class); + } + + public OrderListResponse list(final Filters filters, final String qParam) { + return client.get(new QueryStringFactory(PATH, null, filters, hashParams(qParam)).generate(), OrderListResponse.class); + } + + public OrderListResponse list(final Pagination pagination, final Filters filters, final String qParam) { + return client.get(new QueryStringFactory(PATH, pagination, filters, hashParams(qParam)).generate(), OrderListResponse.class); + } + + private Map hashParams(final String qParam) { + return new HashMap() {{ + put("q", qParam); + }}; } } \ No newline at end of file diff --git a/src/main/java/br/com/moip/api/filter/Filters.java b/src/main/java/br/com/moip/api/filter/Filters.java new file mode 100644 index 0000000..0534e71 --- /dev/null +++ b/src/main/java/br/com/moip/api/filter/Filters.java @@ -0,0 +1,47 @@ +package br.com.moip.api.filter; + +import com.sun.deploy.util.StringUtils; + +import java.util.ArrayList; + +public class Filters { + + private ArrayList filters = new ArrayList<>(); + + public Filters greaterThan(String field, String value) { + filters.add(String.format("%s::gt(%s)", field, value)); + + return this; + } + + public Filters greaterThanOrEqual(String field, String value) + { + filters.add(String.format("%s::ge(%s)", field, value)); + + return this; + } + + public Filters lessThan(String field, String value) { + filters.add(String.format("%s::lt(%s)", field, value)); + + return this; + } + + public Filters between(String field, String value1, String value2) { + filters.add(String.format("%s::bt(%s,%s)", field, value1, value2)); + + return this; + } + + public Filters in (String field, ArrayListvalues) { + filters.add(String.format("%s::in(%s)", field, StringUtils.join(values, ","))); + + return this; + } + + @Override + public String toString() { + return StringUtils.join(filters, "|"); + } + +} diff --git a/src/main/java/br/com/moip/api/filter/Pagination.java b/src/main/java/br/com/moip/api/filter/Pagination.java index 69fd5e6..c9602ce 100644 --- a/src/main/java/br/com/moip/api/filter/Pagination.java +++ b/src/main/java/br/com/moip/api/filter/Pagination.java @@ -8,6 +8,11 @@ public Pagination(){ } + public Pagination (int limit, int offset) { + this.limit = limit; + this.offset = offset; + } + public Pagination(final String url) { String[] params = url.split("&"); String limit = params[0].split("=")[1]; diff --git a/src/main/java/br/com/moip/response/OrderListResponse.java b/src/main/java/br/com/moip/response/OrderListResponse.java new file mode 100644 index 0000000..00f77e2 --- /dev/null +++ b/src/main/java/br/com/moip/response/OrderListResponse.java @@ -0,0 +1,45 @@ +package br.com.moip.response; + +import br.com.moip.resource.Links; +import br.com.moip.resource.Order; +import br.com.moip.resource.invoice.Summary; + +import java.util.List; + +public class OrderListResponse { + + public static final String NEXT = "next"; + public static final String PREVIOUS = "previous"; + + private List orders; + private Summary summary; + private Links links; + + public String next() { + return links.getLinks().get(NEXT).toString(); + } + + public String previous() { + return links.getLinks().get(PREVIOUS).toString(); + } + + public List getOrders() { + return orders; + } + + public Summary getSummary() { + return summary; + } + + public void setSummary(Summary summary) { + this.summary = summary; + } + + @Override + public String toString() { + return new StringBuilder("OrderListResponse{") + .append("summary=").append(summary) + .append(", orders=").append(orders) + .append('}').toString(); + } +} diff --git a/src/main/java/br/com/moip/util/QueryStringFactory.java b/src/main/java/br/com/moip/util/QueryStringFactory.java new file mode 100644 index 0000000..02fd04e --- /dev/null +++ b/src/main/java/br/com/moip/util/QueryStringFactory.java @@ -0,0 +1,62 @@ +package br.com.moip.util; + +import br.com.moip.api.filter.Filters; +import br.com.moip.api.filter.Pagination; +import br.com.moip.exception.MoipException; +import org.apache.http.client.utils.URIBuilder; + +import java.net.URISyntaxException; +import java.util.Map; + +public class QueryStringFactory { + + private URIBuilder uri; + private Pagination pagination; + private Filters filters; + private Map params; + + public QueryStringFactory(String endpoint, Pagination pagination, Filters filters, Map params) { + try { + this.uri = new URIBuilder(endpoint); + this.pagination = pagination; + this.filters = filters; + this.params = params; + } catch (URISyntaxException e) { + throw new MoipException("Error trying to build URL: " + e.getMessage()); + } + } + + private void addPagination() { + if (pagination.getLimit() != 0) { + uri.addParameter("limit", String.valueOf(pagination.getLimit())); + } + + if (pagination.getOffset() >= 0) { + uri.addParameter("offset", String.valueOf(pagination.getOffset())); + } + } + + private void addParams() { + for (Map.Entry entry : params.entrySet()) + { + uri.addParameter(entry.getKey(), entry.getValue()); + } + } + + public String generate() { + if (pagination != null) { + addPagination(); + } + + if (filters != null) { + uri.addParameter("filters", filters.toString()); + } + + if (params != null) { + addParams(); + } + + return uri.toString(); + } + +} diff --git a/src/test/java/br/com/moip/api/OrderAPITest.java b/src/test/java/br/com/moip/api/OrderAPITest.java index cffdf42..283cf1b 100644 --- a/src/test/java/br/com/moip/api/OrderAPITest.java +++ b/src/test/java/br/com/moip/api/OrderAPITest.java @@ -1,5 +1,8 @@ package br.com.moip.api; +import br.com.moip.Client; +import br.com.moip.api.filter.Filters; +import br.com.moip.api.filter.Pagination; import br.com.moip.request.AmountRequest; import br.com.moip.request.ApiDateRequest; import br.com.moip.request.CustomerRequest; @@ -11,6 +14,7 @@ import br.com.moip.request.CheckoutPreferencesRequest; import br.com.moip.request.InstallmentRequest; import br.com.moip.resource.Order; +import br.com.moip.response.OrderListResponse; import com.rodrigosaito.mockwebserver.player.Play; import com.rodrigosaito.mockwebserver.player.Player; import org.junit.Before; @@ -18,8 +22,11 @@ import org.junit.Test; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import static org.junit.Assert.assertArrayEquals; @@ -178,4 +185,15 @@ public void testCreateWithFullCustomer() { assertEquals("BRA", createdOrder.getCustomer().getShippingAddress().getCountry()); } + + @Play("orders/list") + @Test + public void testGetOrderList() { + OrderListResponse orderListResponse = api.list(); + assertEquals(20, orderListResponse.getOrders().size()); + assertEquals("ORD-UQUCZIB66I4U", orderListResponse.getOrders().get(0).getId()); + assertEquals("ORD-OBC77GS8R20D", orderListResponse.getOrders().get(1).getId()); + assertEquals("jose silva", orderListResponse.getOrders().get(0).getCustomer().getFullname()); + } + } diff --git a/src/test/java/br/com/moip/api/filter/FiltersTest.java b/src/test/java/br/com/moip/api/filter/FiltersTest.java new file mode 100644 index 0000000..903e73c --- /dev/null +++ b/src/test/java/br/com/moip/api/filter/FiltersTest.java @@ -0,0 +1,67 @@ +package br.com.moip.api.filter; + +import org.junit.Test; + +import java.util.ArrayList; + +import static org.junit.Assert.assertEquals; + +public class FiltersTest { + + @Test + public void testGreaterThanFilter() + { + Filters filter = new Filters(); + filter.greaterThan("createdAt", "2017-08-17"); + assertEquals("createdAt::gt(2017-08-17)", filter.toString()); + } + + @Test + public void testGreaterEqualFilter() + { + Filters filter = new Filters(); + filter.greaterThanOrEqual("createdAt", "2017-08-17"); + assertEquals("createdAt::ge(2017-08-17)", filter.toString()); + } + + @Test + public void testLessThanFilter() + { + Filters filter = new Filters(); + filter.lessThan("value", "100000"); + assertEquals("value::lt(100000)", filter.toString()); + } + + @Test + public void testInFilter() + { + Filters filter = new Filters(); + ArrayList values = new ArrayList<>(); + values.add("BOLETO"); + values.add("DEBIT_CARD"); + values.add("ONLINE_BANK_DEBIT"); + filter.in("paymentMethod", values); + assertEquals("paymentMethod::in(BOLETO,DEBIT_CARD,ONLINE_BANK_DEBIT)", filter.toString()); + } + + @Test + public void testBetweenFilter() + { + Filters filter = new Filters(); + filter.between("createdAt", "2017-08-10", "2017-08-17"); + assertEquals("createdAt::bt(2017-08-10,2017-08-17)", filter.toString()); + } + + @Test + public void testMultipleFilter() + { + Filters filter = new Filters(); + ArrayList values = new ArrayList<>(); + values.add("BOLETO"); + values.add("DEBIT_CARD"); + filter.greaterThanOrEqual("createdAt", "2017-08-17") + .in("paymentMethod", values) + .lessThan("value", "100000"); + assertEquals("createdAt::ge(2017-08-17)|paymentMethod::in(BOLETO,DEBIT_CARD)|value::lt(100000)", filter.toString()); + } +} diff --git a/src/test/java/br/com/moip/util/QueryStringFactoryTest.java b/src/test/java/br/com/moip/util/QueryStringFactoryTest.java new file mode 100644 index 0000000..9c67f1f --- /dev/null +++ b/src/test/java/br/com/moip/util/QueryStringFactoryTest.java @@ -0,0 +1,59 @@ +package br.com.moip.util; + +import br.com.moip.api.filter.Filters; +import br.com.moip.api.filter.Pagination; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class QueryStringFactoryTest { + + private Pagination pagination; + private Filters filters; + + @Before + public void setUp() { + ArrayList status = new ArrayList<>(); + status.add("NOT_PAID"); + status.add("WAITING"); + + filters = new Filters() + .between("amount", "1000", "10000") + .in("status", status); + + pagination = new Pagination(50, 0); + } + + @Test + public void testQueryStringNoParameters() { + QueryStringFactory queryString = new QueryStringFactory("/v2/orders", null, null, null); + assertEquals("/v2/orders", queryString.generate()); + } + + @Test + public void testQueryStringPagination() { + QueryStringFactory queryString = new QueryStringFactory("/v2/orders", pagination, null, null); + assertEquals("/v2/orders?limit=50&offset=0", queryString.generate()); + } + + @Test + public void testQueryStringPaginationFilter() { + QueryStringFactory queryString = new QueryStringFactory("/v2/orders", pagination, filters, null); + assertEquals("/v2/orders?limit=50&offset=0&filters=amount%3A%3Abt%281000%2C10000%29%7Cstatus%3A%3Ain%28NOT_PAID%2CWAITING%29", queryString.generate()); + } + + @Test + public void testQueryStringAllParams() { + Map params = new HashMap(); + params.put("q", "joão silva"); + + QueryStringFactory queryString = new QueryStringFactory("/v2/orders", pagination, filters, params); + assertEquals("/v2/orders?limit=50&offset=0&filters=amount%3A%3Abt%281000%2C10000%29%7Cstatus%3A%3Ain%28NOT_PAID%2CWAITING%29&q=jo%C3%A3o+silva", queryString.generate()); + } + +} diff --git a/src/test/resources/plays/orders/list.yaml b/src/test/resources/plays/orders/list.yaml new file mode 100644 index 0000000..f5e1cc3 --- /dev/null +++ b/src/test/resources/plays/orders/list.yaml @@ -0,0 +1,15 @@ +!play +interactions: +- + request: + uri: /v2/orders + headers: + "Content-Type": application/json + Authorization: Basic MDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDE6QUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQg== + method: GET + response: + status: 200 + headers: + "Content-Type": application/json + body: | + {"_links":{"next":{"href":"https://test.moip.com.br/v2/orders?filters=&limit=0&offset=0"},"previous":{"href":"https://test.moip.com.br/v2/orders?filters=&limit=0&offset=0"}},"summary":{"count":1972,"amount":1089874890},"orders":[{"id":"ORD-UQUCZIB66I4U","ownId":"ord-595d1b2c13b9d","status":"WAITING","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-VHECVJN1TDRM","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-UQUCZIB66I4U"}},"createdAt":"2017-09-22T07:28:10-0300","updatedAt":"2017-09-22T07:30:17-0300"},{"id":"ORD-OBC77GS8R20D","ownId":"ord-59c3fa5cf0ea0","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-J7TMPYV2ADH0","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:44:00Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-OBC77GS8R20D"}},"createdAt":"2017-09-21T14:43:57-0300","updatedAt":"2017-09-21T14:44:00-0300"},{"id":"ORD-4RVLVSFSC9AE","ownId":"ord-59c3fa58766e4","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-W9HD9BLVNL3E","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:43:55Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-4RVLVSFSC9AE"}},"createdAt":"2017-09-21T14:43:53-0300","updatedAt":"2017-09-21T14:43:55-0300"},{"id":"ORD-H5FIMINR0W3O","ownId":"ord-59c3fa5638128","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-TN95744ZUME5","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:43:52Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-H5FIMINR0W3O"}},"createdAt":"2017-09-21T14:43:50-0300","updatedAt":"2017-09-21T14:43:52-0300"},{"id":"ORD-85OYQBNROZKM","ownId":"ord-59c3fa5290806","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-QEM2UXMVVWWE","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:43:50Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-85OYQBNROZKM"}},"createdAt":"2017-09-21T14:43:48-0300","updatedAt":"2017-09-21T14:43:50-0300"},{"id":"ORD-W19WYVL6GKKS","ownId":"ord-59c3fa504341b","status":"WAITING","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-T5XSX2GHIMQK","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-W19WYVL6GKKS"}},"createdAt":"2017-09-21T14:43:44-0300","updatedAt":"2017-09-21T14:43:46-0300"},{"id":"ORD-JIDFTO3BTXSR","ownId":"ord-59c3fa4df22ed","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-8ZKQPK447Z3H","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:43:44Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-JIDFTO3BTXSR"}},"createdAt":"2017-09-21T14:43:42-0300","updatedAt":"2017-09-21T14:43:44-0300"},{"id":"ORD-74SBQG24I8XO","ownId":"ord-59c3fa4315bb6","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-1GZSUTWLROOX","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:43:33Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-74SBQG24I8XO"}},"createdAt":"2017-09-21T14:43:31-0300","updatedAt":"2017-09-21T14:43:33-0300"},{"id":"ORD-52V6IDQXO7LI","ownId":"ord-59c3f9ddabed0","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-M046NUS62E84","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:41:52Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-52V6IDQXO7LI"}},"createdAt":"2017-09-21T14:41:50-0300","updatedAt":"2017-09-21T14:41:52-0300"},{"id":"ORD-VHW1WBYW3RML","ownId":"ord-59c3f9d7812aa","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-MAHQ5E1DHA1R","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:41:46Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-VHW1WBYW3RML"}},"createdAt":"2017-09-21T14:41:44-0300","updatedAt":"2017-09-21T14:41:46-0300"},{"id":"ORD-Q1P5FM2D3JIM","ownId":"ord-59c3f9d5323fc","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-ZIFEHK3GZCU1","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:41:43Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-Q1P5FM2D3JIM"}},"createdAt":"2017-09-21T14:41:41-0300","updatedAt":"2017-09-21T14:41:43-0300"},{"id":"ORD-0MJN4QICU207","ownId":"ord-59c3f9d2cf466","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-4PO2XZCNFVDE","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:41:41Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-0MJN4QICU207"}},"createdAt":"2017-09-21T14:41:39-0300","updatedAt":"2017-09-21T14:41:41-0300"},{"id":"ORD-UODQFL1V325X","ownId":"ord-59c3f9d03c09f","status":"WAITING","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-W0SCISBNFN4W","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-UODQFL1V325X"}},"createdAt":"2017-09-21T14:41:37-0300","updatedAt":"2017-09-21T14:41:38-0300"},{"id":"ORD-74EBKG0G6XDS","ownId":"ord-59c3f9ce1e6d3","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-QBKL7A2U3M66","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:41:36Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-74EBKG0G6XDS"}},"createdAt":"2017-09-21T14:41:34-0300","updatedAt":"2017-09-21T14:41:36-0300"},{"id":"ORD-110DW585PTMP","ownId":"ord-59c3f9c34604c","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-6VMXLZKDBFI3","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:41:25Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-110DW585PTMP"}},"createdAt":"2017-09-21T14:41:23-0300","updatedAt":"2017-09-21T14:41:25-0300"},{"id":"ORD-UUZ9XBNLBFII","ownId":"ord-59c3f975eb800","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-XVOH50HEOM15","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:40:09Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-UUZ9XBNLBFII"}},"createdAt":"2017-09-21T14:40:06-0300","updatedAt":"2017-09-21T14:40:09-0300"},{"id":"ORD-K50I1EIAC3WJ","ownId":"ord-59c3f971278c0","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-PA668QF0BL5J","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:40:04Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-K50I1EIAC3WJ"}},"createdAt":"2017-09-21T14:40:01-0300","updatedAt":"2017-09-21T14:40:04-0300"},{"id":"ORD-FJ99S3TMHWR0","ownId":"ord-59c3f96eddb27","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-Y5CP70KKOXOX","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:40:01Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-FJ99S3TMHWR0"}},"createdAt":"2017-09-21T14:39:59-0300","updatedAt":"2017-09-21T14:40:01-0300"},{"id":"ORD-VKY49B4FCFTH","ownId":"ord-59c3f96cb9850","status":"PAID","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-OZ4SACSGA8NQ","installmentCount":1,"fundingInstrument":{"method":"CREDIT_CARD","brand":"MASTERCARD"}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-09-21T14:39:58Z"}],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-VKY49B4FCFTH"}},"createdAt":"2017-09-21T14:39:57-0300","updatedAt":"2017-09-21T14:39:58-0300"},{"id":"ORD-6GZTVWVQHFL9","ownId":"ord-59c3f96a71db4","status":"WAITING","blocked":false,"amount":{"total":102470,"addition":0,"fees":0,"deduction":0,"otherReceivers":0,"currency":"BRL"},"receivers":[{"type":"PRIMARY","moipAccount":{"id":"MPA-8D5DBB4EF8B8"}}],"customer":{"fullname":"jose silva","email":"jose_silva0@email.com"},"items":[{"product":null}],"payments":[{"id":"PAY-U4KESKJWJGKD","installmentCount":1,"fundingInstrument":{"method":"BOLETO","brand":null}}],"events":[],"_links":{"self":{"href":"https://test.moip.com.br/v2/orders/ORD-6GZTVWVQHFL9"}},"createdAt":"2017-09-21T14:39:55-0300","updatedAt":"2017-09-21T14:39:56-0300"}]} From b2596a6de266d536c345597f58fda262b11ea6b4 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Wed, 27 Sep 2017 11:15:25 -0300 Subject: [PATCH 02/11] style: Fixing style Fixing style --- src/main/java/br/com/moip/util/QueryStringFactory.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/br/com/moip/util/QueryStringFactory.java b/src/main/java/br/com/moip/util/QueryStringFactory.java index 02fd04e..88800e7 100644 --- a/src/main/java/br/com/moip/util/QueryStringFactory.java +++ b/src/main/java/br/com/moip/util/QueryStringFactory.java @@ -37,15 +37,14 @@ private void addPagination() { } private void addParams() { - for (Map.Entry entry : params.entrySet()) - { + for (Map.Entry entry : params.entrySet()) { uri.addParameter(entry.getKey(), entry.getValue()); } } public String generate() { if (pagination != null) { - addPagination(); + this.addPagination(); } if (filters != null) { @@ -53,7 +52,7 @@ public String generate() { } if (params != null) { - addParams(); + this.addParams(); } return uri.toString(); From 7bb93c4bc1945783d6df0307fba9a5f06649325a Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Wed, 27 Sep 2017 11:39:58 -0300 Subject: [PATCH 03/11] refactor(Filters): Change dependecies to fix build Change dependecies to fix build --- build.gradle | 5 +++-- src/main/java/br/com/moip/api/filter/Filters.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 9ac0dd9..a074dc8 100644 --- a/build.gradle +++ b/build.gradle @@ -19,6 +19,7 @@ dependencies { compile group: "org.slf4j", name: "slf4j-api", version: "1.7.12" compile group: "com.google.code.gson", name: "gson", version: "2.4" compile group: "org.apache.httpcomponents", name: "httpclient", version: "4.5.3" + compile group: "org.apache.commons", name: "commons-lang3", version: "3.6" testCompile group: "org.slf4j", name: "slf4j-simple", version: "1.7.12" testCompile group: "com.github.rodrigosaito", name: "mockwebserver-player", version: "1.1.0" @@ -53,11 +54,11 @@ uploadArchives { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { - authentication(userName: ossrhUsername, password: ossrhPassword) + authentication(userName: "ossrhUsername", password: "ossrhPassword") } snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: ossrhUsername, password: ossrhPassword) + authentication(userName: "ossrhUsername", password: "ossrhPassword") } pom.project { diff --git a/src/main/java/br/com/moip/api/filter/Filters.java b/src/main/java/br/com/moip/api/filter/Filters.java index 0534e71..ca60cc2 100644 --- a/src/main/java/br/com/moip/api/filter/Filters.java +++ b/src/main/java/br/com/moip/api/filter/Filters.java @@ -1,6 +1,6 @@ package br.com.moip.api.filter; -import com.sun.deploy.util.StringUtils; +import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; From 0309a235c97a60689dd85741e76d4dff12cff8b1 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Wed, 27 Sep 2017 11:41:22 -0300 Subject: [PATCH 04/11] style(Filters): Fix style Fix style --- src/main/java/br/com/moip/api/filter/Filters.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/br/com/moip/api/filter/Filters.java b/src/main/java/br/com/moip/api/filter/Filters.java index ca60cc2..4261b6d 100644 --- a/src/main/java/br/com/moip/api/filter/Filters.java +++ b/src/main/java/br/com/moip/api/filter/Filters.java @@ -14,8 +14,7 @@ public Filters greaterThan(String field, String value) { return this; } - public Filters greaterThanOrEqual(String field, String value) - { + public Filters greaterThanOrEqual(String field, String value) { filters.add(String.format("%s::ge(%s)", field, value)); return this; From 5349675bd6627d44a274584bf3043b7cb34331b1 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Wed, 27 Sep 2017 14:21:21 -0300 Subject: [PATCH 05/11] refactor: Applied sugested fixes from PR Applied sugested fixes from PR --- build.gradle | 4 +-- .../java/br/com/moip/api/filter/Filters.java | 5 +-- .../java/br/com/moip/api/OrderAPITest.java | 6 ---- .../br/com/moip/api/filter/FiltersTest.java | 34 ++++++------------- 4 files changed, 16 insertions(+), 33 deletions(-) diff --git a/build.gradle b/build.gradle index a074dc8..3654aef 100644 --- a/build.gradle +++ b/build.gradle @@ -54,11 +54,11 @@ uploadArchives { beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) } repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") { - authentication(userName: "ossrhUsername", password: "ossrhPassword") + authentication(userName: ossrhUsername, password: ossrhPassword) } snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") { - authentication(userName: "ossrhUsername", password: "ossrhPassword") + authentication(userName: ossrhUsername, password: ossrhPassword) } pom.project { diff --git a/src/main/java/br/com/moip/api/filter/Filters.java b/src/main/java/br/com/moip/api/filter/Filters.java index 4261b6d..c909559 100644 --- a/src/main/java/br/com/moip/api/filter/Filters.java +++ b/src/main/java/br/com/moip/api/filter/Filters.java @@ -3,10 +3,11 @@ import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; +import java.util.List; public class Filters { - private ArrayList filters = new ArrayList<>(); + private List filters = new ArrayList<>(); public Filters greaterThan(String field, String value) { filters.add(String.format("%s::gt(%s)", field, value)); @@ -32,7 +33,7 @@ public Filters between(String field, String value1, String value2) { return this; } - public Filters in (String field, ArrayListvalues) { + public Filters in (String field, List values) { filters.add(String.format("%s::in(%s)", field, StringUtils.join(values, ","))); return this; diff --git a/src/test/java/br/com/moip/api/OrderAPITest.java b/src/test/java/br/com/moip/api/OrderAPITest.java index 283cf1b..8d532d9 100644 --- a/src/test/java/br/com/moip/api/OrderAPITest.java +++ b/src/test/java/br/com/moip/api/OrderAPITest.java @@ -1,8 +1,5 @@ package br.com.moip.api; -import br.com.moip.Client; -import br.com.moip.api.filter.Filters; -import br.com.moip.api.filter.Pagination; import br.com.moip.request.AmountRequest; import br.com.moip.request.ApiDateRequest; import br.com.moip.request.CustomerRequest; @@ -22,11 +19,8 @@ import org.junit.Test; import java.text.SimpleDateFormat; -import java.util.ArrayList; import java.util.Calendar; import java.util.GregorianCalendar; -import java.util.HashMap; -import java.util.Map; import java.util.UUID; import static org.junit.Assert.assertArrayEquals; diff --git a/src/test/java/br/com/moip/api/filter/FiltersTest.java b/src/test/java/br/com/moip/api/filter/FiltersTest.java index 903e73c..ad4919f 100644 --- a/src/test/java/br/com/moip/api/filter/FiltersTest.java +++ b/src/test/java/br/com/moip/api/filter/FiltersTest.java @@ -2,65 +2,53 @@ import org.junit.Test; -import java.util.ArrayList; +import java.util.Arrays; import static org.junit.Assert.assertEquals; public class FiltersTest { @Test - public void testGreaterThanFilter() - { + public void testGreaterThanFilter() { Filters filter = new Filters(); filter.greaterThan("createdAt", "2017-08-17"); assertEquals("createdAt::gt(2017-08-17)", filter.toString()); } @Test - public void testGreaterEqualFilter() - { + public void testGreaterEqualFilter() { Filters filter = new Filters(); filter.greaterThanOrEqual("createdAt", "2017-08-17"); assertEquals("createdAt::ge(2017-08-17)", filter.toString()); } @Test - public void testLessThanFilter() - { + public void testLessThanFilter() { Filters filter = new Filters(); filter.lessThan("value", "100000"); assertEquals("value::lt(100000)", filter.toString()); } @Test - public void testInFilter() - { + public void testInFilter() { Filters filter = new Filters(); - ArrayList values = new ArrayList<>(); - values.add("BOLETO"); - values.add("DEBIT_CARD"); - values.add("ONLINE_BANK_DEBIT"); - filter.in("paymentMethod", values); + filter.in("paymentMethod", Arrays.asList(new String[] {"BOLETO","DEBIT_CARD","ONLINE_BANK_DEBIT"})); assertEquals("paymentMethod::in(BOLETO,DEBIT_CARD,ONLINE_BANK_DEBIT)", filter.toString()); } @Test - public void testBetweenFilter() - { + public void testBetweenFilter() { Filters filter = new Filters(); filter.between("createdAt", "2017-08-10", "2017-08-17"); + assertEquals("createdAt::bt(2017-08-10,2017-08-17)", filter.toString()); } @Test - public void testMultipleFilter() - { + public void testMultipleFilter() { Filters filter = new Filters(); - ArrayList values = new ArrayList<>(); - values.add("BOLETO"); - values.add("DEBIT_CARD"); - filter.greaterThanOrEqual("createdAt", "2017-08-17") - .in("paymentMethod", values) + filter.greaterThanOrEqual("createdAt", "2017-08-17"); + filter.in("paymentMethod", Arrays.asList(new String[] {"BOLETO","DEBIT_CARD"})) .lessThan("value", "100000"); assertEquals("createdAt::ge(2017-08-17)|paymentMethod::in(BOLETO,DEBIT_CARD)|value::lt(100000)", filter.toString()); } From a7c3e961f5ba3e9d34dc80e5e2c86bff5d94bf15 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Wed, 27 Sep 2017 14:33:34 -0300 Subject: [PATCH 06/11] docs: Update readme Update readme --- README.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README.md b/README.md index 0e9963e..cf6f505 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,12 @@ - [Pedidos](#pedidos) - [Criação](#criação) - [Consulta](#consulta) + - [Pedido Específico](#pedido-específico) + - [Todos os Pedidos](#todos-os-pedidos) + - [Sem Filtro](#sem-filtro) + - [Com Filtros](#com-filtros) + - [Com Paginação](#com-paginação) + - [Consulta Valor Específico](#consulta-valor-específico) - [Pagamentos](#pagamentos) - [Criação](#criação-1) - [Cartão de Crédito](#cartão-de-crédito) @@ -124,12 +130,46 @@ Order createdOrder = api.order().create(new OrderRequest() ``` ### Consulta +#### Pedido Específico ```java String orderId = "ORD-HPMZSOM611M2"; Order order = api.order().get(orderId); System.out.println(order.toString()); ``` +#### Todos os Pedidos +##### Sem Filtro +```java +OrderListResponse orders = api.order().list(); +``` + +##### Com Filtros +```java +Filters filters = new Filters() + .between("amount", "1000", "10000") + .in("status", status); +OrderListResponse orders = api.order().list(filters); +``` + +##### Com Paginação +```java +OrderListResponse orders = api.order().list(new Pagination(10,0)); +``` + +##### Consulta Valor Específico +```java +OrderListResponse orders = api.order().list("josé silva"); +``` + +> Também é possível usar paginação, filtros e consulta de valor específico juntos + +```java +Filters filters = new Filters() + .between("amount", "1000", "10000") + .in("status", status); +OrderListResponse orders = api.order().list(new Pagination(10,0), filters, "josé silva"); +``` + ## Pagamentos ### Criação From 30bfeba4d4f8ab1c7058847d306cd473f9cfa68d Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Tue, 3 Oct 2017 11:09:52 -0300 Subject: [PATCH 07/11] refactor(OrderApi): Renaming variable 'qParam'to 'queryParam' Renaming variable 'qParam'to 'queryParam' --- src/main/java/br/com/moip/api/OrderAPI.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/br/com/moip/api/OrderAPI.java b/src/main/java/br/com/moip/api/OrderAPI.java index d69d44f..8343d78 100644 --- a/src/main/java/br/com/moip/api/OrderAPI.java +++ b/src/main/java/br/com/moip/api/OrderAPI.java @@ -43,29 +43,29 @@ public OrderListResponse list(final Filters filters) { return client.get(new QueryStringFactory(PATH, null, filters, null).generate(), OrderListResponse.class); } - public OrderListResponse list(final String qParam) { - return client.get(new QueryStringFactory(PATH, null, null, hashParams(qParam)).generate(), OrderListResponse.class); + public OrderListResponse list(final String queryParam) { + return client.get(new QueryStringFactory(PATH, null, null, hashParams(queryParam)).generate(), OrderListResponse.class); } public OrderListResponse list(final Pagination pagination, final Filters filters) { return client.get(new QueryStringFactory(PATH, pagination, filters, null).generate(), OrderListResponse.class); } - public OrderListResponse list(final Pagination pagination, final String qParam) { - return client.get(new QueryStringFactory(PATH, pagination, null, hashParams(qParam)).generate(), OrderListResponse.class); + public OrderListResponse list(final Pagination pagination, final String queryParam) { + return client.get(new QueryStringFactory(PATH, pagination, null, hashParams(queryParam)).generate(), OrderListResponse.class); } - public OrderListResponse list(final Filters filters, final String qParam) { - return client.get(new QueryStringFactory(PATH, null, filters, hashParams(qParam)).generate(), OrderListResponse.class); + public OrderListResponse list(final Filters filters, final String queryParam) { + return client.get(new QueryStringFactory(PATH, null, filters, hashParams(queryParam)).generate(), OrderListResponse.class); } public OrderListResponse list(final Pagination pagination, final Filters filters, final String qParam) { return client.get(new QueryStringFactory(PATH, pagination, filters, hashParams(qParam)).generate(), OrderListResponse.class); } - private Map hashParams(final String qParam) { + private Map hashParams(final String queryParam) { return new HashMap() {{ - put("q", qParam); + put("q", queryParam); }}; } } \ No newline at end of file From 3fc071c1eba52b3b5ab24ca6b61c3da25e1fbec6 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Tue, 3 Oct 2017 15:59:05 -0300 Subject: [PATCH 08/11] feat(Multipayment): Added method 'capture' Added method 'capture' --- .../java/br/com/moip/api/MultipaymentAPI.java | 5 +++++ .../java/br/com/moip/api/MultipaymentAPITest.java | 11 +++++++++++ .../resources/plays/multipayment/capture.yaml | 15 +++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/test/resources/plays/multipayment/capture.yaml diff --git a/src/main/java/br/com/moip/api/MultipaymentAPI.java b/src/main/java/br/com/moip/api/MultipaymentAPI.java index 630314c..d0ecd00 100644 --- a/src/main/java/br/com/moip/api/MultipaymentAPI.java +++ b/src/main/java/br/com/moip/api/MultipaymentAPI.java @@ -19,4 +19,9 @@ public Multipayment create(PaymentRequest multipayment) { public Multipayment get(String multipaymentId) { return client.get("/v2/multipayments/" + multipaymentId, Multipayment.class); } + + public Multipayment capture(String multipaymentId) { + return client.post(String.format("/v2/multipayments/%s/capture", multipaymentId), null, Multipayment.class); + } + } diff --git a/src/test/java/br/com/moip/api/MultipaymentAPITest.java b/src/test/java/br/com/moip/api/MultipaymentAPITest.java index 5315bbc..3e9feb8 100644 --- a/src/test/java/br/com/moip/api/MultipaymentAPITest.java +++ b/src/test/java/br/com/moip/api/MultipaymentAPITest.java @@ -116,4 +116,15 @@ public void testGetMultipayment() { assertEquals(1, multipayment.getInstallmentCount()); assertNotNull(multipayment.getPayments()); } + + @Play("multipayment/capture") + @Test + public void testCaptureMultipayment() { + Multipayment multipayment = api.capture("MPY-UGZLJMVJ37LX"); + + assertEquals("MPY-UGZLJMVJ37LX", multipayment.getId()); + assertEquals((Integer)8000, multipayment.getAmount().getTotal()); + assertEquals(PaymentStatus.AUTHORIZED, multipayment.getStatus()); + } + } diff --git a/src/test/resources/plays/multipayment/capture.yaml b/src/test/resources/plays/multipayment/capture.yaml new file mode 100644 index 0000000..c5957fe --- /dev/null +++ b/src/test/resources/plays/multipayment/capture.yaml @@ -0,0 +1,15 @@ +!play +interactions: +- + request: + uri: /v2/multipayments/MPY-UGZLJMVJ37LX/capture + headers: + "Content-Type": application/json + Authorization: Basic MDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDE6QUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQg== + method: POST + response: + status: 200 + headers: + "Content-Type": application/json + body: | + {"id":"MPY-UGZLJMVJ37LX","status":"AUTHORIZED","amount":{"total":8000,"gross":8000,"currency":"BRL"},"installmentCount":1,"payments":[{"id":"PAY-9GG93Z35GIL9","status":"AUTHORIZED","delayCapture":true,"amount":{"total":4000,"gross":4000,"fees":289,"refunds":0,"liquid":3711,"currency":"BRL"},"installmentCount":1,"fundingInstrument":{"creditCard":{"id":"CRC-UARTCOXFHAQI","brand":"VISA","first6":"401200","last4":"1112","store":true,"holder":{"birthdate":"1988-12-30","birthDate":"1988-12-30","taxDocument":{"type":"CPF","number":"22222222222"},"fullname":"Jose Portador da Silva"}},"method":"CREDIT_CARD"},"fees":[{"type":"TRANSACTION","amount":289}],"escrows":[{"id":"ECW-WJXZOH0PNEMW","status":"HOLD_PENDING","description":"Teste","amount":4000,"createdAt":"2017-10-03T14:08:58.000-03","updatedAt":"2017-10-03T14:08:58.000-03","_links":{"self":{"href":"https://sandbox.moip.com.br/v2/escrows/ECW-WJXZOH0PNEMW"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-AKHZSK8LS19F","title":"ORD-AKHZSK8LS19F"},"payment":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-9GG93Z35GIL9","title":"PAY-9GG93Z35GIL9"}}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-10-03T14:09:19.983-03"},{"type":"PAYMENT.CREATED","createdAt":"2017-10-03T14:08:58.000-03"},{"type":"PAYMENT.IN_ANALYSIS","createdAt":"2017-10-03T14:08:58.000-03"},{"type":"PAYMENT.WAITING","createdAt":"2017-10-03T14:08:58.000-03"},{"type":"PAYMENT.PRE_AUTHORIZED","createdAt":"2017-10-03T14:08:58.000-03"}],"receivers":[{"moipAccount":{"id":"MPA-E3C9493A06AE","login":"test2@moip.com.br","fullname":"Moip Test2"},"type":"PRIMARY","amount":{"total":3500,"refunds":0}},{"moipAccount":{"id":"MPA-8D5DCB4EF8B8","login":"test@moip.com.br","fullname":"Moip Test"},"type":"SECONDARY","amount":{"total":500,"fees":0,"refunds":0},"feePayor":false}],"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-9GG93Z35GIL9"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-AKHZSK8LS19F","title":"ORD-AKHZSK8LS19F"}},"createdAt":"2017-10-03T14:08:58.000-03","updatedAt":"2017-10-03T14:09:19.978-03"},{"id":"PAY-T0GZIGY9UZ32","status":"AUTHORIZED","delayCapture":true,"amount":{"total":4000,"gross":4000,"fees":289,"refunds":0,"liquid":3711,"currency":"BRL"},"installmentCount":1,"fundingInstrument":{"creditCard":{"id":"CRC-UARTCOXFHAQI","brand":"VISA","first6":"401200","last4":"1112","store":true,"holder":{"birthdate":"1988-12-30","birthDate":"1988-12-30","taxDocument":{"type":"CPF","number":"22222222222"},"fullname":"Jose Portador da Silva"}},"method":"CREDIT_CARD"},"fees":[{"type":"TRANSACTION","amount":289}],"escrows":[{"id":"ECW-78UXUWN40EH7","status":"HOLD_PENDING","description":"Teste","amount":4000,"createdAt":"2017-10-03T14:08:58.000-03","updatedAt":"2017-10-03T14:08:58.000-03","_links":{"self":{"href":"https://sandbox.moip.com.br/v2/escrows/ECW-78UXUWN40EH7"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-SYIZEXS47ZSK","title":"ORD-SYIZEXS47ZSK"},"payment":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-T0GZIGY9UZ32","title":"PAY-T0GZIGY9UZ32"}}}],"events":[{"type":"PAYMENT.AUTHORIZED","createdAt":"2017-10-03T14:09:19.984-03"},{"type":"PAYMENT.CREATED","createdAt":"2017-10-03T14:08:58.000-03"},{"type":"PAYMENT.IN_ANALYSIS","createdAt":"2017-10-03T14:08:58.000-03"},{"type":"PAYMENT.WAITING","createdAt":"2017-10-03T14:08:58.000-03"},{"type":"PAYMENT.PRE_AUTHORIZED","createdAt":"2017-10-03T14:08:58.000-03"}],"receivers":[{"moipAccount":{"id":"MPA-8D5DCB4EF8B8","login":"test@moip.com.br","fullname":"Moip Test"},"type":"PRIMARY","amount":{"total":4000,"refunds":0}}],"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-T0GZIGY9UZ32"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-SYIZEXS47ZSK","title":"ORD-SYIZEXS47ZSK"}},"createdAt":"2017-10-03T14:08:58.000-03","updatedAt":"2017-10-03T14:09:19.983-03"}],"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/multipayments/MPY-UGZLJMVJ37LX"},"multiorder":{"href":"https://sandbox.moip.com.br/v2/multiorders/MOR-UVC9O3PH7DIU"}}} \ No newline at end of file From 99dc90cbb59a381b1d9a5b79c59ab7e3a270f439 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Tue, 3 Oct 2017 16:39:02 -0300 Subject: [PATCH 09/11] feat(Multipayment): Added method 'cancelPreAuthorized' to cancel pre authorized payments Added method 'cancelPreAuthorized' to cancel pre authorized payments --- README.md | 14 ++++++++++++++ .../java/br/com/moip/api/MultipaymentAPI.java | 8 ++++++-- .../java/br/com/moip/api/MultipaymentAPITest.java | 9 +++++++++ .../plays/multipayment/cancel_pre_authorized.yaml | 15 +++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/test/resources/plays/multipayment/cancel_pre_authorized.yaml diff --git a/README.md b/README.md index cf6f505..0ad278e 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,8 @@ - [Cartão de Crédito](#cartão-de-crédito-1) - [Boleto Bancário](#boleto-bancário) - [Consulta](#consulta-6) + - [Capturar multipagamento pré-autorizado](#capturar-multipagamento-pré-autorizado) + - [Cancelar multipagamento pré-autorizado](#cancelar-multipagamento-pré-autorizado) - [Conta Moip](#conta-moip) - [Criação](#criação-6) - [Consulta](#consulta-7) @@ -541,6 +543,18 @@ Multipayment multipayment = api.multipayment().get("MPY-OUGA0AHH2BOF"); System.out.println(multipayment); ``` +### Capturar multipagamento pré-autorizado +```java +Multipayment capturedMultipayment = api.multipayment().capture("MPY-UGZLJMVJ37LX"); +System.out.println(capturedMultipayment); +``` + +### Cancelar multipagamento pré-autorizado +```java +Multipayment cancelledMultipayment = api.multipayment().cancelPreAuthorized("MPY-YDNM3U17OSDD"); +System.out.println(cancelledMultipayment); +``` + ## Conta Moip ### Criação ```java diff --git a/src/main/java/br/com/moip/api/MultipaymentAPI.java b/src/main/java/br/com/moip/api/MultipaymentAPI.java index d0ecd00..272144f 100644 --- a/src/main/java/br/com/moip/api/MultipaymentAPI.java +++ b/src/main/java/br/com/moip/api/MultipaymentAPI.java @@ -13,15 +13,19 @@ public MultipaymentAPI(final Client client) { } public Multipayment create(PaymentRequest multipayment) { - return client.post("/v2/multiorders/" + multipayment.getOrderId() + "/multipayments", multipayment, Multipayment.class); + return client.post(String.format("/v2/multiorders/%s/multipayments", multipayment.getOrderId()), multipayment, Multipayment.class); } public Multipayment get(String multipaymentId) { - return client.get("/v2/multipayments/" + multipaymentId, Multipayment.class); + return client.get(String.format("/v2/multipayments/%s", multipaymentId), Multipayment.class); } public Multipayment capture(String multipaymentId) { return client.post(String.format("/v2/multipayments/%s/capture", multipaymentId), null, Multipayment.class); } + public Multipayment cancelPreAuthorized(String multipaymentId) { + return client.post(String.format("/v2/multipayments/%s/void", multipaymentId), null, Multipayment.class); + } + } diff --git a/src/test/java/br/com/moip/api/MultipaymentAPITest.java b/src/test/java/br/com/moip/api/MultipaymentAPITest.java index 3e9feb8..8511ac2 100644 --- a/src/test/java/br/com/moip/api/MultipaymentAPITest.java +++ b/src/test/java/br/com/moip/api/MultipaymentAPITest.java @@ -127,4 +127,13 @@ public void testCaptureMultipayment() { assertEquals(PaymentStatus.AUTHORIZED, multipayment.getStatus()); } + @Play("multipayment/cancel_pre_authorized") + @Test + public void testCancelMultipayment() { + Multipayment cancelledPayment = api.cancelPreAuthorized("MPY-YDNM3U17OSDD"); + + assertEquals("MPY-YDNM3U17OSDD", cancelledPayment.getId()); + assertEquals(PaymentStatus.CANCELLED, cancelledPayment.getStatus()); + } + } diff --git a/src/test/resources/plays/multipayment/cancel_pre_authorized.yaml b/src/test/resources/plays/multipayment/cancel_pre_authorized.yaml new file mode 100644 index 0000000..f967801 --- /dev/null +++ b/src/test/resources/plays/multipayment/cancel_pre_authorized.yaml @@ -0,0 +1,15 @@ +!play +interactions: +- + request: + uri: /v2/multipayments/MPY-YDNM3U17OSDD/void + headers: + "Content-Type": application/json + Authorization: Basic MDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDEwMTAxMDE6QUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQkFCQUJBQg== + method: POST + response: + status: 200 + headers: + "Content-Type": application/json + body: | + {"id":"MPY-YDNM3U17OSDD","status":"CANCELLED","amount":{"total":8000,"gross":8000,"currency":"BRL"},"installmentCount":1,"payments":[{"id":"PAY-QVDIEWUXS63X","status":"CANCELLED","delayCapture":true,"cancellationDetails":{"code":"7","description":"Política do Moip","cancelledBy":"MOIP"},"amount":{"total":4000,"gross":4000,"fees":289,"refunds":0,"liquid":3711,"currency":"BRL"},"installmentCount":1,"fundingInstrument":{"creditCard":{"id":"CRC-UARTCOXFHAQI","brand":"VISA","first6":"401200","last4":"1112","store":true,"holder":{"birthdate":"1988-12-30","birthDate":"1988-12-30","taxDocument":{"type":"CPF","number":"22222222222"},"fullname":"Jose Portador da Silva"}},"method":"CREDIT_CARD"},"fees":[{"type":"TRANSACTION","amount":289}],"escrows":[{"id":"ECW-IYMNMWOWNLPE","status":"HOLD_PENDING","description":"Teste","amount":4000,"createdAt":"2017-10-03T16:15:01.000-03","updatedAt":"2017-10-03T16:15:01.000-03","_links":{"self":{"href":"https://sandbox.moip.com.br/v2/escrows/ECW-IYMNMWOWNLPE"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-JXXOQ5UZDIPT","title":"ORD-JXXOQ5UZDIPT"},"payment":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-QVDIEWUXS63X","title":"PAY-QVDIEWUXS63X"}}}],"events":[{"type":"PAYMENT.CANCELLED","createdAt":"2017-10-03T16:15:30.744-03"},{"type":"PAYMENT.CREATED","createdAt":"2017-10-03T16:15:01.000-03"},{"type":"PAYMENT.IN_ANALYSIS","createdAt":"2017-10-03T16:15:01.000-03"},{"type":"PAYMENT.WAITING","createdAt":"2017-10-03T16:15:01.000-03"},{"type":"PAYMENT.PRE_AUTHORIZED","createdAt":"2017-10-03T16:15:01.000-03"}],"receivers":[{"moipAccount":{"id":"MPA-E3C9493A06AE","login":"test2@moip.com.br","fullname":"Moip Test2"},"type":"PRIMARY","amount":{"total":3500,"refunds":0}},{"moipAccount":{"id":"MPA-8D5DCB4EF8B8","login":"test@moip.com.br","fullname":"Moip Test"},"type":"SECONDARY","amount":{"total":500,"fees":0,"refunds":0},"feePayor":false}],"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-QVDIEWUXS63X"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-JXXOQ5UZDIPT","title":"ORD-JXXOQ5UZDIPT"}},"createdAt":"2017-10-03T16:15:01.000-03","updatedAt":"2017-10-03T16:15:30.743-03"},{"id":"PAY-Z56NGYZIGWLS","status":"CANCELLED","delayCapture":true,"cancellationDetails":{"code":"7","description":"Política do Moip","cancelledBy":"MOIP"},"amount":{"total":4000,"gross":4000,"fees":289,"refunds":0,"liquid":3711,"currency":"BRL"},"installmentCount":1,"fundingInstrument":{"creditCard":{"id":"CRC-UARTCOXFHAQI","brand":"VISA","first6":"401200","last4":"1112","store":true,"holder":{"birthdate":"1988-12-30","birthDate":"1988-12-30","taxDocument":{"type":"CPF","number":"22222222222"},"fullname":"Jose Portador da Silva"}},"method":"CREDIT_CARD"},"fees":[{"type":"TRANSACTION","amount":289}],"escrows":[{"id":"ECW-36HP0I3XS88K","status":"HOLD_PENDING","description":"Teste","amount":4000,"createdAt":"2017-10-03T16:15:01.000-03","updatedAt":"2017-10-03T16:15:01.000-03","_links":{"self":{"href":"https://sandbox.moip.com.br/v2/escrows/ECW-36HP0I3XS88K"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-KHE8B9HCYRGG","title":"ORD-KHE8B9HCYRGG"},"payment":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-Z56NGYZIGWLS","title":"PAY-Z56NGYZIGWLS"}}}],"events":[{"type":"PAYMENT.CANCELLED","createdAt":"2017-10-03T16:15:30.745-03"},{"type":"PAYMENT.CREATED","createdAt":"2017-10-03T16:15:01.000-03"},{"type":"PAYMENT.IN_ANALYSIS","createdAt":"2017-10-03T16:15:01.000-03"},{"type":"PAYMENT.WAITING","createdAt":"2017-10-03T16:15:01.000-03"},{"type":"PAYMENT.PRE_AUTHORIZED","createdAt":"2017-10-03T16:15:01.000-03"}],"receivers":[{"moipAccount":{"id":"MPA-8D5DCB4EF8B8","login":"test@moip.com.br","fullname":"Moip Test"},"type":"PRIMARY","amount":{"total":4000,"refunds":0}}],"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/payments/PAY-Z56NGYZIGWLS"},"order":{"href":"https://sandbox.moip.com.br/v2/orders/ORD-KHE8B9HCYRGG","title":"ORD-KHE8B9HCYRGG"}},"createdAt":"2017-10-03T16:15:01.000-03","updatedAt":"2017-10-03T16:15:30.744-03"}],"_links":{"self":{"href":"https://sandbox.moip.com.br/v2/multipayments/MPY-YDNM3U17OSDD"},"multiorder":{"href":"https://sandbox.moip.com.br/v2/multiorders/MOR-EKNI6LTAGTL2"}}} \ No newline at end of file From 072e5c1f4210a292be6f3ebbd3bc5d9f0ccfcd79 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Tue, 10 Oct 2017 10:14:35 -0300 Subject: [PATCH 10/11] chore: Bump version to v3.3.0 Bump version to v3.3.0 --- CHANGELOG.md | 14 +++++++++++++- build.gradle | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index beb6138..039c4f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,15 @@ + +# [](https://github.com/moip/moip-sdk-java/compare/v3.2.0...v3.3.0) (2017-10-10) + + +### Features + +* **Multipayment:** Added method 'cancelPreAuthorized' to cancel pre authorized payments ([be2fc6c](https://github.com/moip/moip-sdk-java/commit/be2fc6c)) +* **Multipayment:** Added method 'capture' ([0a634c3](https://github.com/moip/moip-sdk-java/commit/0a634c3)) +* **Order:** added method to get an order list ([8602c59](https://github.com/moip/moip-sdk-java/commit/8602c59)) + + + # [](https://github.com/moip/moip-sdk-java/compare/v3.1.0...v3.2.0) (2017-09-28) @@ -16,7 +28,7 @@ ### Features -* Create structure to HATEOAS links in Payment and fix toString methods to some Link Resources ([cf6c46f](https://github.com/caiogaspar/moip-sdk-java/commit/cf6c46f)) +* Create structure to HATEOAS links in Payment and fix toString methods to some Link Resources ([cf6c46f](https://github.com/moip/moip-sdk-java/commit/cf6c46f)) diff --git a/build.gradle b/build.gradle index 3654aef..0f80920 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ apply plugin: 'signing' group = 'br.com.moip' archivesBaseName = "java-sdk" -version = '3.2.0' +version = '3.3.0' description = "Moip v2 SDK" From b2674c5abd6e50beb02d47afa087d5fb07b20663 Mon Sep 17 00:00:00 2001 From: Caio Gaspar Date: Tue, 10 Oct 2017 10:22:45 -0300 Subject: [PATCH 11/11] docs: Update readme Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ad278e..9e22a01 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ Adicionar no seu pom.xml: br.com.moip java-sdk - 3.2.0 + 3.3.0 ```