I am not sure why this isn’t better documented (at least not that I could easily find), but often times when building a subscription-based service (SaaS) you may want to offer a free plan along with your premium paid plans. I am not talking about a free trial period, but a forever free plan. For some reason, the Laravel documentation wants to assume only that you would offer a free trial period. There are a couple ways you could handle offering a free Stripe plan using Laravel, but I am going to highlight the one I prefer.
The reason I prefer this method is it makes upgrading, downgrading, and managing customers much simpler if you don’t have to worry about the backend headaches of having free users configured one way and paid users another way. With this method they are all managed by Stripe, and when you want to upgrade you simply pass billing information, and if you want to downgrade a user you can simply use the Stripe API to handle the plan switch.
For brevity, I am going to assume you have built out the rest of the code, and am only going to show the code necessary for subscribing a Laravel user to a Free Stripe plan.
Go to where ever you manage subscribing customers in your code. You will want to add a section to determine if the plan they are subscribing to is Free. Then within that section of code you can simply do the following:
// if you don't already have the user object get it
$user = User::find($user_id);
// let's set up some basic details for this Stripe customer
// these fields are based on the Stripe API documentation
$details = [
'plan' => $stripePlanId,
'email' => $user->email,
// the rest is optional, you can add whatever metadata you like
'metadata' => [
'user_id' => $user->id,
'first_name' => $user->first_name,
'last_name' => $user->last_name
// createStripeCustomer($token, $properties) is an undocumented Laravel
// subscription function. As the function name implies it allows you
// to manually create a Stripe customer.
$customer = $user->subscription()->createStripeCustomer(null, $details);
$subscriptions = Collection::make($customer->subscriptions->data);
$subscription = $subscriptions->last();
// since we are bypassing the standard Laravel subscription model
// we need to manually update the user table with the Stripe details
$user->stripe_active = true;
$user->stripe_id = $customer->id;
$user->stripe_subscription = $subscription->id;
$user->stripe_plan = $plan->plan_code;
// save the user object
// you are done. Your user is now subscribed to a free plan via Stripe
// the rest is up to you
That’s it. You user is now a Stripe customer subscribed to your free Stripe plan. You can upgrade/downgrade/cancel the subscription just like you would a paid subscription.
Latest posts by Jeff Mould (see all)
- Laravel 5.4 Redirect after Password Reset Email Sent - September 8, 2017
- How to Fix Laravel 5.2 Token Mismatch Errors - June 30, 2016
- How to Get Sirius XM Deals - May 27, 2016