'system']) { if ($lang === null) { $lang = $this->language; } $booking = $this; // 1. Check custom message exists $restaurant_id = $booking->restaurant_id; $customMessage = WhatsappCustomMessage::where('restaurant_id', $restaurant_id) ->where('id', $customMessageId) ->first(); if (!$customMessage) { return false; } // 2. Check custom message is active if (!$customMessage->isEnabled()) { return false; } // 3. Get template configuration for the language $templateConfig = $customMessage->getTemplateConfig($lang); if (!$templateConfig || empty($templateConfig['template_sid'])) { return false; } $templateSid = $templateConfig['template_sid']; $templateName = $templateConfig['template_name'] ?? ''; $fieldMapping = $templateConfig['field_mapping'] ?? []; $staticValues = $templateConfig['static_values'] ?? []; // 4. Map booking fields to template variables, including static values $variables = $this->mapBookingFieldsToVariables($booking, $fieldMapping, null, $staticValues); // 5. Get customer phone $customerPhone = $this->getCustomerPhone($booking); if (empty($customerPhone)) { return false; } // 5b. Check if customer has WhatsApp if ($booking->customer_id) { $customer = Customer::find($booking->customer_id); if ($customer && !$customer->has_whatsapp) { return false; } } // 6. Send the WhatsApp template using WhatsappManager $manager = new WhatsappManager($booking->restaurant_id); $result = $manager->sendTemplate( $customerPhone, $templateSid, $variables, $booking->id, $booking->customer_id, $templateName, $senderInfo ); // 7. Store the notification $this->storeWhatsappSent( $customerPhone, $templateName, $templateSid, $variables, $result, 'whatsapp_custom_message', $customMessage->id ); return $result; } }