如何设置Laravel收银台的默认货币

How to set the default currency of Laravel Cashier

如何设置收银员工作的默认货币?抓耳挠腮——有多个源文件和发行说明提到了它,但我正在努力寻找设置它的位置。我想将其从 USD 更改为 GBP

此外,还有一些功能,如 dollars(),最好删除或重命名,最好的解决方法是什么?

编辑 2016 年 10 月:这不再正确。请检查下面的@Vercoutere 答案。 Taylor 于 2016 年 4 月引入了新方法 - 检查提交 https://github.com/laravel/cashier/commit/d7e9766e20a2fc772e88d24c39c40b331c6c68e6

我自己找不到这个问题的答案,所以我 post 以备将来参考。

为了使用 Laravel 收银台,我们必须向 User 模型添加 Billable 特征和 BillableContract。这样我们的 User 模型现在可以使用 Billable 特征中的方法。当你研究它时,你会发现像 getCurrency()getCurrencyLocale()addCurrencySymbol($amount) 这样的方法。只需从原始特征中复制这 3 种方法,粘贴到您的 User 模型中,然后进行编辑以应用您的货币和区域设置。

对我(英国)来说:

/**
 * 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;
}

您可以在 Alex Kaye's blog

上阅读更多相关信息

为您的应用程序定义货币的建议方法是在收银台上使用 useCurrency 方法 class。

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

我建议将此方法放在 CashierServiceProvider 的注册方法中。

按照已接受答案中的建议覆盖 Billable 方法,您将放弃可配置性并降低代码的灵活性。 (当您决定使用多种货币时会发生什么?)

正如 Laravel 在 Currency Configuration session in the document 中所建议的那样,您应该将以下内容放入您的 CashierServiceProvider.php 中。

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

值得一提的是,它应该放在 boot 方法中,而不是 registered 方法中。