From d2b85e9c1f44d502ee6160fe1c1985f97e4f53b9 Mon Sep 17 00:00:00 2001 From: Sander van Hooft Date: Thu, 5 Dec 2019 16:52:16 +0100 Subject: [PATCH] Added currency and currencyLocale support --- src/Cashier.php | 30 +++++++++++++++++++- src/CashierServiceProvider.php | 19 +++++++++++++ tests/CashierServiceProviderTest.php | 41 ++++++++++++++++++++++++++++ tests/CashierTest.php | 11 ++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/CashierServiceProviderTest.php diff --git a/src/Cashier.php b/src/Cashier.php index 953d6ba8..8fcf3792 100644 --- a/src/Cashier.php +++ b/src/Cashier.php @@ -26,6 +26,13 @@ class Cashier */ protected static $currencySymbol = '€'; + /** + * The current currency symbol. + * + * @var string + */ + protected static $currencyLocale = 'de_DE'; + /** * The custom currency formatter. * @@ -89,6 +96,17 @@ public static function useCurrencySymbol($symbol) static::$currencySymbol = $symbol; } + /** + * Set the currency locale to be used when formatting currency. + * + * @param string $locale + * @return void + */ + public static function useCurrencyLocale($locale) + { + static::$currencyLocale = $locale; + } + /** * Guess the currency symbol for the given currency. * @@ -132,6 +150,16 @@ public static function usesCurrencySymbol() return static::$currencySymbol; } + /** + * Get the currency locale currently in use. + * + * @return string + */ + public static function usesCurrencyLocale() + { + return static::$currencyLocale; + } + /** * Set the custom currency formatter. * @@ -155,7 +183,7 @@ public static function formatAmount(Money $money) return call_user_func(static::$formatCurrencyUsing, $money); } - $numberFormatter = new \NumberFormatter('de_DE', \NumberFormatter::CURRENCY); + $numberFormatter = new \NumberFormatter(static::$currencyLocale, \NumberFormatter::CURRENCY); $moneyFormatter = new IntlMoneyFormatter($numberFormatter, new ISOCurrencies); return $moneyFormatter->format($money); diff --git a/src/CashierServiceProvider.php b/src/CashierServiceProvider.php index b6aeeced..0a0c0d7e 100644 --- a/src/CashierServiceProvider.php +++ b/src/CashierServiceProvider.php @@ -34,6 +34,9 @@ public function boot() $this->publishConfig('cashier-configs'); $this->publishViews('cashier-views'); } + + $this->configureCurrency(); + $this->configureCurrencyLocale(); } /** @@ -97,4 +100,20 @@ protected function publishViews(string $tag) __DIR__.'/../resources/views' => $this->app->basePath('resources/views/vendor/cashier'), ], $tag); } + + protected function configureCurrency() + { + $currency = config('cashier.currency', false); + if($currency) { + Cashier::useCurrency($currency); + } + } + + protected function configureCurrencyLocale() + { + $locale = config('cashier.currency_locale', false); + if($locale) { + Cashier::useCurrencyLocale($locale); + } + } } diff --git a/tests/CashierServiceProviderTest.php b/tests/CashierServiceProviderTest.php new file mode 100644 index 00000000..ebaf1b62 --- /dev/null +++ b/tests/CashierServiceProviderTest.php @@ -0,0 +1,41 @@ +assertEquals('INEXISTENT', config('cashier.currency', 'INEXISTENT')); + + $this->assertEquals('€', Cashier::usesCurrencySymbol()); + $this->assertEquals('eur', Cashier::usesCurrency()); + + config(['cashier.currency' => 'usd']); + $this->rebootCashierServiceProvider(); + + $this->assertEquals('usd', Cashier::usesCurrency()); + $this->assertEquals('$', Cashier::usesCurrencySymbol()); + } + + /** @test */ + public function canOptionallySetCurrencyLocaleInConfig() + { + $this->assertEquals('INEXISTENT', config('cashier.currency_locale', 'INEXISTENT')); + $this->assertEquals('de_DE', Cashier::usesCurrencyLocale()); + + config(['cashier.currency_locale' => 'nl_NL']); + $this->rebootCashierServiceProvider(); + + $this->assertEquals('nl_NL', Cashier::usesCurrencyLocale()); + } + + protected function rebootCashierServiceProvider() + { + tap(new CashierServiceProvider($this->app))->register()->boot(); + } +} diff --git a/tests/CashierTest.php b/tests/CashierTest.php index 0d99c597..0c72d4f1 100644 --- a/tests/CashierTest.php +++ b/tests/CashierTest.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Model; use Laravel\Cashier\Cashier; +use Laravel\Cashier\CashierServiceProvider; use Laravel\Cashier\Order\Order; use Laravel\Cashier\Order\OrderItem; use Laravel\Cashier\Subscription; @@ -174,4 +175,14 @@ public function canOverrideDefaultCurrencySymbol() $this->assertEquals('usd', Cashier::usesCurrency()); $this->assertEquals('$', Cashier::usesCurrencySymbol()); } + + /** @test */ + public function canOverrideDefaultCurrencyLocale() + { + $this->assertEquals('de_DE', Cashier::usesCurrencyLocale()); + + Cashier::useCurrencyLocale('nl_NL'); + + $this->assertEquals('nl_NL', Cashier::usesCurrencyLocale()); + } }