walletservice/app/Traits/Helper.php

116 lines
4.2 KiB
PHP
Raw Normal View History

<?php
namespace App\Traits;
2020-06-25 08:01:59 +00:00
use App\Models\CountriesCurrency;
use App\Models\Country;
2020-06-25 16:54:46 +00:00
use Brick\Money\Context\CustomContext;
2020-06-25 08:01:59 +00:00
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;
}
2020-06-19 18:33:10 +00:00
public function hashSSHA($string)
{
2020-06-19 18:33:10 +00:00
$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();
2020-06-25 16:54:46 +00:00
$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);
2020-06-25 16:54:46 +00:00
$money = Money::of(round($amount, 2),$country->currency->code,new CustomContext(2));
return $money->formatTo('fr_FR');
}
2020-06-25 08:01:59 +00:00
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);
2020-06-25 19:01:49 +00:00
$init_money = Money::of(round(str_replace(',' , '',number_format($amount,2)), 2),$init_country->currency->code,new CustomContext(2));
2020-06-25 08:01:59 +00:00
return $converter->convert($init_money, $final_country->currency->code, RoundingMode::DOWN);
}
2020-06-25 08:01:59 +00:00
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();
}
2020-06-24 07:36:54 +00:00
2020-06-25 08:01:59 +00:00
public function getTransactionID(){
$d = new DateTime();
$first = str_replace(['-',':'], '',$d->format("Y-m-d H:i:s.u"));
return str_replace(' ' ,'.',$first);
2020-06-24 07:36:54 +00:00
}
2020-06-25 08:01:59 +00:00
public function getCurrency($id_country){
$cc = CountriesCurrency::findOrFail($id_country);
return $cc->currency_code;
}
}