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); +});