diff --git a/modules/turnero/api/wa_profile.php b/modules/turnero/api/wa_profile.php new file mode 100644 index 0000000..5f43aa6 --- /dev/null +++ b/modules/turnero/api/wa_profile.php @@ -0,0 +1,152 @@ + true, + CURLOPT_TIMEOUT => 15, + ]); + $resp = curl_exec($ch); + $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $data = json_decode($resp, true); + if ($http !== 200 || isset($data['error'])) { + jsonError($data['error']['message'] ?? 'Error al leer perfil de WhatsApp', $http); + } + + $perfil = $data['data'][0] ?? $data; + jsonOk(['perfil' => $perfil]); +} + +// ── POST ──────────────────────────────────────────────────── +requireMethod('POST'); + +// Detectar si viene como multipart (foto) o JSON +$contentType = $_SERVER['CONTENT_TYPE'] ?? ''; +$esMultipart = str_contains($contentType, 'multipart/form-data'); + +if ($esMultipart) { + $accion = $_POST['accion'] ?? ''; +} else { + $datos = inputJson(); + $accion = $datos['accion'] ?? ''; +} + +// ── POST accion=foto ───────────────────────────────────────── +if ($accion === 'foto') { + if (empty($_FILES['foto']) || $_FILES['foto']['error'] !== UPLOAD_ERR_OK) { + jsonError('No se recibió imagen válida.'); + } + $file = $_FILES['foto']; + $mime = mime_content_type($file['tmp_name']); + $allowed = ['image/jpeg', 'image/png']; + if (!in_array($mime, $allowed, true)) jsonError('Solo se aceptan JPG o PNG.'); + if ($file['size'] > 5 * 1024 * 1024) jsonError('Imagen máxima: 5 MB.'); + + // 1. Subir media a WhatsApp + $uploadUrl = $apiBase . $phoneId . '/media'; + $ch = curl_init($uploadUrl); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_POST => true, + CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $token], + CURLOPT_POSTFIELDS => [ + 'messaging_product' => 'whatsapp', + 'type' => $mime, + 'file' => new CURLFile($file['tmp_name'], $mime, $file['name']), + ], + CURLOPT_TIMEOUT => 30, + ]); + $resp = curl_exec($ch); + $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $uploadData = json_decode($resp, true); + if ($http !== 200 || empty($uploadData['id'])) { + jsonError('Error al subir imagen: ' . ($uploadData['error']['message'] ?? $resp)); + } + $mediaHandle = $uploadData['id']; + + // 2. Aplicar como foto de perfil + $profileUrl = $apiBase . $phoneId . '/whatsapp_business_profile'; + $payload = json_encode([ + 'messaging_product' => 'whatsapp', + 'profile_picture_handle' => $mediaHandle, + ]); + $ch = curl_init($profileUrl); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $payload, + CURLOPT_HTTPHEADER => [ + 'Authorization: Bearer ' . $token, + 'Content-Type: application/json', + ], + CURLOPT_TIMEOUT => 15, + ]); + $resp2 = curl_exec($ch); + $http2 = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $r2 = json_decode($resp2, true); + if (isset($r2['error'])) jsonError('Error al aplicar foto: ' . $r2['error']['message']); + jsonOk([], 'Foto de perfil actualizada'); +} + +// ── POST accion=guardar ────────────────────────────────────── +if ($accion === 'guardar') { + $campos = ['about', 'description', 'address', 'email', 'vertical']; + $body = ['messaging_product' => 'whatsapp']; + + foreach ($campos as $c) { + $val = trim($datos[$c] ?? ''); + if ($val !== '') $body[$c] = $val; + } + // Websites: array de hasta 2 + $web = array_filter(array_map('trim', (array)($datos['websites'] ?? []))); + if ($web) $body['websites'] = array_values($web); + + $url = $apiBase . $phoneId . '/whatsapp_business_profile'; + $payload = json_encode($body); + $ch = curl_init($url); + curl_setopt_array($ch, [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_POSTFIELDS => $payload, + CURLOPT_HTTPHEADER => [ + 'Authorization: Bearer ' . $token, + 'Content-Type: application/json', + ], + CURLOPT_TIMEOUT => 15, + ]); + $resp = curl_exec($ch); + $http = curl_getinfo($ch, CURLINFO_HTTP_CODE); + curl_close($ch); + + $r = json_decode($resp, true); + if (isset($r['error'])) jsonError('Error de WhatsApp: ' . $r['error']['message']); + jsonOk([], 'Perfil de WhatsApp actualizado'); +} + +jsonError('Acción no reconocida.'); diff --git a/modules/turnero/views/configuracion.php b/modules/turnero/views/configuracion.php index 26f20e6..0433ba2 100644 --- a/modules/turnero/views/configuracion.php +++ b/modules/turnero/views/configuracion.php @@ -794,6 +794,87 @@ $tab = $_GET['tab'] ?? 'lugares'; + +
Perfil de WhatsApp Business
+