2022-08-11 09:46:51 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
2023-07-12 19:27:10 +00:00
|
|
|
use App\Models\Country;
|
|
|
|
use Brick\Math\RoundingMode;
|
|
|
|
use Brick\Money\Context\AutoContext;
|
|
|
|
use Brick\Money\Context\CashContext;
|
|
|
|
use Brick\Money\CurrencyConverter;
|
|
|
|
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
|
|
|
|
use Brick\Money\ExchangeRateProvider\PDOProvider;
|
|
|
|
use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
|
|
|
|
use Brick\Money\Money;
|
2022-08-11 09:46:51 +00:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2023-07-12 19:27:10 +00:00
|
|
|
use PDO;
|
2022-08-11 09:46:51 +00:00
|
|
|
|
|
|
|
trait Helper
|
|
|
|
{
|
2023-06-07 19:56:04 +00:00
|
|
|
public function getTransactionID($table = 'payment_transactions')
|
2022-08-11 09:46:51 +00:00
|
|
|
{
|
|
|
|
do {
|
|
|
|
$code = randomString(8);
|
2023-06-07 19:56:04 +00:00
|
|
|
$result = collect(DB::select("SELECT * FROM $table WHERE transaction_id = :code", ['code' => $code]));
|
2022-08-11 09:46:51 +00:00
|
|
|
$codeCorrect = sizeof($result) < 0;
|
|
|
|
} while ($codeCorrect);
|
|
|
|
return $code;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convertir vers le multiple de 5 le plus proche
|
2023-07-12 19:27:10 +00:00
|
|
|
function roundUpToAny($n, $x = 5)
|
|
|
|
{
|
|
|
|
return (ceil($n) % $x === 0) ? ceil($n) : round(($n + $x / 2) / $x) * $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function convertMoney($amount, $sourceCurrency, $targetCurrency)
|
|
|
|
{
|
|
|
|
// 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(
|
|
|
|
tableName: 'exchange_rate',
|
|
|
|
exchangeRateColumnName: 'exchange_rate',
|
|
|
|
sourceCurrencyCode: $baseCurrency,
|
|
|
|
targetCurrencyColumnName: 'target_currency'
|
|
|
|
);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
$converter = new CurrencyConverter($provider);
|
|
|
|
$sourceMoney = Money::of(round($amount, 2), $sourceCurrency, new AutoContext());
|
|
|
|
return $converter->convert($sourceMoney, $targetCurrency, null, RoundingMode::UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toMoneyAmount($amount, $sourceCurrency, $targetCurrency)
|
|
|
|
{
|
|
|
|
return $this->convertMoney($amount, $sourceCurrency, $targetCurrency)->getAmount()->toFloat();
|
2022-08-11 09:46:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|