Installation
composer require immeyti/v-wallet
php artisan vendor:publish --provider="Immeyti\VWallet\VWalletServiceProvider" --tag="migrations"
php artisan migrate
--tag="migrations" flag to set up the database schema.First Use Case: Creating a Wallet
use Immeyti\VWallet\Models\Wallet;
$wallet = Wallet::create([
'user_id' => auth()->id(), // Link to a user
'balance' => 0.00,
'currency' => 'USD', // Default or configurable
]);
user_id foreign key constraint is respected.Where to Look First
app/Models/Wallet.php (or vendor/immeyti/v-wallet/src/Models/Wallet.php).database/migrations/[timestamp]_create_wallets_table.php.Immeyti\VWallet\VWalletServiceProvider for bootstrapping events/bindings.Wallet Operations
$wallet->deposit(100.00); // Updates balance atomically
if ($wallet->withdraw(50.00)) {
// Success: balance updated
} else {
// Failed: insufficient funds or other constraints
}
$wallet1->transfer($wallet2, 25.00); // Atomic operation
Integration with Laravel Ecosystem
wallet.created, wallet.deposited, etc. (check VWalletServiceProvider for event bindings).WalletObserver (if provided) or attach custom observers to the Wallet model.use Immeyti\VWallet\Policies\WalletPolicy;
$this->authorize('view', $wallet); // Requires WalletPolicy
API/Controller Layer
namespace App\Http\Controllers;
use Immeyti\VWallet\Http\Controllers\WalletController;
use App\Http\Controllers\Controller;
class CustomWalletController extends WalletController {
// Override methods as needed (e.g., add custom validation)
}
Route::apiResource('wallets', \App\Http\Controllers\CustomWalletController::class)
->middleware('auth:sanctum');
Testing
createWallet() helper (if provided) or factory:
$wallet = Wallet::factory()->create(['balance' => 1000.00]);
Beta Stage Risks
Transaction Handling
DB::transaction(function () use ($wallet) {
$wallet->withdraw(50.00);
});
$wallet->balance >= $amount || throw new \Exception('Insufficient funds');
Currency Support
Wallet model or use a trait:
class MultiCurrencyWallet extends Wallet {
protected $currency;
// Custom logic for currency conversion
}
Observer/Event Quirks
WalletObserver.event(new \Immeyti\VWallet\Events\WalletTransferAttempted($wallet1, $wallet2, $amount));
Log Transactions
transactions table or log to wallet_logs:
\Log::info('Wallet operation', ['wallet_id' => $wallet->id, 'action' => 'deposit', 'amount' => $amount]);
Database Constraints
ON UPDATE CASCADE on user_id if users are soft-deleted. Add a scope:
public function scopeActive($query) {
return $query->whereNull('user_deleted_at');
}
Testing Edge Cases
refreshModel() or fresh() to test race conditions.bcmath or gmp for financial calculations:
$balance = bcadd($wallet->balance, $amount, 2);
Custom Fields
wallets table via migration, then extend the Wallet model:
protected $casts = [
'is_active' => 'boolean',
'metadata' => 'json',
];
Webhooks
WalletObserver:
public function deposited(Wallet $wallet) {
Http::post('https://your-service.com/webhook', [
'event' => 'deposit',
'amount' => $wallet->balance,
]);
}
Rate Limiting
throttle middleware or a custom trait:
use Illuminate\Cache\RateLimiter;
RateLimiter::for('wallet-transactions', function () {
return Limit::perMinute(10)->by($this->user()->id);
});
How can I help you explore Laravel packages today?