Implement new API controllers for wallet management and discount code validation. This includes adding an Artisan command for exchange rate updates and refining the API routing structure. Additionally, perform a significant cleanup of the repository by removing obsolete migrations, debug scripts, and temporary test files. Security and configuration improvements include restricting CORS origins via environment variables and updating the system settings schema. - Add `WalletController` and `DiscountCodeController` APIs - Add `UpdateExchangeRates` command - Remove redundant migrations and debug/test scripts - Secure CORS configuration using `CORS_ALLOWED_ORIGINS` - Update `STATUS.md` to reflect current phase progress
98 lines
3.0 KiB
PHP
98 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Models\SystemSetting;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class UpdateExchangeRates extends Command
|
|
{
|
|
protected $signature = 'ifnex:update-rates {--source=ecb : Data source for exchange rates (ecb or freecurrencyapi)}';
|
|
|
|
protected $description = 'Update exchange rates from external APIs';
|
|
|
|
public function handle(): int
|
|
{
|
|
$source = $this->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");
|
|
}
|
|
} |