coreproc / laravel-wallet-plus
Easily add a virtual wallet to your Laravel models. Features multiple wallets and a ledger system to help keep track of all transactions in the wallets.
Installs: 12 672
Dependents: 0
Suggesters: 0
Security: 0
Stars: 28
Watchers: 7
Forks: 10
Open Issues: 4
Requires
- php: ^7.3 || ^8.0
Requires (Dev)
- phpunit/phpunit: ^8.2 || ^9.3
- symfony/var-dumper: ^4.3 || ^5.1 || ^6.0
README
Easily add a virtual wallet to your Laravel models. Features multiple wallets and a ledger system to help keep track of all transactions in the wallets.
Installation
You can install the package via composer:
composer require coreproc/laravel-wallet-plus
You can publish the migration with:
php artisan vendor:publish --provider="CoreProc\WalletPlus\WalletPlusServiceProvider" --tag="migrations"
After the migration file has been published you can create the wallet-plus tables by running the migration:
php artisan migrate
Usage
First, you'll need to add the HasWallets
trait to your model.
use CoreProc\WalletPlus\Models\Traits\HasWallets; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use Notifiable, HasWallets; }
By adding the HasWallets
trait, you've essentially added all the wallet relationships to the model.
You can start by creating a wallet for the given model.
$user = User::find(1); $wallet = $user->wallets()->create();
You can then increment the wallet balance by:
$wallet->incrementBalance(100);
Or decrement the balance by:
$wallet->decrementBalance(100);
To get the balance of the wallet, you can use the balance
accessor:
$wallet->balance;
A wallet can be accessed using the wallet()
method in the model:
$user->wallet();
You can set up multiple types of wallets by defining a WalletType
. Simply create a wallet type entry in the
wallet_types
table and create a wallet using this wallet type.
use CoreProc\WalletPlus\Models\WalletType; $walletType = WalletType::create([ 'name' => 'Peso Wallet', 'decimals' => 2, // Set how many decimal points your wallet accepts here. Defaults to 0. ]); $user->wallets()->create(['wallet_type_id' => $walletType->id]);
You can access a model's particular wallet type by using the wallet()
method as well:
$pesoWallet = $user->wallet('Peso Wallet'); // This method also accepts the ID of the wallet type as a parameter $pesoWallet->incrementBalance(100); $pesoWallet->balance; // Returns the updated balance without having to refresh the model.
All movements made in the wallet are recorded in the wallet_ledgers
table.
Defining Transactions
Ideally, we want to record all transactions concerning the wallet by linking it to a transaction model. Let's say we
have a PurchaseTransaction
model which holds the data of a purchase the user makes in our app.
use Illuminate\Database\Eloquent\Model; class PurchaseTransaction extends Model { // }
We can link this PurchaseTransaction
to the wallet ledger by implementing the WalletTransaction
contract to this
model and using this transaction to decrement (or increment, whatever the case may be) the wallet balance.
use CoreProc\WalletPlus\Contracts\WalletTransaction; use Illuminate\Database\Eloquent\Model; class PurchaseTransaction extends Model implements WalletTransaction { public function getAmount() { return $this->amount; } }
Now we can use this in the wallet:
$wallet = $user->wallet('Peso Wallet'); $purchaseTransaction = PurchaseTransaction::create([ ..., 'amount' => 100, ]); $wallet->decrementBalance($purchaseTransaction);
By doing this, you will be able to see in the wallet_ledgers
table the transaction that is related to the movement
in the wallet.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email chris.bautista@coreproc.ph instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.