Laravel wallet is a package with expressive pronounced syntax that provide top-notch development covering deposits, withdrawals, transactions and balances all quite simply.
This package is tested with Laravel v8 it my not work on Laravel v7 or v6 or v5
Software | Version |
---|---|
php | ^7.3 | ^8.0 | ^8.1 | ^8.2 |
Composer | ^2.3 |
Laravel | ^8.0 | ^9.0 | ^10.0 |
Install the package by using composer:
composer require yemenopensource/laravel-wallet
You can scape this step if you want to use default configuration, but you can publish wallet configuration by running:
php artisan vendor:publish --provider="YemeniOpenSource\LaravelWallet\WalletServiceProvider" --tag=config
This will merge the config/wallet.php
config file to your root config
directory. You are free to modify it before migrating the database.
After that install the wallet tables
php artisan migrate
If you want to add your customization, publish the migrations:
php artisan vendor:publish --provider="YemeniOpenSource\LaravelWallet\WalletServiceProvider" --tag=migrations
Laravel will use the publish migrations at database/migrations
.
Add the HasWallet
trait to any model which you want to add the wallet functionality of it, for example User
model.
use YemeniOpenSource\LaravelWallet\Traits\HasWallet;
class User extends Model
{
use HasWallet;
//...
}
You can create wallet and transactions for your User
model as an example mentioned above.
// The wallet balance initially will be [0]
$user = User::first();
$user->wallet->balance; // 0
// This will add to the wallet balance as passed amount.
$user->wallet->deposit(643.646);
$user->wallet->balance; // 643.6460
// This will subtract from the wallet balance as passed amount.
$user->wallet->withdraw(168.545);
$user->wallet->balance; // 475.1010
// It will throw [UnacceptedTransactionException] exception if passed amount greater wallet balance
$user->wallet->withdraw(500);
$user->wallet->balance; // UnacceptedTransactionException
// If you want to force withdraw, use [forceWithdraw]
$user->wallet->forceWithdraw(500);
$user->wallet->balance; // -24.8990
If you don't want [UnacceptedTransactionException] you can change the
wallet.disable_insufficient_exception
config value to disable the exception or setWALLET_DISABLE_INSUFFICIENT_EXCEPTION
totrue
on your.env
You can easily add meta information to the transactions to suit your needs. For example:
$user = User::first();
$user->wallet->deposit(100, ['currency' => 'USD', 'bank' => 'TEST BANK']);
$user->wallet->withdraw(64, ['currency' => 'USD', 'description' => 'Purchase of Item #101']);
$user->wallet->withdraw(64, ['currency' => 'USD', 'transfer' => 'Western union tracking number #101']);
Digital money vector created by fullvector - www.freepik.com
The MIT License (MIT). Please see MIT license File for more information.