Skip to content

Commit 376cb8a

Browse files
committed
Fix cart checkout flow issues
- Store Stripe checkout session ID on cart before redirect - Allow GET requests to checkout route for post-login redirects - Fix guest cart transfer to only merge into active (non-completed) carts
1 parent 5cd8d47 commit 376cb8a

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

app/Http/Controllers/CartController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ public function checkout(Request $request): RedirectResponse
216216
try {
217217
$session = $this->createMultiItemCheckoutSession($cart, $user);
218218

219+
$cart->update(['stripe_checkout_session_id' => $session->id]);
220+
219221
return redirect($session->url);
220222
} catch (\Exception $e) {
221223
Log::error('Cart checkout failed', [
@@ -240,7 +242,8 @@ public function success(Request $request): View|RedirectResponse
240242
->with('error', 'Invalid checkout session. Please try again.');
241243
}
242244

243-
// Cart will be marked as completed by the webhook when payment is confirmed
245+
// Cart will be marked as completed by the webhook after licenses are created
246+
244247
return view('cart.success', [
245248
'sessionId' => $sessionId,
246249
]);

app/Services/CartService.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ public function transferGuestCartToUser(User $user): ?Cart
150150
return null;
151151
}
152152

153-
$userCart = Cart::where('user_id', $user->id)->first();
153+
$userCart = Cart::where('user_id', $user->id)
154+
->whereNull('completed_at')
155+
->first();
154156

155157
if ($userCart) {
156158
// Merge guest cart items into user cart

routes/web.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@
284284
Route::delete('cart/bundle/{bundle:slug}', [CartController::class, 'removeBundle'])->name('cart.bundle.remove');
285285
Route::delete('cart/clear', [CartController::class, 'clear'])->name('cart.clear');
286286
Route::get('cart/count', [CartController::class, 'count'])->name('cart.count');
287-
Route::post('cart/checkout', [CartController::class, 'checkout'])->name('cart.checkout');
287+
Route::match(['get', 'post'], 'cart/checkout', [CartController::class, 'checkout'])->name('cart.checkout');
288288
Route::get('cart/success', [CartController::class, 'success'])->name('cart.success')->middleware('auth');
289289
Route::get('cart/status/{sessionId}', [CartController::class, 'status'])->name('cart.status')->middleware('auth');
290290
Route::get('cart/cancel', [CartController::class, 'cancel'])->name('cart.cancel');

0 commit comments

Comments
 (0)