Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
275 changes: 275 additions & 0 deletions src/main/java/com/mercadopago/client/store/StoreClient.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>Provides CRUD operations for stores (create, get, update, delete) and search with
* pagination. Stores represent physical or logical locations associated with a seller user.
*
* <p>Usage example:
* <pre>{@code
* StoreClient client = new StoreClient();
* Store store = client.create(userId, storeCreateRequest);
* }</pre>
*
* @see <a href="https://www.mercadopago.com/developers/en/reference/stores/_users_user_id_stores/post">
* Stores API reference</a>
*/
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 MPElementsResourcesPage<Store> search(Long userId, MPSearchRequest request)
throws MPException, MPApiException {
return this.search(userId, request, null);
}

/**
* Searches for stores belonging to the given user with custom request options.
*
* @param userId the unique identifier of the user that owns the stores
* @param request the {@link MPSearchRequest} containing search filters and pagination parameters
* @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or
* timeouts for this single request; may be {@code null}
* @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<Store> search(
Long userId, MPSearchRequest request, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending search store request");

MPResponse response =
search(
String.format("/users/%s/stores/search", encodePathParam(userId)),
request,
requestOptions);

Type responseType = new TypeToken<MPElementsResourcesPage<Store>>() {}.getType();
MPElementsResourcesPage<Store> result =
Serializer.deserializeElementsResourcesPageFromJson(responseType, response.getContent());
result.setResponse(response);
return result;
}

/**
* Updates an existing store.
*
* @param userId the unique identifier of the user that owns the store
* @param storeId the unique identifier of the store to update
* @param request the {@link StoreUpdateRequest} with the updated store details
* @return the updated {@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 update(Long userId, String storeId, StoreUpdateRequest request)
throws MPException, MPApiException {
return this.update(userId, storeId, request, null);
}

/**
* Updates an existing store with custom request options.
*
* @param userId the unique identifier of the user that owns the store
* @param storeId the unique identifier of the store to update
* @param request the {@link StoreUpdateRequest} with the updated store details
* @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or
* timeouts for this single request; may be {@code null}
* @return the updated {@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 update(
Long userId, String storeId, StoreUpdateRequest request, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending update store request");

JsonObject payload = Serializer.serializeToJson(request);
MPRequest mpRequest =
MPRequest.buildRequest(
String.format("/users/%s/stores/%s", encodePathParam(userId), encodePathParam(storeId)),
HttpMethod.PUT,
payload,
null,
requestOptions);
MPResponse response = send(mpRequest);

Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
store.setResponse(response);
return store;
}

/**
* Deletes a store by its unique identifier.
*
* @param userId the unique identifier of the user that owns the store
* @param storeId the unique identifier of the store to delete
* @return the deleted {@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 delete(Long userId, String storeId) throws MPException, MPApiException {
return this.delete(userId, storeId, null);
}

/**
* Deletes a store by its unique identifier with custom request options.
*
* @param userId the unique identifier of the user that owns the store
* @param storeId the unique identifier of the store to delete
* @param requestOptions optional {@link MPRequestOptions} to override access token, headers, or
* timeouts for this single request; may be {@code null}
* @return the deleted {@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 delete(Long userId, String storeId, MPRequestOptions requestOptions)
throws MPException, MPApiException {
LOGGER.info("Sending delete store request");

MPRequest mpRequest =
MPRequest.buildRequest(
String.format("/users/%s/stores/%s", encodePathParam(userId), encodePathParam(storeId)),
HttpMethod.DELETE,
null,
null,
requestOptions);
MPResponse response = send(mpRequest);

Store store = Serializer.deserializeFromJson(Store.class, response.getContent());
store.setResponse(response);
return store;
}
}
82 changes: 82 additions & 0 deletions src/main/java/com/mercadopago/client/store/StoreCreateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.mercadopago.client.store;

import lombok.Builder;
import lombok.Getter;

/**
* Request DTO used to create a store in Mercado Pago via
* {@code POST /users/{user_id}/stores}.
*
* <p>Fields 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 <a href="https://www.mercadopago.com/developers/en/reference/stores/_users_user_id_stores/post">
* Stores API reference</a>
*/
@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<String> addressLine;

/** Additional reference to locate the store. */
private final String reference;

/** City where the store is located. */
private final StoreCityRequest city;

/** State where the store is located. */
private final String state;

/** Postal code of the store location. */
private final String zipCode;

/** Latitude of the store location. */
private final Double latitude;

/** Longitude of the store location. */
private final Double longitude;
}

/** City information of a store location, as exposed by the Stores API. */
@Getter
@Builder
public static final class StoreCityRequest {
/** Identifier of the city. */
private final String id;

/** Name of the city. */
private final String name;
}
}
40 changes: 40 additions & 0 deletions src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.mercadopago.client.store;

import lombok.Builder;
import lombok.Getter;

/**
* Request object for updating an existing store through the Stores API.
*
* <p>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.
*
* <p>Usage example:
* <pre>{@code
* StoreUpdateRequest request = StoreUpdateRequest.builder()
* .name("Store name")
* .businessHours(StoreBusinessHoursRequest.builder().build())
* .location(StoreLocationRequest.builder().build())
* .externalId("external-123")
* .build();
* }</pre>
*
* @see StoreClient#update(Long, String, StoreUpdateRequest)
*/
@Getter
@Builder

Check failure on line 26 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 26 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 26 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 26 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 26 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 26 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol
public class StoreUpdateRequest {

/** Name of the store. */
private final String name;

/** Business hours of the store. */
private final StoreBusinessHoursRequest businessHours;

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 33 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

/** Location of the store. */
private final StoreLocationRequest location;

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 21

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 11

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

Check failure on line 36 in src/main/java/com/mercadopago/client/store/StoreUpdateRequest.java

View workflow job for this annotation

GitHub Actions / Test on Java 17

cannot find symbol

/** External identifier used to correlate the store with the integrator's system. */
private final String externalId;
}
Loading
Loading