55use Bavix \Wallet \Exceptions \AmountInvalid ;
66use Bavix \Wallet \Exceptions \BalanceIsEmpty ;
77use Bavix \Wallet \Exceptions \InsufficientFunds ;
8- use Bavix \Wallet \Tax ;
98use Bavix \Wallet \Interfaces \Wallet ;
10- use Bavix \Wallet \Models \Wallet as WalletModel ;
119use Bavix \Wallet \Models \Transaction ;
1210use Bavix \Wallet \Models \Transfer ;
11+ use Bavix \Wallet \Models \Wallet as WalletModel ;
12+ use Bavix \Wallet \Tax ;
1313use Bavix \Wallet \WalletProxy ;
1414use Illuminate \Database \Eloquent \Model ;
1515use Illuminate \Database \Eloquent \Relations \MorphMany ;
@@ -117,7 +117,7 @@ public function canWithdraw(int $amount): bool
117117 */
118118 public function transfer (Wallet $ wallet , int $ amount , ?array $ meta = null ): Transfer
119119 {
120- return DB ::transaction (function () use ($ amount , $ wallet , $ meta ) {
120+ return DB ::transaction (function () use ($ amount , $ wallet , $ meta ) {
121121 $ fee = Tax::fee ($ wallet , $ amount );
122122 $ withdraw = $ this ->withdraw ($ amount + $ fee , $ meta );
123123 $ deposit = $ wallet ->deposit ($ amount , $ meta );
@@ -153,7 +153,7 @@ public function safeTransfer(Wallet $wallet, int $amount, ?array $meta = null):
153153 */
154154 public function forceTransfer (Wallet $ wallet , int $ amount , ?array $ meta = null ): Transfer
155155 {
156- return DB ::transaction (function () use ($ amount , $ wallet , $ meta ) {
156+ return DB ::transaction (function () use ($ amount , $ wallet , $ meta ) {
157157 $ fee = Tax::fee ($ wallet , $ amount );
158158 $ withdraw = $ this ->forceWithdraw ($ amount + $ fee , $ meta );
159159 $ deposit = $ wallet ->deposit ($ amount , $ meta );
@@ -198,21 +198,18 @@ protected function assemble(Wallet $wallet, Transaction $withdraw, Transaction $
198198 */
199199 protected function change (int $ amount , ?array $ meta , bool $ confirmed ): Transaction
200200 {
201- return DB ::transaction (function () use ($ amount , $ meta , $ confirmed ) {
201+ return DB ::transaction (function () use ($ amount , $ meta , $ confirmed ) {
202202
203- if ($ this instanceof WalletModel) {
204- $ payable = $ this ->holder ;
205- $ wallet = $ this ;
206- } else {
207- $ payable = $ this ;
203+ $ wallet = $ this ;
204+ if (!($ this instanceof WalletModel)) {
208205 $ wallet = $ this ->wallet ;
209206 }
210207
211208 if ($ confirmed ) {
212209 $ this ->addBalance ($ wallet , $ amount );
213210 }
214211
215- return $ payable ->transactions ()->create ([
212+ return $ this ->transactions ()->create ([
216213 'type ' => $ amount > 0 ? 'deposit ' : 'withdraw ' ,
217214 'wallet_id ' => $ wallet ->getKey (),
218215 'uuid ' => Uuid::uuid4 ()->toString (),
@@ -230,7 +227,8 @@ protected function change(int $amount, ?array $meta, bool $confirmed): Transacti
230227 */
231228 public function transactions (): MorphMany
232229 {
233- return $ this ->morphMany (config ('wallet.transaction.model ' ), 'payable ' );
230+ return ($ this instanceof WalletModel ? $ this ->holder : $ this )
231+ ->morphMany (config ('wallet.transaction.model ' ), 'payable ' );
234232 }
235233
236234 /**
@@ -241,7 +239,8 @@ public function transactions(): MorphMany
241239 */
242240 public function transfers (): MorphMany
243241 {
244- return $ this ->morphMany (config ('wallet.transfer.model ' ), 'from ' );
242+ return ($ this instanceof WalletModel ? $ this ->holder : $ this )
243+ ->morphMany (config ('wallet.transfer.model ' ), 'from ' );
245244 }
246245
247246 /**
@@ -252,7 +251,8 @@ public function transfers(): MorphMany
252251 */
253252 public function wallet (): MorphOne
254253 {
255- return $ this ->morphOne (config ('wallet.wallet.model ' ), 'holder ' )
254+ return ($ this instanceof WalletModel ? $ this ->holder : $ this )
255+ ->morphOne (config ('wallet.wallet.model ' ), 'holder ' )
256256 ->withDefault ([
257257 'name ' => config ('wallet.wallet.default.name ' ),
258258 'slug ' => config ('wallet.wallet.default.slug ' ),
@@ -289,7 +289,7 @@ public function getBalanceAttribute(): int
289289 if ($ this instanceof WalletModel) {
290290 $ this ->exists or $ this ->save ();
291291 if (!WalletProxy::has ($ this ->getKey ())) {
292- WalletProxy::set ($ this ->getKey (), (int ) ($ this ->attributes ['balance ' ] ?? 0 ));
292+ WalletProxy::set ($ this ->getKey (), (int )($ this ->attributes ['balance ' ] ?? 0 ));
293293 }
294294
295295 return WalletProxy::get ($ this ->getKey ());
0 commit comments