Skip to content

Commit

Permalink
Add allowed wallets param
Browse files Browse the repository at this point in the history
  • Loading branch information
3m1n3nc3 committed Jan 11, 2024
1 parent 218bc2c commit 11bf4a1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Interfaces/WalletOperations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
8 changes: 4 additions & 4 deletions src/Services/PocketServices.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,4 +23,4 @@ public function walletBalanceByType($user, $type)
{
return $user->getWalletBalanceByType($type);
}
}
}
15 changes: 12 additions & 3 deletions src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 11bf4a1

Please sign in to comment.