Laravel Cashier provides an expressive, fluent interface to Stripe's and Braintree's subscription billing services.
Laravel Cashier 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.
Configuration
Composer
First, add the Cashier package to your composer.json
file and run the composer update command:
"laravel/cashier": "~5.0" (For Stripe SDK ~2.0, and Stripe APIs on 2015-02-18 version and later)
"laravel/cashier": "~4.0" (For Stripe APIs on 2015-02-18 version and later)
"laravel/cashier": "~3.0" (For Stripe APIs up to and including 2015-02-16 version)
Service Provider
Next, register the Laravel\Cashier\CashierServiceProvider
service provider in your app configuration file.
Migration
Before using Cashier, we'll need to add several columns to your database. Don't worry, you can use the cashier:table
Artisan command to create a migration to add the necessary column. For example, to add the column to the users table run the command: php artisan cashier:table users
.
Model Setup
Next, add the BillableTrait and appropriate date mutators to your model definition:
use Laravel\Cashier\Billable;
use Laravel\Cashier\Contracts\Billable as BillableContract;
class User extends Eloquent implements BillableContract {
use Billable;
protected $dates = ['trial_ends_at', 'subscription_ends_at'];
}
Adding the columns to your model's $dates
property will instruct Eloquent to return the columns as Carbon / DateTime instances instead of raw strings.
Stripe Key
Finally, set your Stripe key in your services.php
configuration file:
'stripe' => [
'model' => 'User',
'secret' => env('STRIPE_API_SECRET'),
],