mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-03 05:41:07 +00:00
* refactor: stabilize model identities for domain migration * refactor: extract module platform context * refactor: assign models to domain contexts * refactor: extract ai platform context * refactor: extract storage platform context * refactor: extract mail platform context * refactor: extract pdf platform context * refactor: extract operations platform context * refactor: move installation into operations platform * refactor: extract money domain context * refactor: extract taxation domain context * refactor: extract catalog domain context * refactor: extract metadata domain context * refactor: extract reporting domain context * refactor: extract purchases domain context * refactor: extract receivables domain context * refactor: extract accounts domain context * refactor: complete reporting statement boundary * refactor: extract contacts domain context * refactor: extract sales domain context * refactor: remove legacy application layers * fix: migrate legacy bouncer role identities
69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Money\ExchangeRates;
|
|
|
|
use App\Support\Net\BlockedUrlException;
|
|
use App\Support\Net\PrivateNetworkGuard;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class CurrencyConverterDriver extends ExchangeRateDriver
|
|
{
|
|
public function getExchangeRate(string $baseCurrency, string $targetCurrency): array
|
|
{
|
|
$baseUrl = $this->getBaseUrl();
|
|
$query = "{$baseCurrency}_{$targetCurrency}";
|
|
$url = "{$baseUrl}/api/v7/convert?apiKey={$this->apiKey}&q={$query}&compact=y";
|
|
$response = Http::get($url)->json();
|
|
|
|
return array_values($response[$query]);
|
|
}
|
|
|
|
public function getSupportedCurrencies(): array
|
|
{
|
|
$baseUrl = $this->getBaseUrl();
|
|
$url = "{$baseUrl}/api/v7/currencies?apiKey={$this->apiKey}";
|
|
$response = Http::get($url)->json();
|
|
|
|
if ($response == null) {
|
|
throw new ExchangeRateException('Server not responding', 'server_error');
|
|
}
|
|
|
|
if (array_key_exists('results', $response)) {
|
|
return array_keys($response['results']);
|
|
}
|
|
|
|
throw new ExchangeRateException('Please Enter Valid Provider Key.', 'invalid_key');
|
|
}
|
|
|
|
public function validateConnection(): array
|
|
{
|
|
$baseUrl = $this->getBaseUrl();
|
|
$query = 'INR_USD';
|
|
$url = "{$baseUrl}/api/v7/convert?apiKey={$this->apiKey}&q={$query}&compact=y";
|
|
$response = Http::get($url)->json();
|
|
|
|
return array_values($response[$query]);
|
|
}
|
|
|
|
private function getBaseUrl(): string
|
|
{
|
|
$type = $this->config['type'] ?? 'FREE';
|
|
|
|
$url = match ($type) {
|
|
'PREMIUM' => 'https://api.currconv.com',
|
|
'PREPAID' => 'https://prepaid.currconv.com',
|
|
'FREE' => 'https://free.currconv.com',
|
|
'DEDICATED' => $this->config['url'] ?? 'https://free.currconv.com',
|
|
};
|
|
|
|
// SSRF guard: the DEDICATED plan lets the user supply this URL.
|
|
try {
|
|
PrivateNetworkGuard::assertAllowed($url);
|
|
} catch (BlockedUrlException $e) {
|
|
throw new ExchangeRateException('Invalid provider URL: '.$e->getMessage(), 'invalid_url');
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
}
|