Giter Site home page Giter Site logo

laravel-wallet's Introduction

Laravel Wallet

Some apps require a prepayment system like a virtual wallet where customers can recharge credits which they can then use to pay in app stuff. With this package you can equip your eloquent models with one or multiple digital wallets that handle that for you. All the wallet activities are tracked with transactions.

Installation

Install the package with composer:

composer require mannikj/laravel-wallet

Run Migrations

Per default the package will automatically load the migrations from the vendor folder.

If you want more flexibility, you can publish the migration files to your own migration directory with the following artisan command:

php artisan vendor:publish --provider="MannikJ\Laravel\Wallet\WalletServiceProvider" --tag=migrations

Make sure to deactivate automatic migration loadingby setting the config variable load_migrations to false when you have published the migration file.

Configuration

You can publish the config file with this artisan command:

php artisan vendor:publish --provider="MannikJ\Laravel\Wallet\WalletServiceProvider" --tag=config

This will merge the wallet.php config file where you can specify the Users, Wallets & Transactions classes if you have custom ones.

Usage

Add the HasWallet trait to your User model.

use MannikJ\Laravel\Wallet\Traits\HasWallet;

class User extends Model
{
    use HasWallet;

    ...
}

Then you can easily make transactions from your user model.

$user = User::find(1);
$user->wallet->balance; // 0

$user->wallet->deposit(100);
$user->wallet->balance; // 100

$user->wallet->withdraw(50);
$user->wallet->balance; // 50

$user->wallet->forceWithdraw(200);
$user->wallet->balance; // -150

You can easily add meta information to the transactions to suit your needs.

$user = User::find(1);
$user->wallet->deposit(100, ['stripe_source' => 'ch_BEV2Iih1yzbf4G3HNsfOQ07h', 'description' => 'Deposit of 100 credits from Stripe Payment']);
$user->wallet->withdraw(10, ['description' => 'Purchase of Item #1234']);

Testing

This package makes use of https://github.com/orchestral/testbench to create a laravel testing environment. The tests will execute with a pre-configured in-memory sqlite database, so you don't need setup a database on your own.

To run the phpunit tests just make sure to install the package dependencies first via

composer install

Then execute from within the project root directory

composer test

Security

If you discover any security related issues, open an issue on the github repository.

Credits

License

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

laravel-wallet's People

Contributors

mannikj avatar depsimon avatar chadtomkiss avatar tolawho 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.