option('source'); $this->info("Fetching exchange rates from source: {$source}..."); try { $rates = match ($source) { 'freecurrencyapi' => $this->fetchFromFreeCurrencyApi(), default => $this->fetchFromEcb(), }; } catch (\Throwable $e) { $this->error("Failed to fetch rates: {$e->getMessage()}"); return Command::FAILURE; } if ($rates) { SystemSetting::where('key', 'aed_to_irr')->update(['value' => (string) $rates['aed_to_irr']]); SystemSetting::where('key', 'usd_to_irr')->update(['value' => (string) $rates['usd_to_irr']]); $this->info("Exchange rates updated successfully:"); $this->line(" AED/IRR: {$rates['aed_to_irr']}"); $this->line(" USD/IRR: {$rates['usd_to_irr']}"); } else { $this->warn("No rates were updated. Using fallback values."); $this->setFallbackRates(); } return Command::SUCCESS; } private function fetchFromEcb(): ?array { $response = Http::get('https://api.exchangerate-api.com/v4/latest/AED'); if ($response->failed()) { return null; } $data = $response->json(); return [ 'aed_to_irr' => round($data['rates']['IRR'] ?? 455000, 2), 'usd_to_irr' => round($data['rates']['IRR'] ?? 425000, 2), ]; } private function fetchFromFreeCurrencyApi(): ?array { $response = Http::get('https://free.currencyapi.com/api/v3/latest', [ 'apikey' => config('ifnex.currency_api_key'), 'base_currency' => 'AED', ]); if ($response->failed()) { return null; } $data = $response->json(); if (!isset($data['data'])) { return null; } return [ 'aed_to_irr' => round(($data['data']['IRR']['value'] ?? 455000) * 100, 2), 'usd_to_irr' => round(($data['data']['IRR']['value'] ?? 425000) * 100, 2), ]; } private function setFallbackRates(): void { SystemSetting::updateOrCreate( ['key' => 'aed_to_irr'], ['value' => '455000', 'type' => 'number', 'description' => 'AED to IRR exchange rate'] ); SystemSetting::updateOrCreate( ['key' => 'usd_to_irr'], ['value' => '425000', 'type' => 'number', 'description' => 'USD to IRR exchange rate'] ); $this->info("Fallback rates set: AED/IRR=455000, USD/IRR=425000"); } }