centrex/laravel-wallet

Add wallet functionality in laravel application

v1.0.2 2024-01-03 07:10 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.

Contents

Installation

You can install the package via composer:

composer require centrex/laravel-wallet

You can publish the config file with:

php artisan vendor:publish --tag="laravel-wallet-config"

This is the contents of the published config file:

return [
];

Optionally, you can publish the views using

php artisan vendor:publish --tag="laravel-wallet-views"

Usage

First, you'll need to add the HasWallets trait to your model.

use Centrex\Wallet\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

🧹 Keep a modern codebase with Pint:

composer lint

✅ Run refactors using Rector

composer refacto

⚗️ Run static analysis using PHPStan:

composer test:types

✅ Run unit tests using PEST

composer test:unit

🚀 Run the entire test suite:

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.