spatie/checkout
Laravel package to manage the current order ID during a webshop checkout. Store the newly created order ID in the session and retrieve it later via a simple facade/API, keeping checkout steps clean and consistent across requests.
Installation:
composer require spatie/checkout
Publish the config file (if needed):
php artisan vendor:publish --provider="Spatie\Checkout\CheckoutServiceProvider"
First Use Case:
use Spatie\Checkout\Checkout;
Checkout::store('order-123');
$orderId = Checkout::get();
Where to Look First:
config/checkout.php for session driver and key settings.Spatie\Checkout\Checkout facade for core methods (store(), get(), forget()).Order Creation & Session Storage:
// In your order controller after creating an order
$order = Order::create([...]);
Checkout::store($order->id); // Store ID in session
Checkout Flow Integration:
public function handle($request, Closure $next)
{
if (!Checkout::get()) {
return redirect()->route('cart');
}
return $next($request);
}
Checkout::forget(); // After successful payment
Multi-Step Checkout:
session()->put('checkout.cart_hash', $cart->hash);
.env session driver (SESSION_DRIVER=file,redis,etc.) is compatible.Checkout facade in unit tests:
$this->app->instance(Checkout::class, new MockCheckout());
Checkout::macro('isValid', function () {
return !is_null($this->get());
});
Session Driver Mismatches:
SESSION_DRIVER=database, ensure session:table is configured in .env.config/session.php matches your .env settings.Race Conditions:
session()->put() directly for atomic writes if needed.Outdated Package:
laravel/framework to ^8.0).Missing Order ID:
session()->getId()).checkout.order_id) isn’t overridden.Session Not Persisting:
SESSION_DOMAIN and SESSION_SECURE in .env align with your environment.Custom Session Keys: Override the default key in config:
'order_id' => 'custom_order_key',
Event-Based Workflows: Listen for order creation to auto-store IDs:
Order::created(function ($order) {
Checkout::store($order->id);
});
Fallback Logic: Handle missing order IDs gracefully:
$orderId = Checkout::get() ?? throw new \RuntimeException('No order in session');
How can I help you explore Laravel packages today?