Giter VIP home page Giter VIP logo

laravel-wallet-plus's Introduction

Laravel Wallet Plus

Latest Version on Packagist Quality Score GitHub Tests 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.

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 [email protected] instead of using the issue tracker.

Credits

License

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

laravel-wallet-plus's People

Contributors

chrisbjr avatar welox avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.