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

milosradic's avatar

Laravel Cashier prevent multiple same subscriptions.

Hi,

I have POST route in my API for buying premium account subscription using Stripe/Cashier.

        try {
            if($request->user()->subscribed('premium') || is_null($request->stripeToken))
                return response()->json([
                    'message' => __('app.went_wrong')
                ], 404);
            
            
            $request->user()->newSubscription('premium', 'XXXXXX')->create($request->stripeToken, [
                    'name' => $request->user()->first_name.' '.$request->user()->last_name,
                ]);

            return response()->json([
                'message' => __('app.premium_bought')
            ], 200);
        } catch (Exception $e) {
            return response()->json([
                'message' => __('app.went_wrong')
            ], 404);
        }

If i buy premium account and wait for few second and try to buy it again with same user authenticated i will get message that something went wrong and i have checked that with this if statement

            if($request->user()->subscribed('premium'))

But if i call this url two times in a row it create two subscriptions because this if statement return false because first request didn't finish yet and user is not premium yet.

How can i solve this problem?

0 likes
2 replies
shez1983's avatar

you can disable the button until you get a response back from first call.

milosradic's avatar

Hi,

Thanks for your help.

I will implement that definitely but that is frontend solution.

I have to be sure that this never happens and would like to check it in my backend so there is no chance for this to happen to someone.

Is there any way i can set limit per authenticated user so he can execute URL only once at time?

Please or to participate in this conversation.