diff --git a/src/main/java/com/mercadopago/client/order/OrderClient.java b/src/main/java/com/mercadopago/client/order/OrderClient.java index 24d39ec2..29bc05d9 100644 --- a/src/main/java/com/mercadopago/client/order/OrderClient.java +++ b/src/main/java/com/mercadopago/client/order/OrderClient.java @@ -112,4 +112,42 @@ public Order get(String id, MPRequestOptions requestOptions) throws MPException, return result; } + + /** + * Method responsible for process an order by ID + * + * @param id orderId + * @return order response + * @throws MPException an error if the request fails + * @throws MPApiException an error if the request fails + */ + public Order process(String id) throws MPException, MPApiException { + return this.process(id, null); + } + + /** + * Method responsible for process an order by ID with request options + * + * @param id orderId + * @param requestOptions metadata to customize the request + * @return order response + * @throws MPException an error if the request fails + * @throws MPApiException an error if the request fails + */ + public Order process(String id, MPRequestOptions requestOptions) throws MPException, MPApiException { + LOGGER.info("Sending order process request"); + + if (id == null || id.isEmpty()) { + throw new IllegalArgumentException("Order id cannot be null or empty"); + } + + String processUrl = String.format(URL_WITH_ID, id) + "/process"; + + MPResponse response = send(processUrl, HttpMethod.POST, null, null, requestOptions); + + Order result = Serializer.deserializeFromJson(Order.class, response.getContent()); + result.setResponse(response); + + return result; + } } diff --git a/src/main/java/com/mercadopago/example/apis/order/ProcessOrderById.java b/src/main/java/com/mercadopago/example/apis/order/ProcessOrderById.java new file mode 100644 index 00000000..4122bc88 --- /dev/null +++ b/src/main/java/com/mercadopago/example/apis/order/ProcessOrderById.java @@ -0,0 +1,33 @@ +package com.mercadopago.example.apis.order; + +import com.mercadopago.MercadoPagoConfig; +import com.mercadopago.client.order.OrderClient; +import com.mercadopago.core.MPRequestOptions; +import com.mercadopago.exceptions.MPApiException; +import com.mercadopago.exceptions.MPException; +import com.mercadopago.resources.order.Order; + +import java.util.HashMap; +import java.util.Map; + +public class ProcessOrderById { + + public static void main(String[] args) { + MercadoPagoConfig.setAccessToken("{{ACCESS_TOKEN}}"); + + OrderClient client = new OrderClient(); + + Map headers = new HashMap<>(); + headers.put("X-Sandbox", "true"); + MPRequestOptions requestOptions = MPRequestOptions.builder() + .customHeaders(headers) + .build(); + + try { + Order order = client.process("{{ORDER_ID}}", requestOptions); + System.out.println("Process} order: " + order.getId()); + } catch (MPException | MPApiException e) { + System.out.println("Error getting order: " + e.getMessage()); + } + } +} \ No newline at end of file diff --git a/src/test/java/com/mercadopago/client/order/OrderClientTest.java b/src/test/java/com/mercadopago/client/order/OrderClientTest.java index 03c8eeb0..d9ae8a9c 100644 --- a/src/test/java/com/mercadopago/client/order/OrderClientTest.java +++ b/src/test/java/com/mercadopago/client/order/OrderClientTest.java @@ -81,4 +81,16 @@ void getSuccess() throws MPException, MPApiException, IOException { Assertions.assertEquals(orderId, order.getId()); } + + @Test + void processSucess() throws MPException, MPApiException, IOException { + HttpResponse response = MockHelper.generateHttpResponseFromFile(CREATE_ORDER_RESPONSE_FILE, HttpStatus.OK); + Mockito.doReturn(response).when(HTTP_CLIENT).execute(any(HttpRequestBase.class), any(HttpContext.class)); + + String orderId = "123"; + Order order = client.process(orderId); + + Assertions.assertNotNull(order); + Assertions.assertEquals(orderId, order.getId()); + } } \ No newline at end of file