laravel / cashier-paddle

Cashier Paddle provides an expressive, fluent interface to Paddle's subscription billing services.
https://laravel.com/docs/cashier-paddle
MIT License
245 stars 57 forks source link

Paddle Customer already exists in the database. #274

Closed bluzeey closed 3 months ago

bluzeey commented 3 months ago

Cashier Paddle Version

2.5.1

Laravel Version

11.2.0

PHP Version

8.2

Database Driver & Version

No response

Description

So I create a customer when i create or register a user .

public function handleGoogleCallback(Request $request)
    {
        try {
            $user = User::firstOrCreate(
                ['email' => $socialite->getEmail()],
                [
                    'name'          => $socialite->getName(),
                    'email'         => $socialite->getEmail(),
                    'avatar'        => $socialite->getAvatar(),
                    'user_id'       => $socialite->getId(),
                    'social_token'  => $socialite->token,
                    'login_type'    => 'google',
                    'password'      => bcrypt('test@123'),
                    'trial_ends_at' => $twoWeeksLater,
                ]
            );
            // Check if trial_ends_at is null and update it if necessary
            if (is_null($user->trial_ends_at)) {
                $user->trial_ends_at = $twoWeeksLater;
                $user->save();
            }
            if(!$user->customer()){
                $user->createAsCustomer(['name'=>$user->name, "email"=> $user->email]);
                $user->save();
            }

            // Calculate the remaining trial days
            $trialLeft = Carbon::today()->diffInDays(Carbon::parse($user->trial_ends_at), false);
            if ($trialLeft >= 0) {
                $message = "Trial left for $trialLeft days.";
                return redirect(route('dashboard'))->banner($message);
            } else {
                $billingUrl=route('billing');
                $message = "Your trial period has ended. Please go to the billing page to subscribe to a plan.";
                return redirect(route('dashboard'))->banner($message);
            }
        } catch (\Exception $e) {
            Log::error($e);
            return redirect('/login')->withErrors(['error' => 'Google authentication failed.']);
        }
    }

On my checkout page as well. I first check whether there is a customer or not and then I create a customer if not existing (for legacy users) , and then show them the checkout.

public function checkout(Request $request,$priceId)
    {
        // If your checkout requires auth user
        // Replace this with Auth::user()->checkout($priceId)->returnTo(route('dashboard'))
        $user= auth()->user();

        if(!$user->customer()){
            $user->createAsCustomer(['name'=>$user->name, "email"=> $user->email]);
            $user->save();
            $user->refresh();
        }

        $checkout = $user->checkout($priceId)->returnTo(route('dashboard'));

        $checkout = [
            'items' => $checkout->getItems(),
            'custom' => $checkout->getCustomData(),
            'return_url' => $checkout->getReturnUrl(),
        ];

        return \response()->json($checkout);
    }

However i am getting user already exists in database for my production users. This is urgent and I would appreciate a resolution on this. Funnily enough, i am not getting this error in my local. I also updated the composer library in the production.

Steps To Reproduce

https://github.com/bluzeey/bug-report Here's the repository for the bug report. image

driesvints commented 3 months ago

Hi there,

Thanks for reporting but it looks like this is a question which can be asked on a support channel. Please only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repo you can try one of the following channels:

However, this issue will not be locked and everyone is still free to discuss solutions to your problem!

Thanks.