Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Added currency and currencyLocale support
Browse files Browse the repository at this point in the history
  • Loading branch information
sandervanhooft committed Dec 5, 2019
1 parent 738320c commit d2b85e9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Cashier.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ class Cashier
*/
protected static $currencySymbol = '';

/**
* The current currency symbol.
*
* @var string
*/
protected static $currencyLocale = 'de_DE';

/**
* The custom currency formatter.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions src/CashierServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public function boot()
$this->publishConfig('cashier-configs');
$this->publishViews('cashier-views');
}

$this->configureCurrency();
$this->configureCurrencyLocale();
}

/**
Expand Down Expand Up @@ -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);
}
}
}
41 changes: 41 additions & 0 deletions tests/CashierServiceProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Laravel\Cashier\Tests;

use Laravel\Cashier\Cashier;
use Laravel\Cashier\CashierServiceProvider;

class CashierServiceProviderTest extends BaseTestCase
{
/** @test */
public function canOptionallySetCurrencyInConfig()
{
$this->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();
}
}
11 changes: 11 additions & 0 deletions tests/CashierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit d2b85e9

Please sign in to comment.