4

How do you set the default currency for Cashier to work in? Scratching my head – there are multiple source files and release notes that mention it, but I'm struggling to find where to set it. I'd like to change it from USD to GBP.

Furthermore, there are a few functions such as dollars() which it would be nice to remove or rename, what would be the best way of going about this?

Djave
  • 8,595
  • 8
  • 70
  • 124

3 Answers3

13

The suggested way of defining the currency for your application is by using the useCurrency method on the Cashier class.

Cashier::useCurrency('gbp', '£');

I would suggest placing this method in the CashierServiceProvider's register method.

By overwriting the Billable methods as suggested in the accepted answer you are giving up configurability and making your code less flexible. (What happens when you decide to use more than one currency?)

Jonathan
  • 1,041
  • 1
  • 12
  • 21
  • This should be the accepted answer. The currently accepted answer is poor practice and dangerous for application support. – kirgy Oct 14 '16 at 14:30
  • I've updated my answer which was the only way to change currency before April 2016, so before Taylor's Github update https://github.com/laravel/cashier/commit/d7e9766e20a2fc772e88d24c39c40b331c6c68e6 – Raff W Oct 26 '16 at 14:46
  • NOTE : This answer is obsolete as of Cashier v10. See https://stackoverflow.com/questions/59127672/laravel-upgrade-from-5-8-to-laravel-6-call-to-undefined-method-laravel-cashier – Tyler Stone Jun 15 '22 at 06:40
1

EDIT Oct'2016: This is no longer correct. Please check @Vercoutere answer below. New method was introduced by Taylor in April 2016 - check commit https://github.com/laravel/cashier/commit/d7e9766e20a2fc772e88d24c39c40b331c6c68e6

I couldn't find the answer to this question myself, so I post it for future reference.

In order to use Laravel Cashier we have to add Billable trait and BillableContract to User model. This way our User model can now use methods found in Billable trait. When you look into it, you will find methods like getCurrency(), getCurrencyLocale() and addCurrencySymbol($amount). Just copy these 3 methods from original trait, paste into your User model and edit afterwards to apply your currency and locale.

For me (UK) it was:

/**
 * Get the Stripe supported currency used by the entity.
 *
 * @return string
 */
public function getCurrency()
{
    return 'gbp';
}

/**
 * Get the locale for the currency used by the entity.
 *
 * @return string
 */
public function getCurrencyLocale()
{
    return 'en_GB';
}

/**
 * Add the currency symbol to a given amount.
 *
 * @param  string  $amount
 * @return string
 */
public function addCurrencySymbol($amount)
{
    return '£'.$amount;
}

You can read more about it on Alex Kaye's blog

Raff W
  • 188
  • 2
  • 9
0

As Laravel suggested in the Currency Configuration session in the document, you should place the following in your CashierServiceProvider.php.

Cashier::useCurrency('gbp', '£');

One thing worth mentioning is that it should be placed within the boot method instead of the registered method.

Andre W.
  • 895
  • 9
  • 19
  • 1
    This answer is a copy of an existing answer, only the last sentence adds something to it. Please use the comments to extend futher upon existing answers. – Jesse Apr 11 '18 at 09:55