lmsqueezy / laravel

A package to easily integrate your Laravel application with Lemon Squeezy.
https://lemonsqueezy.com
MIT License
521 stars 51 forks source link

Implement create customer endpoints #53

Open driesvints opened 1 year ago

driesvints commented 1 year ago

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 https://github.com/lmsqueezy/laravel/pull/52

heyjorgedev commented 8 months ago

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

driesvints commented 8 months ago

@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 commented 7 months ago

Could you please help with this one.

I'm not sure I understand how it works. I have a product that users purchase. At the time they do so, they might not exist in users table created by Laravel by default.

So, ideally, when I receive order_created webhook, I need to check if there is a user, and if there is - make a customer. If not, create user in advance. I'm talking about my own database, since I need to know a) if there is a user b) if they bought my product

Could you please give me a hint how to implement this: should I somehow write my own webhook?

driesvints commented 7 months ago

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

priyashpatil commented 3 weeks ago

any update on this?

priyashpatil commented 3 weeks ago

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 commented 2 weeks ago

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