Просмотр исходного кода

fix: reconfigure currency exchange rate feature

3.x
wallo 2 лет назад
Родитель
Сommit
f1caa47a6c
3 измененных файлов: 61 добавлений и 3 удалений
  1. 50
    0
      README.md
  2. 6
    3
      app/Services/CurrencyService.php
  3. 5
    0
      config/services.php

+ 50
- 0
README.md Просмотреть файл

@@ -75,6 +75,56 @@ Run the database seeder
75 75
 
76 76
     php artisan migrate:refresh
77 77
 
78
+## Currency Exchange Rates
79
+
80
+### Overview
81
+
82
+This application offers support for real-time currency exchange rates. This feature is disabled by default. To enable it, you must first register for an API key at [ExchangeRate-API](https://www.exchangerate-api.com/). The application uses this service due to its generous provision of up to 1,500 free API calls per month, which should be enough for development and testing purposes.
83
+
84
+**Disclaimer**: There is no affiliation between this application and ExchangeRate-API.
85
+
86
+Once you have your API key, you can enable the feature by setting the `CURRENCY_API_KEY` environment variable in your `.env` file.
87
+
88
+### Configuration
89
+
90
+Of course, you may use any service you wish to retrieve currency exchange rates. If you decide to use a different service, you can update the `config/services.php` file with your choice:
91
+
92
+```php
93
+'currency_api' => [
94
+    'key' => env('CURRENCY_API_KEY'),
95
+    'base_url' => 'https://v6.exchangerate-api.com/v6',
96
+],
97
+```
98
+
99
+Additionally, you may update the following method in the `app/Services/CurrencyService.php` file which is responsible for retrieving the exchange rate:
100
+
101
+```php
102
+public function getExchangeRate($from, $to)
103
+{
104
+    $api_key = config('services.currency_api.key');
105
+    $base_url = config('services.currency_api.base_url');
106
+
107
+    $req_url = "{$base_url}/{$api_key}/pair/{$from}/{$to}";
108
+
109
+    $response = Http::get($req_url);
110
+
111
+    if ($response->successful()) {
112
+        $responseData = $response->json();
113
+        if (isset($responseData['conversion_rate'])) {
114
+            return $responseData['conversion_rate'];
115
+        }
116
+    }
117
+
118
+    return null;
119
+}
120
+```
121
+
122
+### Important Information
123
+
124
+- To use the currency exchange rate feature, you must first obtain an API key from a service provider. This application is configured to use a service that offers a free tier suitable for development and testing purposes.
125
+- Your API key is sensitive information and should be kept secret. Do not commit it to your repository or share it with anyone.
126
+- Note that API rate limits may apply depending on the service you choose. Make sure to review the terms for your chosen service.
127
+
78 128
 ## Dependencies
79 129
 
80 130
 - [filamentphp/filament](https://github.com/filamentphp/filament) - A collection of beautiful full-stack components

+ 6
- 3
app/Services/CurrencyService.php Просмотреть файл

@@ -8,14 +8,17 @@ class CurrencyService
8 8
 {
9 9
     public function getExchangeRate($from, $to)
10 10
     {
11
-        $req_url = 'https://api.exchangerate.host/latest?base=' . $from . '&symbols=' . $to;
11
+        $api_key = config('services.currency_api.key');
12
+        $base_url = config('services.currency_api.base_url');
13
+
14
+        $req_url = "{$base_url}/{$api_key}/pair/{$from}/{$to}";
12 15
 
13 16
         $response = Http::get($req_url);
14 17
 
15 18
         if ($response->successful()) {
16 19
             $responseData = $response->json();
17
-            if (isset($responseData['rates'][$to])) {
18
-                return $responseData['rates'][$to];
20
+            if (isset($responseData['conversion_rate'])) {
21
+                return $responseData['conversion_rate'];
19 22
             }
20 23
         }
21 24
 

+ 5
- 0
config/services.php Просмотреть файл

@@ -37,4 +37,9 @@ return [
37 37
         'redirect' => 'http://erpsaas.test/company/oauth/github/callback',
38 38
     ],
39 39
 
40
+    'currency_api' => [
41
+        'key' => env('CURRENCY_API_KEY'),
42
+        'base_url' => 'https://v6.exchangerate-api.com/v6',
43
+    ],
44
+
40 45
 ];

Загрузка…
Отмена
Сохранить