From 11bf4a1e941b3ea718ebe826203d23894d1d42cb Mon Sep 17 00:00:00 2001 From: 3m1n3nc3 Date: Thu, 11 Jan 2024 22:35:37 +0100 Subject: [PATCH] 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; }