tomatophp/filament-wallet
Filament Wallet adds wallet/balance management to FilamentPHP using bavix/laravel-wallet. Install the plugin, add the wallet trait to your model, then manage wallets, charges, and transactions from Filament resources with ready-made pages/actions.
composer require tomatophp/filament-wallet
php artisan filament-wallet:install
app/Providers/Filament/AdminPanelProvider.php:
->plugin(\TomatoPHP\FilamentWallet\FilamentWalletPlugin::make())
Account):
use Bavix\Wallet\Interfaces\Wallet;
use Bavix\Wallet\Traits\HasWalletFloat;
class Account extends Model implements Wallet
{
use HasWalletFloat;
}
use TomatoPHP\FilamentWallet\Filament\Actions\WalletAction;
public function table(Table $table): Table
{
$table->actions([
WalletAction::make('wallet'),
]);
}
Charge a user’s wallet directly from the Filament admin panel by clicking the "Wallet" action in the user table. The modal will appear, allowing you to input an amount and confirm the transaction.
Wallet Management:
HasWalletFloat trait to enable wallet functionality on any model (e.g., User, Customer, Subscription).WalletAction to trigger deposits/withdrawals from Filament tables.Transaction Tracking:
bavix/laravel-wallet (underlying package).Integration with Filament Accounts:
php artisan vendor:publish --tag="filament-accounts-model"
useAccounts():
\TomatoPHP\FilamentWallet\FilamentWalletPlugin::make()->useAccounts()
Customization:
FilamentWalletPlugin or WalletAction classes.php artisan vendor:publish --tag="filament-wallet-notifications"
Model Design:
Implement the Wallet interface and use HasWalletFloat for decimal precision (e.g., cents, crypto).
class User extends Authenticatable implements Wallet
{
use HasWalletFloat;
}
Action Placement:
Add WalletAction to relevant tables (e.g., UserResource, CustomerResource) for granular control.
$table->actions([
WalletAction::make('wallet')->label('Top Up Balance'),
]);
Permissions: Use Filament’s built-in policies or custom gates to restrict wallet actions:
public static function getPages(): array
{
return [
'wallets' => fn (User $user) => $user->can('manage-wallets'),
];
}
Testing: Run the included test suite to validate core functionality:
composer test
Filament Version Mismatch:
live() → lazy() changes).filament-wallet (e.g., v1.x).Floating-Point Precision:
HasWalletFloat uses floats for balance storage, which can lead to rounding errors in financial calculations.Bavix\Wallet\Traits\HasWalletInteger for integer-based currencies (e.g., cents) and handle division manually.Transaction Notifications:
Notification facade isn’t properly configured.php artisan vendor:publish --tag="filament-wallet-notifications"
Livewire Lazy Loading:
lazy() method may cause unexpected delays if not used correctly (e.g., blocking UI updates).WalletAction is properly configured for async operations:
WalletAction::make('wallet')->lazy()->modalWidth('50%')
Database Migrations:
wallets table (handled by bavix/laravel-wallet).php artisan vendor:publish --provider="Bavix\Wallet\WalletServiceProvider" --tag="wallet-migrations"
php artisan migrate
Check Wallet Balance: Use Tinker to verify balances:
php artisan tinker
>> $user = App\Models\User::first();
>> $user->wallet->balance
Transaction Logs:
Inspect the wallet_transactions table for discrepancies:
SELECT * FROM wallet_transactions WHERE walletable_id = {user_id} ORDER BY created_at DESC;
Filament Plugin Registration: Ensure the plugin is registered after Filament’s core resources:
->plugin(\TomatoPHP\FilamentWallet\FilamentWalletPlugin::make())
->navigationGroups([
'Users' => ['wallets', 'transactions'],
]);
Custom Wallet Actions:
Extend WalletAction to add pre/post-logic:
use TomatoPHP\FilamentWallet\Filament\Actions\WalletAction as BaseWalletAction;
class CustomWalletAction extends BaseWalletAction
{
protected function handle(): void
{
// Custom logic before charging
parent::handle();
// Custom logic after charging
}
}
Override Default Views: Publish and modify the Blade views:
php artisan vendor:publish --tag="filament-wallet-views"
Add Custom Fields:
Extend the wallet model to include metadata (e.g., currency, last_updated):
class User extends Authenticatable implements Wallet
{
use HasWalletFloat;
protected $casts = [
'wallet_currency' => 'string',
];
}
Webhook Integration:
Use Laravel’s wallet:transaction event to trigger external actions:
use Bavix\Wallet\Events\TransactionCreated;
TransactionCreated::dispatch($transaction)
->then(fn () => $this->dispatchWebhook($transaction));
Decimal Places:
Configure the number of decimal places in config/wallet.php:
'decimal_places' => 2, // Default for cents
Default Currency: Set the default currency in the config:
'default_currency' => 'USD',
Plugin Pages: Customize the plugin’s navigation group:
FilamentWalletPlugin::make()
->navigationGroup('Finance')
->navigationIcon('heroicon-o-wallet')
How can I help you explore Laravel packages today?