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

Money Php Laravel Package

99designs/money-php

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Monetary Logic Centralization: Ideal for applications requiring precise financial calculations (e.g., e-commerce, billing, accounting systems). Encapsulates currency conversion, rounding, and arithmetic in a single, reusable package.
  • Domain-Driven Design (DDD) Alignment: Fits well with DDD patterns (e.g., Money as a value object) for financial domains.
  • Separation of Concerns: Decouples monetary logic from business logic, improving testability and maintainability.

Integration Feasibility

  • PHP Compatibility: Works seamlessly with Laravel (PHP 8.0+ recommended; Laravel 8+ supports this).
  • Service Provider Pattern: Can be bootstrapped via Laravel’s service container for dependency injection.
  • Database Agnostic: No ORM assumptions; works with Eloquent, raw SQL, or NoSQL.
  • API/CLI Integration: Can be used in API endpoints, background jobs (Queues), or CLI commands (e.g., batch currency conversions).

Technical Risk

  • Precision Handling: Floating-point arithmetic risks (e.g., 0.1 + 0.2 ≠ 0.3). Mitigate by using Money::zero() or Money::of() with integer cents.
  • Currency Data Source: Requires external API (e.g., ExchangeRate-API) for real-time rates. Cache responses to reduce latency.
  • Time-Sensitive Operations: Currency conversions may need transactional guarantees (e.g., database transactions for atomicity).
  • Legacy Code Impact: May require refactoring if existing code uses raw floats for monetary values.

Key Questions

  1. Currency Strategy: Will rates be hardcoded, fetched dynamically, or cached? How often will they update?
  2. Precision Requirements: Does the app need sub-cent precision (e.g., crypto)? If so, consider bcmath or gmp extensions.
  3. Auditability: Are immutable Money objects sufficient, or are audit logs needed for changes?
  4. Multi-Currency Support: How many currencies will the app handle? Is a Currency enum or database table needed?
  5. Testing: How will edge cases (e.g., division by zero, invalid amounts) be validated?

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Service Container: Register the package as a singleton/binding for global access.
    • Facades: Create a Money facade for fluent syntax (e.g., Money::dollar(100)->add(Money::euro(50))).
    • Eloquent Models: Use accessors/mutators to convert between Money objects and database storage (e.g., store as cents).
  • API Layer:
    • Validate incoming monetary values (e.g., Money::parse($request->amount)).
    • Use middleware to enforce currency rules (e.g., reject non-supported currencies).
  • Queue Jobs:
    • Offload currency conversions to background jobs (e.g., ConvertCurrencyJob) for async processing.

Migration Path

  1. Phase 1: Core Integration
    • Add package via Composer (composer require 99designs/money-php).
    • Create a MoneyService class to wrap the library and handle currency rates.
    • Update models to use Money objects (e.g., protected $price; // Money object).
  2. Phase 2: Data Layer
    • Modify database schema to store amounts as integers (cents) and currencies as strings/enums.
    • Add migrations/seeders for initial currency configurations.
  3. Phase 3: API/Business Logic
    • Replace raw arithmetic with Money operations (e.g., order->total->add(tax)).
    • Add validation rules (e.g., required|money).
  4. Phase 4: Testing
    • Write unit tests for Money operations (use Money::zero() for test data).
    • Test edge cases (e.g., Money::of(999999999999.99)).

Compatibility

  • Laravel Versions: Tested on Laravel 8+ (PHP 8.0+). For older versions, check for BC breaks.
  • Database: Works with any PDO-supported database. For PostgreSQL, consider numeric type for cents.
  • Third-Party Services: Integrate with payment gateways (e.g., Stripe) by converting their APIs to Money objects.

Sequencing

  1. Proof of Concept: Validate with a single feature (e.g., product pricing).
  2. Incremental Rollout: Start with read operations, then add write operations.
  3. Deprecation Plan: Gradually replace legacy monetary logic (e.g., mark old methods as @deprecated).

Operational Impact

Maintenance

  • Dependency Updates: Monitor 99designs/money-php for breaking changes (e.g., PHP 8.1+ features).
  • Currency Data: Maintain a schedule for updating exchange rates (e.g., daily cron job).
  • Documentation: Document Money usage patterns (e.g., "Always use cents for storage").

Support

  • Debugging: Use Money::getAmount() and Money::getCurrency() for logging/debugging.
  • Common Issues:
    • Rounding Errors: Educate devs to use Money::of() with integers.
    • Currency Mismatches: Add validation to prevent operations across unsupported currencies.
  • Support Matrix: Define SLA for currency-related issues (e.g., "Rate updates take <24h").

Scaling

  • Performance:
    • Cache currency rates (e.g., Redis) to reduce API calls.
    • Use Laravel’s query caching for frequent Money queries.
  • Concurrency: Thread-safe for read operations; ensure write operations (e.g., rate updates) are atomic.
  • Horizontal Scaling: Stateless operations (e.g., conversions) scale naturally; stateful operations (e.g., rate caching) need shared storage.

Failure Modes

Failure Scenario Impact Mitigation
Currency rate API downtime Failed conversions Fallback to cached rates or manual override.
Database corruption (cents field) Invalid monetary values Use migrations with checks; backup regularly.
Floating-point precision bugs Incorrect calculations Enforce integer cents; use bcmath for checks.
Unsupported currency operations Runtime errors Validate currencies early (e.g., middleware).
Timezone issues in rate updates Stale data Use UTC for all rate timestamps.

Ramp-Up

  • Onboarding:
    • Developers: Conduct a workshop on Money objects vs. raw floats.
    • QA: Create test cases for edge cases (e.g., Money::of(-100)).
  • Training:
    • Document migration steps (e.g., "How to update a model to use Money").
    • Provide cheat sheets for common operations (e.g., add(), divide()).
  • Adoption Metrics:
    • Track usage of Money objects in code reviews.
    • Monitor reduction in floating-point bugs.
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle