Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default currency of Laravel Cashier

Tags:

laravel

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?

like image 818
Djave Avatar asked Dec 09 '22 05:12

Djave


2 Answers

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

like image 98
Jonathan Avatar answered Dec 11 '22 07:12

Jonathan


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

like image 43
Raff W Avatar answered Dec 11 '22 09:12

Raff W