<?php
 namespace App\Modules\Sms\Clicksend; use App\Core\Sms\AbstractSms; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; class Clicksend extends AbstractSms { 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('ClickSend TEST', ['to' => $to, 'content' => $content]); return ['success' => true, 'message_id' => 'test_cs_'.uniqid()]; } $username = $this->getConfig('username'); $apiKey = $this->getConfig('api_key'); if (! $username || ! $apiKey) { return ['success' => false, 'error' => 'ClickSend configuration incomplete']; } try { $resp = Http::withBasicAuth($username, $apiKey) ->timeout(30) ->post('https://rest.clicksend.com/v3/sms/send', [ 'messages' => [[ 'source' => 'php', 'from' => $this->getConfig('from'), 'to' => $this->normalizeMsisdn($to, $this->getConfig('default_country_code')), 'body' => $content, ]], ]); if (! $resp->successful()) { return ['success' => false, 'error' => 'ClickSend HTTP '.$resp->status()]; } $data = $resp->json(); $responseCode = (string) ($data['response_code'] ?? ''); $msg = $data['data']['messages'][0] ?? []; $msgStatus = (string) ($msg['status'] ?? ''); $messageId = $msg['message_id'] ?? null; if ($responseCode !== 'SUCCESS' || $msgStatus !== 'SUCCESS' || ! $messageId) { return [ 'success' => false, 'error' => 'ClickSend rejected: response_code='.($responseCode ?: 'unknown') .', status='.($msgStatus ?: 'unknown'), ]; } return ['success' => true, 'message_id' => (string) $messageId]; } catch (\Throwable $e) { return ['success' => false, 'error' => $e->getMessage()]; } } public function testConnection(array $params): array { $username = $params['username'] ?? $this->getConfig('username'); $apiKey = $params['api_key'] ?? $this->getConfig('api_key'); if (! $username || ! $apiKey) { return ['success' => false, 'message' => 'username/api_key required']; } try { $resp = Http::withBasicAuth($username, $apiKey)->timeout(15) ->get('https://rest.clicksend.com/v3/account'); } catch (\Throwable $e) { return ['success' => false, 'message' => 'ClickSend connection failed: '.$e->getMessage()]; } return $resp->successful() ? ['success' => true, 'message' => 'ClickSend connection OK'] : ['success' => false, 'message' => 'ClickSend HTTP '.$resp->status()]; } public function getRequiredConfigFields(): array { return ['username', 'api_key']; } public function getConfigFields(): array { return [ 'username' => ['type' => 'text', 'label' => 'ClickSend Username', 'required' => true], 'api_key' => ['type' => 'password', 'label' => 'ClickSend API Key', 'required' => true, 'encrypted' => true], 'from' => ['type' => 'text', 'label' => 'Sender ID', 'required' => false], 'default_country_code' => ['type' => 'text', 'label' => 'Default Country Code (ör. 90, 1)', 'required' => false], 'test_mode' => ['type' => 'boolean', 'label' => 'Test Mode', 'default' => true], ]; } } 