| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace App\Traits; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 08:01:59 +00:00
										 |  |  | use App\Models\CountriesCurrency; | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  | use App\Models\Country; | 
					
						
							| 
									
										
										
										
											2020-06-25 19:06:43 +00:00
										 |  |  | use Brick\Money\Context\AutoContext; | 
					
						
							| 
									
										
										
										
											2020-06-25 16:54:46 +00:00
										 |  |  | use Brick\Money\Context\CustomContext; | 
					
						
							| 
									
										
										
										
											2020-06-25 08:01:59 +00:00
										 |  |  | use DateTime; | 
					
						
							| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  | use Illuminate\Support\Facades\Mail; | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2020-06-23 16:21:26 +00:00
										 |  |  | use Illuminate\Support\Facades\DB; | 
					
						
							| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | trait Helper | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |     public function sendMail($email, $title, $messageText) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $recipients = [$email]; | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |         Mail::mailer('smtp')->raw($messageText, function ($message) use ($recipients, $title) { | 
					
						
							| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  |             $message->subject($title); | 
					
						
							|  |  |  |             $message->to($recipients); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | //        return $this->successResponse("mail envoye");
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |     public function checkPassword($password, $encrypted_password, $salt) | 
					
						
							| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         $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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +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; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 05:36:12 +00:00
										 |  |  |     public function getCountryName($id_country){ | 
					
						
							|  |  |  |         return Country::findOrFail($id_country)->name; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-23 16:21:26 +00:00
										 |  |  |     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 19:08:55 +00:00
										 |  |  |         $money = Money::of(round($amount, 2),$currency ? $currency->code : 'XAF',new AutoContext()); | 
					
						
							| 
									
										
										
										
											2020-06-23 16:21:26 +00:00
										 |  |  |         return $money->formatTo('fr_FR'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |     public function toMoney($amount, $id_country) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $country = Country::findOrFail($id_country); | 
					
						
							| 
									
										
										
										
											2020-06-25 19:08:55 +00:00
										 |  |  |         $money = Money::of(round($amount, 2),$country->currency->code,new AutoContext()); | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |         return $money->formatTo('fr_FR'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-25 08:01:59 +00:00
										 |  |  |     private function convertMoney($amount , $init_country , $final_country) | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |     { | 
					
						
							|  |  |  |         // 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-26 16:55:56 +00:00
										 |  |  |         $init_money = Money::of(round($amount, 2),$init_country->currency->code,new AutoContext()); | 
					
						
							| 
									
										
										
										
											2020-06-25 08:01:59 +00:00
										 |  |  |         return $converter->convert($init_money, $final_country->currency->code, RoundingMode::DOWN); | 
					
						
							| 
									
										
										
										
											2020-06-20 15:08:13 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-29 16:46:43 +00:00
										 |  |  |     public function toUSDAmount($amount , $init_country , $final_currency_code = 'USD'){ | 
					
						
							|  |  |  |         // 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); | 
					
						
							|  |  |  |         $init_money = Money::of(round($amount, 2),$init_country->currency->code,new AutoContext()); | 
					
						
							|  |  |  |         return $converter->convert($init_money, $final_currency_code, RoundingMode::DOWN)->getAmount()->toFloat(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 16:09:25 +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; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-06-19 13:48:27 +00:00
										 |  |  | } |