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..0fd4e860 --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreClient.java @@ -0,0 +1,278 @@ +package com.mercadopago.client.store; + +import static com.mercadopago.MercadoPagoConfig.getStreamHandler; +import static com.mercadopago.serialization.Serializer.deserializeElementsResourcesPageFromJson; +import static com.mercadopago.serialization.Serializer.deserializeFromJson; +import static com.mercadopago.serialization.Serializer.serializeToJson; + +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.net.UrlFormatter; +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 account.
+ *
+ * @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 the store creation and search endpoints. */
+ private static final String URL_WITH_USER_ID = "/users/%s/stores";
+
+ /** URL template for the single-store endpoints. */
+ private static final String URL_WITH_ID = "/stores/%s";
+
+ /** URL template for the update and delete endpoints. */
+ private static final String URL_WITH_USER_AND_ID = "/users/%s/stores/%s";
+
+ /**
+ * 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 unique identifier of the store owner user
+ * @param request the {@link StoreCreateRequest} with the store details
+ * @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
+ */
+ public Store create(Long userId, StoreCreateRequest request) throws MPException, MPApiException {
+ return this.create(userId, request, null);
+ }
+
+ /**
+ * Creates a new store for the given user with custom request options.
+ *
+ * @param userId the unique identifier of the store owner user
+ * @param request the {@link StoreCreateRequest} with the store details
+ * @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
+ */
+ public Store create(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, encodePathParam(userId)))
+ .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
+ */
+ public Store get(String id) throws MPException, MPApiException {
+ return this.get(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
+ */
+ public Store get(String id, MPRequestOptions requestOptions) throws MPException, MPApiException {
+ LOGGER.info("Sending get store request");
+
+ MPRequest mpRequest =
+ MPRequest.builder()
+ .uri(String.format(URL_WITH_ID, encodePathParam(id)))
+ .method(HttpMethod.GET)
+ .build();
+
+ MPResponse response = send(mpRequest, requestOptions);
+ Store result = deserializeFromJson(Store.class, response.getContent());
+ result.setResponse(response);
+
+ return result;
+ }
+
+ /**
+ * Searches for stores of the given user matching the specified criteria.
+ *
+ * @param userId the unique identifier of the store owner user
+ * @param searchRequest the {@link MPSearchRequest} containing search filters and pagination
+ * parameters
+ * @return an {@link MPElementsResourcesPage} of {@link Store} with matching results
+ * @throws MPException if a transport-level or SDK-internal error occurs
+ * @throws MPApiException if the API returns a non-successful HTTP status code
+ */
+ public MPElementsResourcesPage Only the attributes that the Stores API accepts on update are modeled here. Any field left
+ * as {@code null} is omitted from the JSON payload by the serializer, so only the provided
+ * attributes are updated.
+ *
+ * @see
+ * Stores API reference
+ * @see StoreClient#update(Long, String, StoreUpdateRequest)
+ */
+@Getter
+@Builder
+public class StoreUpdateRequest {
+ /** Name of the store to be updated. */
+ private final String name;
+
+ /** Geographic location of the store. */
+ private final LocationRequest location;
+
+ /** Business hours of the store. */
+ private final BusinessHoursRequest businessHours;
+
+ /** Additional data about the store. */
+ private final StoreUpdateAdditionalInfoRequest additionalInfo;
+
+ /**
+ * Geographic location of the store included in an update request.
+ *
+ * Mirrors the {@code location} object of the store update request schema.
+ */
+ @Getter
+ @Builder
+ public static class LocationRequest {
+ /** Street number of the store address. */
+ private final Integer streetNumber;
+
+ /** Street name of the store address. */
+ private final String streetName;
+
+ /** City name where the store is located. */
+ private final String cityName;
+
+ /** State name where the store is located. */
+ private final String stateName;
+
+ /** Latitude coordinate of the store location. */
+ private final Double latitude;
+
+ /** Longitude coordinate of the store location. */
+ private final Double longitude;
+
+ /** Reference to help find the store location. */
+ private final String reference;
+ }
+
+ /**
+ * Business hours of the store included in an update request.
+ *
+ * Mirrors the {@code business_hours} object of the store update request schema.
+ */
+ @Getter
+ @Builder
+ public static class BusinessHoursRequest {
+ /** Business hours for Monday. */
+ private final DayHoursRequest monday;
+
+ /** Business hours for Tuesday. */
+ private final DayHoursRequest tuesday;
+
+ /** Business hours for Wednesday. */
+ private final DayHoursRequest wednesday;
+
+ /** Business hours for Thursday. */
+ private final DayHoursRequest thursday;
+
+ /** Business hours for Friday. */
+ private final DayHoursRequest friday;
+
+ /** Business hours for Saturday. */
+ private final DayHoursRequest saturday;
+
+ /** Business hours for Sunday. */
+ private final DayHoursRequest sunday;
+ }
+
+ /**
+ * Open and close hours for a single day of the week.
+ */
+ @Getter
+ @Builder
+ public static class DayHoursRequest {
+ /** Opening hour of the store (e.g. {@code "08:00"}). */
+ private final String open;
+
+ /** Closing hour of the store (e.g. {@code "18:00"}). */
+ private final String close;
+ }
+
+ /**
+ * Additional data about the store included in an update request.
+ *
+ * Mirrors the {@code additional_info} object of the store update request schema.
+ */
+ @Getter
+ @Builder
+ public static class StoreUpdateAdditionalInfoRequest {
+ /** External identifier of the store in the integrator's system. */
+ private final String externalId;
+ }
+}
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..bae83754
--- /dev/null
+++ b/src/main/java/com/mercadopago/resources/store/Store.java
@@ -0,0 +1,89 @@
+package com.mercadopago.resources.store;
+
+import com.google.gson.annotations.SerializedName;
+import com.mercadopago.net.MPResource;
+import lombok.Getter;
+
+/**
+ * Resource representing a MercadoPago store.
+ *
+ * Stores are physical or logical branches registered under a seller user account, used for
+ * in-person payments (e.g. Point of Sale devices). A store holds business information such as
+ * its name, opening hours, location, and an external identifier supplied by the integrator.
+ *
+ * Instances are returned by the create, get, update and search operations of {@link
+ * com.mercadopago.client.store.StoreClient}.
+ *
+ * @see com.mercadopago.client.store.StoreClient
+ */
+@Getter
+public class Store extends MPResource {
+
+ /** Unique numeric identifier of the store. */
+ private Long id;
+
+ /** Numeric identifier of the user (seller) that owns the store. */
+ @SerializedName("user_id")
+ private Long userId;
+
+ /** Name of the store. */
+ private String name;
+
+ /** Opening hours of the store, as supplied by the integrator. */
+ @SerializedName("business_hours")
+ private Object businessHours;
+
+ /** ISO code of the timezone the store operates in (e.g. America/Argentina/Buenos_Aires). */
+ @SerializedName("time_zone")
+ private String timeZone;
+
+ /** Properties describing the geographic location of the store. */
+ private StoreLocation location;
+
+ /** External identifier supplied by the integrator when the store was created. */
+ private String externalId;
+
+ /** Date when the store was created, in ISO 8601 format. */
+ @SerializedName("date_created")
+ private String dateCreated;
+
+ /**
+ * Nested location properties of the store as declared by the Store response schema.
+ */
+ @Getter
+ public static class StoreLocation {
+
+ /** Street address line of the store (street name and number). */
+ @SerializedName("address_line")
+ private String addressLine;
+
+ /** Street name of the store address. */
+ @SerializedName("street_name")
+ private String streetName;
+
+ /** House or street number of the store address. */
+ @SerializedName("street_number")
+ private String streetNumber;
+
+ /** Name of the city where the store is located. */
+ @SerializedName("city")
+ private String city;
+
+ /** Name of the state or province where the store is located. */
+ @SerializedName("state")
+ private String state;
+
+ /** Postal code of the store address. */
+ @SerializedName("zip_code")
+ private String zipCode;
+
+ /** Latitude coordinate of the store location. */
+ private Double latitude;
+
+ /** Longitude coordinate of the store location. */
+ private Double longitude;
+
+ /** Reference point or additional address hint (e.g. a nearby landmark). */
+ private String reference;
+ }
+}
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..b8873435
--- /dev/null
+++ b/src/test/java/com/mercadopago/client/store/StoreClientTest.java
@@ -0,0 +1,210 @@
+package com.mercadopago.client.store;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+
+import com.mercadopago.BaseClientTest;
+import com.mercadopago.core.MPRequestOptions;
+import com.mercadopago.exceptions.MPApiException;
+import com.mercadopago.exceptions.MPException;
+import com.google.gson.JsonElement;
+import com.mercadopago.helper.MockHelper;
+import com.mercadopago.net.HttpMethod;
+import com.mercadopago.net.HttpStatus;
+import com.mercadopago.net.MPElementsResourcesPage;
+import com.mercadopago.net.MPSearchRequest;
+import com.mercadopago.resources.store.Store;
+import java.io.IOException;
+import org.apache.http.HttpHeaders;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.protocol.HttpContext;
+import org.junit.jupiter.api.Test;
+
+/** Tests for {@link StoreClient}. */
+class StoreClientTest extends BaseClientTest {
+
+ private static final String STORE_BASE_RESPONSE =
+ "/mocks/response/store/store_base.json";
+ private static final String STORE_UPDATED_RESPONSE =
+ "/mocks/response/store/store_updated.json";
+ private static final String STORE_SEARCH_RESPONSE =
+ "/mocks/response/store/store_search.json";
+ private static final String STORE_CREATE_REQUEST =
+ "/mocks/request/store/store_base.json";
+ private static final String STORE_UPDATE_REQUEST =
+ "/mocks/request/store/store_update.json";
+
+ @Test
+ void createSuccess() throws IOException, MPException, MPApiException {
+ HttpResponse httpResponse =
+ MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.CREATED);
+ httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreClient client = new StoreClient(HTTP_CLIENT);
+ StoreCreateRequest request =
+ StoreCreateRequest.builder().name("Store Name").build();
+ Store store = client.create(123L, request);
+
+ assertNotNull(store);
+ assertEquals("Store Name", store.getName());
+
+ JsonElement requestPayload =
+ MockHelper.generateJsonElementFromUriRequest(httpRequestCaptor.getValue());
+ assertEquals(
+ MockHelper.generateJsonElement(STORE_CREATE_REQUEST), requestPayload);
+ }
+
+ @Test
+ void createWithOptionsSuccess() throws IOException, MPException, MPApiException {
+ HttpResponse httpResponse =
+ MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.CREATED);
+ httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreClient client = new StoreClient(HTTP_CLIENT);
+ StoreCreateRequest request =
+ StoreCreateRequest.builder().name("Store Name").build();
+ Store store = client.create(123L, request, buildRequestOptions());
+
+ assertNotNull(store);
+ assertEquals("Store Name", store.getName());
+ }
+
+ @Test
+ void getSuccess() throws IOException, MPException, MPApiException {
+ HttpResponse httpResponse =
+ MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.OK);
+ httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreClient client = new StoreClient(HTTP_CLIENT);
+ Store store = client.get("123");
+
+ assertNotNull(store);
+ assertEquals("Store Name", store.getName());
+ }
+
+ @Test
+ void getWithOptionsSuccess() throws IOException, MPException, MPApiException {
+ HttpResponse httpResponse =
+ MockHelper.generateHttpResponseFromFile(STORE_BASE_RESPONSE, HttpStatus.OK);
+ httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreClient client = new StoreClient(HTTP_CLIENT);
+ Store store = client.get("123", buildRequestOptions());
+
+ assertNotNull(store);
+ assertEquals("Store Name", store.getName());
+ }
+
+ @Test
+ void searchSuccess() throws IOException, MPException, MPApiException {
+ HttpResponse httpResponse =
+ MockHelper.generateHttpResponseFromFile(STORE_SEARCH_RESPONSE, HttpStatus.OK);
+ httpResponse.setHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
+ doReturn(httpResponse)
+ .when(HTTP_CLIENT)
+ .execute(any(HttpRequestBase.class), any(HttpContext.class));
+
+ StoreClient client = new StoreClient(HTTP_CLIENT);
+ MPSearchRequest searchRequest =
+ MPSearchRequest.builder().limit(10).offset(0).build();
+ MPElementsResourcesPage