feat(pricing): add currency api

This commit is contained in:
imlonghao 2022-06-27 21:56:29 +08:00
parent a9b3f4eed6
commit 209528da15
No known key found for this signature in database
GPG Key ID: BB80A757B3E37324
3 changed files with 63 additions and 50 deletions

View File

@ -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.*']);

View File

@ -1,12 +1,10 @@
<div class="input-group">
<div class="input-group-prepend"><span class="input-group-text">{{ $title ??'Currency'}}</span></div>
<select class="form-control" name="{{$name ?? 'currency'}}">
<option value="AUD" {{(isset($current) && (string)$current === 'AUD')? 'selected' : ''}}>AUD</option>
<option value="USD" {{(isset($current) && (string)$current === 'USD')? 'selected' : ''}}>USD</option>
<option value="GBP" {{(isset($current) && (string)$current === 'GBP')? 'selected' : ''}}>GBP</option>
<option value="EUR" {{(isset($current) && (string)$current === 'EUR')? 'selected' : ''}}>EUR</option>
<option value="NZD" {{(isset($current) && (string)$current === 'NZD')? 'selected' : ''}}>NZD</option>
<option value="JPY" {{(isset($current) && (string)$current === 'JPY')? 'selected' : ''}}>JPY</option>
<option value="CAD" {{(isset($current) && (string)$current === 'CAD')? 'selected' : ''}}>CAD</option>
@foreach (App\Models\Pricing::getCurrencyList() as $currency)
<option value="{{$currency}}" {{(isset($current) && (string)$current === $currency)? 'selected' : ''}}>
{{$currency}}
</option>
@endforeach
</select>
</div>

View File

@ -71,7 +71,10 @@
<div class="card">
<div class="card-body text-center shadow">
<div class="row">
<h4>{{$information['total_cost_weekly']}} <small class="text-muted">{{$information['currency']}}</small></h4>
<h4>
{{$information['total_cost_weekly']}}
<small class="text-muted">{{$information['currency']}}</small>
</h4>
<p>Weekly cost</p>
</div>
</div>
@ -81,7 +84,9 @@
<div class="card">
<div class="card-body text-center shadow">
<div class="row">
<h4>{{$information['total_cost_monthly']}} <small class="text-muted">{{$information['currency']}}</small>
<h4>
{{$information['total_cost_monthly']}}
<small class="text-muted">{{$information['currency']}}</small>
</h4>
<p>Monthly cost</p>
</div>
@ -92,7 +97,10 @@
<div class="card">
<div class="card-body text-center shadow">
<div class="row">
<h4>{{$information['total_cost_yearly']}} <small class="text-muted">{{$information['currency']}}</small></h4>
<h4>
{{$information['total_cost_yearly']}}
<small class="text-muted">{{$information['currency']}}</small>
</h4>
<p>Yearly cost</p>
</div>
</div>
@ -102,7 +110,9 @@
<div class="card">
<div class="card-body text-center shadow">
<div class="row">
<h4>{{$information['total_cost_2_yearly']}} <small class="text-muted">{{$information['currency']}}</small>
<h4>
{{$information['total_cost_2_yearly']}}
<small class="text-muted">{{$information['currency']}}</small>
</h4>
<p>2 yearly cost</p>
</div>
@ -388,8 +398,9 @@
@if(Session::has('timer_version_footer') && Session::get('timer_version_footer') === 1)
<p class="text-muted mt-4 text-end"><small>Page took {{$information['execution_time']}} seconds,
Built on Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }}
)</small>
Built on Laravel v{{ Illuminate\Foundation\Application::VERSION }} (PHP v{{ PHP_VERSION }}),
Rates By <a href="https://www.exchangerate-api.com">Exchange Rate API</a>
</small>
</p>
@endif
</div>