From 4b3bac0774bf549fc3f1ed9bea718a9bb22cac67 Mon Sep 17 00:00:00 2001 From: Djery-Tom Date: Thu, 27 Jul 2023 15:51:15 +0100 Subject: [PATCH] fix: add 6 decimal while calculate commission --- app/Traits/Helper.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/app/Traits/Helper.php b/app/Traits/Helper.php index 86e7edd..523c302 100644 --- a/app/Traits/Helper.php +++ b/app/Traits/Helper.php @@ -24,7 +24,9 @@ use App\Models\WalletsUser; use Barryvdh\DomPDF\Facade as PDF; use Brick\Math\RoundingMode; use Brick\Money\Context\AutoContext; +use Brick\Money\Context\CustomContext; use Brick\Money\CurrencyConverter; +use Brick\Money\Exception\CurrencyConversionException; use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider; use Brick\Money\ExchangeRateProvider\PDOProvider; use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration; @@ -211,9 +213,36 @@ trait Helper return $this->convertMoney($amount, $init_country, $final_country)->getAmount()->toFloat(); } + /** + * @throws CurrencyConversionException + */ public function getExchangeRate($init_country, $final_country) { - return $this->toMoney(1, $init_country) . ' = ' . $this->toMoneyWithCurrency(1, $init_country, $final_country, 6); + // set to whatever your rates are relative to + $baseCurrency = 'USD'; + + // use your own credentials, or re-use your existing PDO connection + $pdo = new PDO('mysql:host=' . env('DB_HOST') . ';dbname=' . env('DB_DATABASE'), env('DB_USERNAME'), env('DB_PASSWORD')); + + $configuration = new PDOProviderConfiguration(); + + $configuration->tableName = 'exchange_rate'; + $configuration->exchangeRateColumnName = 'exchange_rate'; + $configuration->targetCurrencyColumnName = 'target_currency'; + $configuration->sourceCurrencyCode = $baseCurrency; + + // this provider loads exchange rates from your database + $provider = new PDOProvider($pdo, $configuration); + + // this provider calculates exchange rates relative to the base currency + $provider = new BaseCurrencyProvider($provider, $baseCurrency); + + $sourceCurrencyCode = CountriesCurrency::findOrFail($init_country)->currency_code; + $targetCurrencyCode = CountriesCurrency::findOrFail($final_country)->currency_code; + + $rate = round($provider->getExchangeRate($sourceCurrencyCode, $targetCurrencyCode)->toFloat(), 6); + + return $this->toMoneyWithCurrencyCode(1, $sourceCurrencyCode) . ' = ' . $rate. ' '. $targetCurrencyCode; } public function toUSDAmount($amount, $init_country, $final_currency_code = 'USD')