├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── wallet.php ├── database └── migrations │ └── add_wallet_balance_column_to_model_table.php.stub └── src ├── Exceptions ├── InsufficientFundException.php └── InvalidAmountException.php ├── Interfaces └── Wallet.php ├── Traits └── HasWallet.php └── WalletServiceProvider.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `wallet` will be documented in this file. 4 | 5 | ## 2.0.1 - 2025-01-27 6 | 7 | - Bump dependabot/fetch-metadata from 2.2.0 to 2.3.0 in #28 8 | 9 | ## 2.0.0 - 2024-07-15 10 | 11 | - Laravel 11 support by @xabdulhady 12 | 13 | ## 1.0.1 - 2023-10-02 14 | 15 | - Bump actions/checkout from 3 to 4 #18 16 | 17 | ## 1.0.0 - 2023-04-24 18 | 19 | - Laravel 10 support 20 | 21 | ## 0.0.7 - 2023-01-30 22 | 23 | - Bump dependabot/fetch-metadata from 1.3.5 to 1.3.6 24 | 25 | ## 0.0.6 - 2023-01-09 26 | 27 | - Deposit and withdrawal database optimization using Laravel ORM increment and decrement methods - #12 28 | 29 | ## 0.0.5 - 2022-07-04 30 | 31 | - Bump dependabot/fetch-metadata from 1.3.1 to 1.3.3 #9 32 | 33 | ## 0.0.4 - 2022-05-18 34 | 35 | - Fixed Github Action for automatic code styling 36 | 37 | ## 0.0.3 - 2022-05-18 38 | 39 | - Laravel 9 support 40 | 41 | ## 0.0.1 - 2022-05-18 42 | 43 | - Initial release 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) stephenjude 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Wallet 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/stephenjude/laravel-wallet.svg?style=flat-square)](https://packagist.org/packages/stephenjude/laravel-wallet) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/stephenjude/laravel-wallet/run-tests.yml?label=tests)](https://github.com/stephenjude/laravel-wallet/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/stephenjude/laravel-wallet/php-cs-fixer.yml?label=code%20style)](https://github.com/stephenjude/laravel-wallet/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/stephenjude/laravel-wallet.svg?style=flat-square)](https://packagist.org/packages/stephenjude/laravel-wallet) 7 | 8 | A simple wallet implementation for Laravel. 9 | 10 | ## Learn More 11 | - [A Simple Wallet Implementation for Laravel - Laravel News](https://laravel-news.com/laravel-wallet) 12 | - [Interfaces and Traits: How to Use Them in Laravel Packages - Laravel Daily](https://www.youtube.com/watch?v=s2vF84rSaEA) 13 | - [Laravel Virtual Wallet with Coupons: 3 Packages Demo - Laravel Daily](https://www.youtube.com/watch?v=Rgu7iEpXRFM&t=44s) 14 | 15 | ## Installation 16 | 17 | You can install the package via composer: 18 | 19 | ```bash 20 | composer require stephenjude/laravel-wallet 21 | ``` 22 | 23 | You can publish and run the migrations with: 24 | 25 | ```bash 26 | php artisan vendor:publish --tag="wallet-migrations" 27 | php artisan migrate 28 | ``` 29 | 30 | [//]: # () 31 | 32 | [//]: # (```bash) 33 | 34 | [//]: # (php artisan vendor:publish --tag="wallet-config") 35 | 36 | [//]: # (```) 37 | 38 | ## Usage 39 | 40 | ### Prepare User Model 41 | 42 | ```php 43 | 44 | use Stephenjude\Wallet\Interfaces\Wallet; 45 | use Stephenjude\Wallet\Traits\HasWallet; 46 | 47 | class User extends Authenticatable implements Wallet 48 | { 49 | use HasWallet; 50 | } 51 | ``` 52 | 53 | ### Deposit 54 | 55 | ```php 56 | $user = User::first(); 57 | 58 | $user->deposit(200.22); // returns the wallet balance: 200.22 59 | 60 | $user->deposit(200); // returns the wallet balance: 400.22 61 | ``` 62 | 63 | ### Withdraw 64 | ```php 65 | $user->withdraw(200); // returns the wallet balance: 200.22 66 | 67 | $user->withdraw(0.22); // returns the wallet balance: 200 68 | ``` 69 | 70 | ### Balance 71 | 72 | ```php 73 | $user->balance 74 | 75 | $user->wallet_balance 76 | ``` 77 | 78 | ### Exceptions 79 | #### InvalidAmountException 80 | The `InvalidAmountException` is thrown whenever the deposit or withdrawal amount is a negative numeric value or zero. 81 | 82 | #### InsufficientFundException 83 | The `InsufficientFundException` is thrown whenever the withdrawal amount is less than the user's wallet balance. 84 | 85 | ### Alternative Package 86 | If you are looking for somthing much bigger and elaborate checkout [Bavix Laravel Wallet](https://bavix.github.io/laravel-wallet/#/). 87 | 88 | ## Testing 89 | 90 | ```bash 91 | composer test 92 | ``` 93 | 94 | ## Changelog 95 | 96 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 97 | 98 | ## Contributing 99 | 100 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. 101 | 102 | ## Security Vulnerabilities 103 | 104 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 105 | 106 | ## Credits 107 | 108 | - [stephenjude](https://github.com/stephenjude) 109 | - [All Contributors](../../contributors) 110 | 111 | ## License 112 | 113 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 114 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stephenjude/laravel-wallet", 3 | "description": "A simple wallet implementation for Laravel", 4 | "keywords": [ 5 | "stephenjude", 6 | "laravel", 7 | "wallet" 8 | ], 9 | "homepage": "https://github.com/stephenjude/laravel-wallet", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "stephenjude", 14 | "email": "stephenjudesuccess@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.0|^8.1|^8.2", 20 | "spatie/laravel-package-tools": "^1.9.2", 21 | "illuminate/contracts": "^9.0|^10.0|^11.0" 22 | }, 23 | "require-dev": { 24 | "nunomaduro/collision": "^7.0|^8.0", 25 | "orchestra/testbench": "^9.0", 26 | "pestphp/pest": "^2.28", 27 | "phpunit/phpunit": "^10.0" 28 | }, 29 | "autoload": { 30 | "psr-4": { 31 | "Stephenjude\\Wallet\\": "src", 32 | "Stephenjude\\Wallet\\Tests\\Database\\Factories\\": "tests/database/factories" 33 | } 34 | }, 35 | "autoload-dev": { 36 | "psr-4": { 37 | "Stephenjude\\Wallet\\Tests\\": "tests" 38 | } 39 | }, 40 | "scripts": { 41 | "analyse": "vendor/bin/phpstan analyse", 42 | "test": "vendor/bin/pest", 43 | "test-coverage": "vendor/bin/pest --coverage" 44 | }, 45 | "config": { 46 | "sort-packages": true, 47 | "allow-plugins": { 48 | "pestphp/pest-plugin": true 49 | } 50 | }, 51 | "extra": { 52 | "laravel": { 53 | "providers": [ 54 | "Stephenjude\\Wallet\\WalletServiceProvider" 55 | ], 56 | "aliases": { 57 | } 58 | } 59 | }, 60 | "minimum-stability": "dev", 61 | "prefer-stable": true 62 | } 63 | -------------------------------------------------------------------------------- /config/wallet.php: -------------------------------------------------------------------------------- 1 | decimal("wallet_balance", 16, 2)->default(0); 13 | }); 14 | } 15 | 16 | public function down() 17 | { 18 | Schema::table('users', function (Blueprint $table) { 19 | $table->dropColumns("wallet_balance"); 20 | }); 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /src/Exceptions/InsufficientFundException.php: -------------------------------------------------------------------------------- 1 | throwExceptionIfAmountIsInvalid($amount); 14 | 15 | $this->increment('wallet_balance', $amount); 16 | 17 | return $this->wallet_balance; 18 | } 19 | 20 | public function withdraw(int|float $amount): float|int 21 | { 22 | $this->throwExceptionIfAmountIsInvalid($amount); 23 | 24 | $this->throwExceptionIfFundIsInsufficient($amount); 25 | 26 | $this->decrement('wallet_balance', $amount); 27 | 28 | return $this->wallet_balance; 29 | } 30 | 31 | public function canWithdraw(int|float $amount): bool 32 | { 33 | $this->throwExceptionIfAmountIsInvalid($amount); 34 | 35 | $balance = $this->wallet_balance ?? 0; 36 | 37 | return $balance >= $amount; 38 | } 39 | 40 | public function balance(): Attribute 41 | { 42 | return Attribute::get(fn () => $this->wallet_balance ?? 0); 43 | } 44 | 45 | public function throwExceptionIfAmountIsInvalid(int|float $amount): void 46 | { 47 | if ($amount <= 0) { 48 | throw new InvalidAmountException(); 49 | } 50 | } 51 | 52 | public function throwExceptionIfFundIsInsufficient(int|float $amount): void 53 | { 54 | if (! $this->canWithdraw($amount)) { 55 | throw new InsufficientFundException(); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/WalletServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('wallet') 19 | ->hasConfigFile() 20 | ->hasMigration('add_wallet_balance_column_to_model_table'); 21 | } 22 | } 23 | --------------------------------------------------------------------------------