From 218bc2c5070ae1cc4a9355f728b70e430711bbb8 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 11 Jan 2024 22:24:38 +0100 Subject: [PATCH 1/6] Update README --- README.md | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 335bc91..ec1b0ea 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ This command will automatically publish the `WalletEnums.php` file into your app ## Updating -If updating from version `<= 1.0.3`, new migration and config files have been added to support the new [Transaction Info Feature](#transaction-info) +If updating from version `<= 1.0.3`, new migration and config files have been added to support the new [Transaction Notes Feature](#transaction-info) Follow the [Installation](#installation) Steps 2 and 3 to update your migrations. @@ -108,7 +108,7 @@ 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, notes: null) +deposit(type: string, amount: float|int, notes: string null) ``` Deposit funds into `wallet_1` @@ -137,19 +137,19 @@ LaravelPayPocket::deposit($user, 'wallet_1', 123.45); Note: `wallet_1` and `wallet_2` must already be defined in the `WalletEnums`. -#### Transaction Info ([#8][i8]) +#### Transaction Notes ([#8][i8]) In a case where you want to enter descriptions for a particular transaction, the `$notes` param allows you to provide information about why a transaction happened. ```php $user = auth()->user(); -$user->deposit('wallet_1', 67.89, 'You ordered pizza.'); +$user->deposit('wallet_1', 67.89, 'You sold pizza.'); ``` ### Pay ```php -pay(amount: 12.34, notes: null) +pay(amount: int, allowedWallets: array [], notes: string null) ``` Pay the value using the total combined balance available across all allowed wallets @@ -168,6 +168,28 @@ $user = auth()->user(); LaravelPayPocket::pay($user, 12.34); ``` +By default the sytem will attempt to pay using all available wallets unless the `allowedWallets` param is provided. + +#### Allowed Wallets ([#8][i8]) + +Sometimes you want to mark certain wallets as allowed so that when the `pay()` method is called, the system does not attempt to charge other wallets, a possible use case is an escrow system, the `$allowedWallets` param of the pay method allows you to do just that. + +```php +$user = auth()->user(); +$user->pay(12.34, ['wallet_1']); +``` + +When the `$allowedWallets` param is provided and is not an empty array, the system would attempt to charge only the wallets specified in the array. + +#### Transaction Notes ([#8][i8]) + +In a case where you want to enter descriptions for a particular transaction, the `$note` param allows you to provide information about why a transaction happened. + +```php +$user = auth()->user(); +$user->pay(12.34, [], 'You ordered pizza.'); +``` + ### Balance - **Wallets** From 11bf4a1e941b3ea718ebe826203d23894d1d42cb Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 11 Jan 2024 22:35:37 +0100 Subject: [PATCH 2/6] Add allowed wallets param --- src/Interfaces/WalletOperations.php | 4 ++-- src/Services/PocketServices.php | 8 ++++---- src/Traits/HandlesPayment.php | 15 ++++++++++++--- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/Interfaces/WalletOperations.php b/src/Interfaces/WalletOperations.php index 47e132d..9e8c2b2 100644 --- a/src/Interfaces/WalletOperations.php +++ b/src/Interfaces/WalletOperations.php @@ -10,7 +10,7 @@ public function getWalletBalanceByType(string $walletType); public function hasSufficientBalance($value): bool; - public function pay(int|float $orderValue, ?string $notes = null); + public function pay(int|float $orderValue, array $allowedWallets = [], ?string $notes = null): void; public function deposit(string $type, int|float $amount, ?string $notes = null): bool; -} +} \ No newline at end of file diff --git a/src/Services/PocketServices.php b/src/Services/PocketServices.php index 91db495..aa609bb 100644 --- a/src/Services/PocketServices.php +++ b/src/Services/PocketServices.php @@ -4,14 +4,14 @@ class PocketServices { - public function deposit($user, $type, $amount, $notes = null) + public function deposit($user, $type, $amount, $notes = null): bool { return $user->deposit($type, $amount, $notes); } - public function pay($user, $orderValue, $notes = null) + public function pay($user, $orderValue, array $allowedWallets = [], ?string $notes = null): void { - return $user->pay($orderValue, $notes); + $user->pay($orderValue, $allowedWallets, $notes); } 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/HandlesPayment.php b/src/Traits/HandlesPayment.php index 4bfa835..7f6714e 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -13,19 +13,28 @@ trait HandlesPayment * * @throws InsufficientBalanceException */ - public function pay(int|float $orderValue, ?string $notes = null): void + public function pay(int|float $orderValue, array $allowedWallets = [], ?string $notes = null): void { if (! $this->hasSufficientBalance($orderValue)) { throw new InsufficientBalanceException('Insufficient balance to cover the order.'); } - DB::transaction(function () use ($orderValue, $notes) { + DB::transaction(function () use ($orderValue, $notes, $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); + + /** + * @var BalanceOperation $wallet + */ foreach ($walletsInOrder as $wallet) { - if (! $wallet || ! $wallet->hasBalance()) { + if (! $wallet || ! $wallet->hasBalance() || !$useWallet($wallet->type->value)) { continue; } From b2729b812cb3cac231ec996b6548bf696aeef421 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 11 Jan 2024 22:53:57 +0100 Subject: [PATCH 3/6] Implement allowed wallets feature. --- src/Traits/HandlesPayment.php | 10 +++++++--- tests/OperationsWithFacadeTest.php | 19 ++++++++++++++++++- tests/OperationsWithoutFacadeTest.php | 19 ++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Traits/HandlesPayment.php b/src/Traits/HandlesPayment.php index 7f6714e..a249d6b 100644 --- a/src/Traits/HandlesPayment.php +++ b/src/Traits/HandlesPayment.php @@ -25,16 +25,20 @@ public function pay(int|float $orderValue, array $allowedWallets = [], ?string $ $walletsInOrder = $this->wallets()->whereIn('type', $this->walletsInOrder())->get(); /** - * @param string $wallet + * @param string|\App\Enums\WalletEnums * @return bool $useWallet * */ - $useWallet = fn ($wallet) => count($allowedWallets) < 1 || in_array($wallet, $allowedWallets); + $useWallet = function (string|\App\Enums\WalletEnums $wallet) use ($allowedWallets) { + return count($allowedWallets) < 1 || + in_array($wallet, $allowedWallets) || + in_array($wallet->value, $allowedWallets); + }; /** * @var BalanceOperation $wallet */ foreach ($walletsInOrder as $wallet) { - if (! $wallet || ! $wallet->hasBalance() || !$useWallet($wallet->type->value)) { + if (! $wallet || ! $wallet->hasBalance() || !$useWallet($wallet->type)) { continue; } diff --git a/tests/OperationsWithFacadeTest.php b/tests/OperationsWithFacadeTest.php index 2421416..36f928e 100644 --- a/tests/OperationsWithFacadeTest.php +++ b/tests/OperationsWithFacadeTest.php @@ -115,7 +115,7 @@ $description = \Illuminate\Support\Str::random(); LaravelPayPocket::deposit($user, $type, 234.56); - LaravelPayPocket::pay($user, 234.56, $description); + LaravelPayPocket::pay($user, 234.56, [$type], $description); expect(WalletsLog::where('notes', $description)->exists())->toBe(true); }); @@ -129,3 +129,20 @@ expect(WalletsLog::whereNotNull('reference')->exists())->toBe(true); }); + +test('only the allowed wallets should be charged.', function () { + + $user = User::factory()->create(); + + $type = 'wallet_1'; + + LaravelPayPocket::deposit($user, $type, 234.56); + LaravelPayPocket::pay($user, 234.56, [$type]); + + $last = $user->wallets() + ->where('type', \App\Enums\WalletEnums::WALLET1) + ->first() + ->logs()->latest()->first(); + + expect($last->value)->toBeFloat(234.56); +}); diff --git a/tests/OperationsWithoutFacadeTest.php b/tests/OperationsWithoutFacadeTest.php index e64143f..28cd5ba 100644 --- a/tests/OperationsWithoutFacadeTest.php +++ b/tests/OperationsWithoutFacadeTest.php @@ -116,7 +116,7 @@ $description = \Illuminate\Support\Str::random(); $user->deposit($type, 234.56); - $user->pay(234.56, $description); + $user->pay(234.56, [$type], $description); expect(WalletsLog::where('notes', $description)->exists())->toBe(true); }); @@ -130,3 +130,20 @@ expect(WalletsLog::whereNotNull('reference')->exists())->toBe(true); }); + +test('only the allowed wallets should be charged.', function () { + + $user = User::factory()->create(); + + $type = 'wallet_1'; + + $user->deposit($type, 234.56); + $user->pay(234.56, [$type]); + + $last = $user->wallets() + ->where('type', \App\Enums\WalletEnums::WALLET1) + ->first() + ->logs()->latest()->first(); + + expect($last->value)->toBeFloat(234.56); +}); From 363b519dd58f1bdb253fb657a5bd640c97c03be8 Mon Sep 17 00:00:00 2001 From: Legacy <52163001+3m1n3nc3@users.noreply.github.com> Date: Tue, 14 May 2024 14:29:36 +0100 Subject: [PATCH 4/6] Update laravel dependencies --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3b503da..6e673d8 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "require": { "php": "^8.1", "spatie/laravel-package-tools": "^1.14.0", - "illuminate/contracts": "^10.0" + "illuminate/contracts": "^11.0" }, "require-dev": { "laravel/pint": "^1.0", From c3ce1fd6b39f2bb31a650db3924c056081d2f7f1 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 26 Jun 2024 21:19:31 +0100 Subject: [PATCH 5/6] Remove all .DS_stores and add to .gitignore --- tests/ExceptionsWithoutFacadeTest.php | 3 --- tests/TestCase.php | 12 ++++++------ 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/ExceptionsWithoutFacadeTest.php b/tests/ExceptionsWithoutFacadeTest.php index 5226676..e904c8c 100644 --- a/tests/ExceptionsWithoutFacadeTest.php +++ b/tests/ExceptionsWithoutFacadeTest.php @@ -59,8 +59,5 @@ $user = User::factory()->create(); - $type = 'wallet_1'; - $user->getWalletBalanceByType('wallet_2'); - })->throws(WalletNotFoundException::class); diff --git a/tests/TestCase.php b/tests/TestCase.php index 1dfaf4c..ec89c99 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,16 +22,16 @@ public function getEnvironmentSetUp($app) $migration->up(); */ - $migration = include __DIR__.'/database/migrations/create_users_tables.php'; + $migration = include __DIR__ . '/database/migrations/create_users_tables.php'; $migration->up(); - $migration = include __DIR__.'/../database/migrations/create_wallets_logs_table.php.stub'; + $migration = include __DIR__ . '/../database/migrations/create_wallets_logs_table.php.stub'; $migration->up(); - $migration = include __DIR__.'/../database/migrations/create_wallets_table.php.stub'; + $migration = include __DIR__ . '/../database/migrations/create_wallets_table.php.stub'; $migration->up(); - $migration = include __DIR__.'/../database/migrations/add_notes_and_reference_columns_to_wallets_logs_table.php.stub'; + $migration = include __DIR__ . '/../database/migrations/add_notes_and_reference_columns_to_wallets_logs_table.php.stub'; $migration->up(); } @@ -46,9 +46,9 @@ protected function setUp(): void */ Factory::guessFactoryNamesUsing( - fn (string $modelName) => 'HPWebdeveloper\\LaravelPayPocket\\Tests\\Database\\Factories\\'.class_basename( + fn (string $modelName) => 'HPWebdeveloper\\LaravelPayPocket\\Tests\\Database\\Factories\\' . class_basename( $modelName - ).'Factory' + ) . 'Factory' ); } From 4033ae0ce09ff9adc4f88806d54ac06933dd6544 Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Wed, 26 Jun 2024 21:19:33 +0100 Subject: [PATCH 6/6] Remove all .DS_Store and add to git ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a7f372d..4dc59c3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea .phpunit.cache +.DS_Store build composer.lock coverage