getRates($target, $date); if (! isset($rates[$source]) || $rates[$source] == 0) { Log::warning('Exchange rate not found, returning unconverted amount', [ 'source' => $source, 'target' => $target, 'date' => $date, ]); return $amountInCents; } return (int) round($amountInCents / $rates[$source]); } /** * Get all exchange rates for a base currency on a given date. * * Checks the DB cache first, then fetches from the external API * and stores the result for future lookups. * * @return array */ public function getRates(string $baseCurrency, string $date): array { $baseCurrency = strtolower($baseCurrency); // Cap future dates to today — the API only has rates up to the current date. $today = Carbon::today()->toDateString(); if ($date > $today) { $date = $today; } $cached = ExchangeRate::query() ->where('base_currency', $baseCurrency) ->where('date', $date) ->first(); if ($cached) { return $cached->rates; } $rates = $this->currencyApi->getRatesForCurrency($baseCurrency, $date); if (! empty($rates)) { ExchangeRate::query()->create([ 'base_currency' => $baseCurrency, 'date' => $date, 'rates' => $rates, ]); } return $rates; } }