<?php
 namespace App\Modules\Sms\Ozteksms; use App\Core\Sms\AbstractSms; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class Ozteksms extends AbstractSms { protected const DEFAULT_API_URL = 'http://www.ozteksms.com/panel/smsgonder1Npost.php'; public function sendSms(array $params): array { $to = $params['to'] ?? null; $content = $params['content'] ?? null; if (! $to || ! $content) { return ['success' => false, 'error' => 'Required fields missing (to, content)']; } if ($this->isTestMode) { Log::info('Öztek SMS TEST', ['to' => $to, 'content' => $content]); return ['success' => true, 'message_id' => 'test_oztek_'.uniqid()]; } $usernameId = $this->getConfig('username_id'); $username = $this->getConfig('username'); $password = $this->getConfig('password'); $sender = $this->getConfig('sender'); $apiUrl = $this->getConfig('api_url') ?: static::DEFAULT_API_URL; if (! $usernameId || ! $username || ! $password || ! $sender) { return ['success' => false, 'error' => 'Öztek SMS configuration incomplete']; } try { $number = $this->normalizeMsisdn((string) $to, '90'); $xml = '<sms>' .'<kno>'.$this->xml($usernameId).'</kno>' .'<kulad>'.$this->xml($username).'</kulad>' .'<sifre>'.$this->xml($password).'</sifre>' .'<gonderen>'.$this->xml($sender).'</gonderen>' .'<mesaj>'.$this->xml($content).'</mesaj>' .'<numaralar>'.$this->xml($number).'</numaralar>' .'<tur>Normal</tur>' .'</sms>'; $resp = Http::asForm() ->timeout(30) ->post($apiUrl, ['data' => $xml]); if (! $resp->successful()) { return ['success' => false, 'error' => 'Öztek HTTP '.$resp->status()]; } $body = trim($resp->body()); if ($body === '') { return ['success' => false, 'error' => 'Öztek SMS empty response']; } $parts = explode(':', $body); $code = trim($parts[0] ?? ''); $rid = isset($parts[1]) ? trim($parts[1]) : ''; if ($code !== '1') { return ['success' => false, 'error' => 'Öztek SMS rejected (code '.$code.')']; } if ($rid === '') { return ['success' => false, 'error' => 'Öztek SMS accepted without report id']; } return ['success' => true, 'message_id' => $rid]; } catch (\Throwable $e) { return ['success' => false, 'error' => $e->getMessage()]; } } public function testConnection(array $params): array { $usernameId = $params['username_id'] ?? $this->getConfig('username_id'); $username = $params['username'] ?? $this->getConfig('username'); $password = $params['password'] ?? $this->getConfig('password'); if (! $usernameId || ! $username || ! $password) { return ['success' => false, 'message' => 'username_id/username/password required']; } return ['success' => true, 'message' => 'Öztek SMS credentials provided (verified on first send)']; } public function getRequiredConfigFields(): array { return ['username_id', 'username', 'password', 'sender']; } public function getConfigFields(): array { return [ 'username_id' => ['type' => 'text', 'label' => 'Öztek Kullanıcı No (kno)', 'required' => true], 'username' => ['type' => 'text', 'label' => 'Öztek Kullanıcı Adı', 'required' => true], 'password' => ['type' => 'password', 'label' => 'Öztek Şifre', 'required' => true, 'encrypted' => true], 'sender' => ['type' => 'text', 'label' => 'Gönderici Adı (Originator)', 'required' => true], 'api_url' => ['type' => 'url', 'label' => 'API URL', 'required' => false, 'default' => self::DEFAULT_API_URL], 'test_mode' => ['type' => 'boolean', 'label' => 'Test Mode', 'default' => true], ]; } protected function getModuleLabel(): string { return 'Öztek SMS'; } protected function getModuleDescription(): string { return 'Öztek SMS provider (Turkey)'; } private function xml(string $value): string { return htmlspecialchars($value, ENT_XML1 | ENT_QUOTES, 'UTF-8'); } } 