Page MenuHomePhorge

D2383.1775190672.diff
No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None

D2383.1775190672.diff

diff --git a/src/app/Backends/OpenExchangeRates.php b/src/app/Backends/OpenExchangeRates.php
new file mode 100644
--- /dev/null
+++ b/src/app/Backends/OpenExchangeRates.php
@@ -0,0 +1,50 @@
+<?php
+
+namespace App\Backends;
+
+class OpenExchangeRates
+{
+ /**
+ * Import exchange rates from openexchangerates.org
+ */
+ public static function retrieveRates($base_currency)
+ {
+ $base_currency = strtoupper($base_currency);
+ $api_key = \config('currency.api_key');
+ $query = http_build_query(array('app_id' => $api_key, 'base' => 'USD'));
+ $url = 'https://openexchangerates.org/api/latest.json?' . $query;
+ $html = file_get_contents($url, false);
+ $rates = array();
+
+ if ($html && ($result = json_decode($html, true)) && !empty($result['rates'])) {
+ foreach ($result['rates'] as $code => $rate) {
+ $rates[strtoupper($code)] = $rate;
+ }
+
+ if ($base_currency != 'USD') {
+ if ($base = $rates[$base_currency]) {
+ foreach ($rates as $code => $rate) {
+ $rates[$code] = $rate / $base;
+ }
+ } else {
+ $rates = array();
+ }
+ }
+
+ foreach ($rates as $code => $rate) {
+ \Log::debug(sprintf("Update %s: %0.8f", $code, $rate));
+ }
+ } else {
+ \Log::error("Failed to parse content of $url");
+ throw new \Exception("Failed to parse exchange rates");
+ }
+
+ if (count($rates) > 1) {
+ $rates[$base_currency] = 1;
+ return $rates;
+ }
+
+ \Log::error("Failed to parse content of $url");
+ throw new \Exception("Failed to retrieve exchange rates");
+ }
+}
diff --git a/src/app/Providers/Payment/Mollie.php b/src/app/Providers/Payment/Mollie.php
--- a/src/app/Providers/Payment/Mollie.php
+++ b/src/app/Providers/Payment/Mollie.php
@@ -562,15 +562,15 @@
$providerMethods = array_merge(
// Fallback to EUR methods (later provider methods will override earlier ones)
- //mollie()->methods()->allActive(
- // [
- // 'sequenceType' => $type,
- // 'amount' => [
- // 'value' => '1.00',
- // 'currency' => 'EUR'
- // ]
- // ]
- //),
+ (array)mollie()->methods()->allActive(
+ [
+ 'sequenceType' => $type,
+ 'amount' => [
+ 'value' => '1.00',
+ 'currency' => 'EUR'
+ ]
+ ]
+ ),
// Prefer CHF methods
(array)mollie()->methods()->allActive(
[
diff --git a/src/app/Providers/PaymentProvider.php b/src/app/Providers/PaymentProvider.php
--- a/src/app/Providers/PaymentProvider.php
+++ b/src/app/Providers/PaymentProvider.php
@@ -199,8 +199,7 @@
protected function exchangeRate(string $sourceCurrency, string $targetCurrency): float
{
if (strcasecmp($sourceCurrency, $targetCurrency)) {
- throw new \Exception("Currency conversion is not yet implemented.");
- //FIXME Not yet implemented
+ return \App\Utils::exchangeRate($sourceCurrency, $targetCurrency);
}
return 1.0;
}
@@ -312,11 +311,10 @@
'id' => self::METHOD_PAYPAL,
'icon' => self::$paymentMethodIcons[self::METHOD_PAYPAL]
],
- // TODO Enable once we're ready to offer them
- // self::METHOD_BANKTRANSFER => [
- // 'id' => self::METHOD_BANKTRANSFER,
- // 'icon' => self::$paymentMethodIcons[self::METHOD_BANKTRANSFER]
- // ]
+ self::METHOD_BANKTRANSFER => [
+ 'id' => self::METHOD_BANKTRANSFER,
+ 'icon' => self::$paymentMethodIcons[self::METHOD_BANKTRANSFER]
+ ]
];
case PaymentProvider::TYPE_RECURRING:
return [
diff --git a/src/app/Utils.php b/src/app/Utils.php
--- a/src/app/Utils.php
+++ b/src/app/Utils.php
@@ -5,6 +5,7 @@
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Ramsey\Uuid\Uuid;
+use Illuminate\Support\Facades\Cache;
/**
* Small utility functions for App.
@@ -406,4 +407,30 @@
return $env;
}
+
+
+ /**
+ * Retrieve an exchange rate.
+ *
+ * @param string $sourceCurrency: Currency from which to convert
+ * @param string $targetCurrency: Currency to convert to
+ *
+ * @return float Exchange rate
+ */
+ public static function exchangeRate(string $sourceCurrency, string $targetCurrency): float
+ {
+ $cacheKey = "openexchangerates-" . $sourceCurrency;
+ $rates = Cache::get($cacheKey);
+ if (!$rates) {
+ $rates = \App\Backends\OpenExchangeRates::retrieveRates($sourceCurrency);
+ Cache::put($cacheKey, $rates, now()->addHours(24));
+ }
+
+ if (!isset($rates[$targetCurrency])) {
+ \Log::error("Failed to retrieve exchange rate for " . $targetCurrency);
+ throw new \Exception("Failed to find exchange rate");
+ }
+
+ return floatval($rates[$targetCurrency]);
+ }
}
diff --git a/src/config/currency.php b/src/config/currency.php
--- a/src/config/currency.php
+++ b/src/config/currency.php
@@ -2,19 +2,6 @@
return [
- /*
- |--------------------------------------------------------------------------
- | Application Currency
- |--------------------------------------------------------------------------
- |
- | The application currency determines the default currency that will be
- | used by the currency service provider. You are free to set this value
- | to any of the currencies which will be supported by the application.
- |
- */
-
- 'default' => 'USD',
-
/*
|--------------------------------------------------------------------------
| API Key for OpenExchangeRates.org

File Metadata

Mime Type
text/plain
Expires
Fri, Apr 3, 4:31 AM (6 h, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
18822578
Default Alt Text
D2383.1775190672.diff (6 KB)

Event Timeline