$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. Probar conexión'; } 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()]; } } ?>
Diagnóstico y configuración automática
Una vez guardada la configuración, prueba la conexión:
El Phone Number ID no corresponde a ningún número verificado en tu cuenta de Meta Business.
El token de acceso no tiene permisos para acceder a este número específico.
El número de teléfono no está completamente verificado en Meta Business Manager.
1. Verifica en Meta for Developers que el número esté activo
2. Copia el Phone Number ID correcto (no uses el del ejemplo)
3. Asegúrate de que el token tenga permisos whatsapp_business_messaging
4. Prueba la conexión antes de enviar mensajes