From 209528da1528d0b22d525ce9180fd5c10306d93e Mon Sep 17 00:00:00 2001 From: imlonghao Date: Mon, 27 Jun 2022 21:56:29 +0800 Subject: [PATCH] feat(pricing): add currency api --- app/Models/Pricing.php | 78 ++++++++++--------- .../components/currency-select.blade.php | 12 ++- resources/views/home.blade.php | 23 ++++-- 3 files changed, 63 insertions(+), 50 deletions(-) diff --git a/app/Models/Pricing.php b/app/Models/Pricing.php index 0a99d9c..b5af33d 100644 --- a/app/Models/Pricing.php +++ b/app/Models/Pricing.php @@ -2,11 +2,12 @@ namespace App\Models; +use Exception; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Session; +use Illuminate\Support\Facades\Log; class Pricing extends Model { @@ -14,46 +15,49 @@ class Pricing extends Model protected $fillable = ['service_id', 'service_type', 'currency', 'price', 'term', 'as_usd', 'usd_per_month', 'next_due_date']; - public static function convertFromUSD(string $amount, string $convert_to): float - {//Code rates update from an API?? - if ($convert_to === 'AUD') { - return (1.39 * $amount); - } elseif ($convert_to === "USD") { - return $amount; - } elseif ($convert_to === "GBP") { - return (0.79 * $amount); - } elseif ($convert_to === "EUR") { - return (0.93 * $amount); - } elseif ($convert_to === "NZD") { - return (1.53 * $amount); - } elseif ($convert_to === "JPY") { - return (127.12 * $amount); - } elseif ($convert_to === "CAD") { - return (1.27 * $amount); - } else { - return $amount; + private static function refreshRates(): object + { + if (Cache::has("currency_rates")) { + return Cache::get("currency_rates"); } + $response_json = file_get_contents("https://open.er-api.com/v6/latest/USD"); + if (false === $response_json) { + Log::error("do file_get_contents failed"); + return (object)null; + } + try { + $response = json_decode($response_json); + if ('success' === $response->result) { + return Cache::remember("currency_rates", now()->addWeek(1), function () use ($response) { + return $response->rates; + }); + } + Log::error("server response is " . $response->result . ", expecting success"); + } catch (Exception $e) { + Log::error("failed to request v6.exchangerate-api.com", ['err' => $e]); + } + return (object)null; + } + + private static function getRates($currency): float + { + $rate = self::refreshRates()->$currency; + return $rate === null ? 1.00 : $rate; + } + + public static function getCurrencyList(): array + { + return array_keys((array)self::refreshRates()); + } + + public static function convertFromUSD(string $amount, string $convert_to): float + { + return $amount * self::getRates($convert_to); } public function convertToUSD(string $amount, string $convert_from): float { - if ($convert_from === 'AUD') { - return (0.76 * $amount); - } elseif ($convert_from === "USD") { - return $amount; - } elseif ($convert_from === "GBP") { - return (1.35 * $amount); - } elseif ($convert_from === "EUR") { - return (1.23 * $amount); - } elseif ($convert_from === "NZD") { - return (0.72 * $amount); - } elseif ($convert_from === "JPY") { - return (0.0097 * $amount); - } elseif ($convert_from === "CAD") { - return (0.78 * $amount); - } else { - return 1.00; - } + return $amount / self::getRates($convert_from); } public function costAsPerMonth(string $cost, int $term): float @@ -140,7 +144,7 @@ class Pricing extends Model public static function pricingForService(string $service_id) { return Cache::remember("service_pricing.$service_id", now()->addWeek(1), function () use ($service_id) { - return DB::table('servers as s') + return DB::table('servers as s') ->join('pricings as p', 's.id', '=', 'p.service_id') ->where('s.id', '=', $service_id) ->get(['s.*', 'p.*']); diff --git a/resources/views/components/currency-select.blade.php b/resources/views/components/currency-select.blade.php index c068bf4..9218187 100644 --- a/resources/views/components/currency-select.blade.php +++ b/resources/views/components/currency-select.blade.php @@ -1,12 +1,10 @@
{{ $title ??'Currency'}}
diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index d9515ef..8a0587b 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -71,7 +71,10 @@
-

{{$information['total_cost_weekly']}} {{$information['currency']}}

+

+ {{$information['total_cost_weekly']}} + {{$information['currency']}} +

Weekly cost

@@ -81,7 +84,9 @@
-

{{$information['total_cost_monthly']}} {{$information['currency']}} +

+ {{$information['total_cost_monthly']}} + {{$information['currency']}}

Monthly cost

@@ -92,7 +97,10 @@
-

{{$information['total_cost_yearly']}} {{$information['currency']}}

+

+ {{$information['total_cost_yearly']}} + {{$information['currency']}} +

Yearly cost

@@ -102,7 +110,9 @@
-

{{$information['total_cost_2_yearly']}} {{$information['currency']}} +

+ {{$information['total_cost_2_yearly']}} + {{$information['currency']}}

2 yearly cost

@@ -388,8 +398,9 @@ @if(Session::has('timer_version_footer') && Session::get('timer_version_footer') === 1)

Page took {{$information['execution_time']}} seconds, - Built on Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }} - ) + Built on Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }}), + Rates By Exchange Rate API +

@endif