Skip to content

Commit

Permalink
Implement allowed wallets feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
3m1n3nc3 committed Jan 11, 2024
1 parent 11bf4a1 commit b2729b8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/Traits/HandlesPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
19 changes: 18 additions & 1 deletion tests/OperationsWithFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});
19 changes: 18 additions & 1 deletion tests/OperationsWithoutFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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);
});

0 comments on commit b2729b8

Please sign in to comment.