increment('balance', $amount); return $this->createTransaction($wallet, $amount, 'deposit', $description, $transactionable); }); } /** * برداشت وجه از کیف پول */ public function withdraw(Wallet $wallet, float $amount, string $description = null, $transactionable = null): WalletTransaction { if ($wallet->balance < $amount) { throw new \Exception('موجودی کیف پول کافی نیست.'); } return DB::transaction(function () use ($wallet, $amount, $description, $transactionable) { $wallet->decrement('balance', $amount); // مبلغ برداشت به صورت منفی ثبت می‌شود return $this->createTransaction($wallet, -$amount, 'withdraw', $description, $transactionable); }); } /** * ثبت تراکنش در دیتابیس */ private function createTransaction(Wallet $wallet, float $amount, string $type, ?string $description, $transactionable): WalletTransaction { return WalletTransaction::create([ 'wallet_id' => $wallet->id, 'amount' => $amount, 'type' => $type, 'description' => $description, 'transactionable_type' => $transactionable ? get_class($transactionable) : null, 'transactionable_id' => $transactionable?->id, ]); } }