Will NOT using User as Billable Model make Cashier & Stripe subscriptions harder to code?
In short yes – At least for me. So I set my Stripe checkout up with User as Billable Model first, check that I can make that work. Then I change to for example Team as Billable Model.
Check out Cashier docs about billable model they're tidy.
Here is my checklist for Stripe checkout
- I add
Billabletrait to User model (Team model is the billable entity, not User, but I'm waiting with that.) - I install the Cashier package for Stripe
composer require laravel/cashier - I publish Cashier's migrations using
php artisan vendor:publish --tag="cashier-migrations" - Then, I migrate our database:
php artisan migrate - I add Stripe Checkout session via
$request->user()->newSubscription()->checkout() - I add
'success-route'and'cancel-route' - I copy paste in my price from Stripe here
->newSubscription('default', 'price_1Sw...6Fy') - I copy paste my
STRIPE_SECRET=pk_test_... and STRIPE_KEY=sk_test_... insidemy .env. from my Stripe. - I copy paste http://galleon.test/subscription-checkout into my browser.
- I "pay" with my "42" credit card, it worked as advertised, now
- I Add
Billabletrait to Team model (Team model is the billable entity, not User) - in the
bootmethod of ourAppServiceProviderclass: I add thisuse App\Models\Cashier\User;and thisuse App\Models\Cashier\User; - Then, I also add this inside the
boot()functionCashier::useCustomerModel(User::class); - Then, I migrate our database again:
php artisan migrate:fresh - Cashier migration (adds stripe columns to teams table)
- Now change this
$request->user()->newSubscription()->checkout()to$team->newSubscription()->checkout()with this above it$team = Team::current(); - Several things have gone wrong for me at this stage, this link will show you a few of them in the future ... (another post is coming closer to the summer)
- Stripe Billing Portal via
$team->redirectToBillingPortal() - Cashier webhook route (handles subscription lifecycle automatically)
- Middleware: gate dashboard features behind
$team->subscribed() - Billing page in dashboard: current plan status, manage subscription link
- Redirect to checkout after registration