Djave
Djave

Reputation: 9349

How to set the default currency of Laravel Cashier

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?

Upvotes: 4

Views: 5691

Answers (3)

Andre W.
Andre W.

Reputation: 1023

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.

Upvotes: 0

Raff W
Raff W

Reputation: 188

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

Upvotes: 1

Jonathan
Jonathan

Reputation: 1071

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?)

Upvotes: 13

Related Questions