Last active
September 14, 2024 12:17
-
-
Save ClaraLeigh/7a325b8c12138167b8a3eb7e6976700a to your computer and use it in GitHub Desktop.
Laravel Currency Conversion - Quick workaround
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Support; | |
class Currency | |
{ | |
/** | |
* Required base currency | |
* | |
* @var null | |
*/ | |
private $from = null; | |
/** | |
* Required target currency | |
* | |
* @var null | |
*/ | |
private $to = null; | |
/** | |
* @var float | |
*/ | |
private $amount = 1.00; | |
/** | |
* @var null | |
*/ | |
private $places = null; | |
public static function convert() | |
{ | |
return new self(); | |
} | |
/** | |
* @param $currency | |
* | |
* @return $this | |
*/ | |
public function from(string $currency) | |
{ | |
$this->from = strtolower($currency); | |
return $this; | |
} | |
/** | |
* @param $currency | |
* | |
* @return $this | |
*/ | |
public function to(string $currency) | |
{ | |
$this->to = strtolower($currency); | |
return $this; | |
} | |
/** | |
* @param $places | |
* | |
* @return $this | |
*/ | |
public function round(int $places) | |
{ | |
$this->places = $places; | |
return $this; | |
} | |
/** | |
* @param float $amount | |
* | |
* @return $this | |
*/ | |
public function amount(float $amount) | |
{ | |
$this->amount = $amount; | |
return $this; | |
} | |
public function get(): ?float | |
{ | |
$data = rescue(fn() => $this->getData(), null); | |
if ($data === null) { | |
return null; | |
} | |
$rate = $data[$this->to]; | |
$result = $this->amount * $rate; | |
if ($this->places !== null) { | |
$result = round($result, $this->places); | |
} | |
return (float) $result; | |
} | |
protected function getData() | |
{ | |
$data = cache()->get("currency.{$this->from}.{$this->to}", function () { | |
return $this->getFromApi(); | |
}); | |
if ($data === null) { | |
$data = cache()->get("currency.long.{$this->from}.{$this->to}", null); | |
} | |
return $data; | |
} | |
protected function getFromApi() | |
{ | |
$url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/{$this->from}/{$this->to}.min.json"; | |
$response = \Http::get($url); | |
if (!$response->successful()) { | |
return null; | |
} | |
$key = "{$this->from}.{$this->to}"; | |
$data = $response->json(); | |
cache()->put("currency.$key", $data, now()->addDay()->startOfDay()); | |
cache()->put("currency.long.$key", $data, now()->addDays(30)); | |
return $data; | |
} | |
} |
One of the currency conversion packages I was using stopped working. Here is a quick hack to get things working for the convert() functionality OG Package: amrshawky/laravel-currency#19
Just wanted to say THANK YOU! This saved me tons of time and was very easy to implement. Cheers 😄
Thanks for the code.
I made some updates, since the https://github.com/fawazahmed0/currency-api was migrate to https://github.com/fawazahmed0/exchange-api
<?php
namespace App\Support;
class Currency
{
/**
* Required base currency
*
* @var null
*/
private $from = null;
/**
* Required target currency
*
* @var null
*/
private $to = null;
/**
* @var float
*/
private $amount = 1.00;
/**
* @var null
*/
private $places = null;
public static function convert()
{
return new self();
}
/**
* @param $currency
*
* @return $this
*/
public function from(string $currency)
{
$this->from = strtolower($currency);
return $this;
}
/**
* @param $currency
*
* @return $this
*/
public function to(string $currency)
{
$this->to = strtolower($currency);
return $this;
}
/**
* @param $places
*
* @return $this
*/
public function round(int $places)
{
$this->places = $places;
return $this;
}
/**
* @param float $amount
*
* @return $this
*/
public function amount(float $amount)
{
$this->amount = $amount;
return $this;
}
public function get(): ?float
{
$data = rescue(fn() => $this->getData(), null);
if ($data === null) {
return null;
}
$rate = $data[$this->from][$this->to];
$result = $this->amount * $rate;
if ($this->places !== null) {
$result = round($result, $this->places);
}
return (float) $result;
}
protected function getData()
{
$data = cache()->get("currency.{$this->from}.{$this->to}", function () {
return $this->getFromApi();
});
if ($data === null) {
$data = cache()->get("currency.long.{$this->from}.{$this->to}", null);
}
return $data;
}
protected function getFromApi()
{
// $url = "https://cdn.jsdelivr.net/gh/fawazahmed0/currency-api@1/latest/currencies/{$this->from}/{$this->to}.min.json";
$url = "https://latest.currency-api.pages.dev/v1/currencies/{$this->from}.json";
$response = \Http::get($url);
if (!$response->successful()) {
return null;
}
// $key = "{$this->from}.{$this->to}";
$key = "{$this->from}";
$data = $response->json();
cache()->put("currency.$key", $data, now()
->addDay()
->startOfDay());
cache()->put("currency.long.$key", $data, now()->addDays(30));
return $data;
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
One of the currency conversion packages I was using stopped working. Here is a quick hack to get things working for the convert() functionality
OG Package: amrshawky/laravel-currency#19