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..27c1d7bb --- /dev/null +++ b/src/main/java/com/mercadopago/client/store/StoreClient.java @@ -0,0 +1,275 @@ +package com.mercadopago.client.store; + +import static com.mercadopago.MercadoPagoConfig.getStreamHandler; + +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 CRUD operations for stores (create, get, update, delete) and search with + * pagination. Stores represent physical or logical locations associated with a seller user. + * + *
Usage example: + *
{@code
+ * StoreClient client = new StoreClient();
+ * Store store = client.create(userId, storeCreateRequest);
+ * }
+ *
+ * @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());
+
+ /** 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 user that owns the store
+ * @param request the {@link StoreCreateRequest} with the store details
+ * @return the newly 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 user that owns the store
+ * @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 newly 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");
+
+ JsonObject payload = Serializer.serializeToJson(request);
+ MPRequest mpRequest =
+ MPRequest.buildRequest(
+ String.format("/users/%s/stores", encodePathParam(userId)),
+ HttpMethod.POST,
+ payload,
+ null,
+ requestOptions);
+ MPResponse response = send(mpRequest);
+
+ Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
+ store.setResponse(response);
+ return store;
+ }
+
+ /**
+ * Retrieves a store by its unique identifier.
+ *
+ * @param storeId 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 storeId) throws MPException, MPApiException {
+ return this.get(storeId, null);
+ }
+
+ /**
+ * Retrieves a store by its unique identifier with custom request options.
+ *
+ * @param storeId 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 storeId, MPRequestOptions requestOptions)
+ throws MPException, MPApiException {
+ LOGGER.info("Sending get store request");
+
+ MPResponse response =
+ send(
+ String.format("/stores/%s", encodePathParam(storeId)),
+ HttpMethod.GET,
+ null,
+ null,
+ requestOptions);
+
+ Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
+ store.setResponse(response);
+ return store;
+ }
+
+ /**
+ * Searches for stores belonging to the given user.
+ *
+ * @param userId the unique identifier of the user that owns the stores
+ * @param request 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 MPElementsResourcesPageFields follow the Stores API schema. No requiredness or validation is enforced by the
+ * SDK beyond what the API specification exposes.
+ *
+ * @see StoreClient#create(Long, StoreCreateRequest)
+ * @see
+ * Stores API reference
+ */
+@Getter
+@Builder
+public class StoreCreateRequest {
+ /** Name of the store. */
+ private final String name;
+
+ /** Business hours of the store. */
+ private final StoreBusinessHoursRequest businessHours;
+
+ /** Geographic location of the store. */
+ private final StoreLocationRequest location;
+
+ /** External identifier used to correlate the store with the integrator's system. */
+ private final String externalId;
+
+ /** Opening hours for a single weekday, as exposed by the Stores API. */
+ @Getter
+ @Builder
+ public static final class StoreBusinessHoursRequest {
+ /** Day of the week this entry applies to (e.g. {@code monday}). */
+ private final String day;
+
+ /** Opening time of the store on this day. */
+ private final String open;
+
+ /** Closing time of the store on this day. */
+ private final String close;
+ }
+
+ /** Geographic location of a store, as exposed by the Stores API. */
+ @Getter
+ @Builder
+ public static final class StoreLocationRequest {
+ /** Street address lines of the store. */
+ private final java.util.List All fields are optional: the API accepts a partial payload and only updates the
+ * attributes that are present. No requiredness or validation is imposed beyond what the
+ * API specification exposes.
+ *
+ * Usage example:
+ * Set your access token in the A store identifies one of the merchant's physical business locations, including its
+ * opening hours ({@code business_hours}) and geographic {@code location}. Stores are managed
+ * under a MercadoPago user account and can be referenced by point-of-sale and payment
+ * operations.
+ *
+ * Instances of this class are returned by the store operations of the SDK and are populated
+ * by deserializing the JSON response payload of the API.
+ *
+ * @see
+ * Stores API reference
+ */
+@Getter
+public class Store extends MPResource {
+
+ /** Unique identifier of the store. */
+ private String id;
+
+ /** Unique identifier of the MercadoPago user that owns the store. */
+ private Long userId;
+
+ /** External identifier used by the integrator to correlate the store with its own systems. */
+ private String externalId;
+
+ /** Name that identifies the store. */
+ private String name;
+
+ /** Date and time when the store was created in MercadoPago. */
+ private OffsetDateTime dateCreated;
+
+ /** Business opening hours of the store, keyed by day of the week. */
+ private Map{@code
+ * StoreUpdateRequest request = StoreUpdateRequest.builder()
+ * .name("Store name")
+ * .businessHours(StoreBusinessHoursRequest.builder().build())
+ * .location(StoreLocationRequest.builder().build())
+ * .externalId("external-123")
+ * .build();
+ * }
+ *
+ * @see StoreClient#update(Long, String, StoreUpdateRequest)
+ */
+@Getter
+@Builder
+public class StoreUpdateRequest {
+
+ /** Name of the store. */
+ private final String name;
+
+ /** Business hours of the store. */
+ private final StoreBusinessHoursRequest businessHours;
+
+ /** Location of the store. */
+ private final StoreLocationRequest location;
+
+ /** External identifier used to correlate the store with the integrator's system. */
+ private final String externalId;
+}
diff --git a/src/main/java/com/mercadopago/example/store/CreateStore.java b/src/main/java/com/mercadopago/example/store/CreateStore.java
new file mode 100644
index 00000000..ce0a0c9c
--- /dev/null
+++ b/src/main/java/com/mercadopago/example/store/CreateStore.java
@@ -0,0 +1,51 @@
+package com.mercadopago.example.store;
+
+import com.mercadopago.client.store.StoreClient;
+import com.mercadopago.client.store.StoreCreateRequest;
+import com.mercadopago.exceptions.MPApiException;
+import com.mercadopago.exceptions.MPException;
+import com.mercadopago.resources.store.Store;
+
+/**
+ * Creates a store associated to a Mercado Pago account.
+ *
+ * MERCADO_PAGO_ACCESS_TOKEN environment variable
+ * before running this example.
+ */
+public class CreateStore {
+
+ /** Private constructor to prevent instantiation. */
+ private CreateStore() {}
+
+ /**
+ * Runs the store creation example.
+ *
+ * @param args command line arguments (not used)
+ */
+ public static void main(String[] args) {
+ StoreClient client = new StoreClient();
+
+ StoreCreateRequest request =
+ StoreCreateRequest.builder()
+ .name("Official Store")
+ .externalId("store-123")
+ .build();
+
+ try {
+ Long userId = 123456789L;
+ Store store = client.create(userId, request);
+ System.out.println("Store created successfully:");
+ System.out.println(" Id: " + store.getId());
+ System.out.println(" Name: " + store.getName());
+ System.out.println(" External reference: " + store.getExternalReference());
+ } catch (MPApiException apiException) {
+ System.err.println(
+ "API error - status: "
+ + apiException.getStatusCode()
+ + ", content: "
+ + apiException.getApiResponse().getContent());
+ } catch (MPException exception) {
+ System.err.println("MP error: " + exception.getMessage());
+ }
+ }
+}
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..5982611f
--- /dev/null
+++ b/src/main/java/com/mercadopago/resources/store/Store.java
@@ -0,0 +1,49 @@
+package com.mercadopago.resources.store;
+
+import com.google.gson.annotations.SerializedName;
+import com.mercadopago.net.MPResource;
+import java.time.OffsetDateTime;
+import java.util.Map;
+import lombok.Getter;
+
+/**
+ * Represents a store resource from the MercadoPago Stores API.
+ *
+ *