Skip to content

Commit b116905

Browse files
committed
Added endpoint get order by id
1 parent 5fe947d commit b116905

File tree

4 files changed

+83
-2
lines changed

4 files changed

+83
-2
lines changed

src/main/java/com/mercadopago/client/order/OrderClient.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
public class OrderClient extends MercadoPagoClient {
2222
private static final Logger LOGGER = Logger.getLogger(OrderClient.class.getName());
2323

24+
private static final String URL_WITH_ID = "/v1/orders/%s";
25+
2426
/** Default constructor. Uses the default http client used by the SDK. */
2527
public OrderClient() {
2628
this(MercadoPagoConfig.getHttpClient());
@@ -66,7 +68,7 @@ public Order create(OrderCreateRequest request, MPRequestOptions requestOptions)
6668
}
6769

6870
/**
69-
* Method responsible for creating order with request options
71+
* Method responsible for creating order without request options
7072
*
7173
* @param request request
7274
* @return order response
@@ -77,4 +79,37 @@ public Order create(OrderCreateRequest request) throws MPException, MPApiExcepti
7779
return this.create(request, null);
7880
}
7981

82+
/**
83+
* Method responsible for obtaining order by id
84+
*
85+
* @param id orderId
86+
* @return order response
87+
* @throws MPException an error if the request fails
88+
* @throws MPApiException an error if the request fails
89+
*/
90+
public Order get(String id) throws MPException, MPApiException {
91+
return this.get(id, null);
92+
}
93+
94+
/**
95+
* Method responsible for obtaining order by id with request options
96+
*
97+
* @param id orderId
98+
* @param requestOptions metadata to customize the request
99+
* @return order response
100+
* @throws MPException an error if the request fails
101+
* @throws MPApiException an error if the request fails
102+
*/
103+
public Order get(String id, MPRequestOptions requestOptions) throws MPException, MPApiException {
104+
LOGGER.info("Sending order get request");
105+
106+
MPResponse response =
107+
send(String.format(URL_WITH_ID, id), HttpMethod.GET, null, null, requestOptions);
108+
109+
Order result = Serializer.deserializeFromJson(Order.class, response.getContent());
110+
result.setResponse(response);
111+
112+
return result;
113+
114+
}
80115
}

src/main/java/com/mercadopago/example/apis/order/createOrder.java renamed to src/main/java/com/mercadopago/example/apis/order/CreateOrder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import java.util.List;
1111
import java.util.Map;
1212

13-
public class createOrder {
13+
public class CreateOrder {
1414

1515
public static void main(String[] args) {
1616
MercadoPagoConfig.setAccessToken("{{ACCESS_TOKEN}}");
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.mercadopago.example.apis.order;
2+
3+
import com.mercadopago.MercadoPagoConfig;
4+
import com.mercadopago.client.order.OrderClient;
5+
import com.mercadopago.core.MPRequestOptions;
6+
import com.mercadopago.exceptions.MPApiException;
7+
import com.mercadopago.exceptions.MPException;
8+
import com.mercadopago.resources.order.Order;
9+
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
public class GetOrderById {
14+
15+
public static void main(String[] args) {
16+
MercadoPagoConfig.setAccessToken("{{ACCESS_TOKEN}}");
17+
18+
OrderClient client = new OrderClient();
19+
20+
Map<String, String> headers = new HashMap<>();
21+
headers.put("X-Sandbox", "true");
22+
MPRequestOptions requestOptions = MPRequestOptions.builder()
23+
.customHeaders(headers)
24+
.build();
25+
26+
try {
27+
Order order = client.get("{{ORDER_ID}}", requestOptions);
28+
System.out.println("Getting order: " + order.getId());
29+
} catch (MPException | MPApiException e) {
30+
System.out.println("Error getting order: " + e.getMessage());
31+
}
32+
}
33+
}

src/test/java/com/mercadopago/client/order/OrderClientTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,17 @@ private static OrderCreateRequest getMinimumOrderCreateRequest() {
6868
.build())
6969
.build();
7070
}
71+
72+
@Test
73+
void getSuccess() throws MPException, MPApiException, IOException {
74+
HttpResponse response = MockHelper.generateHttpResponseFromFile(CREATE_ORDER_RESPONSE_FILE, HttpStatus.OK);
75+
Mockito.doReturn(response).when(HTTP_CLIENT).execute(any(HttpRequestBase.class), any(HttpContext.class));
76+
77+
String orderId = "123";
78+
Order order = client.get(orderId);
79+
80+
Assertions.assertNotNull(order);
81+
Assertions.assertEquals(orderId, order.getId());
82+
83+
}
7184
}

0 commit comments

Comments
 (0)