332 lines
16 KiB
PHP
332 lines
16 KiB
PHP
<?php
|
|
/**
|
|
* Solucionador de problemas WhatsApp API
|
|
* Fecha: 5 de enero de 2026
|
|
*/
|
|
|
|
require_once 'config/config.php';
|
|
|
|
// Verificar autenticación
|
|
if (!isUserLoggedIn()) {
|
|
header('Location: login.php');
|
|
exit;
|
|
}
|
|
|
|
header('Content-Type: text/html; charset=utf-8');
|
|
|
|
$success = '';
|
|
$error = '';
|
|
|
|
// Procesar formulario
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
try {
|
|
$action = $_POST['action'] ?? '';
|
|
|
|
if ($action === 'update_config') {
|
|
$newToken = trim($_POST['whatsapp_token'] ?? '');
|
|
$newPhoneId = trim($_POST['phone_number_id'] ?? '');
|
|
$newApiUrl = trim($_POST['api_url'] ?? WHATSAPP_API_URL);
|
|
$newWebhookToken = trim($_POST['webhook_token'] ?? WEBHOOK_VERIFY_TOKEN);
|
|
|
|
if (empty($newToken) || empty($newPhoneId)) {
|
|
throw new Exception('Token y Phone Number ID son obligatorios');
|
|
}
|
|
|
|
$db = Database::getInstance();
|
|
|
|
// Actualizar en base de datos
|
|
$configs = [
|
|
'whatsapp_token' => $newToken,
|
|
'whatsapp_phone_number_id' => $newPhoneId,
|
|
'whatsapp_api_url' => $newApiUrl,
|
|
'webhook_verify_token' => $newWebhookToken
|
|
];
|
|
|
|
foreach ($configs as $key => $value) {
|
|
$existing = $db->fetch("SELECT id FROM system_config WHERE config_key = ?", [$key]);
|
|
|
|
if ($existing) {
|
|
$db->query("UPDATE system_config SET config_value = ?, updated_at = NOW() WHERE config_key = ?", [$value, $key]);
|
|
} else {
|
|
$db->query("INSERT INTO system_config (config_key, config_value, created_at, updated_at) VALUES (?, ?, NOW(), NOW())", [$key, $value]);
|
|
}
|
|
}
|
|
|
|
$success = 'Configuración actualizada correctamente. <a href="#test">Probar conexión</a>';
|
|
|
|
} elseif ($action === 'test_connection') {
|
|
// Probar conexión con los valores actuales
|
|
$testResult = testWhatsAppConnection();
|
|
if ($testResult['success']) {
|
|
$success = 'Conexión exitosa: ' . $testResult['message'];
|
|
} else {
|
|
$error = 'Error en conexión: ' . $testResult['message'];
|
|
}
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
$error = 'Error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
|
|
function testWhatsAppConnection() {
|
|
try {
|
|
$token = getConfigFromDB('whatsapp_token', WHATSAPP_TOKEN);
|
|
$phoneId = getConfigFromDB('whatsapp_phone_number_id', WHATSAPP_PHONE_NUMBER_ID);
|
|
$apiUrl = getConfigFromDB('whatsapp_api_url', WHATSAPP_API_URL);
|
|
|
|
if ($token === 'TU_TOKEN_DE_WHATSAPP_AQUI' || $phoneId === 'TU_PHONE_ID_AQUI') {
|
|
return ['success' => false, 'message' => 'Configuración incompleta'];
|
|
}
|
|
|
|
$ch = curl_init();
|
|
$url = $apiUrl . $phoneId;
|
|
|
|
curl_setopt_array($ch, [
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_HTTPHEADER => [
|
|
'Authorization: Bearer ' . $token,
|
|
'Content-Type: application/json'
|
|
],
|
|
CURLOPT_TIMEOUT => 15,
|
|
CURLOPT_SSL_VERIFYPEER => true
|
|
]);
|
|
|
|
$response = curl_exec($ch);
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
curl_close($ch);
|
|
|
|
if ($error) {
|
|
return ['success' => false, 'message' => 'Error cURL: ' . $error];
|
|
}
|
|
|
|
$decoded = json_decode($response, true);
|
|
|
|
if ($httpCode === 200) {
|
|
$displayName = $decoded['display_phone_number'] ?? 'N/A';
|
|
return ['success' => true, 'message' => "Número verificado: $displayName"];
|
|
} else {
|
|
$errorMsg = $decoded['error']['message'] ?? 'Error desconocido';
|
|
return ['success' => false, 'message' => $errorMsg];
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
return ['success' => false, 'message' => $e->getMessage()];
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="es">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Solucionador WhatsApp API</title>
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
|
|
<style>
|
|
.status-card { border-left: 5px solid #dc3545; }
|
|
.status-card.success { border-left-color: #28a745; }
|
|
.status-card.warning { border-left-color: #ffc107; }
|
|
.step-card { margin-bottom: 1rem; transition: all 0.3s; }
|
|
.step-card:hover { transform: translateY(-2px); box-shadow: 0 4px 15px rgba(0,0,0,0.1); }
|
|
.code-block { background: #f8f9fa; border-radius: 8px; padding: 15px; font-family: monospace; }
|
|
.whatsapp-green { background: linear-gradient(135deg, #25D366, #128C7E); }
|
|
</style>
|
|
</head>
|
|
<body class="bg-light">
|
|
<div class="container my-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-lg-10">
|
|
|
|
<!-- Header -->
|
|
<div class="card whatsapp-green text-white mb-4">
|
|
<div class="card-body text-center">
|
|
<h1><i class="fab fa-whatsapp me-2"></i>Solucionador WhatsApp API</h1>
|
|
<p class="mb-0">Diagnóstico y configuración automática</p>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Alertas -->
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success">
|
|
<i class="fas fa-check-circle me-2"></i><?= $success ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger">
|
|
<i class="fas fa-exclamation-triangle me-2"></i><?= $error ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<!-- Estado Actual -->
|
|
<div class="card status-card mb-4">
|
|
<div class="card-header">
|
|
<h5><i class="fas fa-info-circle me-2"></i>Estado Actual del Sistema</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<h6>Configuración Actual:</h6>
|
|
<ul class="list-unstyled">
|
|
<li><strong>Token:</strong>
|
|
<?php if (WHATSAPP_TOKEN !== 'TU_TOKEN_DE_WHATSAPP_AQUI'): ?>
|
|
<span class="text-success">✅ Configurado</span>
|
|
<?php else: ?>
|
|
<span class="text-danger">❌ No configurado</span>
|
|
<?php endif; ?>
|
|
</li>
|
|
<li><strong>Phone ID:</strong>
|
|
<?php if (WHATSAPP_PHONE_NUMBER_ID !== 'TU_PHONE_ID_AQUI'): ?>
|
|
<span class="text-warning">⚠️ <?= WHATSAPP_PHONE_NUMBER_ID ?></span>
|
|
<?php if (WHATSAPP_PHONE_NUMBER_ID === '858157464051987'): ?>
|
|
<br><small class="text-danger">Este ID está causando errores</small>
|
|
<?php endif; ?>
|
|
<?php else: ?>
|
|
<span class="text-danger">❌ No configurado</span>
|
|
<?php endif; ?>
|
|
</li>
|
|
<li><strong>API URL:</strong> <?= WHATSAPP_API_URL ?></li>
|
|
</ul>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<h6>Error Reportado:</h6>
|
|
<div class="code-block">
|
|
<small>Object with ID '858157464051987' does not exist, cannot be loaded due to missing permissions, or does not support this operation.</small>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Pasos de Solución -->
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<div class="card step-card">
|
|
<div class="card-header bg-primary text-white">
|
|
<h6><i class="fas fa-cog me-2"></i>Paso 1: Actualizar Configuración</h6>
|
|
</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="update_config">
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Token de WhatsApp Business API</label>
|
|
<input type="text" class="form-control" name="whatsapp_token"
|
|
value="<?= htmlspecialchars(getConfigFromDB('whatsapp_token', WHATSAPP_TOKEN)) ?>"
|
|
placeholder="EAAxxxxxxxxx..." required>
|
|
<small class="text-muted">Obténlo desde Facebook for Developers</small>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Phone Number ID</label>
|
|
<input type="text" class="form-control" name="phone_number_id"
|
|
value="<?= htmlspecialchars(getConfigFromDB('whatsapp_phone_number_id', WHATSAPP_PHONE_NUMBER_ID)) ?>"
|
|
placeholder="123456789012345" required>
|
|
<small class="text-muted">ID del número verificado en Meta</small>
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">API URL</label>
|
|
<input type="text" class="form-control" name="api_url"
|
|
value="<?= htmlspecialchars(getConfigFromDB('whatsapp_api_url', WHATSAPP_API_URL)) ?>">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Webhook Verify Token</label>
|
|
<input type="text" class="form-control" name="webhook_token"
|
|
value="<?= htmlspecialchars(getConfigFromDB('webhook_verify_token', WEBHOOK_VERIFY_TOKEN)) ?>">
|
|
</div>
|
|
|
|
<button type="submit" class="btn btn-primary">
|
|
<i class="fas fa-save me-2"></i>Guardar Configuración
|
|
</button>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="col-md-6">
|
|
<div class="card step-card">
|
|
<div class="card-header bg-success text-white">
|
|
<h6><i class="fas fa-test-tube me-2"></i>Paso 2: Probar Conexión</h6>
|
|
</div>
|
|
<div class="card-body" id="test">
|
|
<p>Una vez guardada la configuración, prueba la conexión:</p>
|
|
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="test_connection">
|
|
<button type="submit" class="btn btn-success">
|
|
<i class="fas fa-plug me-2"></i>Probar Conexión
|
|
</button>
|
|
</form>
|
|
|
|
<hr>
|
|
|
|
<h6>Guía de obtención de credenciales:</h6>
|
|
<ol class="small">
|
|
<li>Ve a <a href="https://developers.facebook.com/apps" target="_blank">Facebook for Developers</a></li>
|
|
<li>Selecciona tu aplicación WhatsApp Business</li>
|
|
<li>Ve a "WhatsApp" → "Configuración"</li>
|
|
<li>Copia el "Token de acceso" y el "Phone Number ID"</li>
|
|
<li>Verifica que el número esté validado y activo</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Información Adicional -->
|
|
<div class="card mb-4">
|
|
<div class="card-header">
|
|
<h5><i class="fas fa-lightbulb me-2"></i>Posibles Causas del Error</h5>
|
|
</div>
|
|
<div class="card-body">
|
|
<div class="row">
|
|
<div class="col-md-4">
|
|
<h6 class="text-danger">❌ ID Incorrecto</h6>
|
|
<p class="small">El Phone Number ID no corresponde a ningún número verificado en tu cuenta de Meta Business.</p>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h6 class="text-warning">⚠️ Sin Permisos</h6>
|
|
<p class="small">El token de acceso no tiene permisos para acceder a este número específico.</p>
|
|
</div>
|
|
<div class="col-md-4">
|
|
<h6 class="text-info">🔄 Número No Verificado</h6>
|
|
<p class="small">El número de teléfono no está completamente verificado en Meta Business Manager.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="alert alert-info mt-3">
|
|
<h6><i class="fas fa-info-circle me-2"></i>Solución Recomendada:</h6>
|
|
<p class="mb-0">
|
|
1. Verifica en Meta for Developers que el número esté activo<br>
|
|
2. Copia el Phone Number ID correcto (no uses el del ejemplo)<br>
|
|
3. Asegúrate de que el token tenga permisos <code>whatsapp_business_messaging</code><br>
|
|
4. Prueba la conexión antes de enviar mensajes
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Enlaces útiles -->
|
|
<div class="text-center">
|
|
<a href="index.php" class="btn btn-outline-primary me-2">
|
|
<i class="fas fa-arrow-left me-2"></i>Volver al Panel
|
|
</a>
|
|
<a href="diagnostico_whatsapp_avanzado.php" class="btn btn-outline-info me-2">
|
|
<i class="fas fa-search me-2"></i>Diagnóstico Completo
|
|
</a>
|
|
<a href="test_whatsapp_completo.php" class="btn btn-outline-success">
|
|
<i class="fas fa-vial me-2"></i>Pruebas Avanzadas
|
|
</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|