Skip to content

Commit

Permalink
include process by id
Browse files Browse the repository at this point in the history
  • Loading branch information
danielen-meli authored Nov 4, 2024
1 parent b116905 commit d8f678a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/main/java/com/mercadopago/client/order/OrderClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, String> 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());
}
}
}
12 changes: 12 additions & 0 deletions src/test/java/com/mercadopago/client/order/OrderClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit d8f678a

Please sign in to comment.