A Laravel SaaS Boilerplate to start working on your next project
Check out my blog for more articles like this: https://hbakouane.dev/blog
To build a SaaS, you have to deal with customers, plans, payment methods, subscriptions …, This can be so complex, but working with Laravel & Laravel Cashier makes this very easy, That’s why I made this boilerplate that will help you and save your time, this repository will handle the registration of the users, adding subscriptions plans by the admin, ability to customers to add a payment method, ability to customers to Subscribe to your plans, and letting them cancel their subscription.
GitHub Repo: https://github.com/hbakouane/laravel-saas-boilerplate
1 — Adding a payment method
After creating an account, the registered user (customer) will be registered in your Stripe account as a stripe customer, and that is done in the Registration Controller, like so
// File: app/Http/Controllers/Auth/RegisterController.php// Register the user as Stripe customer$user->createAsStripeCustomer();
Since the customer is new, they will be directed to a settings page where they will be able to enter their credit card and save it as their default payment method.
// File: app/Http/Controllers/SettingsController.php$user->updateDefaultPaymentMethod($paymentMethod);// $paymentMethod is an ID comming from Stripe's API
2 — Adding Subscription Plans
To add plans, you can go to /plans and start creating them, under the hood we are creating a Stripe product, right after, we create a plan in this product:
// File: app/Http/Controllers/PlansController.php
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));$stripe_product = $stripe->products->create([ 'name' => $data['title'],]);$stripe_plan = $stripe->plans->create([ 'amount' => $data['price'], 'currency' => 'usd', 'interval' => $data['interval'], 'product' => $stripe_product->id]);$data['stripe_plan_id'] = $stripe_plan->id;$data['stripe_product_id'] = $stripe_product->id;// Persist the plan to the database$plan = (new Plan())->create($data);
3 — Subscribing to plans
Now, it’s time for your customers to subscribe to the created plans, to do so, we have to retrieve the user’s default payment method, as well as the Plan’s Stripe ID that we created before
// File: app/Http/Controllers/PlansController.php
$request->user()->newSubscription( $plan->title, $plan>stripe_plan_id)->create($paymentMethod);
4 — Cancelling a subscription by a customer
After subscribing to a plan, the user can both cancel their subscription at the same time he clicked the ‘Cancel’ button, or cancel the subscription until the subscription period finishes, we have to separate methods to do this:
// File: app/Http/Controllers/PlansController.php$user->subscription()->cancel();
$user->subscription()->cancelNow();
The first method cancel() will cancel the subscription until its period finishes, and this period is called the “Grace Period”, but the second will cancel the subscription right on time, if you chose the first one, here is a method that will help you to let your users know when their subscription is going to finish:
if ($user->subscription($planName)->onGracePeriod()) {
// The user is still subscribed and their subscription will be cancelled after a period
}
Read more about this on Cashier’s Official Documentation
GitHub Repo: https://github.com/hbakouane/laravel-saas-boilerplate
Check out my blog for more articles like this: https://hbakouane.dev/blog
Read More:
Integration of Laravel + VueJs + AdminLTE made easy
Models Pruning in Laravel 8.50.0 explained
5 Javascript libraries that will help you build your stunning Portfolio
A Laravel SaaS Boilerplate to start working on your next project