repositório com os arquivos utilizados para integração entre o sistema SimplesIP e diversos sistemas.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

226 lines
6.9 KiB

3 years ago
<?php
require '/var/lib/asterisk/scripts/integracao/dbAbstract.php';
include '/var/lib/asterisk/scripts/integracao/custom/funcoesCustom.php';
$scrpt = $argv[0];
$config = array('itgc_host', 'itgc_port', 'itgc_database', 'itgc_user', 'itgc_password');
$config = GetConfigIntegracao($connPG, 'ENVIA_BOLETOS', $config);
$user = $config['itgc_user'];
$passWord = $config['itgc_password'];
$host = $config['itgc_host'];
$port = $config['itgc_port'];
$dataBase = $config['itgc_database'];
$tipoDb = DB__PGSQL;
$pathSom = '/var/lib/asterisk/sounds/customizados/';
/*
* Interage com o asterisk, por funcoes agi.
*/
$agi = GetAgi();
/*
* Variaveis para o status da integração.
*/
$reg_retorno = $numero; $reg_msg = ''; $reg_status_exec = '1'; $retorno_cliente = '';
/*
* Registra o inicio da integração. As variaveis passadas na funções são iniciali-
* zadas em serverAgi.php.
*/
@RegistraIntegracao($idMetodo, $uid, $uidOld, $numero);
try
{
$opcao = $agi->get_variable('URA', true);
$idFatura = $agi->get_variable('IDFATURA', true);
$emailCliente = $agi->get_variable('EMAIL', true);
$telefoneCliente = $agi->get_variable('TELCLIENTE', true);
if ($opcao == 1) {
$res = EnviaBoletosEmail($idFatura, $emailCliente);
if ($res == 'success') {
$agi->exec_goto(GetAnuncio('FATURA_ENVIADA_FIBNET'));
} else {
$agi->exec_goto(GetAnuncio('ENVIO_FATURA_FALHA_FIBNET'));
}
} elseif ($opcao == 2) {
$res = EnviaBoletosSms($idFatura,$telefoneCliente);
if ($res == 'success') {
$agi->exec_goto(GetAnuncio('FATURA_ENVIADA_FIBNET'));
} else {
$agi->exec_goto(GetAnuncio('ENVIO_FATURA_FALHA_FIBNET'));
}
} else {
$agi->exec_goto(GetFila('ATENDIMENTO_FIBNET'));
}
}
catch (Exception $ex)
{
$reg_msg = $ex->getMessage();
$reg_status_exec = 'Er';
__logStr("Envia Boletos", $reg_msg . "Envio de Boletos", $script, true);
}
@AtualizaIntegracao($uid, $reg_retorno, $reg_msg, $reg_status_exec, $retorno_cliente);
if(!$dadosIntegra)@grava_dadosIntegra($reg_retorno);
function EnviaBoletosEmail($idFatura, $emailAdd = null)
{
$clientId = "5";
$clientSecret = "B7fuHYQykrCYmtWkz7YgDcybwUk509PiF8u99oBp";
$username = "api@simplesip.com.br";
$password = "!@#simplesip_api_fibnet!@#";
if (!empty($idFatura)) {
$requestBody = array(
"client_id" => $clientId,
"client_secret" => $clientSecret,
"username" => $username,
"password" => $password,
"grant_type" => "password"
);
try {
$req = curl_init("https://api.fibnet.hubsoft.com.br/oauth/token");
$header = array();
$header[] = 'Accept: application/json';
curl_setopt($req, CURLOPT_HTTPHEADER, $header);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($requestBody));
$resp = json_decode(curl_exec($req), true);
curl_close($req);
} catch (Exception $ex) {
$msg = $ex->getMessage();
return $msg;
}
}
try {
$tokenType = $resp['token_type'];
$accessToken = $resp['access_token'];
$authorizationToken = $tokenType . " " . $accessToken;
$postfields = array(
"id_fatura" => $idFatura,
"email_adicional" => [
$emailAdd
]
);
$req = curl_init("https://api.fibnet.hubsoft.com.br/api/v1/integracao/cliente/financeiro/enviar_email");
$header = array();
$header[] = 'Accept: application/json';
if (!is_null($authorizationToken)) {
$header[] = 'Authorization: ' . $authorizationToken;
}
curl_setopt($req, CURLOPT_HTTPHEADER, $header);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($postfields));
$result = json_decode(curl_exec($req), true);
curl_close($req);
$cliente = objectToArray($result);
if ($cliente['status'] == 'error') {
return array($cliente['msg'], $cliente['errors']);
} else {
return $cliente['status'];
}
} catch (Exception $ex) {
$msg = $ex->getMessage();
return $msg;
}
}
function EnviaBoletosSms($idFatura, $telAdd = null)
{
$clientId = "5";
$clientSecret = "B7fuHYQykrCYmtWkz7YgDcybwUk509PiF8u99oBp";
$username = "api@simplesip.com.br";
$password = "!@#simplesip_api_fibnet!@#";
if (!empty($idFatura)) {
$requestBody = array(
"client_id" => $clientId,
"client_secret" => $clientSecret,
"username" => $username,
"password" => $password,
"grant_type" => "password"
);
try {
$req = curl_init("https://api.fibnet.hubsoft.com.br/oauth/token");
$header = array();
$header[] = 'Accept: application/json';
curl_setopt($req, CURLOPT_HTTPHEADER, $header);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($requestBody));
$resp = json_decode(curl_exec($req), true);
curl_close($req);
} catch (Exception $ex) {
$msg = $ex->getMessage();
return $msg;
}
}
try {
$tokenType = $resp['token_type'];
$accessToken = $resp['access_token'];
$authorizationToken = $tokenType . " " . $accessToken;
$postfields = array(
"id_fatura" => $idFatura,
"telefone_adicional" => [
$telAdd
]
);
$req = curl_init("https://api.fibnet.hubsoft.com.br/api/v1/integracao/cliente/financeiro/enviar_sms");
$header = array();
$header[] = 'Accept: application/json';
if (!is_null($authorizationToken)) {
$header[] = 'Authorization: ' . $authorizationToken;
}
curl_setopt($req, CURLOPT_HTTPHEADER, $header);
curl_setopt($req, CURLOPT_RETURNTRANSFER, true);
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($postfields));
$result = json_decode(curl_exec($req), true);
curl_close($req);
$cliente = objectToArray($result);
if ($cliente['status'] == 'error') {
return array($cliente['msg'], $cliente['errors']);
} else {
return $cliente['status'];
}
} catch (Exception $ex) {
$msg = $ex->getMessage();
return $msg;
}
}
function objectToArray($d) {
if (is_object($d)) {
$d = get_object_vars($d);
}
if (is_array($d)) {
return array_map(__FUNCTION__, $d);
} else {
return $d;
}
}