src/Helper/IpToCountry.php line 57
<?php
namespace App\Helper;
use App\Helper\GeoPlugin;
class IpToCountry {
public function __construct() {
}
public $countryUrls = array(
'CA' => 'CA',
'CH' => 'CH',
'MA' => 'MA'
);
public function setUserCountry($request,$session) {
$country = $request->query->get('country','');
//TEST1
if($country != ''){
$session->set('country_code',$country);
$session->set('payment_code',$country);
}
//First check if the cookie is set
if ($session->get('country_code',null) == null) {
try {
$ipAddress = $request->getClientIp();
//$url = 'http://api.wipmania.com/'.$ipAddress;
try {
//$countryCode = file_get_contents($url);
$geo = new GeoPlugin();
$geo->locate($ipAddress);
$countryCode = $geo->countryCode;
} catch (\Exception $e) {
$countryCode = 'FR';
}
$countryUrls = $this->countryUrls;
if (isset($countryUrls[$countryCode]))
$session->set('payment_code',$countryCode);
else {
$countryCode = 'FR';
}
//$countryCode = 'FR';
} catch (\Exception $e) {
$countryCode = 'FR';
}
$session->set('payment_code',$countryCode);
$session->set('country_code',$countryCode);
}
}
public static function hasTva($country){
$aTva = self::getTVACountryList();
$hasTva = false;
foreach ($aTva as $row){
if($row == $country){
$hasTva = true;
break;
}
}
return $hasTva;
}
public static function getCurrency($session){
$countryCode = $session->get('payment_code','FR');
$currency = "€";
switch($countryCode){
case 'CA':
$currency = '$CAN';
break;
case 'CH':
$currency = "CHF";
break;
case 'MA':
$currency = "Dh";
break;
default:
$currency = "€";
break;
}
return $currency;
}
public static function getCurrencyCode($session){
$countryCode = $session->get('payment_code','FR');
$currency = "EUR";
switch($countryCode){
case 'CA':
$currency = 'CAD';
break;
case 'CH':
$currency = "CHF";
break;
case 'MA':
$currency = "MAD";
break;
default:
$currency = "EUR";
break;
}
return $currency;
}
public static function getCurrencyRate($from_Currency,$to_Currency,$amount){
/*$amount = urlencode($amount);
$from_Currency = urlencode($from_Currency);
$to_Currency = urlencode($to_Currency);
$url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$rawdata = curl_exec($ch);
curl_close($ch);
$data = explode('"', $rawdata);
$data = explode(' ', $data['3']);
$var = $data['0'];
return round($var,2);*/
/*
$yql_base_url = "http://query.yahooapis.com/v1/public/yql";
$yql_query = "select * from yahoo.finance.xchange where pair in ('". ($from_Currency.$to_Currency) ."')";
$yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
$yql_query_url .= "&format=json&env=store://datatables.org/alltableswithkeys&callback=";
$session = curl_init($yql_query_url);
curl_setopt($session, CURLOPT_RETURNTRANSFER,true);
$json = curl_exec($session);
$phpObj = json_decode($json);
if(!is_null($phpObj) && !is_null($phpObj->query) && !is_null($phpObj->query->results)){
return $phpObj->query->results->rate->Rate;
}else{
return -1;
}
*/
$endpoint = 'live';
$access_key = '6826aeb27ef4f34eafdd6cfaecb66a5c';
$url = 'http://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&curencies='.$from_Currency.','.$to_Currency.'&source=USD&format=1';
// Initialize CURL:
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Store the data:
$json = curl_exec($ch);
curl_close($ch);
// Decode JSON response:
$exchangeRates = json_decode($json, true);
if($exchangeRates != null && isset($exchangeRates['quotes']))
{
$rateUSD_from = $exchangeRates['quotes']['USD'.$from_Currency];
$rateUSD_to = $exchangeRates['quotes']['USD'.$to_Currency];
if($rateUSD_from != -1 && $rateUSD_to != -1)
{
return round($rateUSD_to / $rateUSD_from, 5);
}
}
return -1;
}
public static function getExtension($session){
$countryCode = $session->get('country_code','FR');
$ext = "fr";
switch($countryCode){
case 'CH':
$ext = "ch";
break;
case 'BE':
$ext = "be";
break;
default:
$ext = "fr";
break;
}
return $ext;
}
public static function getDefaultCountry($session){
$countryCode = ($session != null) ? $session->get('country_code','FR') : 'FR';
$ext = "France métropolitaine";
switch($countryCode){
case 'CH':
$ext = "Suisse";
break;
case 'BE':
$ext = "Belgique";
break;
case 'CA':
$ext = "Canada";
break;
case 'MA':
$ext = "Maroc";
break;
default:
$ext = "France métropolitaine";
break;
}
return $ext;
}
public static function getTVACountryList(){
return array("Allemagne",
"Autriche",
"Belgique",
"Bulgarie",
"Chypre",
"Danemark",
"Espagne",
"Estonie",
"Finlande",
"France métropolitaine",
"Grèce",
"Hongrie",
"Irlande",
"Italie",
"Lettonie",
"Lituanie",
"Luxembourg",
"Malte",
"Pays-Bas",
"Pologne",
"Portugal",
"République Tchèque",
"Roumanie",
"Royaume-Uni",
"Slovaquie",
"Slovénie",
"Suède");
}
public static function getCountryList(){
return array("Afghanistan",
"Afrique du Sud",
"Albanie",
"Algérie",
"Allemagne",
"Andorre",
"Angola",
"Anguilla",
"Antarctique",
"Antigua-et-Barbuda",
"Antilles Néerlandaises",
"Arabie Saoudite",
"Argentine",
"Arménie",
"Aruba",
"Australie",
"Autriche",
"Azerbaïdjan",
"Bénin",
"Bahamas",
"Bahreïn",
"Bangladesh",
"Barbade",
"Belau",
"Belgique",
"Belize",
"Bermudes",
"Bhoutan",
"Biélorussie",
"Birmanie",
"Bolivie",
"Bosnie-Herzégovine",
"Botswana",
"Brésil",
"Brunei",
"Bulgarie",
"Burkina Faso",
"Burundi",
"Côte d’Ivoire",
"Cambodge",
"Cameroun",
"Canada",
"Cap-Vert",
"Chili",
"Chine",
"Chypre",
"Clipperton",
"Colombie",
"Comores",
"Congo",
"Corée du Nord",
"Corée du Sud",
"Costa Rica",
"Croatie",
"Cuba",
"Danemark",
"Djibouti",
"Dominique",
"Egypte",
"Emirats Arabes Unis",
"Equateur",
"Erythrée",
"Espagne",
"Estonie",
"Etats-Unis",
"Ethiopie",
"Finlande",
"France métropolitaine",
"France Clipperton",
"France Guadeloupe",
"France Guyane Française",
"France La Réunion",
"France Martinique",
"France Mayotte",
"France Nouvelle-Calédonie",
"France Polynésie Française",
"France Saint Barthélémy",
"France Saint Martin",
"France Saint Pierre et Miquelon",
"France Wallis et Futuna",
"Géorgie",
"Gabon",
"Gambie",
"Ghana",
"Gibraltar",
"Grèce",
"Grenade",
"Groenland",
"Guadeloupe",
"Guam",
"Guatemala",
"Guinée",
"Guinée Equatoriale",
"Guinée-Bissao",
"Guyana",
"Guyane Française",
"Haïti",
"Honduras",
"Hong Kong",
"Hongrie",
"Ile Bouvet",
"Ile Christmas",
"Ile Norfolk",
"Iles Cayman",
"Iles Cook",
"Iles Falkland",
"Iles Fidji",
"Iles Géorgie du Sud",
"Iles Heard et McDonald",
"Iles Marshall",
"Iles Pitcairn",
"Iles Salomon",
"Iles Svalbard et Jan Mayen",
"Iles Turks et Caicos",
"Iles Vierges Américaines",
"Iles Vierges Britanniques",
"Iles des Cocos",
"Inde",
"Indonésie",
"Iran",
"Iraq",
"Irlande",
"Islande",
"Israël",
"Italie",
"Jamaïque",
"Japon",
"Jordanie",
"Kazakhstan",
"Kenya",
"Kirghizistan",
"Kiribati",
"Koweït",
"Laos",
"Lesotho",
"Lettonie",
"Liban",
"Liberia",
"Libye",
"Liechtenstein",
"Lituanie",
"Luxembourg",
"Macao",
"Madagascar",
"Malaisie",
"Malawi",
"Maldives",
"Mali",
"Malte",
"Mariannes du Nord",
"Maroc",
"Martinique",
"Maurice",
"Mauritanie",
"Mayotte",
"Mexique",
"Micronésie",
"Moldavie",
"Monaco",
"Mongolie",
"Montserrat",
"Mozambique",
"Népal",
"Namibie",
"Nauru",
"Nicaragua",
"Niger",
"Nigeria",
"Nioué",
"Norvège",
"Nouvelle-Calédonie",
"Nouvelle-Zélande",
"Oman",
"Ouganda",
"Ouzbékistan",
"Pérou",
"Pakistan",
"Panama",
"Papouasie Nouvelle-Guinée",
"Paraguay",
"Pays-Bas",
"Philippines",
"Pologne",
"Polynésie Française",
"Porto Rico",
"Qatar",
"République Centrafricaine",
"République Démocratique du Congo",
"République Dominicaine",
"République Tchèque",
"Réunion",
"Roumanie",
"Royaume-Uni",
"Russie",
"Rwanda",
"Sénégal",
"Sahara Occidental",
"Saint Barthélémy",
"Saint-Christophe-et-Niévès",
"Saint Martin",
"Saint Pierre et Miquelon",
"Saint-Siège",
"Saint-Vincent-et-les-Grenadines",
"Sainte-Hélène",
"Sainte-Lucie",
"Salvador",
"Samoa",
"Samoa Américaines",
"Sao Tomé-et Principe",
"Seychelles",
"Sierra Leone",
"Singapour",
"Slovénie",
"Slovaquie",
"Somalie",
"Soudan",
"Sri Lanka",
"Suède",
"Suisse",
"Suriname",
"Swaziland",
"Syrie",
"Taiwan",
"Tadjikistan",
"Tanzanie",
"Tchad",
"Terres australes françaises",
"Territoire britannique de l’Océan Indien",
"Thaïlande",
"Timor Oriental",
"Togo",
"Tokélaou",
"Tonga",
"Trinité-et-Tobago",
"Tunisie",
"Turkménistan",
"Turquie",
"Tuvalu",
"Ukraine",
"Uruguay",
"Vanuatu",
"Venezuela",
"Viêt Nam",
"Wallis et Futuna",
"Yémen",
"Yougoslavie",
"Zambie",
"Zimbabwe",
"Ex-République Yougoslave de Macédoine");
}
}