Skip to content

Commit e7f2990

Browse files
committed
Withdraw method improved
1 parent a7b2720 commit e7f2990

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

Diff for: src/Models/Wallet.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,28 +56,28 @@ public function deposit(int $amount, string $description)
5656
});
5757
}
5858

59-
public function withdraw(int $amount, string $description, bool $force = false)
59+
public function withdraw(int $amount, string $description, bool $force = false, $bypass_withdrawable_limit = false)
6060
{
6161
if ($amount < 1) {
6262
throw new Exception("Withdrawl amount must not be less than 1, $amount given.");
6363
}
6464

65-
return DB::transaction(function () use ($amount, $description, $force) {
65+
return DB::transaction(function () use ($amount, $description, $force, $bypass_withdrawable_limit) {
6666
$wallet = $this->lockForUpdate()->find($this->id);
6767

6868
$transaction['ob'] = $wallet->balance;
6969

70-
if ($wallet->balance < $amount && ! $force) {
70+
if ($wallet->balance < $amount && !$force) {
7171
throw new Exception("To withdraw more than wallet balance you need to use force mode.");
7272
}
7373

74-
if ($wallet->withdrawable_balance < $amount) {
74+
if ($wallet->withdrawable_balance < $amount && !$bypass_withdrawable_limit) {
7575
throw new Exception("Withdrawable balance exceeds! Max withdrawable limit is set to {$wallet->withdrawable_balance}.");
7676
}
7777

7878
$this->update([
7979
'balance' => $wallet->balance - $amount,
80-
'withdrawable_balance' => $wallet->withdrawable_balance - $amount,
80+
'withdrawable_balance' => $bypass_withdrawable_limit ? $wallet->withdrawable_balance : $wallet->withdrawable_balance - $amount,
8181
]);
8282

8383
return $this->transactions()->create([

0 commit comments

Comments
 (0)