网页
综合
更新时间 2026-11-13 17:31:01
Laravel Cashier (Stripe) | Laravel 13.x - The clean stack for Artisans and agents
自动蜘蛛
laravel.com
正文约 5999 字
Laravel Cashier Stripe provides an expressive, fluent interface to Stripe's subscription billing services. It handles almost all of the boilerplate subscription billing code you are dreading writing. In addition to basic subscription management, Cashier can handle coupons, swapping subscription, subscription "quantities", cancellation grace periods, and even generate invoice PDFs.
When upgrading to a new version of Cashier, it's important that you carefully review the upgrade guide.
Cashier uses the Stripe API version pinned by your installed version of the stripe/stripe-php SDK. For example, stripe/stripe-php 21.x uses Stripe API version 2026-08-26.dahlia. Since a new major version of the Stripe SDK typically targets a new Stripe API version, you should review Stripe's API changelog before upgrading stripe/stripe-php to a new major version.
First, install the Cashier package for Stripe using the Composer package manager:
After installing the package, publish Cashier's migrations using the vendor:publish Artisan command:
Then, migrate your database:
Cashier's migrations will add several columns to your users table. They will also create a new subscriptions table to hold all of your customer's subscriptions and a subscription_items table for subscriptions with multiple prices.
If you wish, you can also publish Cashier's configuration file using the vendor:publish Artisan command:
Lastly, to ensure Cashier properly handles all Stripe events, remember to configure Cashier's webhook handling.
Stripe recommends that any column used for storing Stripe identifiers should be case-sensitive. Therefore, you should ensure the column collation for the stripe_id column is set to utf8_bin when using MySQL. More information regarding this can be found in the Stripe documentation.
Before using Cashier, add the Billable trait to your billable model definition. Typically, this will be the App\Models\User model. This trait provides various methods to allow you to perform common billing tasks, such as creating subscriptions, applying coupons, and updating payment method information:
Cashier assumes your billable model will be the App\Models\User class that ships with Laravel. If you wish to change this you may specify a different model via the useCustomerModel method. This method should typically be called in the boot method of your AppServiceProvider class:
If you're using a model other than Laravel's supplied App\Models\User model, you'll need to publish and alter the Cashier migrations provided to match your alternative model's table name.
Next, you should configure your Stripe API keys in your application's .env file. You can retrieve your Stripe API keys from the Stripe control panel:
You should ensure that the STRIPE_WEBHOOK_SECRET environment variable is defined in your application's .env file, as this variable is used to ensure that incoming webhooks are actually from Stripe.
The default Cashier currency is United States Dollars (USD). You can change the default currency by setting the CASHIER_CURRENCY environment variable within your application's .env file:
In addition to configuring Cashier's currency, you may also specify a locale to be used when formatting money values for display on invoices. Internally, Cashier utilizes PHP's NumberFormatter class to set the currency locale:
In order to use locales other than en, ensure the ext-intl PHP extension is installed and configured on your server.
Thanks to Stripe Tax, it's possible to automatically calculate taxes for all invoices generated by Stripe. You can enable automatic tax calculation by invoking the calculateTaxes method in the boot method of your application's App\Providers\AppServiceProvider class:
Once tax calculation has been enabled, any new subscriptions and any one-off invoices that are generated will receive automatic tax calculation.
For this feature to work properly, your customer's billing details, such as the customer's name, address, and tax ID, need to be synced to Stripe. You may use the customer data synchronization and Tax ID methods offered by Cashier to accomplish this.
Cashier allows you to specify the log channel to be used when logging fatal Stripe errors. You may specify the log channel by defining the CASHIER_LOGGER environment variable within your application's .env file:
Exceptions that are generated by API calls to Stripe will be logged through your application's default log channel.
You are free to extend the models used internally by Cashier by defining your own model and extending the corresponding Cashier model:
After defining your model, you may instruct Cashier to use your custom model via the Laravel\Cashier\Cashier class. Typically, you should inform Cashier about your custom models in the boot method of your application's App\Providers\AppServiceProvider class:
Before utilizing Stripe Checkout, you should define Products with fixed prices in your Stripe dashboard. In addition, you should configure Cashier's webhook handling.
Offering product and subscription billing via your application can be intimidating. However, thanks to Cashier and Stripe Checkout, you can easily build modern, robust payment integrations.
To charge customers for non-recurring, single-charge products, we'll utilize Cashier to direct customers to Stripe Checkout, where they will provide their payment details and confirm their purchase. Once the payment has been made via Checkout, the customer will be redirected to a success URL of your choosing within your application:
As you can see in the example above, we will utilize Cashier's provided checkout method to redirect the customer to Stripe Checkout for a given "price identifier". When using Stripe, "prices" refer to defined prices for specific products.
If necessary, the checkout method will automatically create a customer in Stripe and connect that Stripe customer record to the corresponding user in your application's database. After completing the checkout session, the customer will
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。