sendSms(['to' => '256700000000', 'message' => 'Hello']); */ namespace Fiskon; class RelayException extends \RuntimeException { public $errorCode; public $status; public $data; public function __construct(string $message, ?string $code = null, int $status = 0, $data = null) { parent::__construct($message); $this->errorCode = $code; $this->status = $status; $this->data = $data; } } class Relay { const DEFAULT_BASE = 'https://relay.naweriindustries.com/api/v1'; private $apiKey; private $publicKey; private $base; private $timeout; public function __construct(?string $apiKey = null, ?string $publicKey = null, string $baseUrl = self::DEFAULT_BASE, int $timeout = 30) { if (! $apiKey && ! $publicKey) { throw new \InvalidArgumentException('Provide an apiKey or a publicKey'); } $this->apiKey = $apiKey; $this->publicKey = $publicKey; $this->base = rtrim($baseUrl, '/'); $this->timeout = $timeout; } public static function withPublicKey(string $publicKey, string $baseUrl = self::DEFAULT_BASE): self { return new self(null, $publicKey, $baseUrl); } private function request(string $method, string $path, ?array $body = null, ?string $idempotencyKey = null, bool $client = false): array { $headers = ['Accept: application/json']; if ($client && $this->publicKey && ! $this->apiKey) { $headers[] = 'X-Public-Key: ' . $this->publicKey; } elseif ($this->apiKey) { $headers[] = 'Authorization: Bearer ' . $this->apiKey; } else { throw new \LogicException('This call needs the secret apiKey'); } if ($idempotencyKey) { $headers[] = 'Idempotency-Key: ' . $idempotencyKey; } $ch = curl_init($this->base . $path); $opts = [CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => $this->timeout]; if ($body !== null) { $headers[] = 'Content-Type: application/json'; $opts[CURLOPT_POSTFIELDS] = json_encode($body); } $opts[CURLOPT_HTTPHEADER] = $headers; curl_setopt_array($ch, $opts); $raw = curl_exec($ch); $status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($raw === false) { $err = curl_error($ch); curl_close($ch); throw new RelayException('Network error: ' . $err, 'network_error'); } curl_close($ch); $res = json_decode($raw, true); if (! is_array($res)) { $res = ['error' => 1, 'code' => 'bad_response', 'message' => 'HTTP ' . $status]; } if ($status >= 400 || ! empty($res['error'])) { throw new RelayException($res['message'] ?? 'Request failed', $res['code'] ?? null, $status, $res['data'] ?? null); } return $res; } public function sendEmail(array $p, ?string $idem = null): array { return $this->request('POST', '/email', $p, $idem); } public function sendSms(array $p, ?string $idem = null): array { return $this->request('POST', '/sms', $p, $idem); } public function sendWhatsapp(array $p, ?string $idem = null): array { return $this->request('POST', '/whatsapp', $p, $idem); } public function sendPush(array $p, ?string $idem = null): array { return $this->request('POST', '/push', $p, $idem); } public function registerDevice(array $p): array { return $this->request('POST', '/push/devices', $p, null, true); } public function removeDevice(array $p): array { return $this->request('DELETE', '/push/devices', $p, null, true); } public function sendTransactional(array $p, ?string $idem = null): array { return $this->request('POST', '/transactional/email', $p, $idem); } public function templates(): array { return $this->request('GET', '/templates'); } public function sendOtp(array $p): array { return $this->request('POST', '/otp', $p); } public function verifyOtp(array $p): array { return $this->request('POST', '/otp/verify', $p); } public function balance(): array { return $this->request('GET', '/balance'); } public function status(string $channel, int $id): array { return $this->request('GET', "/messages/{$channel}/{$id}"); } }