From b15069ef1661161dd4ce0c7aba100c9cc6ffb634 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 04:49:31 +0100 Subject: [PATCH 01/16] Add update_wallets_logs_table migration --- .../update_wallets_logs_table.php.stub | 38 +++++++++++++++++++ src/LaravelPayPocketServiceProvider.php | 10 +++-- 2 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 database/migrations/update_wallets_logs_table.php.stub diff --git a/database/migrations/update_wallets_logs_table.php.stub b/database/migrations/update_wallets_logs_table.php.stub new file mode 100644 index 0000000..5ae26a8 --- /dev/null +++ b/database/migrations/update_wallets_logs_table.php.stub @@ -0,0 +1,38 @@ +string('detail')->nullable()->after('status'); + } + if (!Schema::hasColumn('wallets_logs', 'reference')) { + $table->string('reference')->nullable()->after('ip'); + } + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('wallets_logs', function (Blueprint $table) { + if (Schema::hasColumn('wallets_logs', 'detail')) { + $table->dropColumn('detail'); + } + if (Schema::hasColumn('wallets_logs', 'reference')) { + $table->dropColumn('reference'); + } + }); + } +}; diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index f58c9ae..ef3c4b9 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -18,13 +18,17 @@ public function configurePackage(Package $package): void ->name('laravel-pay-pocket') ->hasConfigFile() ->hasViews() - ->hasMigrations('create_wallets_logs_table', 'create_wallets_table'); + ->hasMigrations( + 'create_wallets_logs_table', + 'create_wallets_table', + 'update_wallets_logs_table' + ); } public function bootingPackage() { $this->publishes([ - __DIR__.'/../Enums/' => app_path('Enums'), + __DIR__ . '/../Enums/' => app_path('Enums'), ], 'pay-pocket-wallets'); } -} +} \ No newline at end of file From 8110c2ecfe71a9aa3c7e8c7c512e3498e0a2e63d Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 05:34:34 +0100 Subject: [PATCH 02/16] Add Reference and Detail to Logs --- config/pay-pocket.php | 10 +++++++-- src/LaravelPayPocketServiceProvider.php | 11 +++++++++ src/Traits/BalanceOperation.php | 30 ++++++++++++++++--------- src/Traits/HandlesDeposit.php | 8 +++---- src/Traits/HandlesPayment.php | 8 +++---- tests/OperationsWithFacadeTest.php | 4 +--- 6 files changed, 48 insertions(+), 23 deletions(-) diff --git a/config/pay-pocket.php b/config/pay-pocket.php index 4cb9728..1b65891 100644 --- a/config/pay-pocket.php +++ b/config/pay-pocket.php @@ -2,5 +2,11 @@ // config for HPWebdeveloper/LaravelPayPocket return [ - -]; + 'log_reference_length' => 12, + 'log_reference_prefix' => null, + /** + * The log reference generator should be static + * The third array item should contain optional parameters to pass to the generator + */ + 'log_reference_generator' => [\Illuminate\Support\Str::class, 'random', [15]], +]; \ No newline at end of file diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index ef3c4b9..57638a6 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -30,5 +30,16 @@ public function bootingPackage() $this->publishes([ __DIR__ . '/../Enums/' => app_path('Enums'), ], 'pay-pocket-wallets'); + + + $this->publishes([ + __DIR__ . '/../config/pay-pocket.php' => config_path('pay-pocket.php'), + ], 'config'); + } + + public function registeringPackage() + { + // Automatically apply the package configuration + $this->mergeConfigFrom(__DIR__ . '/../config/pay-pocket.php', 'pay-pocket'); } } \ No newline at end of file diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index ae895ef..a459774 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -2,12 +2,14 @@ namespace HPWebdeveloper\LaravelPayPocket\Traits; +use Illuminate\Support\Str; + trait BalanceOperation { protected $createdLog; /** - * Check if Balance is more than zero. + * Check if Balance is more than zero. */ public function hasBalance(): bool { @@ -15,32 +17,38 @@ public function hasBalance(): bool } /** - * Decrement Balance and create a log entry. + * Decrement Balance and create a log entry. */ - public function decrementAndCreateLog($value): void + public function decrementAndCreateLog($value, $detail = null): void { - $this->createLog('dec', $value); + $this->createLog('dec', $value, $detail); $this->decrement('balance', $value); } /** - * Increment Balance and create a log entry. + * Increment Balance and create a log entry. */ - public function incrementAndCreateLog($value): void + public function incrementAndCreateLog($value, $detail = null): void { - $this->createLog('inc', $value); + $this->createLog('inc', $value, $detail); $this->increment('balance', $value); } /** - * Create a new log record + * Create a new log record */ - protected function createLog($logType, $value): void + protected function createLog($logType, $value, $detail = null): void { $currentBalance = $this->balance ?? 0; $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; + $refGen = config('pay-pocket.log_reference_generator', [Str::class, 'random', [12]]); + $reference = config('pay-pocket.reference_string_prefix', ''); + $reference .= isset($refGen[0], $refGen[1]) + ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) + : Str::random(config('pay-pocket.log_reference_length', 12)); + $this->createdLog = $this->logs()->create([ 'wallet_name' => $this->type->value, 'from' => $currentBalance, @@ -48,8 +56,10 @@ protected function createLog($logType, $value): void 'type' => $logType, 'ip' => \Request::ip(), 'value' => $value, + 'detail' => $detail, + 'reference' => $reference ]); $this->createdLog->changeStatus('Done'); } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 108c1ac..3294741 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount): bool + public function deposit(string $type, int|float $amount, $detail = null): bool { $depositable = $this->getDepositableTypes(); @@ -27,10 +27,10 @@ public function deposit(string $type, int|float $amount): bool throw new InvalidValueException(); } - DB::transaction(function () use ($type, $amount) { + DB::transaction(function () use ($type, $amount, $detail) { $type = WalletEnums::tryFrom($type); $wallet = $this->wallets()->firstOrCreate(['type' => $type]); - $wallet->incrementAndCreateLog($amount); + $wallet->incrementAndCreateLog($amount, $detail); }); return true; @@ -63,4 +63,4 @@ private function isRequestValid($type, array $depositable) return true; } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 2e31745..b55eb1d 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,13 +13,13 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue): void + public function pay(int|float $orderValue, $detail = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue) { + DB::transaction(function () use ($orderValue, $detail) { $remainingOrderValue = $orderValue; $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); @@ -30,7 +30,7 @@ public function pay(int|float $orderValue): void } $amountToDeduct = min($wallet->balance, $remainingOrderValue); - $wallet->decrementAndCreateLog($amountToDeduct); + $wallet->decrementAndCreateLog($amountToDeduct, $detail); $remainingOrderValue -= $amountToDeduct; if ($remainingOrderValue <= 0) { @@ -43,4 +43,4 @@ public function pay(int|float $orderValue): void } }); } -} +} \ No newline at end of file diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 24a5c5c..5e5fdf9 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -18,7 +18,6 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(234.56); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(234.56); - }); test('user can deposit two times', function () { @@ -34,7 +33,6 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(1023.68); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(1023.68); - }); test('user can pay order', function () { @@ -96,4 +94,4 @@ expect(LaravelPayPocket::walletBalanceByType($user, 'wallet_2'))->toBeFloat(0.12); expect(LaravelPayPocket::checkBalance($user))->toBeFloat(0.12); -}); +}); \ No newline at end of file From 67d7328b628c2784fdcd8d69f45177fe19334573 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 05:38:22 +0100 Subject: [PATCH 03/16] Add wallets restriction for payment --- src/Traits/HandlesPayment.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index b55eb1d..a16e6c6 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,19 +13,19 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, $detail = null): void + public function pay(int|float $orderValue, $detail = null, array $restrictedWallets = []): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue, $detail) { + DB::transaction(function () use ($orderValue, $detail, $restrictedWallets) { $remainingOrderValue = $orderValue; $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); foreach ($walletsInOrder as $wallet) { - if (! $wallet || ! $wallet->hasBalance()) { + if (! $wallet || ! $wallet->hasBalance() || in_array($wallet, $restrictedWallets)) { continue; } From 601aa6ded8a478144289dc7ad902296bb0c2ee1f Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 05:52:42 +0100 Subject: [PATCH 04/16] Add detail and ref to WalletsLog fillable props. --- README.md | 69 ++++++++++++++----------- src/LaravelPayPocketServiceProvider.php | 2 +- src/Models/WalletsLog.php | 11 +++- src/Traits/BalanceOperation.php | 2 + 4 files changed, 52 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 30625af..3212ef7 100644 --- a/README.md +++ b/README.md @@ -7,60 +7,64 @@ [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/hpwebdeveloper/laravel-pay-pocket/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/hpwebdeveloper/laravel-pay-pocket/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) [![Imports](https://github.com/HPWebdeveloper/laravel-pay-pocket/actions/workflows/check_imports.yml/badge.svg?branch=main)](https://github.com/HPWebdeveloper/laravel-pay-pocket/actions/workflows/check_imports.yml) - **Laravel Pay Pocket** is a package designed for Laravel applications, offering the flexibility to manage multiple wallet types within two dedicated database tables, `wallets` and `wallets_logs`. **Demo** https://github.com/HPWebdeveloper/demo-pay-pocket **Note:** This package does not handle payments from payment platforms, but instead offers the concept of virtual money, deposit, and withdrawal. -* **Author**: Hamed Panjeh -* **Vendor**: hpwebdeveloper -* **Package**: laravel-pay-pocket -* **Alias name**: Laravel PPP (Laravel Pay Pocket Package) -* **Version**: `1.x` -* **PHP Version**: 8.1+ -* **Laravel Version**: `10.x` -* **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket` - +- **Author**: Hamed Panjeh +- **Vendor**: hpwebdeveloper +- **Package**: laravel-pay-pocket +- **Alias name**: Laravel PPP (Laravel Pay Pocket Package) +- **Version**: `1.x` +- **PHP Version**: 8.1+ +- **Laravel Version**: `10.x` +- **[Composer](https://getcomposer.org/):** `composer require hpwebdeveloper/laravel-pay-pocket` ### Support Policy -| Version | Laravel | PHP | Release date | End of improvements | End of support | -|---------|----------------|---------------|--------------|---------------------|----------------| -| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | -| x.x | | | | | | | - +| Version | Laravel | PHP | Release date | End of improvements | End of support | +| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | --- | +| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | +| x.x | | | | | | | ## Installation: -- **Step 1:** You can install the package via composer: +- **Step 1:** You can install the package via composer: ```bash composer require hpwebdeveloper/laravel-pay-pocket ``` -- **Step 2:** Publish and run the migrations with: +- **Step 2:** Publish and run the migrations with: ```bash php artisan vendor:publish --tag="pay-pocket-migrations" php artisan migrate ``` + You have successfully added two dedicated database tables, `wallets` and `wallets_logs`, without making any modifications to the `users` table. -- **Step 3:** Publish the wallet types using +- **Step 3:** Publish the wallet types using ```bash php artisan vendor:publish --tag="pay-pocket-wallets" ``` +- **Step 4:** Publish the config file + +```bash +php artisan vendor:publish --tag="pay-pocket-config" +``` + This command will automatically publish the `WalletEnums.php` file into your application's `app/Enums` directory. ## Preparation ### Prepare User Model -To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait. +To use this package you need to implement the `WalletOperations` into `User` model and utilize the `ManagesWallet` trait. ```php @@ -75,9 +79,10 @@ class User extends Authenticatable implements WalletOperations ### Prepare Wallets -In Laravel Pay Pocket, you have the flexibility to define the order in which wallets are prioritized for payments through the use of Enums. The order of wallets in the Enum file determines their priority level. The first wallet listed has the highest priority and will be used first for deducting order values. +In Laravel Pay Pocket, you have the flexibility to define the order in which wallets are prioritized for payments through the use of Enums. The order of wallets in the Enum file determines their priority level. The first wallet listed has the highest priority and will be used first for deducting order values. For example, consider the following wallet types defined in the Enum class (published in step 3 of installation): + ```php namespace App\Enums; @@ -88,15 +93,17 @@ enum WalletEnums: string } ``` -**You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.** +**You have complete freedom to name your wallets as per your requirements and even add more wallet types to the Enum list.** In this particular setup, `wallet_1` (`WALLET1`) is given the **highest priority**. When an order payment is processed, the system will first attempt to use `wallet_1` to cover the cost. If `wallet_1` does not have sufficient funds, `wallet_2` (`WALLET2`) will be used next. ### Example: + If the balance in `wallet_1` is 10 and the balance in `wallet_2` is 20, and you need to pay an order value of 15, the payment process will first utilize the entire balance of `wallet_1`. Since `wallet_1`'s balance is insufficient to cover the full amount, the remaining 5 will be deducted from `wallet_2`. After the payment, `wallet_2` will have a remaining balance of 15." ## Usage, APIs and Operations: + ### Deposit ```php @@ -113,13 +120,15 @@ use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; LaravelPayPocket::deposit($user, 'wallet_1', 123.45); ``` + Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. ### Pay + ```php // Pay the value using the total combined balance available across all wallets $user->pay(12.34); - + // Or using provided facade use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; @@ -129,7 +138,8 @@ LaravelPayPocket::pay($user, 12.34); ### Balance -- **Wallets** +- **Wallets** + ```php $user->walletBalance // Total combined balance available across all wallets @@ -138,7 +148,8 @@ $user->walletBalance // Total combined balance available across all wallets LaravelPayPocket::checkBalance($user); ``` -- **Particular Wallet** +- **Particular Wallet** + ```php $user->getWalletBalanceByType('wallet_1') // Balance available in wallet_1 $user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2 @@ -149,7 +160,8 @@ LaravelPayPocket::walletBalanceByType($user, 'wallet_1'); ``` ### Exceptions -Upon examining the `src/Exceptions` directory within the source code, + +Upon examining the `src/Exceptions` directory within the source code, you will discover a variety of exceptions tailored to address each scenario of invalid entry. Review the [demo](https://github.com/HPWebdeveloper/demo-pay-pocket) that accounts for some of the exceptions. ### Log @@ -157,7 +169,6 @@ you will discover a variety of exceptions tailored to address each scenario of i A typical `wallets_logs` table. ![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/a242d335-8bd2-4af1-aa38-4e95b8870941) - ## Testing ```bash @@ -185,9 +196,9 @@ Please review [our security policy](../../security/policy) on how to report secu ## Credits -- [Hamed Panjeh](https://github.com/HPWebdeveloper) -- [All Contributors](../../contributors) -- Icon in the above image: pocket by Creative Mahira from [Noun Project](https://thenounproject.com/browse/icons/term/pocket/) (CC BY 3.0) +- [Hamed Panjeh](https://github.com/HPWebdeveloper) +- [All Contributors](../../contributors) +- Icon in the above image: pocket by Creative Mahira from [Noun Project](https://thenounproject.com/browse/icons/term/pocket/) (CC BY 3.0) ## License diff --git a/src/LaravelPayPocketServiceProvider.php b/src/LaravelPayPocketServiceProvider.php index 57638a6..4166a74 100644 --- a/src/LaravelPayPocketServiceProvider.php +++ b/src/LaravelPayPocketServiceProvider.php @@ -34,7 +34,7 @@ public function bootingPackage() $this->publishes([ __DIR__ . '/../config/pay-pocket.php' => config_path('pay-pocket.php'), - ], 'config'); + ], 'pay-pocket-config'); } public function registeringPackage() diff --git a/src/Models/WalletsLog.php b/src/Models/WalletsLog.php index ca751e2..0f55988 100644 --- a/src/Models/WalletsLog.php +++ b/src/Models/WalletsLog.php @@ -16,7 +16,14 @@ class WalletsLog extends Model use HasFactory; protected $fillable = [ - 'from', 'to', 'type', 'ip', 'value', 'wallet_name', + 'from', + 'to', + 'type', + 'ip', + 'value', + 'wallet_name', + 'detail', + 'reference', ]; public function loggable(): MorphTo @@ -30,4 +37,4 @@ public function changeStatus($status) return $this->save(); } -} +} \ No newline at end of file diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index a459774..59b0873 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -44,7 +44,9 @@ protected function createLog($logType, $value, $detail = null): void $newBalance = $logType === 'dec' ? $currentBalance - $value : $currentBalance + $value; $refGen = config('pay-pocket.log_reference_generator', [Str::class, 'random', [12]]); + $reference = config('pay-pocket.reference_string_prefix', ''); + $reference .= isset($refGen[0], $refGen[1]) ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) : Str::random(config('pay-pocket.log_reference_length', 12)); From 66e27e95eb06d9120f22b9f720d060817ac62dde Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 05:58:30 +0100 Subject: [PATCH 05/16] Add detail and restrictedWallet to PocketServices --- src/Services/PocketServices.php | 10 +++++----- src/Traits/BalanceOperation.php | 8 ++++---- src/Traits/HandlesDeposit.php | 4 ++-- src/Traits/HandlesPayment.php | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index 1fe31cc..552af80 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -4,14 +4,14 @@ class PocketServices { - public function deposit($user, $type, $amount) + public function deposit($user, $type, $amount, ?string $detail = null) { - return $user->deposit($type, $amount); + return $user->deposit($type, $amount, $detail); } - public function pay($user, $orderValue) + public function pay($user, $orderValue, array $restrictedWallets = [], ?string $detail = null) { - return $user->pay($orderValue); + return $user->pay($orderValue, $restrictedWallets, $detail); } public function checkBalance($user) @@ -23,4 +23,4 @@ public function walletBalanceByType($user, $type) { return $user->getWalletBalanceByType($type); } -} +} \ No newline at end of file diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 59b0873..89e08a7 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -19,7 +19,7 @@ public function hasBalance(): bool /** * Decrement Balance and create a log entry. */ - public function decrementAndCreateLog($value, $detail = null): void + public function decrementAndCreateLog($value, ?string $detail = null): void { $this->createLog('dec', $value, $detail); $this->decrement('balance', $value); @@ -28,7 +28,7 @@ public function decrementAndCreateLog($value, $detail = null): void /** * Increment Balance and create a log entry. */ - public function incrementAndCreateLog($value, $detail = null): void + public function incrementAndCreateLog($value, ?string $detail = null): void { $this->createLog('inc', $value, $detail); $this->increment('balance', $value); @@ -37,7 +37,7 @@ public function incrementAndCreateLog($value, $detail = null): void /** * Create a new log record */ - protected function createLog($logType, $value, $detail = null): void + protected function createLog($logType, $value, ?string $detail = null): void { $currentBalance = $this->balance ?? 0; @@ -64,4 +64,4 @@ protected function createLog($logType, $value, $detail = null): void $this->createdLog->changeStatus('Done'); } -} \ No newline at end of file +} diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 3294741..718405b 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,7 +15,7 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. */ - public function deposit(string $type, int|float $amount, $detail = null): bool + public function deposit(string $type, int|float $amount, ?string $detail = null): bool { $depositable = $this->getDepositableTypes(); @@ -63,4 +63,4 @@ private function isRequestValid($type, array $depositable) return true; } -} \ No newline at end of file +} diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index a16e6c6..0c6ec4d 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,7 +13,7 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, $detail = null, array $restrictedWallets = []): void + public function pay(int|float $orderValue, array $restrictedWallets = [], ?string $detail = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); From 413e1a1e5fb7f6527b239442f3c79f4c8001dee5 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 06:18:56 +0100 Subject: [PATCH 06/16] Change $restrictedWallets param to $allowedWallets --- src/Traits/HandlesPayment.php | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 0c6ec4d..e1862dc 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,19 +13,25 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, array $restrictedWallets = [], ?string $detail = null): void + public function pay(int|float $orderValue, array $allowedWallets = [], ?string $detail = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue, $detail, $restrictedWallets) { + DB::transaction(function () use ($orderValue, $detail, $allowedWallets) { $remainingOrderValue = $orderValue; $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); + /** + * @param string $wallet + * @return bool $useWallet + * */ + $useWallet = fn ($wallet) => count($allowedWallets) < 1 || in_array($wallet, $allowedWallets); + foreach ($walletsInOrder as $wallet) { - if (! $wallet || ! $wallet->hasBalance() || in_array($wallet, $restrictedWallets)) { + if (! $wallet || ! $wallet->hasBalance() || !$useWallet($wallet->type->value)) { continue; } From 187ad8a6e9c7a9462988a37e2540f14a2b7be85d Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 06:46:30 +0100 Subject: [PATCH 07/16] Updated the README.md to reflect new features. --- README.md | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 77 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3212ef7..b403a22 100644 --- a/README.md +++ b/README.md @@ -106,41 +106,95 @@ If the balance in `wallet_1` is 10 and the balance in `wallet_2` is 20, and you ### Deposit +```php +deposit(type: 'wallet_1', amount: 123.45, detail: null) +``` + +Deposit funds into `wallet_1` + ```php $user = auth()->user(); +$user->deposit('wallet_1', 123.45); +``` -$user->deposit('wallet_1', 123.45); // Deposit funds into 'wallet_1' +Deposit funds into `wallet_2` -$user->deposit('wallet_2', 67.89); // Deposit funds into 'wallet_2' +```php +$user = auth()->user(); +$user->deposit('wallet_2', 67.89); +``` -// Or using provided facade +Or using provided facade +```php use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; +$user = auth()->user(); LaravelPayPocket::deposit($user, 'wallet_1', 123.45); ``` Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. +#### Transaction Info ([#8][i8]) + +In a case where you want to enter descriptions for a particular transaction, the `$detail` property allows you to provide information about why a transaction happened. + +```php +$user = auth()->user(); +$user->deposit('wallet_1', 67.89, 'You ordered pizza.'); +``` + ### Pay ```php -// Pay the value using the total combined balance available across all wallets +pay(amount: 12.34, allowedWallets: [], detail: null) +``` + +Pay the value using the total combined balance available across all allowed wallets + +```php +$user = auth()->user(); $user->pay(12.34); +``` -// Or using provided facade +Or using provided facade +```php use HPWebdeveloper\LaravelPayPocket\Facades\LaravelPayPocket; +$user = auth()->user(); LaravelPayPocket::pay($user, 12.34); ``` +By default the sytem will attempt to pay using all available wallets exept the `allowedWallets` param is provided. + +#### Allowed Wallets ([#8][i8]) + +Sometimes you want to mark a wallet as restricted so that when the `pay()` method is called, the system does not attempt to charge that wallet, a possible use case is an escrow wallet, the `$allowedWallets` property of the pay method allows you to do just that. + +```php +$user = auth()->user(); +$user->pay(12.34, ['wallet_1']); +``` + +#### Transaction Info ([#8][i8]) + +In a case where you want to enter descriptions for a particular transaction, the `$detail` property allows you to provide information about why a transaction happened. + +```php +$user = auth()->user(); +$user->pay(12.34, [], 'You ordered pizza.'); +``` + +[i8]: https://github.com/HPWebdeveloper/laravel-pay-pocket/issues/8 + ### Balance - **Wallets** ```php +$user = auth()->user(); $user->walletBalance // Total combined balance available across all wallets // Or using provided facade @@ -151,6 +205,7 @@ LaravelPayPocket::checkBalance($user); - **Particular Wallet** ```php +$user = auth()->user(); $user->getWalletBalanceByType('wallet_1') // Balance available in wallet_1 $user->getWalletBalanceByType('wallet_2') // Balance available in wallet_2 @@ -166,6 +221,23 @@ you will discover a variety of exceptions tailored to address each scenario of i ### Log +Getting your transaction logs is a pretty straight forward process + +```php +$user = auth()->user(); +$query = $user->wallets() + ->where('type', \App\Enums\WalletEnums::WALLET1) + ->first() + ->logs(); +``` + +This will return an instance of `\Illuminate\Database\Eloquent\Relations\MorphMany` +from there you can handle any manipulations with your data. + +```php +$logs = $query->get(); +``` + A typical `wallets_logs` table. ![Laravel Pay Pocket Log](https://github.com/HPWebdeveloper/laravel-pay-pocket/assets/16323354/a242d335-8bd2-4af1-aa38-4e95b8870941) From 4ea2cfe4f15d2ad61fdff8c521132082e3f4ff89 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 06:52:06 +0100 Subject: [PATCH 08/16] Fix README.md Suppport policy. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b403a22..62389af 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ ### Support Policy | Version | Laravel | PHP | Release date | End of improvements | End of support | -| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | --- | -| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | -| x.x | | | | | | | +| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | +| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | +| x.x | | | | | | ## Installation: From 157934220ac34ab451504c1d86dc719e8fe5d51a Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 06:54:57 +0100 Subject: [PATCH 09/16] Fix README --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 62389af..8b1b910 100644 --- a/README.md +++ b/README.md @@ -25,9 +25,9 @@ ### Support Policy | Version | Laravel | PHP | Release date | End of improvements | End of support | -| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | -| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | -| x.x | | | | | | +| ------- | ------- | ------------- | ------------ | ------------------- | -------------- | --- | +| 1.x | ^10.0 | 8.1, 8.2, 8.3 | Nov 30, 2023 | Mar 1, 2024 | | | +| x.x | | | | | | | ## Installation: @@ -138,7 +138,7 @@ Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. #### Transaction Info ([#8][i8]) -In a case where you want to enter descriptions for a particular transaction, the `$detail` property allows you to provide information about why a transaction happened. +In a case where you want to enter descriptions for a particular transaction, the `$detail` param allows you to provide information about why a transaction happened. ```php $user = auth()->user(); @@ -171,7 +171,7 @@ By default the sytem will attempt to pay using all available wallets exept the ` #### Allowed Wallets ([#8][i8]) -Sometimes you want to mark a wallet as restricted so that when the `pay()` method is called, the system does not attempt to charge that wallet, a possible use case is an escrow wallet, the `$allowedWallets` property of the pay method allows you to do just that. +Sometimes you want to mark a wallet as restricted so that when the `pay()` method is called, the system does not attempt to charge that wallet, a possible use case is an escrow wallet, the `$allowedWallets` param of the pay method allows you to do just that. ```php $user = auth()->user(); @@ -180,7 +180,7 @@ $user->pay(12.34, ['wallet_1']); #### Transaction Info ([#8][i8]) -In a case where you want to enter descriptions for a particular transaction, the `$detail` property allows you to provide information about why a transaction happened. +In a case where you want to enter descriptions for a particular transaction, the `$detail` param allows you to provide information about why a transaction happened. ```php $user = auth()->user(); From e65544f9abecd320628c42ee66a50bb3e661022d Mon Sep 17 00:00:00 2001 From: Legacy <52163001+3m1n3nc3@users.noreply.github.com> Date: Wed, 3 Jan 2024 07:02:51 +0100 Subject: [PATCH 10/16] Correct log_reference_prefix config property key --- src/Traits/BalanceOperation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 89e08a7..2a063d7 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -45,7 +45,7 @@ protected function createLog($logType, $value, ?string $detail = null): void $refGen = config('pay-pocket.log_reference_generator', [Str::class, 'random', [12]]); - $reference = config('pay-pocket.reference_string_prefix', ''); + $reference = config('pay-pocket.log_reference_prefix', ''); $reference .= isset($refGen[0], $refGen[1]) ? $refGen[0]::{$refGen[1]}(...$refGen[2] ?? []) From a37deaf3f0f2f4e44f7674e455cd378e75689eff Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 09:50:29 +0100 Subject: [PATCH 11/16] Add type anotations --- src/Traits/BalanceOperation.php | 26 ++++++++++++++++++++++---- src/Traits/HandlesDeposit.php | 14 ++++++++++++-- src/Traits/HandlesPayment.php | 5 ++++- src/Traits/HasWallet.php | 12 ++++++++++-- src/Traits/ManagesWallet.php | 4 +++- 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 89e08a7..44017d1 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -9,7 +9,9 @@ trait BalanceOperation protected $createdLog; /** - * Check if Balance is more than zero. + * Check if Balance is more than zero. + * + * @return bool */ public function hasBalance(): bool { @@ -17,7 +19,12 @@ public function hasBalance(): bool } /** - * Decrement Balance and create a log entry. + * Decrement Balance and create a log entry. + * + * @var int|float $value + * @var ?string $detail + * + * @return void */ public function decrementAndCreateLog($value, ?string $detail = null): void { @@ -26,7 +33,12 @@ public function decrementAndCreateLog($value, ?string $detail = null): void } /** - * Increment Balance and create a log entry. + * Increment Balance and create a log entry. + * + * @var int|float $value + * @var ?string $detail + * + * @return void */ public function incrementAndCreateLog($value, ?string $detail = null): void { @@ -35,7 +47,13 @@ public function incrementAndCreateLog($value, ?string $detail = null): void } /** - * Create a new log record + * Create a new log record + * + * @var string $logType + * @var int|float $value + * @var ?string $detail + * + * @return void */ protected function createLog($logType, $value, ?string $detail = null): void { diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index 718405b..dd604f1 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -14,6 +14,12 @@ trait HandlesDeposit { /** * Deposit an amount to the user's wallet of a specific type. + * + * @var string $type + * @var int|float $amount + * @var ?string $detail + * + * @return void */ public function deposit(string $type, int|float $amount, ?string $detail = null): bool { @@ -38,6 +44,8 @@ public function deposit(string $type, int|float $amount, ?string $detail = null) /** * Get depositable types from WalletEnums. + * + * @return void */ private function getDepositableTypes(): array { @@ -52,7 +60,9 @@ private function getDepositableTypes(): array /** * Check if the given tyep is valid. * - * @param string $type + * @var string $type + * @var array $depositable + * * @return bool */ private function isRequestValid($type, array $depositable) @@ -63,4 +73,4 @@ private function isRequestValid($type, array $depositable) return true; } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index e1862dc..954a561 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -10,6 +10,9 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * + * @var int|float $orderValue + * @var array $allowedWallets + * @var ?string $detail * * @throws InsufficientBalanceException */ @@ -49,4 +52,4 @@ public function pay(int|float $orderValue, array $allowedWallets = [], ?string $ } }); } -} \ No newline at end of file +} diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index d30ca89..d0884a5 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -22,7 +22,9 @@ public function wallets() } /** - * Get User's Wallet Balance + * Get User's Wallet Balance + * + * @return int|float */ public function getWalletBalanceAttribute() { @@ -39,11 +41,14 @@ public function getWalletBalanceAttribute() } return $totalBalance; - } /** * Check if User's wallet balance is more than given value + * + * @var int|float $value + * + * @return bool */ public function hasSufficientBalance($value): bool { @@ -53,6 +58,9 @@ public function hasSufficientBalance($value): bool /** * Get the balance of a specific wallet type. * + * + * @var string $walletType + * * @return float|int */ public function getWalletBalanceByType(string $walletType) diff --git a/src/Traits/ManagesWallet.php b/src/Traits/ManagesWallet.php index 13fcaa8..b6f5dc6 100644 --- a/src/Traits/ManagesWallet.php +++ b/src/Traits/ManagesWallet.php @@ -4,5 +4,7 @@ trait ManagesWallet { - use HandlesDeposit, HandlesPayment, HasWallet; + use HandlesDeposit; + use HandlesPayment; + use HasWallet; } From 3962fc0c9726baff09ae4f0193e5cf4a00a1f2a0 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 10:23:12 +0100 Subject: [PATCH 12/16] Improve type annotations --- src/Facades/LaravelPayPocket.php | 5 +++ src/Interfaces/WalletOperations.php | 22 +++++++++++-- src/Services/PocketServices.php | 50 +++++++++++++++++++++++++---- src/Traits/BalanceOperation.php | 16 ++++----- src/Traits/HandlesDeposit.php | 12 +++---- src/Traits/HandlesPayment.php | 7 ++-- src/Traits/HasWallet.php | 4 +-- tests/Models/User.php | 3 +- 8 files changed, 91 insertions(+), 28 deletions(-) diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 1c33b5f..02568e7 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -6,6 +6,11 @@ /** * @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices + * + * @method void pay($user, $orderValue, array $allowedWallets = [], ?string $detail = null) + * @method bool deposit(string $type, int|float $amount, ?string $detail = null) + * @method int|float checkBalance() + * @method int|float walletBalanceByType($user, string $type) */ class LaravelPayPocket extends Facade { diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 42742ef..d6958f5 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -10,7 +10,25 @@ public function getWalletBalanceByType(string $walletType); public function hasSufficientBalance($value): bool; - public function pay(int|float $orderValue); + /** + * Pay the order value from the user's wallets. + * + * @param int|float $orderValue + * @param array $allowedWallets + * @param ?string $detail + * + * @throws InsufficientBalanceException + */ + public function pay(int|float $orderValue, array $allowedWallets = [], ?string $detail = null): void; - public function deposit(string $type, int|float $amount): bool; + /** + * Deposit an amount to the user's wallet of a specific type. + * + * @param string $type + * @param int|float $amount + * @param ?string $detail + * + * @return bool + */ + public function deposit(string $type, int|float $amount, ?string $detail = null): bool; } diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index 552af80..43fd785 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -4,23 +4,61 @@ class PocketServices { - public function deposit($user, $type, $amount, ?string $detail = null) + /** + * Deposit an amount to the user's wallet of a specific type. + * + * @param $user + * @param string $type + * @param int|float $amount + * @param ?string $detail + * + * @return bool + */ + public function deposit($user, string $type, int|float $amount, ?string $detail = null): bool { return $user->deposit($type, $amount, $detail); } - public function pay($user, $orderValue, array $restrictedWallets = [], ?string $detail = null) + /** + * Pay the order value from the user's wallets. + * + * @param $user + * @param int|float $orderValue + * @param array $allowedWallets + * @param ?string $detail + * + * @return void + * @throws InsufficientBalanceException + */ + public function pay($user, $orderValue, array $allowedWallets = [], ?string $detail = null): void { - return $user->pay($orderValue, $restrictedWallets, $detail); + return $user->pay($orderValue, $allowedWallets, $detail); } - public function checkBalance($user) + /** + * Get the balance of the user. + * + * + * @param $user + * + * @return float|int + */ + public function checkBalance($user): float|int { return $user->walletBalance; } - public function walletBalanceByType($user, $type) + /** + * Get the balance of a specific wallet type. + * + * + * @param $user + * @param string $type + * + * @return float|int + */ + public function walletBalanceByType($user, string $type): float|int { return $user->getWalletBalanceByType($type); } -} \ No newline at end of file +} diff --git a/src/Traits/BalanceOperation.php b/src/Traits/BalanceOperation.php index 45e0f3b..a08ae70 100644 --- a/src/Traits/BalanceOperation.php +++ b/src/Traits/BalanceOperation.php @@ -21,8 +21,8 @@ public function hasBalance(): bool /** * Decrement Balance and create a log entry. * - * @var int|float $value - * @var ?string $detail + * @param int|float $value + * @param ?string $detail * * @return void */ @@ -35,8 +35,8 @@ public function decrementAndCreateLog($value, ?string $detail = null): void /** * Increment Balance and create a log entry. * - * @var int|float $value - * @var ?string $detail + * @param int|float $value + * @param ?string $detail * * @return void */ @@ -49,9 +49,9 @@ public function incrementAndCreateLog($value, ?string $detail = null): void /** * Create a new log record * - * @var string $logType - * @var int|float $value - * @var ?string $detail + * @param string $logType + * @param int|float $value + * @param ?string $detail * * @return void */ @@ -82,4 +82,4 @@ protected function createLog($logType, $value, ?string $detail = null): void $this->createdLog->changeStatus('Done'); } -} +} \ No newline at end of file diff --git a/src/Traits/HandlesDeposit.php b/src/Traits/HandlesDeposit.php index dd604f1..b7a310c 100644 --- a/src/Traits/HandlesDeposit.php +++ b/src/Traits/HandlesDeposit.php @@ -15,11 +15,11 @@ trait HandlesDeposit /** * Deposit an amount to the user's wallet of a specific type. * - * @var string $type - * @var int|float $amount - * @var ?string $detail + * @param string $type + * @param int|float $amount + * @param ?string $detail * - * @return void + * @return bool */ public function deposit(string $type, int|float $amount, ?string $detail = null): bool { @@ -45,7 +45,7 @@ public function deposit(string $type, int|float $amount, ?string $detail = null) /** * Get depositable types from WalletEnums. * - * @return void + * @return array */ private function getDepositableTypes(): array { @@ -73,4 +73,4 @@ private function isRequestValid($type, array $depositable) return true; } -} \ No newline at end of file +} diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 954a561..6a632a1 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -10,10 +10,11 @@ trait HandlesPayment /** * Pay the order value from the user's wallets. * - * @var int|float $orderValue - * @var array $allowedWallets - * @var ?string $detail + * @param int|float $orderValue + * @param array $allowedWallets + * @param ?string $detail * + * @return void * @throws InsufficientBalanceException */ public function pay(int|float $orderValue, array $allowedWallets = [], ?string $detail = null): void diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index d0884a5..d1b803f 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -46,7 +46,7 @@ public function getWalletBalanceAttribute() /** * Check if User's wallet balance is more than given value * - * @var int|float $value + * @param int|float $value * * @return bool */ @@ -59,7 +59,7 @@ public function hasSufficientBalance($value): bool * Get the balance of a specific wallet type. * * - * @var string $walletType + * @param string $walletType * * @return float|int */ diff --git a/tests/Models/User.php b/tests/Models/User.php index 7a10991..c9a560a 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -11,7 +11,8 @@ class User extends Authenticatable implements WalletOperations { - use HasFactory, Notifiable; + use HasFactory; + use Notifiable; use ManagesWallet; /** From caa59248ffa262370f09650020adcfef3e7be696 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 10:27:57 +0100 Subject: [PATCH 13/16] Further improve type annotations. --- src/Interfaces/WalletOperations.php | 26 +++++++++++++++++++++++--- src/Traits/HasWallet.php | 4 ++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index d6958f5..a8198d4 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -4,11 +4,31 @@ interface WalletOperations { - public function getWalletBalanceAttribute(); + /** + * Get User's Wallet Balance + * + * @return int|float + */ + public function getWalletBalanceAttribute(): int|float; - public function getWalletBalanceByType(string $walletType); + /** + * Get the balance of a specific wallet type. + * + * + * @param string $walletType + * + * @return float|int + */ + public function getWalletBalanceByType(string $walletType): float|int; - public function hasSufficientBalance($value): bool; + /** + * Check if User's wallet balance is more than given value + * + * @param int|float $value + * + * @return bool + */ + public function hasSufficientBalance(int|float $value): bool; /** * Pay the order value from the user's wallets. diff --git a/src/Traits/HasWallet.php b/src/Traits/HasWallet.php index d1b803f..b9de74d 100644 --- a/src/Traits/HasWallet.php +++ b/src/Traits/HasWallet.php @@ -26,7 +26,7 @@ public function wallets() * * @return int|float */ - public function getWalletBalanceAttribute() + public function getWalletBalanceAttribute(): int|float { $totalBalance = 0; @@ -63,7 +63,7 @@ public function hasSufficientBalance($value): bool * * @return float|int */ - public function getWalletBalanceByType(string $walletType) + public function getWalletBalanceByType(string $walletType): float|int { if (! WalletEnums::isValid($walletType)) { throw new InvalidWalletTypeException("Invalid wallet type '{$walletType}'."); From 5c1de806c5e18a04901dde8556a23be7e7c53d12 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 10:33:11 +0100 Subject: [PATCH 14/16] Fix Facade anotations --- src/Facades/LaravelPayPocket.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 02568e7..5836313 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -8,8 +8,8 @@ * @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices * * @method void pay($user, $orderValue, array $allowedWallets = [], ?string $detail = null) - * @method bool deposit(string $type, int|float $amount, ?string $detail = null) - * @method int|float checkBalance() + * @method bool deposit($user, string $type, int|float $amount, ?string $detail = null) + * @method int|float checkBalance($user) * @method int|float walletBalanceByType($user, string $type) */ class LaravelPayPocket extends Facade From 260da62c1e71959ade6f5108d0f7f87c73f3cca5 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 10:35:14 +0100 Subject: [PATCH 15/16] Fix Facade annotations --- src/Facades/LaravelPayPocket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 5836313..74509aa 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -7,7 +7,7 @@ /** * @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices * - * @method void pay($user, $orderValue, array $allowedWallets = [], ?string $detail = null) + * @method void pay($user, int|float $orderValue, array $allowedWallets = [], ?string $detail = null) * @method bool deposit($user, string $type, int|float $amount, ?string $detail = null) * @method int|float checkBalance($user) * @method int|float walletBalanceByType($user, string $type) From 356ed6c7b89e94be2aabfca57d6704b5df97c501 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 3 Jan 2024 10:37:50 +0100 Subject: [PATCH 16/16] Add more annotation fixes to Facade --- src/Facades/LaravelPayPocket.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Facades/LaravelPayPocket.php b/src/Facades/LaravelPayPocket.php index 74509aa..06f8ce6 100644 --- a/src/Facades/LaravelPayPocket.php +++ b/src/Facades/LaravelPayPocket.php @@ -7,10 +7,10 @@ /** * @see \HPWebdeveloper\LaravelPayPocket\Services\PocketServices * - * @method void pay($user, int|float $orderValue, array $allowedWallets = [], ?string $detail = null) - * @method bool deposit($user, string $type, int|float $amount, ?string $detail = null) - * @method int|float checkBalance($user) - * @method int|float walletBalanceByType($user, string $type) + * @method static void pay($user, int|float $orderValue, array $allowedWallets = [], ?string $detail = null) + * @method static bool deposit($user, string $type, int|float $amount, ?string $detail = null) + * @method static int|float checkBalance($user) + * @method static int|float walletBalanceByType($user, string $type) */ class LaravelPayPocket extends Facade {