diff --git a/src/main/java/com/mercadopago/client/store/StoreClient.java b/src/main/java/com/mercadopago/client/store/StoreClient.java new file mode 100644 index 00000000..e9c59fe9 --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreClient.java @@ -0,0 +1,316 @@ +package com.mercadopago.client.store; + +import static com.mercadopago.MercadoPagoConfig.getStreamHandler; +import static com.mercadopago.serialization.Serializer.deserializeFromJson; + +import static com.mercadopago.serialization.Serializer.deserializeElementsResourcesPageFromJson; + +import com.google.gson.JsonObject; +import com.google.gson.reflect.TypeToken; +import com.mercadopago.MercadoPagoConfig; +import com.mercadopago.client.MercadoPagoClient; +import com.mercadopago.core.MPRequestOptions; +import com.mercadopago.exceptions.MPApiException; +import com.mercadopago.exceptions.MPException; +import com.mercadopago.net.HttpMethod; +import com.mercadopago.net.MPElementsResourcesPage; +import com.mercadopago.net.MPHttpClient; +import com.mercadopago.net.MPRequest; +import com.mercadopago.net.MPResponse; +import com.mercadopago.net.MPSearchRequest; +import com.mercadopago.resources.store.Store; +import com.mercadopago.serialization.Serializer; +import java.lang.reflect.Type; +import java.util.logging.Logger; +import java.util.logging.StreamHandler; + +/** + * Client for the MercadoPago Stores API. + * + *
Provides operations to create, retrieve, search, update, and delete stores associated with a + * MercadoPago user. Stores represent physical locations where payments can be collected in person. + * + *
Usage example: + *
{@code
+ * StoreClient client = new StoreClient();
+ * Store store = client.createStore(userId, storeCreateRequest);
+ * Store retrieved = client.getStore(store.getId());
+ * }
+ *
+ * @see
+ * Stores API reference
+ */
+public class StoreClient extends MercadoPagoClient {
+
+ /** Class-level logger for store operations. */
+ private static final Logger LOGGER = Logger.getLogger(StoreClient.class.getName());
+
+ /** URL template for store endpoints scoped to a user (e.g. {@code /users/{user_id}/stores}). */
+ private static final String URL_WITH_USER_ID = "/users/%s/stores";
+
+ /** URL template for store endpoints scoped to a user and a store id. */
+ private static final String URL_WITH_USER_ID_AND_ID = "/users/%s/stores/%s";
+
+ /** URL template for the single-store endpoint (e.g. {@code /stores/{id}}). */
+ private static final String URL_WITH_ID = "/stores/%s";
+
+ /** URL template for the store search endpoint scoped to a user. */
+ private static final String URL_SEARCH_WITH_USER_ID = "/users/%s/stores/search";
+
+ /** Default constructor. Uses the default HTTP client provided by {@link MercadoPagoConfig}. */
+ public StoreClient() {
+ this(MercadoPagoConfig.getHttpClient());
+ }
+
+ /**
+ * Constructs a {@code StoreClient} with a custom HTTP client.
+ *
+ * @param httpClient the {@link MPHttpClient} implementation used to execute HTTP requests
+ */
+ public StoreClient(MPHttpClient httpClient) {
+ super(httpClient);
+ StreamHandler streamHandler = getStreamHandler();
+ streamHandler.setLevel(MercadoPagoConfig.getLoggingLevel());
+ LOGGER.addHandler(streamHandler);
+ LOGGER.setLevel(MercadoPagoConfig.getLoggingLevel());
+ }
+
+ /**
+ * Creates a new store for the given user.
+ *
+ * @param userId the identifier of the user that will own the store
+ * @param request the {@link StoreCreateRequest} with the store details (name, business hours,
+ * location, etc.)
+ * @return the created {@link Store}
+ * @throws MPException if a transport-level or SDK-internal error occurs
+ * @throws MPApiException if the API returns a non-successful HTTP status code
+ * @see api
+ * docs
+ */
+ public Store createStore(Long userId, StoreCreateRequest request)
+ throws MPException, MPApiException {
+ return this.createStore(userId, request, null);
+ }
+
+ /**
+ * Creates a new store for the given user with custom request options.
+ *
+ * @param userId the identifier of the user that will own the store
+ * @param request the {@link StoreCreateRequest} with the store details (name, business hours,
+ * location, etc.)
+ * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or
+ * timeouts for this single request; may be {@code null}
+ * @return the created {@link Store}
+ * @throws MPException if a transport-level or SDK-internal error occurs
+ * @throws MPApiException if the API returns a non-successful HTTP status code
+ * @see api
+ * docs
+ */
+ public Store createStore(Long userId, StoreCreateRequest request, MPRequestOptions requestOptions)
+ throws MPException, MPApiException {
+ LOGGER.info("Sending create store request");
+
+ MPRequest mpRequest =
+ MPRequest.builder()
+ .uri(String.format(URL_WITH_USER_ID, userId.toString()))
+ .method(HttpMethod.POST)
+ .payload(Serializer.serializeToJson(request))
+ .build();
+
+ MPResponse response = send(mpRequest, requestOptions);
+ Store result = deserializeFromJson(Store.class, response.getContent());
+ result.setResponse(response);
+
+ return result;
+ }
+
+ /**
+ * Retrieves a store by its unique identifier.
+ *
+ * @param id the unique identifier of the store
+ * @return the requested {@link Store}
+ * @throws MPException if a transport-level or SDK-internal error occurs
+ * @throws MPApiException if the API returns a non-successful HTTP status code
+ * @see api
+ * docs
+ */
+ public Store getStore(Long id) throws MPException, MPApiException {
+ return this.getStore(id, null);
+ }
+
+ /**
+ * Retrieves a store by its unique identifier with custom request options.
+ *
+ * @param id the unique identifier of the store
+ * @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or
+ * timeouts for this single request; may be {@code null}
+ * @return the requested {@link Store}
+ * @throws MPException if a transport-level or SDK-internal error occurs
+ * @throws MPApiException if the API returns a non-successful HTTP status code
+ * @see api
+ * docs
+ */
+ public Store getStore(Long id, MPRequestOptions requestOptions) throws MPException, MPApiException {
+ LOGGER.info("Sending get store request");
+
+ MPRequest mpRequest =
+ MPRequest.builder()
+ .uri(String.format(URL_WITH_ID, id.toString()))
+ .method(HttpMethod.GET)
+ .build();
+
+ MPResponse response = send(mpRequest, requestOptions);
+ Store result = deserializeFromJson(Store.class, response.getContent());
+ result.setResponse(response);
+
+ return result;
+ }
+
+ /**
+ * Searches for stores belonging to the given user matching the specified criteria.
+ *
+ * @param userId the identifier of the user that owns the stores
+ * @param request the {@link MPSearchRequest} containing search filters and pagination parameters
+ * @return an {@link MPResultsResourcesPage} of {@link Store} with matching results and paging
+ * metadata
+ * @throws MPException if a transport-level or SDK-internal error occurs
+ * @throws MPApiException if the API returns a non-successful HTTP status code
+ * @see api
+ * docs
+ */
+ public MPElementsResourcesPagePOST /users/{user_id}/stores endpoint. A store represents a physical
+ * branch of a merchant where payments can be collected.
+ *
+ * The request body is kept opaque with respect to nested structures: only the documented + * fields are exposed, without imposing any requiredness or validation rules.
+ * + * @see Stores API Reference + */ +@Getter +@Builder +public class StoreCreateRequest { + /** Name of the store. */ + private final String name; + + /** Location of the store, including geographic coordinates and address information. */ + private final Object location; + + /** Business hours during which the store is open for operation. */ + private final Object businessHours; +} diff --git a/src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java b/src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java new file mode 100644 index 00000000..b2620d49 --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java @@ -0,0 +1,25 @@ +package com.mercadopago.client.store; + +import lombok.Builder; +import lombok.Getter; + +/** + * Request DTO used to update an existing Store. Only the fields present in the + * PUT /users/{user_id}/stores/{id} request body are exposed; any field left unset is + * not modified. + * + * @see Stores API Reference + */ +@Getter +@Builder +public class StoreUpdateRequest { + + /** Name of the store. */ + private final String name; + + /** Geographic location of the store. */ + private final Object location; + + /** Business hours of the store. */ + private final Object businessHours; +} diff --git a/src/main/java/com/mercadopago/resources/store/Store.java b/src/main/java/com/mercadopago/resources/store/Store.java new file mode 100644 index 00000000..1f7213a5 --- /dev/null +++ b/src/main/java/com/mercadopago/resources/store/Store.java @@ -0,0 +1,41 @@ +package com.mercadopago.resources.store; + +import com.fasterxml.jackson.annotation.JsonInclude; +import com.mercadopago.net.MPResource; +import lombok.Getter; +import lombok.Setter; + +/** + * Resource representing a MercadoPago store. + * + *Stores group physical or online points of sale (POS) belonging to a seller account. They are
+ * managed through the Stores API under the {@code /users/{user_id}/stores} endpoints.
+ *
+ * @see com.mercadopago.client.user.UserClient
+ */
+@Getter
+@Setter
+@JsonInclude(JsonInclude.Include.NON_NULL)
+public class Store extends MPResource {
+
+ /** Unique identifier of the store. */
+ private Long id;
+
+ /** Name of the store. */
+ private String name;
+
+ /** Business hours of the store, keyed by day of the week. */
+ private Object businessHours;
+
+ /** Location of the store. */
+ private Object location;
+
+ /** Identifier of the user account that owns the store. */
+ private Long userId;
+
+ /** Date of creation of the store. */
+ private String dateCreated;
+
+ /** Date of the last update of the store. */
+ private String dateLastUpdated;
+}
diff --git a/src/test/java/com/mercadopago/client/store/StoreClientTest.java b/src/test/java/com/mercadopago/client/store/StoreClientTest.java
new file mode 100644
index 00000000..a884e223
--- /dev/null
+++ b/src/test/java/com/mercadopago/client/store/StoreClientTest.java
@@ -0,0 +1,253 @@
+package com.mercadopago.client.store;
+
+import static com.mercadopago.helper.MockHelper.generateHttpResponseFromFile;
+import static com.mercadopago.helper.MockHelper.generateJsonElement;
+import static com.mercadopago.helper.MockHelper.generateJsonElementFromUriRequest;
+import static com.mercadopago.net.HttpStatus.CREATED;
+import static com.mercadopago.net.HttpStatus.OK;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+
+import com.google.gson.JsonElement;
+import com.mercadopago.BaseClientTest;
+import com.mercadopago.exceptions.MPApiException;
+import com.mercadopago.exceptions.MPException;
+import com.mercadopago.net.MPElementsResourcesPage;
+import com.mercadopago.net.MPSearchRequest;
+import com.mercadopago.resources.store.Store;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.protocol.HttpContext;
+import org.junit.jupiter.api.Test;
+
+class StoreClientTest extends BaseClientTest {
+ private final String storeBaseJson = "store/store_base.json";
+
+ private final String storeUpdatedJson = "store/store_update.json";
+
+ private final String storeListJson = "store/store_list.json";
+
+ private final long userId = 471763365L;
+
+ private final String storeId = "784821936";
+
+ private final StoreClient client = new StoreClient();
+
+ @Test
+ void createSuccess() throws MPException, MPApiException, IOException {
+ HttpResponse httpResponse = generateHttpResponseFromFile(storeBaseJson, CREATED);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreCreateRequest request =
+ StoreCreateRequest.builder()
+ .name("Official Store")
+ .businessHours(
+ StoreBusinessHoursRequest.builder()
+ .monday("08:00-18:00")
+ .tuesday("08:00-18:00")
+ .build())
+ .location(
+ StoreLocationRequest.builder()
+ .addressLine("Av. das Nações Unidas 3003")
+ .zipCode("06233200")
+ .build())
+ .build();
+
+ Store store = client.createStore(userId, request);
+
+ JsonElement requestPayload =
+ generateJsonElementFromUriRequest(HTTP_CLIENT_MOCK.getRequestPayload());
+ JsonElement requestPayloadMock = generateJsonElement(storeBaseJson);
+
+ assertEquals(requestPayloadMock, requestPayload);
+ assertNotNull(store.getResponse());
+ assertEquals(CREATED, store.getResponse().getStatusCode());
+ assertStoreFields(store);
+ }
+
+ @Test
+ void createWithRequestOptionsSuccess() throws MPException, MPApiException, IOException {
+ HttpResponse httpResponse = generateHttpResponseFromFile(storeBaseJson, CREATED);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreCreateRequest request =
+ StoreCreateRequest.builder().name("Official store").build();
+
+ Store store = client.createStore(userId, request, buildRequestOptions());
+
+ JsonElement requestPayload =
+ generateJsonElementFromUriRequest(HTTP_CLIENT_MOCK.getRequestPayload());
+ JsonElement requestPayloadMock = generateJsonElement(storeBaseJson);
+
+ assertEquals(requestPayloadMock, requestPayload);
+ assertNotNull(store.getResponse());
+ assertEquals(CREATED, store.getResponse().getStatusCode());
+ assertStoreFields(store);
+ }
+
+ @Test
+ void getSuccess() throws MPException, MPApiException, IOException {
+ HttpResponse httpResponse = generateHttpResponseFromFile(storeBaseJson, OK);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ Store store = client.get(storeId);
+
+ assertNotNull(store.getResponse());
+ assertEquals(OK, store.getResponse().getStatusCode());
+ assertStoreFields(store);
+ }
+
+ @Test
+ void getWithRequestOptionsSuccess() throws MPException, MPApiException, IOException {
+ HttpResponse httpResponse = generateHttpResponseFromFile(storeBaseJson, OK);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ Store store = client.get(storeId, buildRequestOptions());
+
+ assertNotNull(store.getResponse());
+ assertEquals(OK, store.getResponse().getStatusCode());
+ assertStoreFields(store);
+ }
+
+ @Test
+ void searchSuccess() throws MPException, MPApiException, IOException {
+ HttpResponse httpResponse = generateHttpResponseFromFile(storeListJson, OK);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ Map