This package provides a simple wallet management system for Laravel applications. It allows users to create wallets, manage funds, and transfer money between wallets.
To install the package, run the following command:
composer require algeriany/walletAfter installing, you may want to publish the configuration files using the following commands:
php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=models
php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=migrationsTo create a wallet for a user or model that uses the Walletable trait, you can do the following:
use algeriany\wallet\Models\Wallet;
use algeriany\wallet\Services\WalletService;
// Assuming $user is an instance of a model that uses the Walletable trait
$walletService = new WalletService();
$wallet = $walletService->createWallet($user, 100.00); // Initialize with $100You can add funds to an existing wallet:
$walletService->addFunds($wallet, 50.00); // Add $50To deduct funds from a wallet:
try {
$walletService->deductFunds($wallet, 20.00); // Deduct $20
} catch (\Exception $e) {
echo $e->getMessage(); // Handle insufficient funds exception
}To transfer funds from one wallet to another:
$fromWallet = Wallet::find($fromWalletId);
$toWallet = Wallet::find($toWalletId);
try {
$walletService->transferFunds($fromWallet, $toWallet, 30.00); // Transfer $30
} catch (\Exception $e) {
echo $e->getMessage(); // Handle exceptions
}This package includes migrations for creating the wallets and wallet_transfers tables. To run the migrations, execute:
php artisan migrateid: Unique identifier for the wallet.wallet_id: Unique String identifier for the wallet.walletable_id: The ID of the wallet owner (user or model).walletable_type: The type of the wallet owner (morph relation).balance: The balance of the wallet.created_at: Timestamp for wallet creation.updated_at: Timestamp for last update.
id: Unique identifier for the transfer.from_wallet_id: ID of the wallet from which funds are transferred.to_wallet_id: ID of the wallet to which funds are transferred.amount: Amount transferred.created_at: Timestamp for the transfer creation.updated_at: Timestamp for last update.
To publish the models and migrations, run:
php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=models
php artisan vendor:publish --provider="algeriany\wallet\WalletServiceProvider" --tag=migrationsThis package is licensed under the MIT License. See the LICENSE file for details.