fetch("SELECT id FROM system_config WHERE config_key = 'whatsapp_phone_number_id'"); if ($existing) { $db->query( "UPDATE system_config SET config_value = ?, updated_at = NOW() WHERE config_key = 'whatsapp_phone_number_id'", [$newPhoneId] ); } else { $db->query( "INSERT INTO system_config (config_key, config_value, created_at, updated_at) VALUES ('whatsapp_phone_number_id', ?, NOW(), NOW())", [$newPhoneId] ); } $result = ['success' => true, 'message' => 'Phone Number ID actualizado correctamente']; // Probar nueva configuración $testResult = testNewConfig($newPhoneId); $result['test'] = $testResult; } catch (Exception $e) { $error = 'Error actualizando configuración: ' . $e->getMessage(); } } } } function findAvailablePhoneNumbers($token) { try { // Método 1: Buscar a través de Business Account $ch = curl_init(); $url = 'https://graph.facebook.com/v18.0/me/businesses'; 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); $curlError = curl_error($ch); curl_close($ch); if ($curlError) { return ['success' => false, 'error' => 'Error cURL: ' . $curlError]; } $decoded = json_decode($response, true); if ($httpCode !== 200) { // Método 2: Intentar obtener info de la app directamente return findPhoneNumbersFromApp($token); } $phoneNumbers = []; if (isset($decoded['data']) && is_array($decoded['data'])) { foreach ($decoded['data'] as $business) { $businessId = $business['id']; $phoneNumbersData = getPhoneNumbersFromBusiness($token, $businessId); if ($phoneNumbersData['success']) { $phoneNumbers = array_merge($phoneNumbers, $phoneNumbersData['numbers']); } } } return [ 'success' => true, 'numbers' => $phoneNumbers, 'method' => 'business_account' ]; } catch (Exception $e) { return ['success' => false, 'error' => $e->getMessage()]; } } function findPhoneNumbersFromApp($token) { try { // Intentar con diferentes endpoints $endpoints = [ 'https://graph.facebook.com/v18.0/me', 'https://graph.facebook.com/v18.0/me/accounts', 'https://graph.facebook.com/v18.0/me/applications' ]; foreach ($endpoints as $endpoint) { $ch = curl_init(); curl_setopt_array($ch, [ CURLOPT_URL => $endpoint, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ], CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => true ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200) { $decoded = json_decode($response, true); // Aquí podrías analizar la respuesta para encontrar números } } return [ 'success' => false, 'error' => 'No se pudieron encontrar números automáticamente. Debe obtenerlos manualmente de Facebook for Developers.', 'manual_steps' => [ '1. Ve a https://developers.facebook.com/apps', '2. Selecciona tu aplicación WhatsApp Business', '3. Ve a Productos > WhatsApp > Configuración', '4. En la sección "Números de teléfono", copia el Phone Number ID', '5. Pega el ID en el formulario de abajo' ] ]; } catch (Exception $e) { return ['success' => false, 'error' => $e->getMessage()]; } } function getPhoneNumbersFromBusiness($token, $businessId) { try { $ch = curl_init(); $url = "https://graph.facebook.com/v18.0/{$businessId}/phone_numbers"; curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ], CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => true ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200) { $decoded = json_decode($response, true); if (isset($decoded['data'])) { return [ 'success' => true, 'numbers' => $decoded['data'] ]; } } return ['success' => false, 'numbers' => []]; } catch (Exception $e) { return ['success' => false, 'numbers' => []]; } } function testNewConfig($phoneId) { try { $token = getConfigFromDB('whatsapp_token', WHATSAPP_TOKEN); $ch = curl_init(); $url = WHATSAPP_API_URL . $phoneId; curl_setopt_array($ch, [ CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $token, 'Content-Type: application/json' ], CURLOPT_TIMEOUT => 10, CURLOPT_SSL_VERIFYPEER => true ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($curlError) { return ['success' => false, 'message' => 'Error: ' . $curlError]; } $decoded = json_decode($response, true); if ($httpCode === 200) { $displayName = $decoded['display_phone_number'] ?? 'N/A'; return ['success' => true, 'message' => "✅ Configuración válida. Número: $displayName"]; } else { $errorMsg = $decoded['error']['message'] ?? 'Error desconocido'; return ['success' => false, 'message' => "❌ Error: $errorMsg"]; } } catch (Exception $e) { return ['success' => false, 'message' => 'Error: ' . $e->getMessage()]; } } ?>
Encuentra y configura el Phone Number ID válido para tu cuenta
Phone Number ID Actual: 858157464051987
Error: Este ID no existe, no tienes permisos, o no soporta la operación solicitada.
Intentar encontrar números de WhatsApp asociados a tu token:
No se encontraron números automáticamente.
Pasos manuales:Si conoces el Phone Number ID correcto, ingrésalo aquí: