diff --git a/pom.xml b/pom.xml index 0e71be887..9b551931c 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 br.com.rjconsultores ModelWeb - 1.0.34 + 1.0.35 rj-releases diff --git a/src/com/rjconsultores/ventaboletos/rest/MercadoPagoService.java b/src/com/rjconsultores/ventaboletos/rest/MercadoPagoService.java new file mode 100644 index 000000000..1e84ee13b --- /dev/null +++ b/src/com/rjconsultores/ventaboletos/rest/MercadoPagoService.java @@ -0,0 +1,178 @@ +package com.rjconsultores.ventaboletos.rest; + +import java.util.HashMap; + +import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang.WordUtils; +import org.apache.http.HttpStatus; +import org.apache.http.entity.ContentType; +import org.apache.log4j.Logger; + +import com.google.gson.Gson; +import com.rjconsultores.ventaboletos.entidad.Empresa; +import com.rjconsultores.ventaboletos.enums.TipoEnvioRest; +import com.rjconsultores.ventaboletos.rest.generic.GenericRest; +import com.rjconsultores.ventaboletos.rest.generic.RetornoGenericRest; +import com.rjconsultores.ventaboletos.vo.mercadopago.PosVO; +import com.rjconsultores.ventaboletos.vo.mercadopago.RetornoErro; +import com.rjconsultores.ventaboletos.vo.mercadopago.RetornoPosVO; +import com.rjconsultores.ventaboletos.vo.mercadopago.RetornoStoreVO; +import com.rjconsultores.ventaboletos.vo.mercadopago.StoreVO; +import com.rjconsultores.ventaboletos.vo.mercadopago.StoreVO.Location; + +public class MercadoPagoService { + + private static Logger log = Logger.getLogger(MercadoPagoService.class); + + private static MercadoPagoService instance; + + private MercadoPagoService() { + } + + public static synchronized MercadoPagoService getInstance() { + if (instance == null) { + instance = new MercadoPagoService(); + } + return instance; + } + + public Object cadastrarStoreMercadoPago(Empresa empresa, String nomeStore, String idStoreMercadoPago, + String userIdMercadoPago, String urlApiMercadoPago, String tokenMercadoPago) { + + try { + StoreVO consulta = new StoreVO(); + + consulta.setName(nomeStore); + consulta.setExternal_id(idStoreMercadoPago); + + Location item = new Location(); + item.setStreet_number(empresa.getNumero()); + item.setStreet_name(empresa.getLogradouro()); + item.setCity_name(WordUtils.capitalize(empresa.getCidade().getNombciudad().toLowerCase())); + item.setState_name(WordUtils.capitalize(empresa.getCidade().getEstado().getNombestado().toLowerCase())); + item.setLatitude(empresa.getLatitude()); + item.setLongitude(empresa.getLongitude()); + item.setReference(empresa.getComplemento()); + + consulta.setLocation(item); + + HashMap headers = new HashMap(); + headers.put("Authorization", "Bearer " + tokenMercadoPago); + headers.put("Accept", "application/json"); + headers.put("Content-type", "application/json"); + + String userId = userIdMercadoPago; + + RetornoGenericRest resposta = GenericRest.getInstance().fazerChamada( + urlApiMercadoPago + "/users/" + userId + "/stores", TipoEnvioRest.POST, + new Gson().toJson(consulta).toString(), ContentType.APPLICATION_JSON, headers, StoreVO.class, + Object.class); + + if (resposta != null && (resposta.getStatusResposta().equals(HttpStatus.SC_OK) + || resposta.getStatusResposta().equals(HttpStatus.SC_CREATED))) { + return (StoreVO) resposta.getConteudo(); + } else { + return resposta.getConteudoErro(); + } + + } catch (Exception e) { + log.error("erro ao enviar requisicao ", e); + } + + return null; + } + + public Object cadastrarPOSMercadoPago(String nomePOS, StoreVO storeVO, String idPOSMercadoPago, + String urlApiMercadoPago, String tokenMercadoPago) { + + try { + PosVO consulta = new PosVO(); + + consulta.setName(nomePOS); + consulta.setFixed_amount(false); + + if (storeVO != null) { + consulta.setStore_id(storeVO.getId()); + consulta.setExternal_store_id(storeVO.getExternal_id()); + } + + consulta.setExternal_id(idPOSMercadoPago); + consulta.setCategory(5611203); + + HashMap headers = new HashMap(); + headers.put("Authorization", "Bearer " + tokenMercadoPago); + headers.put("Accept", "application/json"); + headers.put("Content-type", "application/json"); + + RetornoGenericRest resposta = GenericRest.getInstance().fazerChamada(urlApiMercadoPago + "/pos", + TipoEnvioRest.POST, new Gson().toJson(consulta).toString(), ContentType.APPLICATION_JSON, headers, + PosVO.class, RetornoErro.class); + + if (resposta != null && (resposta.getStatusResposta().equals(HttpStatus.SC_OK) + || resposta.getStatusResposta().equals(HttpStatus.SC_CREATED))) { + return (PosVO) resposta.getConteudo(); + } else { + return resposta.getConteudoErro(); + } + + } catch (Exception e) { + log.error("erro ao enviar requisicao ", e); + } + + return null; + } + + public RetornoStoreVO retornarStoreMercadoPago(String userIdMercadoPago, String urlApiMercadoPago, + String tokenMercadoPago) { + + try { + + HashMap headers = new HashMap(); + headers.put("Authorization", "Bearer " + tokenMercadoPago); + headers.put("Accept", "application/json"); + headers.put("Content-type", "application/json"); + + String userId = userIdMercadoPago; + + RetornoGenericRest resposta = GenericRest.getInstance().fazerChamada( + urlApiMercadoPago + "/users/" + userId + "/stores/search", TipoEnvioRest.GET, null, + ContentType.APPLICATION_JSON, headers, RetornoStoreVO.class, RetornoErro.class); + + if (resposta != null && (resposta.getStatusResposta().equals(HttpStatus.SC_OK) + || resposta.getStatusResposta().equals(HttpStatus.SC_CREATED))) { + return (RetornoStoreVO) resposta.getConteudo(); + } + + } catch (Exception e) { + log.error("erro ao enviar requisicao ", e); + } + + return null; + } + + public RetornoPosVO retornarPosMercadoPago(String urlApiMercadoPago, String tokenMercadoPago) { + + try { + + HashMap headers = new HashMap(); + headers.put("Authorization", "Bearer " + tokenMercadoPago); + headers.put("Accept", "application/json"); + headers.put("Content-type", "application/json"); + + RetornoGenericRest resposta = GenericRest.getInstance().fazerChamada(urlApiMercadoPago + "/pos", + TipoEnvioRest.GET, null, ContentType.APPLICATION_JSON, headers, RetornoPosVO.class, + RetornoErro.class); + + if (resposta != null && (resposta.getStatusResposta().equals(HttpStatus.SC_OK) + || resposta.getStatusResposta().equals(HttpStatus.SC_CREATED))) { + return (RetornoPosVO) resposta.getConteudo(); + } + + } catch (Exception e) { + log.error("erro ao enviar requisicao ", e); + } + + return null; + } + +} diff --git a/src/com/rjconsultores/ventaboletos/rest/generic/GenericRest.java b/src/com/rjconsultores/ventaboletos/rest/generic/GenericRest.java index f37e447bd..f18f7bbcd 100644 --- a/src/com/rjconsultores/ventaboletos/rest/generic/GenericRest.java +++ b/src/com/rjconsultores/ventaboletos/rest/generic/GenericRest.java @@ -108,7 +108,7 @@ public class GenericRest { private HttpUriRequest retornaRequestWithHeader(String uri, TipoEnvioRest metodo, String parametro, ContentType contentType, HashMap headers) throws Exception { HttpUriRequest request = new HttpPost(uri); - StringEntity body = new StringEntity(parametro != null ? parametro : ""); + StringEntity body = new StringEntity(parametro != null ? parametro : "", "UTF-8"); if (TipoEnvioRest.GET.equals(metodo)) { request = new HttpGet(parametro != null @@ -130,6 +130,10 @@ public class GenericRest { String value = entry.getValue(); request.addHeader(key, value); } + + if (headers != null && metodo != null) { + headers.put("Content-type", contentType.getMimeType()); + } return request; } diff --git a/src/com/rjconsultores/ventaboletos/vo/mercadopago/PosVO.java b/src/com/rjconsultores/ventaboletos/vo/mercadopago/PosVO.java index 0319f1b02..62358337e 100644 --- a/src/com/rjconsultores/ventaboletos/vo/mercadopago/PosVO.java +++ b/src/com/rjconsultores/ventaboletos/vo/mercadopago/PosVO.java @@ -1,6 +1,8 @@ package com.rjconsultores.ventaboletos.vo.mercadopago; -public class PosVO extends RetornoErro { +import java.util.Objects; + +public class PosVO { private String id; private String name; @@ -71,4 +73,22 @@ public class PosVO extends RetornoErro { return this.getExternal_id(); } + @Override + public int hashCode() { + return Objects.hash(external_id, id, name); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PosVO other = (PosVO) obj; + return Objects.equals(external_id, other.external_id) && Objects.equals(id, other.id) + && Objects.equals(name, other.name); + } + } diff --git a/src/com/rjconsultores/ventaboletos/vo/mercadopago/RetornoErro.java b/src/com/rjconsultores/ventaboletos/vo/mercadopago/RetornoErro.java index 823efbde7..10ac007ae 100644 --- a/src/com/rjconsultores/ventaboletos/vo/mercadopago/RetornoErro.java +++ b/src/com/rjconsultores/ventaboletos/vo/mercadopago/RetornoErro.java @@ -2,9 +2,9 @@ package com.rjconsultores.ventaboletos.vo.mercadopago; public class RetornoErro { - public int statusCode; - public Object message; - public String error; + private int statusCode; + private Object message; + private String error; public int getStatusCode() { return statusCode; diff --git a/src/com/rjconsultores/ventaboletos/vo/mercadopago/StoreVO.java b/src/com/rjconsultores/ventaboletos/vo/mercadopago/StoreVO.java index 6159db858..42cb1ed25 100644 --- a/src/com/rjconsultores/ventaboletos/vo/mercadopago/StoreVO.java +++ b/src/com/rjconsultores/ventaboletos/vo/mercadopago/StoreVO.java @@ -1,6 +1,8 @@ package com.rjconsultores.ventaboletos.vo.mercadopago; -public class StoreVO extends RetornoErro { +import java.util.Objects; + +public class StoreVO { private String id; private String name; @@ -111,4 +113,22 @@ public class StoreVO extends RetornoErro { return this.getExternal_id(); } + @Override + public int hashCode() { + return Objects.hash(external_id, id, name); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + StoreVO other = (StoreVO) obj; + return Objects.equals(external_id, other.external_id) && Objects.equals(id, other.id) + && Objects.equals(name, other.name); + } + }