Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Checkout Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require spatie/checkout
    

    Publish the config file (if needed):

    php artisan vendor:publish --provider="Spatie\Checkout\CheckoutServiceProvider"
    
  2. First Use Case:

    • Store an order ID in the session:
      use Spatie\Checkout\Checkout;
      
      Checkout::store('order-123');
      
    • Retrieve the stored order ID:
      $orderId = Checkout::get();
      
  3. Where to Look First:

    • Review the config/checkout.php for session driver and key settings.
    • Check Spatie\Checkout\Checkout facade for core methods (store(), get(), forget()).

Implementation Patterns

Core Workflows

  1. Order Creation & Session Storage:

    // In your order controller after creating an order
    $order = Order::create([...]);
    Checkout::store($order->id); // Store ID in session
    
  2. Checkout Flow Integration:

    • Use middleware to validate order presence:
      public function handle($request, Closure $next)
      {
          if (!Checkout::get()) {
              return redirect()->route('cart');
          }
          return $next($request);
      }
      
    • Clear order ID post-payment:
      Checkout::forget(); // After successful payment
      
  3. Multi-Step Checkout:

    • Store additional data (e.g., cart hash) alongside the order ID:
      session()->put('checkout.cart_hash', $cart->hash);
      

Integration Tips

  • Session Drivers: Ensure your .env session driver (SESSION_DRIVER=file,redis,etc.) is compatible.
  • Testing: Mock the Checkout facade in unit tests:
    $this->app->instance(Checkout::class, new MockCheckout());
    
  • Laravel Mixins: Extend the facade for custom logic:
    Checkout::macro('isValid', function () {
        return !is_null($this->get());
    });
    

Gotchas and Tips

Pitfalls

  1. Session Driver Mismatches:

    • If using SESSION_DRIVER=database, ensure session:table is configured in .env.
    • Fix: Verify config/session.php matches your .env settings.
  2. Race Conditions:

    • Concurrent requests may overwrite order IDs if not handled.
    • Fix: Use session()->put() directly for atomic writes if needed.
  3. Outdated Package:

    • Last release in 2016 may lack Laravel 8/9 compatibility.
    • Fix: Fork and update dependencies (e.g., laravel/framework to ^8.0).

Debugging

  • Missing Order ID:

    • Check if the session driver is initialized (e.g., session()->getId()).
    • Verify the config key (checkout.order_id) isn’t overridden.
  • Session Not Persisting:

    • Ensure SESSION_DOMAIN and SESSION_SECURE in .env align with your environment.

Extension Points

  1. Custom Session Keys: Override the default key in config:

    'order_id' => 'custom_order_key',
    
  2. Event-Based Workflows: Listen for order creation to auto-store IDs:

    Order::created(function ($order) {
        Checkout::store($order->id);
    });
    
  3. Fallback Logic: Handle missing order IDs gracefully:

    $orderId = Checkout::get() ?? throw new \RuntimeException('No order in session');
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport