A package to make easy wallet for laravel projects.
yasser-elgammal/laravel-easy-wallet is a Laravel package for a package to make easy wallet for laravel projects..
It currently has 9 GitHub stars and 9 downloads on Packagist (latest version v1.1.0).
Install it with composer require yasser-elgammal/laravel-easy-wallet.
Discover more Laravel packages by yasser-elgammal
or browse all Laravel packages to compare alternatives.
Last updated
Laravel Easy Wallet is a simple, secure, and highly extensible wallet system for Laravel applications. It allows you to associate wallets with any model, manage balances, and record transactions safely with concurrency protection, custom exceptions, and event dispatches.
Walletable contract).lockForUpdate on DB transactions to prevent race conditions during debits and transfers.credit and debit operations with secure UUIDs.WalletCredited, WalletDebited, WalletTransferCompleted).InsufficientBalanceException, WalletNotFoundException, InvalidAmountException).composer require yasser-elgammal/laravel-easy-wallet
Walletable and add HasWallet TraitImplement the Walletable contract and add the HasWallet trait into models that need wallets.
use Illuminate\Database\Eloquent\Model;
use YasserElgammal\LaravelEasyWallet\Contracts\Walletable;
use YasserElgammal\LaravelEasyWallet\Traits\HasWallet;
class User extends Model implements Walletable
{
use HasWallet;
/**
* Automatically create a wallet when the model is created.
* Default is [true]. Set to false to disable auto creation.
*/
protected bool $autoCreateWallet = true;
}
use YasserElgammal\LaravelEasyWallet\Facades\EasyWallet;
use App\Models\User;
$user = User::find(1);
EasyWallet::credit($user, 100.00, 'Initial deposit');
try {
EasyWallet::debit($user, 25.00, 'Purchased course');
} catch (\YasserElgammal\LaravelEasyWallet\Exceptions\InsufficientBalanceException $e) {
// Handle lack of funds...
}
$fromUser = User::find(1);
$toUser = User::find(2);
try {
EasyWallet::transfer($fromUser, $toUser, 40.00, 'Transfer to friend');
} catch (\Exception $e) {
// Handle error...
}
$balance = EasyWallet::balance($user);
The package fires events that you can listen to in your EventServiceProvider:
YasserElgammal\LaravelEasyWallet\Events\WalletCreditedYasserElgammal\LaravelEasyWallet\Events\WalletDebitedYasserElgammal\LaravelEasyWallet\Events\WalletTransferCompletedExample Listener:
use YasserElgammal\LaravelEasyWallet\Events\WalletCredited;
public function handle(WalletCredited $event)
{
// Access transaction details:
// $event->transaction->amount
// Send email/notification to user...
}
By default, it uses TXN-{UUID}. You can customize the prefix in your .env file:
WALLET_TXN_PREFIX=MY-PREFIX-
You can completely override the default Wallet and WalletTransaction models by publishing the config file and updating the class references.
php artisan vendor:publish --tag=easy-wallet-config
Then in config/easy-wallet.php:
'models' => [
'wallet' => \App\Models\CustomWallet::class,
'transaction' => \App\Models\CustomWalletTransaction::class,
],
You can optionally publish resources to customize them:
php artisan vendor:publish --tag=easy-wallet-config
php artisan vendor:publish --tag=easy-wallet-migrations
php artisan vendor:publish --tag=easy-wallet-models
(Remember to update the config/easy-wallet.php if you change the model namespaces!)
Contributions are welcome and appreciated!
If you have an idea, feature request, bug fix, or any improvement:
Thank you for supporting the project! π