restaurantId = $restaurant_id; $this->loadConfig(); $this->initializeProvider(); } protected function loadConfig() { $this->config = EmailConfig::where('restaurant_id', $this->restaurantId)->first(); if (!$this->config) { // Si no existe configuración, crear una por defecto $this->config = new EmailConfig(); $this->config->restaurant_id = $this->restaurantId; $defaultValues = $this->config->defaultValue(); foreach ($defaultValues as $key => $value) { $this->config->{$key} = $value; } } } protected function initializeProvider() { if (!$this->config->use_sms) { return; } $provider = $this->config->sms_gateway ?? 'twilio'; // Default to Twilio for backward compatibility switch ($provider) { case 'twilio': $this->provider = new TwilioProvider($this->config); break; case 'bulkgate': $this->provider = new BulkgateProvider($this->config); break; default: $this->provider = null; } } /** * Check if SMS service is enabled and configured */ public function is_configured() { if (!$this->config || !$this->config->use_sms) { return false; } if (!$this->provider || !($this->provider instanceof SmsProviderInterface)) { return false; } return $this->provider->isConfigured(); } /** * Send SMS message using configured provider * * @param string $to_phone Recipient phone number * @param string $type Message type ('sms' or 'whatsapp') * @param string $message Message content * @return bool Success status */ public function send_message($to_phone = false, $type = 'sms', $message = '') { // Clean and validate phone number $to_phone = alexr_clean_phone_number($to_phone); if (!alexr_is_valid_phone($to_phone)) { return false; } if (!$this->is_configured()) { throw new \Exception(__eva('SMS not configured.')); } // Handle WhatsApp for Twilio (backward compatibility) if ($type == 'whatsapp' && $this->provider instanceof TwilioProvider) { return $this->send_whatsapp_message($to_phone, $message); } $result = $this->provider->send($to_phone, $message); if (!$result['success']) { throw new \Exception($result['error']); } return true; } /** * Send WhatsApp message via Twilio (backward compatibility) */ protected function send_whatsapp_message($to_phone, $message) { if (!defined('ALEXR_PRO_PLUGIN_DIR')) { throw new \Exception(__eva('You need to activate the PRO plugin.')); } require_once ALEXR_PRO_PLUGIN_DIR . 'vendors/twilio/vendor/autoload.php'; $whatsapp_phone = $this->config->twilio_whatsapp_phone; $from_phone = $whatsapp_phone; if (!preg_match('#whatsapp:#', $from_phone)) { $from_phone = 'whatsapp:' . $whatsapp_phone; } if (!preg_match('#whatsapp:#', $to_phone)) { $to_phone = 'whatsapp:' . $to_phone; } $client = new \Twilio\Rest\Client( $this->config->twilio_sid, $this->config->twilio_token ); $result = $client->messages->create( $to_phone, [ 'from' => $from_phone, 'body' => $message ] ); return true; } /** * Send test SMS message */ public function send_test_message($to_phone, $type = 'sms') { $to_phone = alexr_clean_phone_number($to_phone); if (!alexr_is_valid_phone($to_phone)) { return false; } if (!$this->is_configured()) { throw new \Exception(__eva('SMS not configured.')); } $message = 'This is a test message!'; if ($type == 'whatsapp') { $message = 'Your appointment is coming up on -THIS IS A TEST-'; return $this->send_whatsapp_message($to_phone, $message); } $result = $this->provider->send($to_phone, $message); if (!$result['success']) { return ['error' => $result['error'], 'debug_log' => $result['debug_log']]; } return true; } /** * Get detailed send result with debug information */ public function send_with_result($to_phone, $message) { $to_phone = alexr_clean_phone_number($to_phone); if (!alexr_is_valid_phone($to_phone)) { return [ 'success' => false, 'error' => 'Invalid phone number', 'debug_log' => '' ]; } if (!$this->is_configured()) { return [ 'success' => false, 'error' => __eva('SMS not configured.'), 'debug_log' => '' ]; } return $this->provider->send($to_phone, $message); } /** * Get the current provider's debug log */ public function getDebugLog() { return $this->provider ? $this->provider->getDebugLog() : ''; } /** * Get current provider name */ public function getProviderName() { if (!$this->provider) { return 'none'; } if ($this->provider instanceof TwilioProvider) { return 'twilio'; } if ($this->provider instanceof BulkgateProvider) { return 'bulkgate'; } return 'unknown'; } }