Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.com.videosoft.totemkoerich.service;
- import br.com.videosoft.totemkoerich.common.Configuration;
- import br.com.videosoft.totemkoerich.common.PaymentMethodEnum;
- import br.com.videosoft.totemkoerich.models.BillInformation;
- import br.com.videosoft.totemkoerich.models.Session;
- import com.sun.jersey.api.client.Client;
- import com.sun.jersey.api.client.ClientResponse;
- import com.sun.jersey.api.client.WebResource;
- import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
- import java.math.BigDecimal;
- import java.security.KeyManagementException;
- import java.security.NoSuchAlgorithmException;
- import java.security.cert.X509Certificate;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import javax.net.ssl.HostnameVerifier;
- import javax.net.ssl.HttpsURLConnection;
- import javax.net.ssl.SSLContext;
- import javax.net.ssl.SSLSession;
- import javax.net.ssl.TrustManager;
- import javax.net.ssl.X509TrustManager;
- import javax.ws.rs.core.HttpHeaders;
- import javax.ws.rs.core.MediaType;
- import org.apache.log4j.Logger;
- import org.json.JSONArray;
- import org.json.JSONObject;
- public class TotemKoerichWebservice {
- private static final Logger LOGGER = Logger.getLogger(TotemKoerichWebservice.class);
- private TotemKoerichWebservice() {
- }
- public static TotemKoerichWebservice getInstance() {
- return TotemKoerichWebserviceHolder.INSTANCE;
- }
- private static class TotemKoerichWebserviceHolder {
- private static final TotemKoerichWebservice INSTANCE = new TotemKoerichWebservice();
- }
- //*****************************************TOTEM KOERICH*****************************************//
- public JSONObject listBillsUnpaid(String method) {
- try {
- String user = Session.getInstance().getUserInformation().getUserIdentifier();
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- // WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/parcelasCarne/"+filial+"/"+user);
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user);
- LOGGER.info("Resource [ " + Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user + " ]");
- String bearerAuthorization = "Bearer "+authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON listBillsUnpaid return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de consulta dos menus", e);
- return null;
- }
- }
- public JSONObject getMenu() {
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/layout/"+filial+"/");
- String bearerAuthorization = "Bearer "+authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getMenu return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de consulta dos menus", e);
- return null;
- }
- }
- public JSONObject getCliente(String cpf){
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/cliente/"+filial+"/"+cpf);
- String bearerAuthorization = "Bearer "+authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getCliente return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de login do cliente", e);
- return null;
- }
- }
- public JSONObject getValoresSaque(){
- try {
- String user = Session.getInstance().getUserInformation().getUserIdentifier();
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/valoresSaque/"+filial+"/"+user);
- String bearerAuthorization = "Bearer "+authorization;
- LOGGER.info("JSON getValoresSaque sent >>" + Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/valoresSaque/"+filial+"/"+user + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getValoresSaque return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de valores de saque do cliente", e);
- return null;
- }
- }
- public JSONObject getProdutosCatalogo(String method){
- try {
- String user = Session.getInstance().getUserInformation().getUserIdentifier();
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- // WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/produtosCatalogo/"+filial+"/"+user);
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user);
- String bearerAuthorization = "Bearer "+authorization;
- LOGGER.info("Resource [ " + Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+user + " ]");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getProdutosCatalogo return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de catalogo de produtos do cliente", e);
- return null;
- }
- }
- public String getDadosCliente(String codigo){
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/consultas/dadosCliente/"+filial+"/"+codigo);
- String bearerAuthorization = "Bearer "+authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getDadosCliente return >>" + jsonObj.toString() + "<<");
- return jsonObj.getString("htmlInformativo");
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de consulta dos dados do cliente", e);
- return null;
- }
- }
- public JSONObject getContent(String method){
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- String resource = "";
- if((boolean) Session.getInstance().getDetailView().get("requerLogin")){
- resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+Session.getInstance().getUserInformation().getUserIdentifier();
- }else{
- resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/";
- }
- LOGGER.info("getContent resource [ " + resource + " ]");
- WebResource webResource = client.resource(resource);
- String bearerAuthorization = "Bearer "+authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .get(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getContent return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de consulta do conteudo", e);
- return null;
- }
- }
- public String getActionButton(String method){
- return getActionButton(method, (boolean) Session.getInstance().getDetailView().get("requerLogin"), "");
- }
- public String getActionButton(String method, boolean userRequired, String product){
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- String resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/";
- if(userRequired){
- resource = resource + Session.getInstance().getUserInformation().getUserIdentifier()+"/";
- }
- if (product != ""){
- resource = resource + product;
- }
- LOGGER.info("getActionButton resource [ " + resource + " ]");
- WebResource webResource = client.resource(resource);
- String bearerAuthorization = "Bearer "+authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, "[]");
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getActionButton return >>" + jsonObj.toString() + "<<");
- String ReturnAction = "";
- if(jsonObj.has("retorno")){
- ReturnAction = jsonObj.getString("retorno");
- }
- return ReturnAction;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de ação de botão", e);
- return null;
- }
- }
- public String postActionCatalogProductsButton(String method, ArrayList<String> productSelected){
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- JSONObject jsonBody = new JSONObject();
- JSONArray jsonArrayProducts = new JSONArray();
- for(int i=0;i<productSelected.size();i++){
- JSONObject product = new JSONObject();
- product.put("id", productSelected.get(i));
- jsonArrayProducts.put(product);
- //ID do produto pega aqui, mas na listagem em horizontal, pode vir mais de um, na vertical SEMPRE vem apenas um, então esse laço n precisa
- }
- // jsonBody.put("dadosOperacao", jsonArrayProducts);
- disableSslVerification();
- String authorization = getAuthorization();
- String resource = "";
- if((boolean) Session.getInstance().getDetailView().get("requerLogin")){
- //ta errado aqui, pq ele ta mandando filial OK, e depois o userIdentifier, é pra mandar o id do produto selecionado
- //só que essa função tb é chamada pelo catalogo de produtos na horizontal, que deve ficar como esta aqui
- // estranho q eu lembro de ter feito, parece q n mexi, cria uma copia dessa funcao para vertical postActionCatalogVerticalProductsButton
- resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+IDdoProduto;
- resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/"+Session.getInstance().getUserInformation().getUserIdentifier();
- }else{
- resource = Configuration.getInstance().getUrlTotemKoerichWebservice()+method+filial+"/";
- }
- LOGGER.info("postActionCatalogProductsButton resource [ " + resource + " ]");
- WebResource webResource = client.resource(resource);
- String bearerAuthorization = "Bearer "+authorization;
- LOGGER.info("JSON postActionCatalogProductsButton sent >>" + jsonArrayProducts.toString() + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonArrayProducts.toString());
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postActionCatalogProductsButton return >>" + jsonObj.toString() + "<<");
- String ReturnAction = "";
- if(jsonObj.has("retorno")){
- ReturnAction = jsonObj.getString("retorno");
- }
- return ReturnAction;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de ação de botão", e);
- return null;
- }
- }
- public JSONObject postTransacoesStart(List<BillInformation> bill) {
- try {
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/start/" + filial + "/" + Session.getInstance().getUserInformation().getUserIdentifier());
- // client.addFilter(new HTTPBasicAuthFilter(username, password));
- String bearerAuthorization = "Bearer " + authorization;
- ArrayList<JSONObject> documentos = new ArrayList<>();
- BigDecimal total = new BigDecimal(0);
- for (BillInformation register : bill) {
- JSONObject registerBill = new JSONObject();
- registerBill.put("documento", register.getBillDocument());
- registerBill.put("tipoDocumento", register.getTypeDocument());
- if(register.getParcel() != 0){
- registerBill.put("referencia", register.getParcel());
- }
- registerBill.put("valorVencimento", register.getValue());
- registerBill.put("valorPagar", register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
- total = total.add(register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
- documentos.add(registerBill);
- }
- JSONObject jsonBody = new JSONObject();
- jsonBody.put("documentos", documentos);
- jsonBody.put("valorAutenticacao", total);
- LOGGER.info("JSON postTransacoesStart sent >>" + jsonBody.toString() + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonBody.toString());
- // ClientResponse response = webResource.accept("application/json").post(ClientResponse.class);
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesStart return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação", e);
- return null;
- }
- }
- public boolean postTransacoesCash(List<BillInformation> bill, PaymentMethodEnum paymentMethod, JSONObject resultPayment, String user, int codeMethodPaymentKoerich) {
- try{
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/cash/" + filial + "/" + user);
- // client.addFilter(new HTTPBasicAuthFilter(username, password));
- String bearerAuthorization = "Bearer " + authorization;
- ArrayList<JSONObject> documentos = new ArrayList<>();
- BigDecimal total = new BigDecimal(0);
- for (BillInformation register : bill) {
- JSONObject registerBill = new JSONObject();
- registerBill.put("documento", register.getBillDocument());
- registerBill.put("tipoDocumento", register.getTypeDocument());
- if(register.getParcel() != 0){
- registerBill.put("referencia", register.getParcel());
- }
- registerBill.put("valorVencimento", register.getValue());
- registerBill.put("valorPagar", register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
- registerBill.put("dataVencimento", register.getExpirationDate().toString());
- total = total.add(register.getValue().add(register.getValueInterest()).subtract(register.getValueDiscount()));
- documentos.add(registerBill);
- }
- ArrayList<JSONObject> formasRecebidas = new ArrayList<>();
- JSONObject item;
- switch(paymentMethod) {
- case CASH :
- item = new JSONObject();
- item.put("formaRecebimento", codeMethodPaymentKoerich);
- item.put("valor", total);
- formasRecebidas.add(item);
- break;
- case CARD_DEBIT :
- case CARD_DEBIT_BILL :
- case CARD_CREDIT :
- String data = resultPayment.getString("dataHora").substring(0, 10);
- String hora = resultPayment.getString("dataHora").substring(11);
- int modalidade = Integer.valueOf(resultPayment.getString("tipoTransacaoCod").substring(0, 2));
- int submodalidade = Integer.valueOf(resultPayment.getString("tipoTransacaoCod").substring(2, 4));
- item = new JSONObject();
- item.put("formaRecebimento", codeMethodPaymentKoerich);
- item.put("valor", total);
- item.put("tipo", "TEF");
- JSONObject dadosTef = new JSONObject();
- dadosTef.put("data", data);
- dadosTef.put("hora", hora);
- dadosTef.put("valor", total);
- dadosTef.put("nsu", resultPayment.get("nsuSitef"));
- dadosTef.put("nsuHost", resultPayment.get("nsuHost"));
- if (resultPayment.has("autorizacaoCredito")) {
- dadosTef.put("autorizacao", resultPayment.get("autorizacaoCredito"));
- } else {
- dadosTef.put("autorizacao", "0");
- }
- dadosTef.put("rede", Integer.valueOf(resultPayment.getString("autorizadorId")));
- dadosTef.put("bandeira", Integer.valueOf(resultPayment.getString("tipoCartaoId")));
- dadosTef.put("modalidade", modalidade);
- dadosTef.put("submodalidade", submodalidade);
- // dadosTef.put("pagamento", 16);
- item.put("dadosTEF", dadosTef);
- formasRecebidas.add(item);
- break;
- }
- JSONObject jsonBody = new JSONObject();
- jsonBody.put("documentos", documentos);
- jsonBody.put("valorAutenticacao", total);
- jsonBody.put("formasRecebidas", formasRecebidas);
- LOGGER.info("JSON postTransacoesCash sent >>" + jsonBody.toString() + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonBody.toString());
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesCash return >>" + jsonObj.toString() + "<<");
- return jsonObj.getBoolean("sucesso");
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação cash", e);
- return false;
- }
- }
- public boolean postTransacoesRollback(int clientId) {
- try{
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/rollback/" + filial + "/" + clientId);
- String bearerAuthorization = "Bearer " + authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class);
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesRollback return >>" + jsonObj.toString() + "<<");
- return jsonObj.getBoolean("sucesso");
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação rollback", e);
- return false;
- }
- }
- public boolean postTransacoesVoucher(String valor, Long cupomFiscal) {
- try{
- JSONObject jsonBody = new JSONObject();
- jsonBody.put("valor", Double.parseDouble(valor));
- jsonBody.put("cupomFiscal", cupomFiscal);
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource;
- if(Session.getInstance().getUserInformation() != null){
- webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/voucher/" + filial + "?cliente=" + Session.getInstance().getUserInformation().getUserIdentifier());
- }else{
- webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/voucher/" + filial);
- }
- // WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/voucher/" + filial + "/" + Session.getInstance().getUserInformation().getUserIdentifier());
- String bearerAuthorization = "Bearer " + authorization;
- LOGGER.info("JSON postTransacoesVoucher sent >>" + jsonBody.toString() + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonBody.toString());
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesVoucher return >>" + jsonObj.toString() + "<<");
- return jsonObj.getBoolean("sucesso");
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação voucher", e);
- return false;
- }
- }
- // public boolean postTransacoesWithdrawal(int total, PaymentMethodEnum paymentMethod, JSONObject resultPayment, String user) {
- public boolean postTransacoesWithdrawal(double total, JSONObject resultWithdrawal, String user) {
- try{
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/withdrawal/" + filial + "/" + user);
- String bearerAuthorization = "Bearer " + authorization;
- ArrayList<JSONObject> formasRecebidas = new ArrayList<>();
- JSONObject item;
- Date dateNow = new Date();
- SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
- SimpleDateFormat formatHour = new SimpleDateFormat("HH:mm:ss");
- String data = formatDate.format( dateNow );
- String hora = formatHour.format( dateNow );
- int modalidade = Integer.valueOf(resultWithdrawal.getString("tipoTransacaoCod").substring(0, 2));
- int submodalidade = Integer.valueOf(resultWithdrawal.getString("tipoTransacaoCod").substring(2, 4));
- item = new JSONObject();
- item.put("valor", total);
- item.put("tipo", "TEF");
- JSONObject dadosTef = new JSONObject();
- dadosTef.put("data", data);
- dadosTef.put("hora", hora);
- dadosTef.put("valor", total);
- dadosTef.put("nsu", resultWithdrawal.get("nsuSitef"));
- dadosTef.put("nsuHost", resultWithdrawal.get("nsuHost"));
- if (resultWithdrawal.has("autorizacaoCredito")) {
- dadosTef.put("autorizacao", resultWithdrawal.get("autorizacaoCredito"));
- } else {
- dadosTef.put("autorizacao", "0");
- }
- dadosTef.put("rede", Integer.valueOf(resultWithdrawal.getString("autorizadorId")));
- dadosTef.put("bandeira", Integer.valueOf(resultWithdrawal.getString("tipoCartaoId")));
- dadosTef.put("modalidade", modalidade);
- dadosTef.put("submodalidade", submodalidade);
- item.put("dadosTEF", dadosTef);
- formasRecebidas.add(item);
- JSONObject jsonBody = new JSONObject();
- jsonBody.put("valorAutenticacao", total);
- jsonBody.put("formasRecebidas", formasRecebidas);
- System.out.println("JSON postTransacoesWithdrawal sent >>" + jsonBody.toString() + "<<");
- LOGGER.info("JSON postTransacoesWithdrawal sent >>" + jsonBody.toString() + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonBody.toString());
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- System.out.println("JSON postTransacoesWithdrawal return >>" + jsonObj.toString() + "<<");
- LOGGER.info("JSON postTransacoesWithdrawal return >>" + jsonObj.toString() + "<<");
- return jsonObj.getBoolean("sucesso");
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação withdrawal", e);
- return false;
- }
- }
- private String getAuthorization() throws Exception {
- try {
- String username = Configuration.getInstance().getWebserviceAuthorizationUser();
- String password = Configuration.getInstance().getWebserviceAuthorizationPassword();
- Client client = Client.create();
- // URL u = new URL(Configuration.getInstance().getUrlTotemKoerichWebservice()); // this would check for the protocol
- // u.getContent();
- disableSslVerification();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice()+"/gtw/autenticacao/autenticar");
- client.addFilter(new HTTPBasicAuthFilter(username, password));
- ClientResponse response = webResource.accept("application/json").post(ClientResponse.class);
- if (response.getStatus() != 200) {
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON getAuthorization return >>" + jsonObj.toString() + "<<");
- return (String) jsonObj.get("access_token");
- } catch (Exception e) {
- LOGGER.error(e.getMessage(), e);
- // e.printStackTrace();
- throw e;
- // return null;
- }
- }
- private static void disableSslVerification() {
- try
- {
- // Create a trust manager that does not validate certificate chains
- TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
- return null;
- }
- public void checkClientTrusted(X509Certificate[] certs, String authType) {
- }
- public void checkServerTrusted(X509Certificate[] certs, String authType) {
- }
- }
- };
- // Install the all-trusting trust manager
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, trustAllCerts, new java.security.SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- // Create all-trusting host name verifier
- HostnameVerifier allHostsValid = new HostnameVerifier() {
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- };
- // Install the all-trusting host verifier
- HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (KeyManagementException e) {
- e.printStackTrace();
- }
- }
- public JSONObject postTransacoesStartRecharge() {
- try{
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/startRecharge/" + filial);
- String bearerAuthorization = "Bearer " + authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class);
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesStartRecharge return >>" + jsonObj.toString() + "<<");
- return jsonObj;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação inicio de recarga", e);
- return null;
- }
- }
- public boolean postTransacoesRecharge(JSONObject resultRecharge, int codeMethodPaymentKoerich) {
- try{
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/recharge/" + filial);
- // client.addFilter(new HTTPBasicAuthFilter(username, password));
- String bearerAuthorization = "Bearer " + authorization;
- JSONObject jsonBody = new JSONObject();
- String data = resultRecharge.getString("dataHora").substring(0, 10);
- String hora = resultRecharge.getString("dataHora").substring(11);
- jsonBody.put("nsu", resultRecharge.get("nsuSitef"));
- jsonBody.put("nsuHost", resultRecharge.get("nsuHost"));
- jsonBody.put("valor", resultRecharge.getDouble("valor"));
- jsonBody.put("data", data);
- jsonBody.put("hora", hora);
- jsonBody.put("parcelas", 1);
- jsonBody.put("operadora", resultRecharge.get("operadoraNome"));
- jsonBody.put("celular", resultRecharge.get("telefone"));
- jsonBody.put("submodalidade", 0);
- // jsonBody.put("usuario", 999993);
- // jsonBody.put("filial", Integer.valueOf(filial));
- // jsonBody.put("cliente", "");
- // jsonBody.put("maquina", "");
- // jsonBody.put("intervensao", "");
- jsonBody.put("saque", false);
- if (resultRecharge.has("pagamento")) {
- JSONObject pagamento = resultRecharge.getJSONObject("pagamento");
- ArrayList<JSONObject> formasRecebidas = new ArrayList<>();
- JSONObject item;
- switch(pagamento.getString("tipo")) {
- case "Dinheiro" :
- item = new JSONObject();
- item.put("formaRecebimento", codeMethodPaymentKoerich);
- item.put("valor", resultRecharge.getDouble("valor"));
- item.put("tipoRecebimento", "VISTA");
- formasRecebidas.add(item);
- break;
- case "Cartão de Débito" :
- case "Cartão de Crédito" :
- int modalidade = Integer.valueOf(pagamento.getString("tipoTransacaoCod").substring(0, 2));
- int submodalidade = Integer.valueOf(pagamento.getString("tipoTransacaoCod").substring(2, 4));
- item = new JSONObject();
- item.put("formaRecebimento", codeMethodPaymentKoerich);
- item.put("valor", resultRecharge.getDouble("valor"));
- item.put("tipo", "TEF");
- JSONObject dadosTef = new JSONObject();
- dadosTef.put("data", data);
- dadosTef.put("hora", hora);
- dadosTef.put("valor", resultRecharge.getDouble("valor"));
- dadosTef.put("nsu", pagamento.get("nsuSitef"));
- dadosTef.put("nsuHost", pagamento.get("nsuHost"));
- if (pagamento.has("autorizacaoCredito")) {
- dadosTef.put("autorizacao", pagamento.get("autorizacaoCredito"));
- } else {
- dadosTef.put("autorizacao", "0");
- }
- dadosTef.put("rede", Integer.valueOf(pagamento.getString("autorizadorId")));
- dadosTef.put("bandeira", Integer.valueOf(pagamento.getString("tipoCartaoId")));
- dadosTef.put("modalidade", modalidade);
- dadosTef.put("submodalidade", submodalidade);
- // dadosTef.put("pagamento", 16);
- item.put("dadosTEF", dadosTef);
- formasRecebidas.add(item);
- break;
- }
- jsonBody.put("formasRecebimentoTef", formasRecebidas);
- }
- LOGGER.info("JSON postTransacoesRecharge sent >>" + jsonBody.toString() + "<<");
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class, jsonBody.toString());
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesRecharge return >>" + jsonObj.toString() + "<<");
- return jsonObj.getBoolean("sucesso");
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação de recarga", e);
- return false;
- }
- }
- public JSONArray postTransacoesStartWithdrawal(int valueWithdrawal, String user) {
- try{
- String filial = Configuration.getInstance().getStoreId();
- Client client = Client.create();
- disableSslVerification();
- String authorization = getAuthorization();
- LOGGER.info("JSON postTransacoesStartWithdrawal sent >>" + Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/startWithdrawal/" + filial + "/" + valueWithdrawal + "/" + user + "<<");
- WebResource webResource = client.resource(Configuration.getInstance().getUrlTotemKoerichWebservice() + "/gtw/transacoes/startWithdrawal/" + filial + "/" + valueWithdrawal + "/" + user);
- String bearerAuthorization = "Bearer " + authorization;
- ClientResponse response = webResource
- .header(HttpHeaders.AUTHORIZATION, bearerAuthorization)
- .type(MediaType.APPLICATION_JSON)
- .accept(MediaType.APPLICATION_JSON)
- .post(ClientResponse.class);
- if (response.getStatus() != 200) {
- LOGGER.error("Return error webservice [" + response.getEntity(String.class) + "]");
- throw new RuntimeException("Failed : HTTP error code : "
- + response.getStatus());
- }
- String output = response.getEntity(String.class);
- JSONArray jsonArray = new JSONArray(output);
- // String teste = jsonArray.getJSONArray(0).getString(0);
- // String teste1 = jsonArray.getString(1);
- // JSONObject jsonObj = new JSONObject(output);
- LOGGER.info("JSON postTransacoesStartWithdrawal return >>" + jsonArray.toString() + "<<");
- return jsonArray;
- } catch (Exception e) {
- LOGGER.error("Erro na chamada do webservice de post de transação inicio parcelamento de saque", e);
- return null;
- }
- }
- public String mountUrlImage(String urlImage) {
- if (urlImage.toLowerCase().startsWith("http")) {
- return urlImage;
- }
- return Configuration.getInstance().getUrlTotemKoerichWebservice() + urlImage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement