diff --git a/app/Http/Controllers/api/CurrencyController.php b/app/Http/Controllers/api/CurrencyController.php index bc9345b..9fbe3e6 100644 --- a/app/Http/Controllers/api/CurrencyController.php +++ b/app/Http/Controllers/api/CurrencyController.php @@ -19,31 +19,37 @@ class CurrencyController extends Controller $latestUpdate = GoldCurrency::max('updated_at'); $isUpdatedToday = $latestUpdate && Carbon::parse($latestUpdate)->isToday(); - // 2. If not updated today, call the API (Cache once per day) + // 2. If not updated today, call the API if (!$isUpdatedToday) { $this->fetchAndCacheGoldPrices(); } // 3. Fetch data from our own database - $goldCurrencies = GoldCurrency::all()->map(function ($currency) { + $goldCurrencies = GoldCurrency::all(); + + // 4. Debug check: If it is STILL empty, the API failed and we have no backup data yet. + if ($goldCurrencies->isEmpty()) { + return $this->failed(null, 'ارتباط با سرور قیمت‌ها برقرار نشد. لطفا بعدا تلاش کنید.'); + } + + // 5. Map the data + $mappedCurrencies = $goldCurrencies->map(function ($currency) { return [ 'change_percent' => $currency->change_percent, 'change_value' => $currency->change_value, - 'date' => $currency->updated_at->format('Y-m-d'), // Format for the DTO + 'date' => $currency->updated_at->format('Y-m-d'), 'name' => $currency->name, 'name_en' => $currency->name_en, 'price' => $currency->price, 'symbol' => $currency->symbol, - 'time' => $currency->updated_at->format('H:i:s'), // Format for the DTO + 'time' => $currency->updated_at->format('H:i:s'), 'time_unix' => $currency->time_unix, 'unit' => $currency->unit, ]; }); - // 4. Return in the exact format your frontend expects! - // We nest it inside ['gold' => ...] to match your CurrencyListBrsDto return $this->success([ - 'gold' => $goldCurrencies + 'gold' => $mappedCurrencies ], 'موفق', 'لیست قیمت طلا و سکه دریافت شد.'); } @@ -53,17 +59,22 @@ class CurrencyController extends Controller private function fetchAndCacheGoldPrices() { try { - $url = 'https://Api.BrsApi.ir/Api/Market/Gold_Currency.php?key=B727Ue3kBrbtCwHR3BmSVZKRRNF1sllp'; + $url = 'https://BrsApi.ir/Api/Market/Gold_Currency.php?key=B727Ue3kBrbtCwHR3BmSVZKRRNF1sllp'; - // Timeout after 10 seconds so the user isn't stuck waiting if the API is down - $response = Http::timeout(10)->get($url); + // FIX: Added withoutVerifying() for Iranian SSL issues and a User-Agent to bypass firewalls + $response = Http::withoutVerifying() + ->withHeaders([ + 'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36', + 'Accept' => 'application/json', + ]) + ->timeout(15) + ->get($url); if ($response->successful()) { $data = $response->json(); if (isset($data['gold']) && is_array($data['gold'])) { foreach ($data['gold'] as $item) { - // updateOrCreate uses the 'name_en' as the unique key to update existing rows GoldCurrency::updateOrCreate( ['name_en' => $item['name_en']], [ @@ -79,11 +90,9 @@ class CurrencyController extends Controller } } } else { - Log::warning('BrsApi returned an error status: ' . $response->status()); + Log::warning('BrsApi returned an error status: ' . $response->status() . ' - ' . $response->body()); } } catch (\Exception $e) { - // If the API completely dies (DNS issue, offline), we log it but don't crash. - // The controller will just safely return yesterday's prices from the DB! Log::error('Failed to fetch from BrsApi: ' . $e->getMessage()); } }