Installation:
composer require zotel-laravel-wallet
Publish the config and migrations:
php artisan vendor:publish --provider="Zotel\Wallet\WalletServiceProvider"
php artisan migrate
First Use Case:
Create a wallet for a user (e.g., User model):
use Zotel\Wallet\Models\Wallet;
// Create a wallet for a user
$user = User::find(1);
$wallet = Wallet::create([
'user_id' => $user->id,
'balance' => 1000.00,
'currency' => 'USD',
]);
Key Files to Review:
config/wallet.php (default settings, currencies, etc.)app/Models/Wallet.php (customize if needed)routes/wallet.php (API endpoints if using web routes)Wallet Creation & Management:
user_id or custom foreign keys.Wallet::create() or user->wallet()->create() (if using relationships).// In User model:
public function wallet() {
return $this->hasOne(Wallet::class);
}
// Usage:
$user->wallet()->create(['balance' => 500.00]);
Transactions:
$wallet->deposit(100.00, 'Payment received', 'invoice_123');
$wallet->withdraw(50.00, 'Product purchase', 'order_456');
$wallet->transfer($targetWallet, 75.00, 'Gift to friend');
Events & Observers:
wallet.created, wallet.deposited, wallet.withdrawn, etc.use Zotel\Wallet\Events\WalletDeposited;
WalletDeposited::dispatch($wallet, $amount, $description);
API Integration:
routes/wallet.php) or create custom controllers.Route::get('/wallet/balance', function (Wallet $wallet) {
return response()->json(['balance' => $wallet->balance]);
});
Multi-Currency Support:
config/wallet.php.$wallet->convert('EUR'); // Returns converted balance
Custom Validation:
Wallet model to add validation rules:
protected $rules = [
'balance' => 'required|numeric|min:0',
'currency' => 'required|in:USD,EUR,GBP',
];
Webhooks:
event(new \Zotel\Wallet\Events\WalletDeposited($wallet, $amount));
Batch Operations:
$wallet->batchTransactions([
['type' => 'deposit', 'amount' => 100.00, 'description' => 'Batch deposit'],
['type' => 'withdraw', 'amount' => 20.00, 'description' => 'Batch withdrawal'],
]);
Testing:
WalletFactory for seed data:
$wallet = Wallet::factory()->create(['balance' => 1000.00]);
$this->partialMock(Wallet::class, 'deposit')
->expects('deposit')
->with(100.00, 'Test', 'test_123');
Floating-Point Precision:
bcmath or gmp for financial calculations to avoid rounding errors:
$amount = bcadd($wallet->balance, '100.50', 2);
Race Conditions:
DB::transaction(function () use ($wallet) {
$wallet->withdraw(50.00, 'Safe purchase');
});
Currency Mismatches:
Observer Conflicts:
Wallet model to avoid duplicate triggers:
class Wallet extends Model
{
public static function boot()
{
parent::boot();
static::observe(\Zotel\Wallet\Observers\WalletObserver::class);
}
}
Migration Issues:
php artisan wallet:migrate to handle schema changes. Always back up your database first.Enable Logging:
config/wallet.php to log transactions:
'log_transactions' => env('WALLET_LOG_TRANSACTIONS', true),
storage/logs/laravel.log.Transaction Tracing:
wallet:transactions Artisan command to inspect history:
php artisan wallet:transactions --wallet=1 --limit=10
Event Debugging:
php artisan tinker
>>> \Zotel\Wallet\Events\WalletDeposited::dispatch($wallet, 100.00, 'Test');
Custom Transaction Types:
Transaction model to add custom fields:
class CustomTransaction extends \Zotel\Wallet\Models\Transaction
{
protected $casts = [
'metadata' => 'json',
];
}
Gateway Integration:
namespace App\Gateways;
use Zotel\Wallet\Contracts\Gateway;
class PayPalGateway implements Gateway
{
public function processPayment($amount, $currency, $reference)
{
// Custom logic
}
}
Webhook Handlers:
config/wallet.php:
'webhooks' => [
'stripe' => \App\Handlers\StripeWebhook::class,
],
Custom Commands:
php artisan make:command WalletReport
Example:
public function handle()
{
$wallets = Wallet::all();
foreach ($wallets as $wallet) {
$this->info("Wallet {$wallet->id}: {$wallet->balance} {$wallet->currency}");
}
}
How can I help you explore Laravel packages today?