HEX
Server: LiteSpeed
System: Linux cp01.bhostbrasil.com.br 5.14.0-611.16.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Dec 22 03:40:39 EST 2025 x86_64
User: onlyfibr (1083)
PHP: 8.2.31
Disabled: NONE
Upload Files
File: /home/onlyfibr/public_html/gestor/crons/cob_auto.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

date_default_timezone_set('America/Sao_Paulo');

require_once __DIR__ . '/../db/Conexao.php';
require_once __DIR__ . '/../master/classes/functions.php';



function sendCurlRequest($url, $token, $data)
{
  $curl = curl_init();

  $idempotencyKey = uniqid();
  curl_setopt_array(
    $curl,
    array(
      CURLOPT_URL => $url,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => $data,
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $token,
        'X-Idempotency-Key: ' . $idempotencyKey,

      ),
    )
  );

  $response = curl_exec($curl);

  curl_close($curl);

  return $response;
}

function sendTextMessageWhats($urlapi, $tokenapi, $phone, $textomsg, $apikey)
{
  $curl = curl_init();

  curl_setopt_array(
    $curl,
    array(
      CURLOPT_URL => $urlapi . "/message/sendText/AbC123" . $tokenapi,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => '{
      "number": "55' . $phone . '",
      "options": {
        "delay": 1200,
        "presence": "composing",
        "linkPreview": false
      },
      "textMessage": {
        "text": "' . $textomsg . '"
      }
    }',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'apikey: ' . $apikey . ''
      )
    )
  );

  curl_exec($curl);

  curl_close($curl);
}

function sendMediaMessageWhats($urlapi, $tokenapi, $phone, $caption, $base64, $apikey)
{
  $curl = curl_init();

  curl_setopt_array(
    $curl,
    array(
      CURLOPT_URL => $urlapi . "/message/sendMedia/AbC123" . $tokenapi,
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => '',
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 0,
      CURLOPT_FOLLOWLOCATION => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => 'POST',
      CURLOPT_POSTFIELDS => '{
      "number": "55' . $phone . '",
      "options": {
        "delay": 1200,
        "presence": "composing"
      },
      "mediaMessage": {
        "mediatype": "image",
        "caption": "' . $caption . '",
        "media": "' . $base64 . '"
      }
    }',
      CURLOPT_HTTPHEADER => array(
        'Content-Type: application/json',
        'apikey: ' . $apikey . ''
      )
    )
  );

  curl_exec($curl);

  curl_close($curl);
}


function sendCobAuto($notifyHour, $actualHour, $msg, $urlapi, $tokenapi, $phone, $messageToSend, $apikey, $paymentType, $connect, $idcli, $installment, $firstName, $lastName, $tokenmp, $msgpix, $msgqr)
{
  try {
    $qrcode_base64 = "";
    $emv = "";

    if ($notifyHour == $actualHour) {
      $messageToSend = preg_replace('/\s+/', ' ', $messageToSend);
      $messageToSend = preg_replace('/\n\s*\n/', "\n\n", $messageToSend);
      $messageToSend = str_replace("\n", "\\n", $messageToSend);

      $messages = [];

      $messages[] = ['type' => 'text', 'phone' => $phone, 'message' => $messageToSend, 'media' => '', 'status' => 'pending'];

      if ($paymentType == "1") {
        $checkQuery = $connect->prepare("SELECT qrcode, linhad FROM mercadopago WHERE idc = :idcli AND status = 'pending' AND DATE(data) >= DATE_SUB(CURDATE(), INTERVAL 5 DAY)");
        $checkQuery->execute(['idcli' => $idcli]);
        $existingRecord = $checkQuery->fetch(PDO::FETCH_ASSOC);

        $qrcode_base64 = $existingRecord['qrcode'];
        $emv = $existingRecord['linhad'];

        if ($msgpix == "1") {
          $messages[] = ['type' => 'media', 'phone' => $phone, 'message' => "Pague agora via pix. Leia o QRCode.", 'media' => $qrcode_base64, 'status' => 'pending'];
        }

        if ($msgqr == "1") {
          $messages[] = ['type' => 'text', 'phone' => $phone, 'message' => $emv, 'media' => '', 'status' => 'pending'];
        }
      }

      $msfg = "*ATENÇÃO* Esta é uma mensagem automática e não precisa ser respondida.\\n*Caso já tenha efetuado o pagamento por favor desconsidere esta cobrança.*";
      $messages[] = ['type' => 'text', 'phone' => $phone, 'message' => $msfg, 'media' => '', 'status' => 'pending'];

      $messages = array_unique($messages, SORT_REGULAR);

      $insertWarningMessage = $connect->prepare("INSERT INTO message_queue (type, phone, message, media, status) VALUES (?, ?, ?, ?, ?)");
      foreach ($messages as $message) {
        $insertWarningMessage->execute([$message['type'], $message['phone'], $message['message'], $message['media'], $message['status']]);
      }

      $stmt = $connect->prepare("SELECT * FROM message_queue WHERE status = 'pending'");
      $stmt->execute();

      while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
        if ($row->type == 'text') {
          $messageToSend = str_replace("\\\\n", "\n", $row->message);
          $result = sendTextMessageWhats($urlapi, $tokenapi, $row->phone, $messageToSend, $apikey);
        } elseif ($row->type == 'media') {
          $result = sendMediaMessageWhats($urlapi, $tokenapi, $row->phone, $row->message, $row->media, $apikey);
        }

        if ($result && $result->getStatusCode() == 200) {
          $update = $connect->prepare("UPDATE message_queue SET status = 'sent' WHERE id = ?");
          $update->execute([$row->id]);
        } else {
          $update = $connect->prepare("UPDATE message_queue SET status = 'falha' WHERE id = ?");
          $update->execute([$row->id]);
          $delete = $connect->prepare("DELETE FROM message_queue WHERE id = ?");
          $delete->execute([$row->id]);
        }
        sleep(5);
      }
    }
  } catch (Exception $exception) {
   error_log($exception->getMessage());
  }
}


$currentDate = date("Ymd");
$dueDate = date("Ymd", strtotime("+5 days", strtotime($currentDate))); // Adicionado 5 dias ao vencimento atual
$dueDate7 = date("Ymd", strtotime("+7 days", strtotime($currentDate))); // Adicionado 7 dias ao vencimento atual
$beforeDate = date("Ymd", strtotime("-3 days", strtotime($currentDate))); // Mudado para 3 dias antes do vencimento atual

$actualHour = date('H:i');

$query = "SELECT * FROM financeiro2 WHERE pagoem = 'n'";
$payments = $connect->query($query);

while ($paymentsRow = $payments->fetch(PDO::FETCH_OBJ)) {
  $dateFormated = DateTime::createFromFormat('d/m/Y', $paymentsRow->datapagamento)->format('Ymd');

  if (($dateFormated >= $beforeDate && $dateFormated <= $dueDate7) || $dateFormated == $dueDate) { // Adicionada condição para cobrar com 7 dias antes do vencimento
    $wallet = $connect->query("SELECT * FROM carteira WHERE Id = '" . $paymentsRow->idm . "'");
    $walletRow = $wallet->fetch(PDO::FETCH_OBJ);

    $tokenapi = $walletRow->tokenapi;
    $token = $walletRow->vjurus;
    $tokenmp = $walletRow->tokenmp;
    $company = $walletRow->nomecom;
    $cnpj = $walletRow->cnpj;
    $address = $walletRow->enderecom;
    $phone = $walletRow->contato;
    $msg = $walletRow->msg;
    $msgqr = $walletRow->msgqr;
    $msgpix = $walletRow->msgpix;

    $paymentType = $walletRow->pagamentos;

    $clients = $connect->query("SELECT * FROM clientes WHERE Id='" . $paymentsRow->idc . "'");
    $clientsRow = $clients->fetch(PDO::FETCH_OBJ);

    if ($clientsRow) {
      $name = explode(" ", $clientsRow->nome);
      $firstName = $name[0];
      $lastName = end($name);
      $phone = $clientsRow->celular;
      $idcli = $clientsRow->Id;

      $installment = $paymentsRow->parcela;
      $idcob = $paymentsRow->Id;
      $paymentDate = $paymentsRow->datapagamento;

      $bytes = random_bytes(16);
      $idempotency = bin2hex($bytes);

      $mercadopagoQuery = "SELECT * FROM mercadopago WHERE instancia = " . $idcob;
      $mercadopago = $connect->query($mercadopagoQuery);
      $mercadopagoRow = $mercadopago->fetch(PDO::FETCH_OBJ);

      if ($mercadopagoRow) {
        $linkcob = "/pagamento/?idCob=" . $mercadopagoRow->id . "&idInst=" . $idcob . "&idFin=" . $idcob;



        $messages1 = $connect->query("SELECT * FROM mensagens WHERE tipo = '1' AND idu = '" . $paymentsRow->idm . "'");
        $messages1Row = $messages1->fetch(PDO::FETCH_OBJ);

        $search = array('#NOME#', '#VENCIMENTO#', '#VALOR#', '#LINK#', '#EMPRESA#', '#CNPJ#', '#ENDERECO#', '#CONTATO#');
        $replace = array($firstName . " " . $lastName, $paymentDate, $installment, $linkcob, $company, $cnpj, $address, $phone);
        $message = str_replace($search, $replace, $messages1Row->msg);

        $messageToSend = str_replace("\n", "\\n", $message);

        if ($dateFormated == date("Ymd", strtotime("+5 days", strtotime(date("Ymd"))))) {
          sendCobAuto($messages1Row->hora, $actualHour, $msg, $urlapi, $tokenapi, $phone, $messageToSend, $apikey, $paymentType, $connect, $idcli, $installment, $firstName, $lastName, $tokenmp, $msgpix, $msgqr);
        }

        $messages2 = $connect->query("SELECT * FROM mensagens WHERE tipo = '2' AND idu = '" . $paymentsRow->idm . "'");
        $messages2Row = $messages2->fetch(PDO::FETCH_OBJ);

        $search = array('#NOME#', '#VENCIMENTO#', '#VALOR#', '#LINK#', '#EMPRESA#', '#CNPJ#', '#ENDERECO#', '#CONTATO#');
        $replace = array($firstName . " " . $lastName, $paymentDate, $installment, $linkcob, $company, $cnpj, $address, $phone);
        $message = str_replace($search, $replace, $messages2Row->msg);

        $messageToSend = str_replace("\n", "\\n", $message);

        if ($dateFormated == $beforeDate) {
          sendCobAuto($messages2Row->hora, $actualHour, $msg, $urlapi, $tokenapi, $phone, $messageToSend, $apikey, $paymentType, $connect, $idcli, $installment, $firstName, $lastName, $tokenmp, $msgpix, $msgqr);
        }

        $messages3 = $connect->query("SELECT * FROM mensagens WHERE tipo = '3' AND idu = '" . $paymentsRow->idm . "'");
        $messages3Row = $messages3->fetch(PDO::FETCH_OBJ);

        $search = array('#NOME#', '#VENCIMENTO#', '#VALOR#', '#LINK#', '#EMPRESA#', '#CNPJ#', '#ENDERECO#', '#CONTATO#');
        $replace = array($firstName . " " . $lastName, $paymentDate, $installment, $linkcob, $company, $cnpj, $address, $phone);
        $message = str_replace($search, $replace, $messages3Row->msg);

        $messageToSend = str_replace("\n", "\\n", $message);

        if ($dateFormated == $currentDate) {
          sendCobAuto($messages3Row->hora, $actualHour, $msg, $urlapi, $tokenapi, $phone, $messageToSend, $apikey, $paymentType, $connect, $idcli, $installment, $firstName, $lastName, $tokenmp, $msgpix, $msgqr);
        }

        $messages4 = $connect->query("SELECT * FROM mensagens WHERE tipo = '4' AND idu = '" . $paymentsRow->idm . "'");
        $messages4Row = $messages4->fetch(PDO::FETCH_OBJ);

        $search = array('#NOME#', '#VENCIMENTO#', '#VALOR#', '#LINK#', '#EMPRESA#', '#CNPJ#', '#ENDERECO#', '#CONTATO#');
        $replace = array($firstName . " " . $lastName, $paymentDate, $installment, $linkcob, $company, $cnpj, $address, $phone);
        $message = str_replace($search, $replace, $messages4Row->msg);

        $messageToSend = str_replace("\n", "\\n", $message);

        if ($dateFormated < date("Ymd")) {
          sendCobAuto($messages4Row->hora, $actualHour, $msg, $urlapi, $tokenapi, $phone, $messageToSend, $apikey, $paymentType, $connect, $idcli, $installment, $firstName, $lastName, $tokenmp, $msgpix, $msgqr);
        }

        // Adicionando a verificação de cobrança com 7 dias de antecedência
        $messages7 = $connect->query("SELECT * FROM mensagens WHERE tipo = '7' AND idu = '" . $paymentsRow->idm . "'");
        $messages7Row = $messages7->fetch(PDO::FETCH_OBJ);

        $search = array('#NOME#', '#VENCIMENTO#', '#VALOR#', '#LINK#', '#EMPRESA#', '#CNPJ#', '#ENDERECO#', '#CONTATO#');
        $replace = array($firstName . " " . $lastName, $paymentDate, $installment, $linkcob, $company, $cnpj, $address, $phone);
        $message = str_replace($search, $replace, $messages7Row->msg);

        $messageToSend = str_replace("\n", "\\n", $message);

        if ($dateFormated == $dueDate7) {
          sendCobAuto($messages7Row->hora, $actualHour, $msg, $urlapi, $tokenapi, $phone, $messageToSend, $apikey, $paymentType, $connect, $idcli, $installment, $firstName, $lastName, $tokenmp, $msgpix, $msgqr);
        }
      }
    }
  }
}