Merge pull request 'AL-1801' (#32) from AL-1801 into master
Reviewed-on: http://18.235.188.113:3000/adm/ModelWeb/pulls/32 Reviewed-by: Valdir Cordeiro <valdir.cordeiro@totvs.com.br> Reviewed-by: Gleison da Cruz <gleison.cruz@totvs.com.br>master
commit
f016c17b83
2
pom.xml
2
pom.xml
|
@ -3,7 +3,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>br.com.rjconsultores</groupId>
|
||||
<artifactId>ModelWeb</artifactId>
|
||||
<version>1.0.34</version>
|
||||
<version>1.0.35</version>
|
||||
<distributionManagement>
|
||||
<repository>
|
||||
<id>rj-releases</id>
|
||||
|
|
|
@ -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<String, String> headers = new HashMap<String, String>();
|
||||
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<String, String> headers = new HashMap<String, String>();
|
||||
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<String, String> headers = new HashMap<String, String>();
|
||||
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<String, String> headers = new HashMap<String, String>();
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -108,7 +108,7 @@ public class GenericRest {
|
|||
private HttpUriRequest retornaRequestWithHeader(String uri, TipoEnvioRest metodo, String parametro,
|
||||
ContentType contentType, HashMap<String, String> 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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue