get_variable('URA',true);
$codCli = $agi->get_variable('CODCLI', true);
$nomeCliente = $agi->get_variable('NOMECLIENTE',true);
$email = $agi->get_variable('EMAIL',true);
$celular = $agi->get_variable('CELULAR',true);
$codFatUltima = $agi->get_variable('ULTIMAFATURA', true);
$codFatPenultima = $agi->get_variable('PENULTIMAFATURA', true);
$vencPenultima = $agi->get_variable('VENCPENULTIMA',true);
$vencUltima = $agi->get_variable('VENCULTIMA',true);
$nomeFantasia = $agi->get_variable('NOMEFANTASIA',true);
$identCliente = !empty($nomeFantasia) ? $nomeFantasia : $nomeCliente;
$arDados = array("opcao" => $opcao, "CodCli" => $codCli, "nomeCliente" => $nomeCliente, "nomeFantasia" => $nomeFantasia);
$destino = $arDados['opcao'] == 1 ? $email : $celular;
if(!empty($codFatUltima)){
$arEnvio[] = array_merge($arDados, array("destino"=>$destino, "vencimento" => $vencUltima, "emissao" => RetornaFatura($opcao, $codFatUltima)));
}
if(!empty($codFatPenultima)){
$arEnvio[] = array_merge($arDados, array("destino"=>$destino, "vencimento" => $vencPenultima, "emissao" => RetornaFatura($opcao,$codFatPenultima)));
}
foreach($arEnvio as $dados){
enviaMensagem($dados['opcao'], $dados['destino'], $dados['nomeCliente'], $dados['emissao'], $dados['vencimento']);
}
$agi->exec_goto(GetUra('FATURA_ENVIADA'));
} catch (Exception $ex) {
$reg_msg = $ex->getMessage();
$agi->exec_goto(GetUra('URA_REPETE_ENVIO_BOLETO'));
$reg_status_exec = 'Er';
__logStr("Envia Boleto pro Cliente", $reg_msg . " Codigo do cliente: $codCli", $script, true);
}
@AtualizaIntegracao($uid, $reg_retorno, $reg_msg, $reg_status_exec, $retorno_cliente);
if (!$dadosIntegra) {
@grava_dadosIntegra($reg_retorno);
}
function objectToArray($d) {
if (is_object($d)) {
$d = get_object_vars($d);
}
if (is_array($d)) {
return array_map(__FUNCTION__, $d);
} else {
return $d;
}
}
function enviaEmail($email, $conta, $nomeCliente, $vencimento) {
$mail = new PHPMailer(true);
try {
//Configurações do Servidor
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.megalink.net.br'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'no_reply@megalink.net.br'; // SMTP username
$mail->Password = 'mega@link'; // SMTP password
//$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Remetente
$mail->setFrom('no-reply@megalink.net.br', 'No-Reply MegaLink');
//Recebedores
$mail->addAddress($email);
//Conteudo
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = '(NO-REPLY) Segunda Via de Boleto MegaLink';
$mail->CharSet = 'UTF-8';
$mail->Body = "
Atendimento Megalink
Ola, {$nomeCliente}
Voce esta recebendo em anexo sua conta com vencimento em: {$vencimento}
Caso ja possua o boleto, favor desconsiderar esse e-mail.
Clique aqui para visualizar sua conta.
Por favor, Nao responda esta mensagem.
Atenciosamente,
MegaLink e Servicos
www.megalink.net.br
";
$mail->send();
$return = "OK - Email enviado com sucesso.";
} catch (Exception $e) {
$msg = $mail->ErrorInfo;
return $msg;
}
return $return;
}
function enviaSMS($telefone, $nomeCliente, $codBarra) {
$numero = soNumero($telefone);
$url = sprintf("http://10.254.254.49/integracao?method=EnviaSMS&TroncoSainte=b0c0-9&"
. "NumeroDestino=%s&Texto=%s&login=aplicativos&senha=1234&tipoRetorno=XML",
$numero, urlencode($nomeCliente." Segue Codigo de barras para pagamento de sua fatura." . $codBarra));
$xml = simplexml_load_string(file_get_contents($url));
$result = array();
$result['result'] = strval($xml->result);
$result['status'] = strval($xml->status);
$result['message'] = 'OK - '.strval($xml->message);
$result['SIPID'] = strval($xml->SIPID);
return $result;
}
function retornaLinhaDigitavel($codFat) {
$json = sprintf('{
"request":{
"sendRequest": "integrator.server",
"method": "execute",
"submethod": "datasource.linhaDigitavel",
"params": {
"_user":"25AT0ME5TF",
"_passwd":"25AT0ME6C4",
"codfat": "%s"
}
}
}', $codFat);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://191.242.216.10/integrator.server.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$linhaDigitavel = json_decode($response);
foreach ($linhaDigitavel as $value) {
$linhaDigitavel = objectToArray($value);
}
foreach ($linhaDigitavel as $valor) {
$retLinha = $valor;
}
return $retLinha[0]['codigo_barras'];
}
}
function retornaBoleto($codFat) {
$json = sprintf('{
"request":{
"sendRequest": "integrator.server",
"method": "execute",
"submethod": "datasource.verBoleto",
"params": {
"_user":"25AT0ME5TF",
"_passwd":"25AT0ME6C4",
"codfat": "%s"
}
}
}', $codFat);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://191.242.216.10/integrator.server.php",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $json,
CURLOPT_HTTPHEADER => array(
"Cache-Control: no-cache",
"Content-Type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
return "cURL Error #:" . $err;
} else {
$boleto = json_decode($response);
foreach($boleto as $value){
$boleto = objectToArray($value);
}
foreach($boleto as $valor){
$returnLink = $valor;
}
return $returnLink[0]['linkBoleto'];
}
}
function EnviaMensagem($opcao, $destino, $nomeCliente, $emissao, $dataFatura = null){
if($opcao == 1){
$envio = enviaEmail($destino, $emissao, $nomeCliente, $dataFatura);
}else{
$envio = enviaSMS($destino, $nomeCliente, $emissao);
}
if(substr($envio,0,2) != 'OK'){
throw new Exception("ERRO - Nao foi possivel enviar a mensagem!");
}
}
function RetornaFatura($opcao, $codFatura){
return $opcao == 1 ? retornaBoleto($codFatura) : retornaLinhaDigitavel($codFatura);
}
?>