116 lines
4.2 KiB
PHP
116 lines
4.2 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Traits;
|
|
|
|
|
|
use App\Models\CountriesCurrency;
|
|
use App\Models\Country;
|
|
use Brick\Money\Context\CustomContext;
|
|
use DateTime;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Brick\Money\CurrencyConverter;
|
|
use Brick\Money\ExchangeRateProvider\PDOProvider;
|
|
use Brick\Money\ExchangeRateProvider\PDOProviderConfiguration;
|
|
use Brick\Money\ExchangeRateProvider\BaseCurrencyProvider;
|
|
use Brick\Money\Money;
|
|
use Brick\Math\RoundingMode;
|
|
use PDO;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
trait Helper
|
|
{
|
|
public function sendMail($email, $title, $messageText)
|
|
{
|
|
|
|
$recipients = [$email];
|
|
Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients, $title) {
|
|
$message->subject($title);
|
|
$message->to($recipients);
|
|
});
|
|
// return $this->successResponse("mail envoye");
|
|
}
|
|
|
|
public function checkPassword($password, $encrypted_password, $salt)
|
|
{
|
|
$encrypted_password_to_check = base64_encode(sha1($password . $salt, true) . $salt);
|
|
return $encrypted_password_to_check == $encrypted_password;
|
|
}
|
|
|
|
public function hashSSHA($string)
|
|
{
|
|
$salt = sha1(rand());
|
|
$salt = substr($salt, 0, 10);
|
|
$encrypted = base64_encode(sha1($string . $salt, true) . $salt);
|
|
$hash = array("salt" => $salt, "encrypted" => $encrypted);
|
|
return $hash;
|
|
}
|
|
|
|
public function getCountryName($id_country){
|
|
return Country::findOrFail($id_country)->name;
|
|
}
|
|
|
|
public function toMoneyWithNetwork($amount , $id_network){
|
|
$currency = collect(DB::select('SELECT cu.code FROM networks n INNER JOIN countries c ON c.id = n.country_id INNER JOIN currencies cu ON cu.id = c.idCurrency
|
|
WHERE n.id = :id',['id'=>$id_network]))->first();
|
|
|
|
$money = Money::of(round($amount, 2),$currency ? $currency->code : 'XAF',new CustomContext(2));
|
|
return $money->formatTo('fr_FR');
|
|
}
|
|
|
|
public function toMoney($amount, $id_country)
|
|
{
|
|
$country = Country::findOrFail($id_country);
|
|
$money = Money::of(round($amount, 2),$country->currency->code,new CustomContext(2));
|
|
return $money->formatTo('fr_FR');
|
|
}
|
|
|
|
private function convertMoney($amount , $init_country , $final_country)
|
|
{
|
|
// 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);
|
|
|
|
// this currency converter can now handle any currency pair
|
|
$converter = new CurrencyConverter($provider);
|
|
$init_country = Country::findOrFail($init_country);
|
|
$final_country = Country::findOrFail($final_country);
|
|
$init_money = Money::of(round($amount, 2),$init_country->currency->code,new CustomContext(2));
|
|
return $converter->convert($init_money, $final_country->currency->code, RoundingMode::DOWN);
|
|
}
|
|
|
|
public function toMoneyWithCurrency($amount , $init_country , $final_country){
|
|
return $this->convertMoney($amount , $init_country , $final_country)->formatTo('fr_FR');
|
|
}
|
|
|
|
public function toMoneyAmount($amount , $init_country , $final_country){
|
|
return $this->convertMoney($amount , $init_country , $final_country)->getAmount()->toFloat();
|
|
}
|
|
|
|
public function getTransactionID(){
|
|
$d = new DateTime();
|
|
$first = str_replace(['-',':'], '',$d->format("Y-m-d H:i:s.u"));
|
|
return str_replace(' ' ,'.',$first);
|
|
}
|
|
|
|
public function getCurrency($id_country){
|
|
$cc = CountriesCurrency::findOrFail($id_country);
|
|
return $cc->currency_code;
|
|
}
|
|
}
|