mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 17:24:10 +00:00
- Fix exchange-rate service types to match actual backend response shapes (exchangeRate array, activeProvider success/error, used currencies as strings) - Add ExchangeRateConverter to payments, expenses, and recurring invoices - Set currency_id from customer currency in invoice/estimate selectCustomer() - Load globalStore.currencies in ExchangeRateConverter on mount - Pass driver/key/driver_config params to getSupportedCurrencies in provider modal - Fix OpenExchangeRateDriver validateConnection to use base=USD (free plan compat) - Fix checkActiveCurrencies SQLite whereJsonContains with array values - Remove broken currency/companyCurrency props from ExpenseCreateView, use stores - Show base currency equivalent in document line items and totals when exchange rate is active
53 lines
1.6 KiB
PHP
53 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\ExchangeRate;
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class OpenExchangeRateDriver extends ExchangeRateDriver
|
|
{
|
|
private string $baseUrl = 'https://openexchangerates.org/api';
|
|
|
|
public function getExchangeRate(string $baseCurrency, string $targetCurrency): array
|
|
{
|
|
$url = "{$this->baseUrl}/latest.json?app_id={$this->apiKey}&base={$baseCurrency}&symbols={$targetCurrency}";
|
|
$response = Http::get($url)->json();
|
|
|
|
if (array_key_exists('error', $response)) {
|
|
throw new ExchangeRateException($response['description'], $response['message']);
|
|
}
|
|
|
|
return array_values($response['rates']);
|
|
}
|
|
|
|
public function getSupportedCurrencies(): array
|
|
{
|
|
$url = "{$this->baseUrl}/currencies.json";
|
|
$response = Http::get($url)->json();
|
|
|
|
if ($response == null) {
|
|
throw new ExchangeRateException('Server not responding', 'server_error');
|
|
}
|
|
|
|
$checkKey = $this->validateConnection();
|
|
|
|
return array_keys($response);
|
|
}
|
|
|
|
public function validateConnection(): array
|
|
{
|
|
$url = "{$this->baseUrl}/latest.json?app_id={$this->apiKey}&base=USD&symbols=EUR";
|
|
$response = Http::get($url)->json();
|
|
|
|
if (array_key_exists('error', $response)) {
|
|
if ($response['status'] == 401) {
|
|
throw new ExchangeRateException('Please Enter Valid Provider Key.', 'invalid_key');
|
|
}
|
|
|
|
throw new ExchangeRateException($response['description'], $response['message']);
|
|
}
|
|
|
|
return array_values($response['rates']);
|
|
}
|
|
}
|