Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

farb's avatar
Level 1

Trouble with 'teamSubscribed' middleware with freeTeamPlan

Anyone know why the VerifyTeamIsSubscribed model (->middleware('teamSubscribed');) does not check if the team is on a free plan? It only seems to do onGenericTrial() which just checks if the trial_ends_at column is after today's date, but that should be null (result in false) on a free plan. (edited)

My spark service provider looks like:

            Spark::freeTeamPlan('DIY Free','test_1234diyfree')
                ->features($diyFree)
                ->yearly()
                ->attributes([
                    'planid'=>'plan_DIYMFree',
                    'free'=>1,
                    'category' => 'all',
                ])
                ->maxTeamMembers(3);

and the VerifyTeamIsSubscribed class gets tripped up on the onGenericTrial() where it gets false from that, of course, because the tial_ends_at is null:

class VerifyTeamIsSubscribed
{
    /**
     * Verify the incoming request's current team has a subscription.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $subscription
     * @param  string  $plan
     * @return \Illuminate\Http\Response
     */
    public function handle($request, $next, $subscription = 'default', $plan = null)
    {
        if ($this->subscribed($request->user(), $subscription, $plan, func_num_args() === 2)) {
            return $next($request);
        }

        return $request->ajax() || $request->wantsJson()
                                ? response('Subscription Required.', 402)
                                : redirect('/settings/'.Spark::teamsPrefix().'/'.$request->user()->currentTeam->id.'#/subscription');
    }

    /**
     * Determine if the given user's current team is subscribed to the given plan.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  string  $subscription
     * @param  bool  $plan
     * @param  bool  $defaultSubscription
     * @return bool
     */
    protected function subscribed($user, $subscription, $plan, $defaultSubscription)
    {
        if (! $user || ! $user->currentTeam) {
            return false;
        }

        return ($defaultSubscription && $user->currentTeam->onGenericTrial()) ||
                $user->currentTeam->subscribed($subscription, $plan);
    }
}
0 likes
5 replies
farb's avatar
Level 1

FWIY I was able to solve this by copying the middleware over to my app's middleware folder and adjusting the 'teamSubscribed' to point to that custom class. I then added a third condition to the bottom return statement:

protected function subscribed($user, $subscription, $plan, $defaultSubscription)
    {

       ...

        return ($defaultSubscription && $user->currentTeam->onGenericTrial()) ||
            $user->currentTeam->subscribed($subscription, $plan) || $user->currentTeam->subscribed('DIY Free');
    }


But it feels janky and I can't see why that would be necessary if Spark has a freeTeamPlan concept. Why wouldn't that be considered? Or have I missed something?

steve_laracasts's avatar

What is your Spark initialisation? e.g.

Spark::useStripe()->noCardUpFront()->trialDays(10);

I traced through the code and yeah, if I understand it correctly, if your config doesn't have trialDays it won't work; in which case your solution is probably as good as any, feel free to adjust this to your needs.

1 like
farb's avatar
Level 1

Thanks @kel_ , I actually did have that in my SparkServiceProvider.php:

Spark::useStripe()->noCardUpFront()->teamTrialDays(30);

Still, it would appear that freeTeamPlan doesn't actually lead to any logic down the road. That's got to be my fault though - I can't imagine Taylor implementing that without having it do something.

steve_laracasts's avatar

Yeah, seems weird!

I'd try myself, but exhausted! Maybe in a few days when I'd back coding.

1 like

Please or to participate in this conversation.