Skip to content

Commit

Permalink
updated location fix
Browse files Browse the repository at this point in the history
*which now retrieves location information from laravel request->ip() helper which was previously auto detecting and resulting in inconsistent ip/location
*this fixes that by forcing retrieval information by the given ip which returns by laravel
  • Loading branch information
Manash Jyoti Sonowal committed Jun 24, 2017
1 parent c46874f commit 2044cdb
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
22 changes: 11 additions & 11 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ class Currency
public $api_url;
public $cache_expiry;

public function __construct($base_currency=null)
public function __construct($base_currency = null)
{
$this->client = new Client();
$this->cache_expiry = config('currency.cache_expiry');
$this->cache_key = config('currency.cache_key');
$this->setBaseCurrency($base_currency);
if ( !$this->isRatesSameAsBaseCurrency() ) {
if (!$this->isRatesSameAsBaseCurrency()) {
$this->updateRates();
}
//saveInputs
Expand All @@ -40,9 +40,9 @@ public function getApiUrl()
{
return self::DEFAULT_API_URL.$this->base_currency;
}
public function setBaseCurrency($currency_code=null)
public function setBaseCurrency($currency_code = null)
{
$this->base_currency = strtoupper( is_null($currency_code) ? config('currency.base_currency') : $currency_code );
$this->base_currency = strtoupper(is_null($currency_code) ? config('currency.base_currency') : $currency_code);
}
public function getBaseCurrency()
{
Expand All @@ -61,20 +61,20 @@ public function updateRates()
//cache([$this->getCacheKey() => $rates], $this->cache_expiry);
$this->rates = Cache::remember($this->getCacheKey(), $this->cache_expiry, function () {
return $this->getLiveRates();
});
});
}
public function setRates($rates)
{
$this->rates = $rates;
}
public function getRates()
{
if ( is_null($this->rates) ) {
if (is_null($this->rates)) {
$this->rates = Cache::remember($this->getCacheKey(), $this->cache_expiry, function () {
return $this->getLiveRates();
});
});
}
if ( !$this->isRatesSameAsBaseCurrency() ) {
if (!$this->isRatesSameAsBaseCurrency()) {
$this->updateRates();
}
//TODO code to updated the rates property via API for the base currency property and then return the rate by recursively calling this method;
Expand All @@ -87,7 +87,7 @@ public function lastUpdated()
}
public function isRatesSameAsBaseCurrency() : bool
{
if ( is_null($this->rates) ) {
if (is_null($this->rates)) {
$this->updateRates();
return $this->isRatesSameAsBaseCurrency();
}
Expand All @@ -96,7 +96,7 @@ public function isRatesSameAsBaseCurrency() : bool
public function getConversionRate(string $currency_code)
{
$currency_code = strtoupper($currency_code);
if( $this->getRates()->base == $currency_code ) {
if ($this->getRates()->base == $currency_code) {
return 1;
}
return $this->getRates()->rates->{$currency_code};
Expand All @@ -110,7 +110,7 @@ public function getConversionRate(string $currency_code)
* @param int $round if decimals are not ignored then it will round up the value to specified by default is 2
* @return float if
*/
public function convertRate($value, string $currency_code, bool $ignore_decimals=true, $round=2)
public function convertRate($value, string $currency_code, bool $ignore_decimals = true, $round = 2)
{
if ($ignore_decimals) {
$original = $value;
Expand Down
5 changes: 3 additions & 2 deletions src/Facades/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

use Illuminate\Support\Facades\Facade;

class Currency extends Facade {
class Currency extends Facade
{

protected static function getFacadeAccessor()
{
return 'Currency';
}
}
}
17 changes: 11 additions & 6 deletions src/Middleware/SetDefaultUserCurrency.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ class SetDefaultUserCurrency
{
public function handle($request, Closure $next)
{
$location = geoip()->getLocation();
Log::info($location->toArray());
$currency = $location->currency;

if ( is_null($currency) || $currency=='' ) {
try {
//$location = geoip()->getLocation(); //Commented cause it was giving inconsistent results
$location = geoip($request->ip());
$currency = $location->currency;
Log::debug('Detected Location ', $location->toArray());
} catch (\Exception $ex) {
Log::debug('Exception caught '. $ex->getMessage());
}
if (is_null($currency) || $currency=='') {
$currency = config('currency.default_currency');
}
Log::debug('Setting Currency if not Set previously :'. $currency);
setDefaultCurrencyIfNotSet($currency);
Log::info($currency);
Log::debug('Loading currency of Session IP :'.$request->ip(). ' Currency: '. $currency);
return $next($request);
}
}
10 changes: 5 additions & 5 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function isCurrencySupported(string $currency_code)
function setUserCurrency($currency_code)
{
$currency_code = strtoupper($currency_code);
if ( !isCurrencySupported($currency_code) ) {
if (!isCurrencySupported($currency_code)) {
$currency_code = getDefaultCurrency();
}
$key = config('currency.session_currency_key');
Expand All @@ -35,25 +35,25 @@ function getUserCurrencySymbol()
{
return getCurrencySymbol(getUserCurrency());
}
function getUserCurrencyValue($value, $ignore_decimals=true)
function getUserCurrencyValue($value, $ignore_decimals = true)
{
$currency_code = getUserCurrency();
return \Msonowal\Laraxchange\Facades\Currency::convertRate($value, $currency_code, $ignore_decimals);
}
function convertCurrency($value, $currency_code, $ignore_decimals=true)
function convertCurrency($value, $currency_code, $ignore_decimals = true)
{
//this will convert base currency to specified currency
return \Msonowal\Laraxchange\Facades\Currency::convertRate($value, $currency_code, $ignore_decimals);
}
//TODO get user price value by determining from session
function setDefaultCurrencyIfNotSet($currency)
{
if ( is_null(getUserCurrency()) ) {
if (is_null(getUserCurrency())) {
setUserCurrency($currency);
}
}
function getCurrencySymbol($currency_code)
{
$currencies = config('currency.valid_currencies');
return $currencies[strtoupper($currency_code)];
}
}

0 comments on commit 2044cdb

Please sign in to comment.