laravel / cashier-paddle

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

Integrity constraint violation: 1048 Column 'name' cannot be null. #266

Closed bluzeey closed 2 months ago

bluzeey commented 2 months ago

Cashier Paddle Version

2.4

Laravel Version

11.2.0

PHP Version

8.2

Database Driver & Version

My SQL 8.0 for MacOS ( Homebrew)

Description

I am creating a user when i login them through Socialite. Here's the controller function.

public function handleGoogleCallback(Request $request)
    {
        try {
            $socialite = Socialite::driver('google')->user();
            $now = Carbon::now();
            $weekLater = $now->copy()->addDays(7);

            // Retrieve or create the user
            $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' => $weekLater,
                ]
            );
            // Log out any existing user session and log in the new user
            Auth::logout();
            session()->flush();
            // Update the name if it's null

            Auth::login($user);
            // Calculate the remaining trial days
            $trialLeft = Carbon::today()->diffInDays(Carbon::parse($user->trial_ends_at), false);

            // Redirect with appropriate message
            if ($trialLeft >= 0) {
                $message = "Trial left for $trialLeft days.";
                return redirect(route('dashboard'))->banner($message);
            } else {
                $billingUrl = route('billing'); // Ensure you have a named route for billing
                $message = "Your trial period has ended. Please <a href='{$billingUrl}'>subscribe to a plan</a>.";
                return redirect(route('billing'))->with('danger', $message);
            }
        } catch (\Exception $e) {
            Log::error($e);
            return redirect('/login')->withErrors(['error' => 'Google authentication failed.']);
        }
    }

However the checkout function which I have used. Keeps on throwing this error while creating the customer. Here's what i get for example. I have cross checked my functions twice. But the user has all the attributes. : "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null (Connection: mysql, SQL: insert into customers (billable_id, billable_type, paddle_id, name, email, trial_ends_at, updated_at, created_at) values (1, App\Models\User, ctm_01j1f4eqj4mv97kgsdpb0w2836, ?, sahilm1711@gmail.com, ?, 2024-07-09 21:01:04, 2024-07-09 21:01:04))"

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

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

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

Steps To Reproduce

Steps to reproduce. 1.) Create a new laravel app. 2.) Integrate Socialite for logging in your user. 3.) Integrating Cashier Paddle your products.

crynobone commented 2 months ago

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

bluzeey commented 2 months ago

Hey @crynobone , thanks for responding, I have created the repository for reproducing the bug. You will need though a google app with api keys and also paddle sandbox store. Here's the link : https://github.com/bluzeey/bug-report. The last commit has everything pertaining to the issue i am facing. Feel free to let me know how i can assist further

crynobone commented 2 months ago

Please refer to https://laravel.com/docs/11.x/queues#jobs-and-database-transactions and the documentation regarding after_commit.

crynobone commented 2 months ago

Hi there,

Thanks for reporting the problem you are encountering, but it looks like this is a question which may be better suited for a support channel. We 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 repository 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.

doncadavona commented 1 month ago

I encountered the same error too: image

I found out that there is no name stored in the customer's Paddle dashboard. If I add a name to it manually, it gets resolved. This is probably caused by some of my customers I registered earlier during development, which I didn't pass a name to paddle checkout before: https://sandbox-vendors.paddle.com/customers-v2 image

I created a PR for this to be gracefully handled here: https://github.com/laravel/cashier-paddle/pull/268

image

bluzeey commented 1 month ago

@doncadavona Apologies, so the way I resolved this was that , I created the customer first and saved it in user. And then created the checkout , that had solved my error.

doncadavona commented 1 month ago

Thanks @bluzeey .

For the others, you may run composer update to update Cashier Paddle to 2.5.1 which includes a fix for this issue.

See Merge Request

bluzeey commented 1 month ago

Hey @doncadavona , I had updated the cashier paddle to 2.5.1 , I am facing some other error. But not facing the name to be null