Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement create customer endpoints #53

Open
driesvints opened this issue Nov 2, 2023 · 7 comments
Open

Implement create customer endpoints #53

driesvints opened this issue Nov 2, 2023 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@driesvints
Copy link
Collaborator

driesvints commented Nov 2, 2023

Create the customer before doing a checkout/subscribe action we no longer have to rely on the billable_id & billable_type keys.

https://docs.lemonsqueezy.com/api/customers#create-a-customer

Revert #52

@heyjorgedev
Copy link
Contributor

Are you working on this @driesvints or can I take a stab at it?

@driesvints
Copy link
Collaborator Author

@heyjorgedev I'd rather tackle this one myself as it'll be quite a crucial part on how the package's gonna evolve. Thanks for offering though!

@victor-ponamariov

This comment was marked as off-topic.

@driesvints
Copy link
Collaborator Author

@victor-ponamariov please create a separate issue with your question.

@priyashpatil
Copy link
Contributor

any update on this?

@priyashpatil
Copy link
Contributor

priyashpatil commented Nov 11, 2024

Here's my current workaround. Directly overriding the method on User model:

/**
     * Create a Lemon Squeezy customer record for this user.
     *
     * @param array $attributes Additional customer attributes
     * @return Customer
     * @throws \Exception
     */
    public function createAsCustomer(array $attributes = []): Customer
    {
        $customer = $this->customer()->create($attributes);

        try {
            $response = LemonSqueezy::api('POST', "customers", [
                'data' => [
                    'type' => 'customers',
                    'attributes' => [
                        'name' => $this->name,
                        'email' => $this->email,
                    ],
                    'relationships' => [
                        'store' => [
                            'data' => [
                                'type' => 'stores',
                                'id' => config('lemon-squeezy.store'),
                            ]
                        ]
                    ]
                ]
            ]);

            // Update customer with Lemon Squeezy ID
            $customer->update([
                'lemon_squeezy_id' => $response['data']['id']
            ]);
        } catch (\LemonSqueezy\Laravel\Exceptions\LemonSqueezyApiError $e) {
            if ($e->getCode() === 422 && str_contains($e->getMessage(), 'email has already been taken')) {
                // Try to fetch existing customer by email
                $existingCustomers = LemonSqueezy::api('GET', 'customers', [
                    'filter' => [
                        'email' => $this->email
                    ]
                ]);

                if (!empty($existingCustomers['data'])) {
                    // Use the existing customer's ID
                    $customer->update([
                        'lemon_squeezy_id' => $existingCustomers['data'][0]['id']
                    ]);
                } else {
                    throw $e; // Re-throw if we can't find the existing customer
                }
            } else {
                throw $e; // Re-throw other types of errors
            }
        }

        return $customer;
    }

@driesvints
Copy link
Collaborator Author

No news I'm afraid. Thanks for posting the workaround 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants