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

dushaun's avatar

Laravel Cashier: Failed Registration Redirect?

Hi There,

I'm currently sorting out webhooks for Stripe in a project I'm working on. The task at hand is when a user tries to register to the website and because their card fails, the user does not receive a subscription status in the subscriptions table.

With this, how would you go about writing middleware to redirect the failed registered user to their account page to update their card information, and redirect a guest to the billing page if they try to access certain pages for subscribed users.

I've tried the demo given in the Laravel docs:

public function handle($request, Closure $next)
{
    if ($request->user() && 
    ! $request->user()->subscribed('subscriber') ||
    ! $request->user()->subscribed('premium')) {
        if (Auth::check()) {
                // This user has signed up but membership has failed or expired
            } else {
                // This user is not a paying customer...
                return redirect('billing');
            }
    }

    return $next($request);
}

Can anyone give me any tips please? Thanks

0 likes
4 replies
martinbean's avatar

@dushaun That if() statement doesn’t look right. There’s no && or || in between ! $request->user()->subscribed('subscriber') and ! $request->user()->subscribed('premium').

dushaun's avatar

@martinbean sorry, I didn't write it out correctly. I've updated the original message.

It still doesn't work properly though

martinbean's avatar

@dushaun That code isn’t very optimal. It’s checking if there’s an authenticated user twice: $request->user() and Auth::check() do the same thing.

Re-reading your problem:

how would you go about writing middleware to redirect the failed registered user to their account page to update their card information

I wouldn’t. This isn’t the job of middleware. Middleware is for taking in HTTP requests and optionally manipulating that request before sending a response. A payment failing is an application event. You should throw an exception if a payment fails, and redirect when catching that exception.

redirect a guest to the billing page if they try to access certain pages for subscribed users.

How would a guest be able to access the billing page? Surely the billing page would only be visible to logged in users?

dushaun's avatar
dushaun
OP
Best Answer
Level 7

@martinbean yes I realised my mistake with misunderstanding Laravel Cashier, my misunderstanding lead to my confused code you see above.

I've resolved the issue by having a dedicated middleware for checking whether the user is subscribed or not. Another handling premium tier. A guest just gets directed to a login page with options to choose subscription.

Here's an example of the subscribed middleware:

public function handle($request, Closure $next)
{
    if ($request->user() && ! $request->user()->subscribed('primary')) {
        // This user has signed up but membership has expired
        return redirect('settings/subscription/card');
    }
    return $next($request);
}

I've also setup a webhooks controller to handle Stripe events.

Please or to participate in this conversation.