'system']) { if ( ! $this->canSendWhatsppToUser() ) { return [ 'success' => false, 'error_code' => 'whatsapp_user_not_allowed', 'message' => __eva('User cannot receive WhatsApp messages'), 'data' => [], ]; } if ($lang == null) { $lang = $this->language; } $booking = $this; // 1. Extraer el mensaje // $messageType // 2. Obtener la configuración de WhatsappMessage $whatsappMessage = WhatsappMessage::where('restaurant_id', $booking->restaurant_id)->first(); if (!$whatsappMessage) { //ray('WhatsApp Messages not configured'); return; } // 3. Comprobar si está activo if (!$whatsappMessage->isEnabled($messageType)) { //ray('WhatsApp message for booked is not enabled'); return; } // 4. Obtener el template asociado para el idioma $templateConfig = $whatsappMessage->getTemplateConfig($messageType, $lang); if (!$templateConfig || empty($templateConfig['template_sid'])) { //ray('No template configured for language: ' . $lang); return; } $templateSid = $templateConfig['template_sid']; $templateName = $templateConfig['template_name'] ?? ''; $fieldMapping = $templateConfig['field_mapping'] ?? []; // 5. Mapear los datos del booking según el field_mapping $variables = $this->mapBookingFieldsToVariables($booking, $fieldMapping); // 6. Obtener el teléfono del cliente $customerPhone = $this->getCustomerPhone($booking); if (empty($customerPhone)) { //ray('Customer phone is empty, cannot send WhatsApp'); return; } // 6b. Check if customer has WhatsApp if ($booking->customer_id) { $customer = Customer::find($booking->customer_id); if ($customer && !$customer->has_whatsapp) { return; } } // 7. Enviar el whatsapp usando el WhatsappTwilioManager $manager = new WhatsappManager($booking->restaurant_id); //ray('Sending Template'); //ray($senderInfo); $result = $manager->sendTemplate( $customerPhone, $templateSid, $variables, $booking->id, $booking->customer_id, $templateName, $senderInfo ); if (!$result['success'] && $result['error_code'] == 'whatsapp_not_configured') { return $result; } // 8. Almacenar la notificación en BookingNotification $this->storeWhatsappSent( $customerPhone, $templateName, $templateSid, $variables, $result, 'whatsapp', $messageType ); if ($result['success']) { //ray('WhatsApp BOOKED sent successfully', $result); } else { //ray('Failed to send WhatsApp BOOKED', $result); } return $result; } /** * Send any WhatsApp template by SID using this booking's context. * Used by WhatsappKeywordHandler and any other external caller * that needs to send a template linked to a booking. * * @param string $templateSid The WhatsApp template SID * @param string $templateName The template name (for display/logging) * @param array $fieldMapping Mapping of template variables to booking field keys * @param string|null $lang Language code (defaults to booking language) * @param string $notificationType Notification type for storing (default 'whatsapp_action') * @param string|null $notificationTypeId Notification type ID for storing * @return array|null */ public function sendWhatsappTemplateBySid( string $templateSid, string $templateName = '', array $fieldMapping = [], ?string $lang = null, string $notificationType = 'whatsapp_action', ?string $notificationTypeId = null, ?int $bookNowWidgetId = null, array $senderInfo = ['sender_type' => 'system'] ): ?array { if (!$this->canSendWhatsppToUser()) { return [ 'success' => false, 'error_code' => 'whatsapp_user_not_allowed', 'message' => __eva('User cannot receive WhatsApp messages'), 'data' => [], ]; } $booking = $this; if ($lang === null) { $lang = $booking->language; } // 1. Map booking fields to template variables $variables = $this->mapBookingFieldsToVariables($booking, $fieldMapping, $bookNowWidgetId); // 2. Get customer phone $customerPhone = $this->getCustomerPhone($booking); if (empty($customerPhone)) { return [ 'success' => false, 'error_code' => 'whatsapp_no_phone', 'message' => __eva('Customer phone is empty, cannot send WhatsApp'), 'data' => [], ]; } // 2b. Check if customer has WhatsApp if ($booking->customer_id) { $customer = Customer::find($booking->customer_id); if ($customer && !$customer->has_whatsapp) { return [ 'success' => false, 'error_code' => 'customer_no_whatsapp', 'message' => __eva('This customer does not have WhatsApp'), 'data' => [], ]; } } // 3. Send via WhatsappManager $manager = new WhatsappManager($booking->restaurant_id); $result = $manager->sendTemplate( $customerPhone, $templateSid, $variables, $booking->id, $booking->customer_id, $templateName, $senderInfo ); // 4. Store notification $this->storeWhatsappSent( $customerPhone, $templateName, $templateSid, $variables, $result, $notificationType, $notificationTypeId ); return $result; } public function sendWhatsappMessage(string $message, $senderInfo = ['sender_type' => 'system']) { if (!$this->canSendWhatsppToUser()) { return [ 'success' => false, 'error_code' => 'whatsapp_user_not_allowed', 'message' => __eva('User cannot receive WhatsApp messages'), 'data' => [], ]; } $booking = $this; $customerPhone = $this->getCustomerPhone($booking); if (empty($customerPhone)) { return [ 'success' => false, 'error_code' => 'whatsapp_no_phone', 'message' => __eva('Customer phone is empty, cannot send WhatsApp'), 'data' => [], ]; } // Check if customer has WhatsApp if ($booking->customer_id) { $customer = Customer::find($booking->customer_id); if ($customer && !$customer->has_whatsapp) { return [ 'success' => false, 'error_code' => 'customer_no_whatsapp', 'message' => __eva('This customer does not have WhatsApp'), 'data' => [], ]; } } $manager = new WhatsappManager($booking->restaurant_id); $result = $manager->sendMessage( $customerPhone, $message, $booking->id, $booking->customer_id, null, $senderInfo ); // Almacenar la notificación en BookingNotification $this->storeWhatsappSent( $customerPhone, 'free_message', null, ['message' => $message], $result, 'whatsapp', 'free_message' ); return $result; } // STATUS MESSAGES public function sendWhatsappPending($lang = null) { //ray('Send Whatsapp PENDING'); return $this->sendWhatsappTemplate('booking_pending', $lang); } public function sendWhatsappPendingPayment($lang = null) { //ray('Send Whatsapp PENDING PAYMENT'); return $this->sendWhatsappTemplate('booking_pending_payment', $lang); } public function sendWhatsappBooked($lang = null) { //ray('Send Whatsapp BOOKED'); return $this->sendWhatsappTemplate('booking_booked', $lang); } public function sendWhatsappConfirmed($lang = null) { //ray('Send Whatsapp CONFIRMED'); return $this->sendWhatsappTemplate('booking_confirmed', $lang); } public function sendWhatsappDenied($lang = null) { //ray('Send Whatsapp DENIED'); return $this->sendWhatsappTemplate('booking_denied', $lang); } public function sendWhatsappCancelled($lang = null) { //ray('Send Whatsapp CANCELLED'); return $this->sendWhatsappTemplate('booking_cancelled', $lang); } public function sendWhatsappNoShow($lang = null) { //ray('Send Whatsapp NO SHOW'); return $this->sendWhatsappTemplate('booking_no_show', $lang); } public function sendWhatsappFinished($lang = null) { //ray('Send Whatsapp FINISHED'); return $this->sendWhatsappTemplate('booking_finished', $lang); } // Guest: "pick your menu" WhatsApp when the menu is still pending after booking. public function sendWhatsappMenuPending($lang = null) { return $this->sendWhatsappTemplate('menu_pending', $lang); } // General funcion used after payment succeed //----------------------------------------------------------------------- public function sendWhatsappNotification() { $booking = $this; //if ($booking->agree_receive_Whatsapp != 1) return; $lang = $booking->language; if (!$lang) { $lang = $booking->restaurant->language; } if ($booking->status == BookingStatus::PENDING) { ////ray('Send Whatsapp PENDING'); $booking->sendWhatsappPending($lang); } else if ($booking->status == BookingStatus::BOOKED) { ////ray("Send Whatsapp BOOKED"); $booking->sendWhatsappBooked($lang); } else if ($booking->status == BookingStatus::FINISHED) { $booking->sendWhatsappFinished($lang); } else if ($booking->status == BookingStatus::DENIED) { $booking->sendWhatsappDenied($lang); } else if ($booking->status == BookingStatus::CANCELLED) { $booking->sendWhatsappCancelled($lang); } else if ($booking->status == BookingStatus::NO_SHOW) { $booking->sendWhatsappNoShow($lang); } } }