src/Helper/IpToCountry.php line 57

  1. <?php
  2. namespace App\Helper;
  3. use App\Helper\GeoPlugin;
  4. class IpToCountry {
  5.     
  6.     public function __construct() {
  7.         
  8.     }
  9.     
  10.     public $countryUrls = array(
  11.                 'CA' => 'CA',
  12.                 'CH' => 'CH',
  13.                 'MA' => 'MA'
  14.             );
  15.     
  16.     public function setUserCountry($request,$session) {
  17.         
  18.         $country $request->query->get('country','');
  19.         //TEST1
  20.         if($country != ''){
  21.             $session->set('country_code',$country);
  22.             $session->set('payment_code',$country);
  23.         }
  24.         
  25.         //First check if the cookie is set
  26.         if ($session->get('country_code',null) == null) {
  27.             
  28.             try {
  29.                 
  30.                 $ipAddress $request->getClientIp();
  31.                 //$url = 'http://api.wipmania.com/'.$ipAddress;
  32.                 try {
  33.                     //$countryCode = file_get_contents($url);
  34.                     $geo = new GeoPlugin();
  35.                     $geo->locate($ipAddress);
  36.                     $countryCode $geo->countryCode;
  37.                 } catch (\Exception $e) {
  38.                     $countryCode 'FR';
  39.                 }
  40.                 $countryUrls $this->countryUrls;
  41.                 if (isset($countryUrls[$countryCode]))
  42.                     $session->set('payment_code',$countryCode);
  43.                 else {
  44.                     $countryCode 'FR';
  45.                 }
  46.                 
  47.                 //$countryCode = 'FR';
  48.             } catch (\Exception $e) {
  49.                 $countryCode 'FR';
  50.             }
  51.             
  52.             $session->set('payment_code',$countryCode);
  53.             $session->set('country_code',$countryCode);
  54.         }
  55.     }
  56.     
  57.     public static function hasTva($country){
  58.         $aTva self::getTVACountryList();
  59.         $hasTva false;
  60.         foreach ($aTva as $row){
  61.             if($row == $country){
  62.                 $hasTva true;
  63.                 break;
  64.             }
  65.         }
  66.         return $hasTva;
  67.     }
  68.     public static function getCurrency($session){
  69.         $countryCode $session->get('payment_code','FR');
  70.         $currency "&euro;";
  71.         switch($countryCode){
  72.             case 'CA':
  73.                 $currency '$CAN';
  74.             break;
  75.             case 'CH':
  76.                 $currency "CHF";
  77.             break;
  78.             case 'MA':
  79.                 $currency "Dh";
  80.             break;
  81.             default:
  82.                 $currency "&euro;";
  83.             break;
  84.         }
  85.         return $currency;
  86.     }
  87.     public static function getCurrencyCode($session){
  88.         $countryCode $session->get('payment_code','FR');
  89.         $currency "EUR";
  90.         switch($countryCode){
  91.             case 'CA':
  92.                 $currency 'CAD';
  93.             break;
  94.             case 'CH':
  95.                 $currency "CHF";
  96.             break;
  97.             case 'MA':
  98.                 $currency "MAD";
  99.             break;
  100.             default:
  101.                 $currency "EUR";
  102.             break;
  103.         }
  104.         return $currency;
  105.     }
  106.     public static function getCurrencyRate($from_Currency,$to_Currency,$amount){
  107.         /*$amount = urlencode($amount);
  108.         $from_Currency = urlencode($from_Currency);
  109.         $to_Currency = urlencode($to_Currency);
  110.         $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
  111.         $ch = curl_init();
  112.         $timeout = 0;
  113.         curl_setopt ($ch, CURLOPT_URL, $url);
  114.         curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  115.         curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  116.         curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  117.         $rawdata = curl_exec($ch);
  118.         curl_close($ch);
  119.         $data = explode('"', $rawdata);
  120.         $data = explode(' ', $data['3']);
  121.         $var = $data['0'];
  122.         return round($var,2);*/
  123.         
  124.         /*
  125.         $yql_base_url = "http://query.yahooapis.com/v1/public/yql";
  126.         $yql_query = "select * from yahoo.finance.xchange where pair in ('". ($from_Currency.$to_Currency) ."')";
  127.         
  128.         $yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
  129.         $yql_query_url .= "&format=json&env=store://datatables.org/alltableswithkeys&callback=";
  130.         
  131.         
  132.         $session = curl_init($yql_query_url);
  133.         curl_setopt($session, CURLOPT_RETURNTRANSFER,true);    
  134.         $json = curl_exec($session); 
  135.         
  136.         $phpObj =  json_decode($json);
  137.         
  138.         if(!is_null($phpObj) && !is_null($phpObj->query) && !is_null($phpObj->query->results)){
  139.             return $phpObj->query->results->rate->Rate;
  140.         }else{
  141.             return -1;
  142.         }
  143.         */
  144.         
  145.         $endpoint 'live';
  146.         $access_key '6826aeb27ef4f34eafdd6cfaecb66a5c';
  147.         
  148.         $url 'http://apilayer.net/api/'.$endpoint.'?access_key='.$access_key.'&curencies='.$from_Currency.','.$to_Currency.'&source=USD&format=1';
  149.         
  150.         // Initialize CURL:
  151.         $ch curl_init($url);
  152.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  153.         // Store the data:
  154.         $json curl_exec($ch);
  155.         curl_close($ch);
  156.         
  157.         // Decode JSON response:
  158.         $exchangeRates json_decode($jsontrue);
  159.         
  160.         if($exchangeRates != null && isset($exchangeRates['quotes']))
  161.         {
  162.             $rateUSD_from $exchangeRates['quotes']['USD'.$from_Currency];
  163.             $rateUSD_to $exchangeRates['quotes']['USD'.$to_Currency];
  164.             if($rateUSD_from != -&& $rateUSD_to != -1)
  165.             {
  166.                 return round($rateUSD_to $rateUSD_from5);
  167.             }
  168.         }
  169.         return -1;
  170.     }
  171.     
  172.     public static function getExtension($session){
  173.         $countryCode $session->get('country_code','FR');
  174.         $ext "fr";
  175.         switch($countryCode){
  176.             case 'CH':
  177.                 $ext "ch";
  178.             break;
  179.             case 'BE':
  180.                 $ext "be";
  181.             break;
  182.             default:
  183.                 $ext "fr";
  184.             break;
  185.         }
  186.         return $ext;
  187.     }
  188.     
  189.     public static function getDefaultCountry($session){
  190.         $countryCode = ($session != null) ? $session->get('country_code','FR') : 'FR';
  191.         $ext "France métropolitaine";
  192.         switch($countryCode){
  193.             case 'CH':
  194.                 $ext "Suisse";
  195.             break;
  196.             case 'BE':
  197.                 $ext "Belgique";
  198.             break;
  199.             case 'CA':
  200.                 $ext "Canada";
  201.             break;
  202.             case 'MA':
  203.                 $ext "Maroc";
  204.             break;
  205.             default:
  206.                 $ext "France métropolitaine";
  207.             break;
  208.         }
  209.         return $ext;
  210.     }
  211.     
  212.     public static function getTVACountryList(){
  213.         return array("Allemagne",
  214.                         "Autriche",
  215.                         "Belgique",
  216.                         "Bulgarie",
  217.                         "Chypre",
  218.                         "Danemark",
  219.                         "Espagne",
  220.                         "Estonie",
  221.                         "Finlande",
  222.                         "France métropolitaine",
  223.                         "Grèce",
  224.                         "Hongrie",
  225.                         "Irlande",
  226.                         "Italie",
  227.                         "Lettonie",
  228.                         "Lituanie",
  229.                         "Luxembourg",
  230.                         "Malte",
  231.                         "Pays-Bas",
  232.                         "Pologne",
  233.                         "Portugal",
  234.                         "République Tchèque",
  235.                         "Roumanie",
  236.                         "Royaume-Uni",
  237.                         "Slovaquie",
  238.                         "Slovénie",
  239.                         "Suède");
  240.     }
  241.     
  242.     public static function getCountryList(){
  243.         return array("Afghanistan",
  244.                     "Afrique du Sud",
  245.                     "Albanie",
  246.                     "Algérie",
  247.                     "Allemagne",
  248.                     "Andorre",
  249.                     "Angola",
  250.                     "Anguilla",
  251.                     "Antarctique",
  252.                     "Antigua-et-Barbuda",
  253.                     "Antilles Néerlandaises",
  254.                     "Arabie Saoudite",
  255.                     "Argentine",
  256.                     "Arménie",
  257.                     "Aruba",
  258.                     "Australie",
  259.                     "Autriche",
  260.                     "Azerbaïdjan",
  261.                     "Bénin",
  262.                     "Bahamas",
  263.                     "Bahreïn",
  264.                     "Bangladesh",
  265.                     "Barbade",
  266.                     "Belau",
  267.                     "Belgique",
  268.                     "Belize",
  269.                     "Bermudes",
  270.                     "Bhoutan",
  271.                     "Biélorussie",
  272.                     "Birmanie",
  273.                     "Bolivie",
  274.                     "Bosnie-Herzégovine",
  275.                     "Botswana",
  276.                     "Brésil",
  277.                     "Brunei",
  278.                     "Bulgarie",
  279.                     "Burkina Faso",
  280.                     "Burundi",
  281.                     "Côte d’Ivoire",
  282.                     "Cambodge",
  283.                     "Cameroun",
  284.                     "Canada",
  285.                     "Cap-Vert",
  286.                     "Chili",
  287.                     "Chine",
  288.                     "Chypre",
  289.                     "Clipperton",
  290.                     "Colombie",
  291.                     "Comores",
  292.                     "Congo",
  293.                     "Corée du Nord",
  294.                     "Corée du Sud",
  295.                     "Costa Rica",
  296.                     "Croatie",
  297.                     "Cuba",
  298.                     "Danemark",
  299.                     "Djibouti",
  300.                     "Dominique",
  301.                     "Egypte",
  302.                     "Emirats Arabes Unis",
  303.                     "Equateur",
  304.                     "Erythrée",
  305.                     "Espagne",
  306.                     "Estonie",
  307.                     "Etats-Unis",
  308.                     "Ethiopie",
  309.                     "Finlande",
  310.                     "France métropolitaine",
  311.                     "France Clipperton",
  312.                     "France Guadeloupe",
  313.                     "France Guyane Française",
  314.                     "France La Réunion",
  315.                     "France Martinique",
  316.                     "France Mayotte",
  317.                     "France Nouvelle-Calédonie",
  318.                     "France Polynésie Française",
  319.                     "France Saint Barthélémy",
  320.                     "France Saint Martin",
  321.                     "France Saint Pierre et Miquelon",
  322.                     "France Wallis et Futuna",
  323.                     "Géorgie",
  324.                     "Gabon",
  325.                     "Gambie",
  326.                     "Ghana",
  327.                     "Gibraltar",
  328.                     "Grèce",
  329.                     "Grenade",
  330.                     "Groenland",
  331.                     "Guadeloupe",
  332.                     "Guam",
  333.                     "Guatemala",
  334.                     "Guinée",
  335.                     "Guinée Equatoriale",
  336.                     "Guinée-Bissao",
  337.                     "Guyana",
  338.                     "Guyane Française",
  339.                     "Haïti",
  340.                     "Honduras",
  341.                     "Hong Kong",
  342.                     "Hongrie",
  343.                     "Ile Bouvet",
  344.                     "Ile Christmas",
  345.                     "Ile Norfolk",
  346.                     "Iles Cayman",
  347.                     "Iles Cook",
  348.                     "Iles Falkland",
  349.                     "Iles Fidji",
  350.                     "Iles Géorgie du Sud",
  351.                     "Iles Heard et McDonald",
  352.                     "Iles Marshall",
  353.                     "Iles Pitcairn",
  354.                     "Iles Salomon",
  355.                     "Iles Svalbard et Jan Mayen",
  356.                     "Iles Turks et Caicos",
  357.                     "Iles Vierges Américaines",
  358.                     "Iles Vierges Britanniques",
  359.                     "Iles des Cocos",
  360.                     "Inde",
  361.                     "Indonésie",
  362.                     "Iran",
  363.                     "Iraq",
  364.                     "Irlande",
  365.                     "Islande",
  366.                     "Israël",
  367.                     "Italie",
  368.                     "Jamaïque",
  369.                     "Japon",
  370.                     "Jordanie",
  371.                     "Kazakhstan",
  372.                     "Kenya",
  373.                     "Kirghizistan",
  374.                     "Kiribati",
  375.                     "Koweït",
  376.                     "Laos",
  377.                     "Lesotho",
  378.                     "Lettonie",
  379.                     "Liban",
  380.                     "Liberia",
  381.                     "Libye",
  382.                     "Liechtenstein",
  383.                     "Lituanie",
  384.                     "Luxembourg",
  385.                     "Macao",
  386.                     "Madagascar",
  387.                     "Malaisie",
  388.                     "Malawi",
  389.                     "Maldives",
  390.                     "Mali",
  391.                     "Malte",
  392.                     "Mariannes du Nord",
  393.                     "Maroc",
  394.                     "Martinique",
  395.                     "Maurice",
  396.                     "Mauritanie",
  397.                     "Mayotte",
  398.                     "Mexique",
  399.                     "Micronésie",
  400.                     "Moldavie",
  401.                     "Monaco",
  402.                     "Mongolie",
  403.                     "Montserrat",
  404.                     "Mozambique",
  405.                     "Népal",
  406.                     "Namibie",
  407.                     "Nauru",
  408.                     "Nicaragua",
  409.                     "Niger",
  410.                     "Nigeria",
  411.                     "Nioué",
  412.                     "Norvège",
  413.                     "Nouvelle-Calédonie",
  414.                     "Nouvelle-Zélande",
  415.                     "Oman",
  416.                     "Ouganda",
  417.                     "Ouzbékistan",
  418.                     "Pérou",
  419.                     "Pakistan",
  420.                     "Panama",
  421.                     "Papouasie Nouvelle-Guinée",
  422.                     "Paraguay",
  423.                     "Pays-Bas",
  424.                     "Philippines",
  425.                     "Pologne",
  426.                     "Polynésie Française",
  427.                     "Porto Rico",
  428.                     "Qatar",
  429.                     "République Centrafricaine",
  430.                     "République Démocratique du Congo",
  431.                     "République Dominicaine",
  432.                     "République Tchèque",
  433.                     "Réunion",
  434.                     "Roumanie",
  435.                     "Royaume-Uni",
  436.                     "Russie",
  437.                     "Rwanda",
  438.                     "Sénégal",
  439.                     "Sahara Occidental",
  440.                     "Saint Barthélémy",
  441.                     "Saint-Christophe-et-Niévès",
  442.                     "Saint Martin",
  443.                     "Saint Pierre et Miquelon",
  444.                     "Saint-Siège",
  445.                     "Saint-Vincent-et-les-Grenadines",
  446.                     "Sainte-Hélène",
  447.                     "Sainte-Lucie",
  448.                     "Salvador",
  449.                     "Samoa",
  450.                     "Samoa Américaines",
  451.                     "Sao Tomé-et Principe",
  452.                     "Seychelles",
  453.                     "Sierra Leone",
  454.                     "Singapour",
  455.                     "Slovénie",
  456.                     "Slovaquie",
  457.                     "Somalie",
  458.                     "Soudan",
  459.                     "Sri Lanka",
  460.                     "Suède",
  461.                     "Suisse",
  462.                     "Suriname",
  463.                     "Swaziland",
  464.                     "Syrie",
  465.                     "Taiwan",
  466.                     "Tadjikistan",
  467.                     "Tanzanie",
  468.                     "Tchad",
  469.                     "Terres australes françaises",
  470.                     "Territoire britannique de l’Océan Indien",
  471.                     "Thaïlande",
  472.                     "Timor Oriental",
  473.                     "Togo",
  474.                     "Tokélaou",
  475.                     "Tonga",
  476.                     "Trinité-et-Tobago",
  477.                     "Tunisie",
  478.                     "Turkménistan",
  479.                     "Turquie",
  480.                     "Tuvalu",
  481.                     "Ukraine",
  482.                     "Uruguay",
  483.                     "Vanuatu",
  484.                     "Venezuela",
  485.                     "Viêt Nam",
  486.                     "Wallis et Futuna",
  487.                     "Yémen",
  488.                     "Yougoslavie",
  489.                     "Zambie",
  490.                     "Zimbabwe",
  491.                     "Ex-République Yougoslave de Macédoine");
  492.     }
  493. }