|
|
<?php |
|
|
|
|
|
require_once 'Integracao.php'; |
|
|
include "config.php"; |
|
|
|
|
|
/** |
|
|
* DESCRICAO DO DESENVOLVEDOR |
|
|
* |
|
|
* @author Jose Henrique Joanoni |
|
|
* @function developer |
|
|
* @company SimplesIP |
|
|
* @version 1.0.0 |
|
|
* @documentation https://docs.hubsoft.com.br/pt/latest/ |
|
|
*/ |
|
|
class HubSoft extends Integracao { |
|
|
|
|
|
/** DADOS PARA AUTENTICACAO */ |
|
|
private $client_id; |
|
|
private $client_secret; |
|
|
private $username; |
|
|
private $password; |
|
|
|
|
|
/** DADOS PARA REQUISICAO */ |
|
|
private $url; |
|
|
private $metodo; |
|
|
private $query; |
|
|
private $curl; |
|
|
private $debug; |
|
|
private $params = array(); |
|
|
|
|
|
######################################################################## |
|
|
## FUNCOES DA API ## |
|
|
######################################################################## |
|
|
|
|
|
/** |
|
|
* <b>Gera o Token para autenticacao na api.</b> |
|
|
* |
|
|
* @param integer $client_id |
|
|
* @param string $client_secret |
|
|
* @param string $username |
|
|
* @param string $password |
|
|
* @return string |
|
|
*/ |
|
|
public function geraToken() { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->http_method = "POST"; |
|
|
$this->params = array( |
|
|
"client_id" => $this->client_id, |
|
|
"client_secret" => $this->client_secret, |
|
|
"username" => $this->username, |
|
|
"password" => $this->password, |
|
|
"grant_type" => "password" |
|
|
); |
|
|
|
|
|
$this->setMetodo('oauth/token'); |
|
|
$result = $this->setParams(); |
|
|
|
|
|
if ($result['token_type']) { |
|
|
return $this->access_token = $result['token_type'] . " " . $result['access_token']; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Identifica o cliente mediante CPF ou CNPJ</b> |
|
|
* |
|
|
* @param string $documento |
|
|
* @return boolean |
|
|
*/ |
|
|
public function identificarDocumento($documento) { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->http_method = "GET"; |
|
|
$this->params = array( |
|
|
"busca" => "cpf_cnpj", |
|
|
"termo_busca" => $documento |
|
|
); |
|
|
|
|
|
$this->setMetodo('api/v1/integracao/cliente?' . http_build_query($this->params)); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Consulta as pendencias financeiras dos clientes</b> |
|
|
* |
|
|
* @param integer $cod_cliente |
|
|
* @return boolean |
|
|
*/ |
|
|
public function consultaPendencia($cod_cliente) { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->http_method = "GET"; |
|
|
$this->params = array( |
|
|
"busca" => "codigo_cliente", |
|
|
"termo_busca" => $cod_cliente |
|
|
); |
|
|
|
|
|
$this->setMetodo('api/v1/integracao/cliente/financeiro?' . http_build_query($this->params)); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Realiza o envio da segunda via de fatura para o email do cliente</b> |
|
|
* |
|
|
* @param integer $id_fatura |
|
|
* @return boolean |
|
|
*/ |
|
|
public function enviaFaturaEmail($id_fatura) { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->httpd_method = "POST"; |
|
|
$this->params = array( |
|
|
"id_fatura" => $id_fatura |
|
|
); |
|
|
|
|
|
$this->setMetodo('api/v1/integracao/cliente/financeiro/enviar_email'); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Realiza o envio da segunda via de fatura por SMS<b> |
|
|
* |
|
|
* @param integer $id_fatura |
|
|
* @return boolean |
|
|
*/ |
|
|
public function enviaFaturaSMS($id_fatura) { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->httpd_method = "POST"; |
|
|
$this->params = array( |
|
|
"id_fatura" => $id_fatura |
|
|
); |
|
|
|
|
|
$this->setMetodo('api/v1/integracao/cliente/financeiro/enviar_sms'); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Realiza a desconexao do cliente, mediante id do servico</b> |
|
|
* |
|
|
* @param integer $id_cliente_servico |
|
|
* @return boolean |
|
|
*/ |
|
|
public function desconexaoCliente($id_cliente_servico) { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->http_method = "GET"; |
|
|
$this->params = null; |
|
|
|
|
|
$this->setMetodo('api/v1/integracao/cliente/solicitar_desconexao/' . $id_cliente_servico); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Realiza a desconexao do cliente, mediante id do servico</b> |
|
|
* |
|
|
* @param integer $id_cliente_servico |
|
|
* @return boolean |
|
|
*/ |
|
|
public function desbloqueioConfianca($id_cliente_servico, $dias_desbloqueio = 1) { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->http_method = "POST"; |
|
|
$this->params = array( |
|
|
"id_cliente_servico" => $id_cliente_servico, |
|
|
"dias_desbloqueio" => $dias_desbloqueio |
|
|
); |
|
|
|
|
|
|
|
|
$this->setMetodo("api/v1/integracao/cliente/desbloqueio_confianca"); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
/** |
|
|
* <b>Realiza abertura de chamado</b> |
|
|
* |
|
|
* |
|
|
* @param int $id_cliente_servico |
|
|
* @param string $nome |
|
|
* @param string $telefone |
|
|
* @param bool $abriros |
|
|
* @param string $fila |
|
|
* @return boolean |
|
|
*/ |
|
|
public function novoAtendimento($id_cliente_servico, $nome, $telefone, $abriros = true, $fila = '') { |
|
|
$this->debug = debug_backtrace(); |
|
|
if ($this->getArgs(func_get_args())) { |
|
|
$this->geraToken(); |
|
|
$this->http_method = "POST"; |
|
|
$this->params = array( |
|
|
"id_cliente_servico" => $id_cliente_servico, |
|
|
"descricao" => "Um novo atendimento foi aberto por {$nome} pelo telefone {$telefone}, para um atendimento referente ao setor de {$fila}!", |
|
|
"nome" => $nome, |
|
|
"telefone" => $telefone, |
|
|
"abrir_os" => $abriros |
|
|
); |
|
|
|
|
|
$this->setMetodo('api/v1/integracao/atendimento'); |
|
|
return $this->setParams(); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
######################################################################## |
|
|
## FUNCOES DEFAULT DA CLASSE ## |
|
|
######################################################################## |
|
|
|
|
|
/** |
|
|
* Coleta as informacoes iniciais para o inicio da integracao com a API. |
|
|
* |
|
|
* @param string $token |
|
|
* @param string $url |
|
|
* @param boolean $log |
|
|
*/ |
|
|
|
|
|
public function __construct($url = CONF_URL_API, $client_id = CONF_USERID_API, $client_secret = CONF_TOKEN_API, $username = CONF_USER_API, $password = CONF_PASSWORD_API, $log = CONF_LOGGER_ATIVO) { |
|
|
$this->setLog($log); |
|
|
$this->url = $url; |
|
|
|
|
|
if ($client_id && $client_secret && $username && $password) { |
|
|
$this->client_id = $client_id; |
|
|
$this->client_secret = $client_secret; |
|
|
$this->username = $username; |
|
|
$this->password = $password; |
|
|
} |
|
|
$this->integracaoReg(); |
|
|
$this->log->info("Iniciando integracao", debug_backtrace()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Parametriza o metodo utilizado para a consulta. |
|
|
* |
|
|
* @param type $metodo |
|
|
*/ |
|
|
private function setMetodo($metodo) { |
|
|
$this->metodo = $metodo; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Escreve a query para ser passada para o curl |
|
|
* |
|
|
* @param string $query |
|
|
*/ |
|
|
private function setQuery($query) { |
|
|
return $this->query .= $query; |
|
|
} |
|
|
|
|
|
/** |
|
|
* retorna a string pronta da query do curl e limpa a variavel. |
|
|
* |
|
|
* @return string $query |
|
|
*/ |
|
|
private function getQuery() { |
|
|
$query = $this->query; |
|
|
unset($this->query); |
|
|
return $query; |
|
|
} |
|
|
|
|
|
/** |
|
|
* Constroi de forma dinamica a string para ser passada para a execucao do Curl |
|
|
* |
|
|
* @void |
|
|
*/ |
|
|
private function curl() { |
|
|
/* |
|
|
* Final da linha para pegar os dados da variavel |
|
|
*/ |
|
|
$header = array(); |
|
|
$header[] = 'Accept: application/json'; |
|
|
if ($this->access_token) { |
|
|
$header[] .= 'Authorization: ' . $this->access_token; |
|
|
} |
|
|
|
|
|
$this->curl = curl_init(); |
|
|
curl_setopt($this->curl, CURLOPT_URL, $this->url . $this->metodo); |
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $header); |
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 0); |
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); |
|
|
if ($this->http_method == 'POST') { |
|
|
curl_setopt($this->curl, CURLOPT_POST, true); |
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $this->params); |
|
|
} else { |
|
|
curl_setopt($this->curl, CURLOPT_POST, false); |
|
|
} |
|
|
|
|
|
$this->log->debug("Curl: {$this->curl}", debug_backtrace()); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Recebe array de forma de indice e valor |
|
|
* |
|
|
* @example array("qtype" => 'test_api', "query" => '123', "oper" => '=') |
|
|
* |
|
|
* @obs sempre chamar a fun<EFBFBD><EFBFBD>o debug_backtrace() para ser dispon<EFBFBD>vel em log |
|
|
* |
|
|
* @param type $params |
|
|
* @debug_track function debug_backtrace() |
|
|
*/ |
|
|
private function setParams() { |
|
|
$this->curl(); |
|
|
unset($this->params); |
|
|
return $this->exec(); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Recebe as informa<EFBFBD><EFBFBD>es e realiza a execucao da API |
|
|
* |
|
|
* @void |
|
|
*/ |
|
|
private function exec() { |
|
|
/** |
|
|
* Caso tenha problema de requisi<EFBFBD><EFBFBD>o |
|
|
*/ |
|
|
$content = curl_exec($this->curl); |
|
|
if (curl_errno($this->curl)) { |
|
|
$this->log->error(curl_error($this->curl), debug_backtrace()); |
|
|
curl_close($this->curl); |
|
|
$this->audioError(); |
|
|
return false; |
|
|
} |
|
|
curl_close($this->curl); |
|
|
return $this->response($content); |
|
|
} |
|
|
|
|
|
/** |
|
|
* Prepara dos dados para ser transmitidos para o metodo a ser retornado a |
|
|
* integracao. |
|
|
* |
|
|
* @return array |
|
|
*/ |
|
|
private function response($data) { |
|
|
$this->log->debug("Reponse API: " . print_r(json_decode($data, true), true), $this->debug); |
|
|
if ($data) { |
|
|
return json_decode($data, true); |
|
|
} else { |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|