Skip to content

Commit

Permalink
feat: add ability to replace UnacceptedTransactionException with 'fal…
Browse files Browse the repository at this point in the history
…se' on insufficient wallet
  • Loading branch information
muath-ye committed Dec 21, 2023
1 parent 4580637 commit 58f829b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ $user->wallet->forceWithdraw(500);
$user->wallet->balance; // -24.8990
```

> If you don't want [UnacceptedTransactionException] you can change the `wallet.disable_insufficient_exception` config value to disable the exception or set `WALLET_DISABLE_INSUFFICIENT_EXCEPTION` to `true` on your `.env`
## Advanced Usage

You can easily add meta information to the transactions to suit your needs. For example:
Expand Down
14 changes: 14 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,18 @@
|
*/
'auto_recalculate_balance' => env('WALLET_AUTO_RECALCULATE_BALANCE', false),

/*
|--------------------------------------------------------------------------
| Throwing Exception for Insufficient Balance
|--------------------------------------------------------------------------
|
| This value is to disable exception handling when the request withdraw
| amount is greater than current user balance on his wallet, if you
| disabled the exception handling, the withdraw process will not proceeded
| and the return will be false the balance will stay as is. default value
| is false.
|
*/
'disable_insufficient_exception' => env('WALLET_DISABLE_INSUFFICIENT_EXCEPTION', false),
];
6 changes: 5 additions & 1 deletion src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,11 @@ public function withdraw($amount, $meta = [], $type = 'withdraw', $shouldAccept
]);

if (!$accepted) {
throw new UnacceptedTransactionException($transaction, 'Withdrawal not accepted due to insufficient funds!');
return config('wallet.disable_insufficient_exception')
? false
: throw new UnacceptedTransactionException(
$transaction, 'Withdrawal not accepted due to insufficient funds!'
);
}
$this->refresh();
return $transaction;
Expand Down

0 comments on commit 58f829b

Please sign in to comment.