- How do I integrate **spatie/tax-calculator** into a Laravel e-commerce app for cart and order totals?
- Implement the `HasTax` interface on your cart items or order models (e.g., `getTaxRate()` and `getTaxablePrice()`). Then use `TaxCalculation::fromCollection($items)->taxedPrice()` to compute totals. For delivery fees, create a separate calculation with `TaxCalculation::fromTaxedPrice()` and add it to the cart total.
- Does this package support multi-country tax rules (e.g., EU VAT vs. US sales tax)?
- The package provides a lightweight foundation but assumes standard tax rates. For multi-country rules, you’ll need to extend the `HasTax` interface or implement custom logic (e.g., region-specific tax rate resolvers). Consider pairing it with a tax API like Avalara for complex jurisdictions.
- What Laravel versions and PHP versions does **spatie/tax-calculator** support?
- The package requires **PHP 8.0+** and is compatible with **Laravel 8.x+**. While it lacks recent updates, it aligns with modern Laravel’s dependency injection and collections. Test thoroughly with your Laravel version, especially if using newer features like enums or attributes.
- How do I calculate tax for a taxed price (e.g., $7.50 including 21% tax) using this package?
- Use `TaxCalculation::fromTaxedPrice(7.50, 0.21)` to reverse-calculate the base and tax amounts. This is useful for splitting pre-taxed fees (e.g., delivery costs) into taxable components. Combine results with `add()` for composite totals.
- Can I use this package for headless Laravel APIs or non-Laravel PHP projects?
- Yes, the package is framework-agnostic and works in any PHP 8.0+ app. In Laravel, it integrates seamlessly with collections and service containers, but you’ll lose those benefits in non-Laravel environments. Focus on the `HasTax` interface and `TaxCalculation` helper for portability.
- What if my tax logic is more complex (e.g., tiered rates, exemptions)? Will this package work?
- For standard VAT or flat-rate taxes, this package suffices. For tiered rates or exemptions, extend the `TaxCalculation` class or implement custom logic via the `HasTax` interface. Example: Override `getTaxRate()` to return dynamic rates based on item type or user location.
- How do I migrate existing hardcoded tax calculations to use **spatie/tax-calculator**?
- Phase 1: Add `HasTax` to models (e.g., `Product`, `OrderItem`). Phase 2: Replace inline calculations (e.g., `$order->subtotal * 1.21`) with `TaxCalculation::fromCollection($order->items)->taxedPrice()`. Use feature flags to test both approaches simultaneously during migration.
- Are there performance concerns for high-volume transactions (e.g., 10K+ orders/sec)?
- The package uses reflection for `HasTax` checks, which adds minimal overhead. For extreme scale, benchmark against a custom implementation or cache tax rates. Avoid dynamic rate lookups per request if possible—pre-resolve rates where feasible.
- Does this package provide tax breakdown logging for audits or refunds?
- No, the package focuses on calculation logic. For audits, log tax breakdowns manually (e.g., via Laravel observers or middleware) or extend `TaxCalculation` to emit events. Example: Trigger `TaxCalculated` events with base/tax/total values for storage.
- What are the alternatives to **spatie/tax-calculator** for Laravel tax logic?
- For simple tax needs, this package is lightweight and composable. For advanced features (e.g., real-time tax APIs, multi-country support), consider **Avalara AvaTax**, **TaxJar**, or **VatComply**. If you need a Laravel-specific solution with UI components, explore **Laravel Cashier** (for subscriptions) or custom packages like **webpatser/laravel-countries**.