Skip to content

Commit 6fd7656

Browse files
committed
Initial commit
Intiail commit, still needs testing etc.
0 parents  commit 6fd7656

File tree

9 files changed

+298
-0
lines changed

9 files changed

+298
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Jack Bayliss | Soluwire
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

composer.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "soluwire/laravel-currencyapi",
3+
"authors": [
4+
{
5+
"name": "Jack Bayliss",
6+
"email": "jjbayliss@icloud.com"
7+
}
8+
],
9+
"license" : "MIT",
10+
"keywords" : [
11+
"laravel",
12+
"php",
13+
"location",
14+
"currency",
15+
"findcurrency",
16+
"currencybyip",
17+
"ip"
18+
],
19+
"require": {
20+
"ext-curl": "*"
21+
},
22+
"minimum-stability": "dev",
23+
"autoload": {
24+
"psr-4": {
25+
"soluwire\\currencyapi\\": "src/"
26+
}
27+
28+
29+
}
30+
}

readme.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Laravel currency api API
2+
3+
4+
## Why?
5+
* Created for my own personal use, allows you to quickly grab the visitors currency & other IP information such as country.
6+
7+
8+
## How do I get/use it?
9+
10+
Composer require the package into your laravel project.
11+
```php
12+
composer require soluwire/laravel-currencyapi
13+
```
14+
15+
> Note: If you're using Laravel >= 5.5, you can skip the registration of the service provider, as they are registered automatically. So no need to add it into your providers array.
16+
17+
Then in your config folder, go into your app.php and add the below to the providers array.
18+
```php
19+
soluwire\currencyapi\CurrencyApiProvider::class,
20+
```
21+
22+
I would then recommend setting your `default_currency` which can be found in vendor->soluwire->settings->config.php
23+
24+
25+
26+
Once you've set your `default_currency`, you're ready to go- simply do the below.
27+
28+
29+
```php
30+
\soluwire\currencyapi\Driver::CallApi("8.8.8.8") //Replace 8.8.8.8 with an ip of your choice, it'll return relevent information.
31+
```
32+
33+
34+
## Authors
35+
* Jack Bayliss - Initial work
36+
37+
## License
38+
This project is licensed under the MIT License - see the [LICENSE](https://github.com/soluwire/laravel-currencyapi/blob/master/LICENSE) file for details
39+
40+
41+
That's all folks 👍
42+

src/CurrencyApiProvider.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace soluwire\currencyapi;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class CurrencyApiProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register services.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
$this->mergeConfigFrom(
17+
__DIR__.'/settings/config.php', 'currencyapi'
18+
);
19+
}
20+
21+
/**
22+
* Bootstrap services.
23+
*
24+
* @return void
25+
*/
26+
public function boot()
27+
{
28+
$this->publishes([
29+
__DIR__.'/settings/config.php' => config_path('config.php'),
30+
]);
31+
}
32+
}

src/Driver.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace soluwire\currencyapi;
4+
use soluwire\currencyapi\api\Position;
5+
6+
7+
Class Driver {
8+
/**
9+
* Calls our default driver, found in our config.
10+
*
11+
* @param String $ip
12+
*/
13+
public static function CallApi(String $ip) {
14+
15+
// Get our config to find the default set.
16+
$api = config('currencyapi.default_api');
17+
18+
$api = new $api($ip);
19+
20+
$ch = curl_init();
21+
22+
curl_setopt($ch, CURLOPT_URL, $api->url);
23+
24+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
25+
26+
$output = curl_exec($ch);
27+
28+
curl_close($ch);
29+
$data = json_decode($output);
30+
31+
// Depending on countrycode, set currency- this needs more countries.
32+
switch ($data->countryCode) {
33+
case 'GB':
34+
$currency = "£";
35+
break;
36+
case 'US':
37+
$currency = "$";
38+
break;
39+
case 'FR' :
40+
$currency = "";
41+
break;
42+
default:
43+
if (strpos($data->timezone, 'Europe') !== false) {
44+
$currency = "";
45+
break;
46+
}else if (strpos($data->timezone, 'Africa') !== false()){
47+
$currency = "R";
48+
break;
49+
}else if (strpos($data->timezone, 'Dubai') !== false()){
50+
$currency = "د.إ";
51+
break;
52+
53+
}else if (strpos($data->timezone, 'Asia') !== false()){
54+
$currency = "¥";
55+
break;
56+
57+
58+
}else{
59+
$currency = config('currencyapi.default_currency');
60+
break;
61+
}
62+
63+
}
64+
65+
// Set our position.
66+
$position = new Position();
67+
$position->latitude = $data->lat;
68+
$position->longitude = $data->lon;
69+
$position->city = $data->city;
70+
$position->region = $data->region;
71+
$position->currency = $currency;
72+
// return position;
73+
return $position;
74+
}
75+
76+
}
77+
78+
79+
?>

src/api/Api.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace soluwire\currencyapi\api;
4+
5+
abstract class Api {
6+
/**
7+
* Url
8+
* @var String
9+
*/
10+
protected $url;
11+
/**
12+
*
13+
* IP address
14+
* @var String;
15+
*/
16+
protected $ipaddress;
17+
}
18+
19+
?>

src/api/IPApi.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace soluwire\currencyapi\api;
4+
5+
class IPApi extends Api {
6+
/**
7+
* {@inheritdoc}
8+
*/
9+
public $url = "http://ip-api.com/json/";
10+
11+
12+
function __construct($ip){
13+
$this->url = $this->url . $ip;
14+
}
15+
}
16+
17+
?>

src/api/Position.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
namespace soluwire\currencyapi\api;
3+
4+
class Position {
5+
/**
6+
* Latitude
7+
*
8+
* @var string|null
9+
*/
10+
public $latitude;
11+
12+
/**
13+
* Longitude
14+
*
15+
* @var string|null
16+
*/
17+
public $longitude;
18+
/**
19+
* City
20+
*
21+
* @var string|null
22+
*/
23+
public $city;
24+
/**
25+
* Region
26+
*
27+
* @var string|null
28+
*/
29+
public $region;
30+
}
31+
32+
?>

src/settings/config.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
return [
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Default API
7+
|--------------------------------------------------------------------------
8+
|
9+
| The default API class you want to use.
10+
|
11+
*/
12+
"default_api" => soluwire\currencyapi\api\IPApi::class,
13+
14+
/*
15+
|--------------------------------------------------------------------------
16+
| Default Currency
17+
|--------------------------------------------------------------------------
18+
|
19+
| If unable to find their currency, it will default to this.
20+
|
21+
*/
22+
23+
"default_currency" => "$"
24+
];
25+
26+
?>

0 commit comments

Comments
 (0)