Closed emtiazzahid closed 2 months ago
What do you mean with "from the same lemon squeezy account"? Can you share some code?
when I tested my application I used different users, but as I logged in to lemonsqueezy I paid with the same account for test purposes which gave the same customer ID. In that case, when I try to purchase a subscription it gives integrity constraint violation: 1062 duplicate entry for key 'lemon_squeezy_customers
that error.
I really need code examples to reproduce this otherwise it's a shot in the dark at what you're trying to do.
Ok, steps to produce:
lmsqueezy:listen is not supported on Windows because it lacks support for signal handling.
I am using windows.thats why i added the route in my web.php Route::post('/lemon-squeezy/webhook', WebhookController::class); and copied the controller WebhookController to my App/Http/Controllers folder with changing namespace
Here is my web.php full code
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\WebhookController;
use Illuminate\Support\Facades\Auth;
Route::view('/', 'welcome');
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
$checkout_link = 'https://ourtechbro.lemonsqueezy.com/buy/2*****b-93f9-48bd-****-******e029e?' . http_build_query([
'checkout[custom][site_id]' => 1,
'checkout[custom][billable_id]' => Auth::user()->id,
'checkout[custom][billable_type]' => 'App\Models\User',
'checkout[custom][redirect_back_url]' => 'https://horse-massive-indirectly.ngrok-free.app',
'checkout[custom][subscription_type]' => 1
]);
return view('dashboard', compact('checkout_link'));
})->name('dashboard');
Route::view('profile', 'profile')
->middleware(['auth'])
->name('profile');
require __DIR__.'/auth.php';
Route::post('/lemon-squeezy/webhook', WebhookController::class);
and added that button in dashboard.blade.php
<a target="_blank" href="{{ $checkout_link }}" class="bg-blue text-black font-bold py-2 px-4 rounded mt-4">Buy</a>
note: I have prepared lemonsqueezy products for subscription and license, and added webhook URL. using ngrok to serve the site.
local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2735709' for key 'lemon_squeezy_customers.lemon_squeezy_customers_lemon_squeezy_id_unique' (Connection: mysql, SQL: insert into `lemon_squeezy_customers` (`billable_id`, `billable_type`, `lemon_squeezy_id`, `updated_at`, `created_at`) values (3, App\Models\User, 2735709, 2024-09-05 13:10:53, 2024-09-05 13:10:53)) {"exception":"[object] (Illuminate\\Database\\UniqueConstraintViolationException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2735709' for key 'lemon_squeezy_customers.lemon_squeezy_customers_lemon_squeezy_id_unique' (Connection: mysql, SQL: insert into `lemon_squeezy_customers` (`billable_id`, `billable_type`, `lemon_squeezy_id`, `updated_at`, `created_at`) values (3, App\\Models\\User, 2735709, 2024-09-05 13:10:53, 2024-09-05 13:10:53)) at C:\\laragon\\www\\test-lemon\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:820)
You absolutely need webhooks for this to work. The package can't function without it. You can take the approach of production is the command isn't working for you: https://github.com/lmsqueezy/laravel?tab=readme-ov-file#webhooks-in-production.
thats why i added the route in my web.php
you don't need to do that. The package registers this for you.
and copied the controller WebhookController to my App/Http/Controllers folder with changing namespace
You don't need to do that either. What did you change in this controller?
Here is my web.php full code
You're building the checkout link manually without using the package's documented way of starting a checkout. This isn't supported and cannot work because of how the package works internally. Please only use the documented way to use this package and create checkouts.
I feel like you're doing too much manually and overriding things which is beyond what I can help with sorry.
Thanks for your reply and valuable time. the webhook was working fine with that custom route, It was just a copy of the controller without any modification.
Without webhook delivery, I think that database error wouldn't be produced. anyway, I removed that route and controller also.
and now I using the route from the documentation
Route::get('/buy', function (Request $request) {
return $request->user()->checkout('variant-id');
});
so here is my web.php file in live
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
Route::view('/', 'welcome');
Route::middleware(['auth', 'verified'])->get('/dashboard', function () {
return view('dashboard');
})->name('dashboard');
Route::view('profile', 'profile')
->middleware(['auth'])
->name('profile');
Route::get('/buy', function (Request $request) {
return $request->user()->checkout('417893');
});
require __DIR__.'/auth.php';
here only variation ID is added, if I visit the site it redirects to the checkout of my product and stores the data successfully with webhook. but the problem remains the same for the second laravel user with the same lmsqueezy customer.
Local.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2735709' for key
Thanks
I am using the User model as billable, when I try to pay from the same lemon squeezy account but a different laravel user it does not let me create a customer as lemon_squeezy_id is unique in the lemon_squeezy_customers table. Maybe I am missing something, can you please give me a clue?