diff --git a/src/app/Observers/UserObserver.php b/src/app/Observers/UserObserver.php --- a/src/app/Observers/UserObserver.php +++ b/src/app/Observers/UserObserver.php @@ -116,18 +116,6 @@ if (\App\Tenant::getConfig($user->tenant_id, 'pgp.enable')) { \App\Jobs\PGP\KeyDeleteJob::dispatch($user->id, $user->email); } - - // Debit the reseller's wallet with the user negative balance - $balance = 0; - foreach ($user->wallets as $wallet) { - // Note: here we assume all user wallets are using the same currency. - // It might get changed in the future - $balance += $wallet->balance; - } - - if ($balance < 0 && $user->tenant && ($wallet = $user->tenant->wallet())) { - $wallet->debit($balance * -1, "Deleted user {$user->email}"); - } } } diff --git a/src/app/Wallet.php b/src/app/Wallet.php --- a/src/app/Wallet.php +++ b/src/app/Wallet.php @@ -119,14 +119,15 @@ // Calculate cost, fee, and end of period [$cost, $fee, $endDate] = $this->entitlementCosts($entitlement, $trial); - // Note: Degraded pays nothing, but we get the money from a tenant. - // Therefore $cost = 0, but $profit < 0. - if ($isDegraded) { - $cost = 0; + // No balance changes for degraded users + if (!$isDegraded) { + $charges += $cost; + // limit profit by wallet balance so a reseller can't build profit by users building debt + $profit += min($cost, $this->balance); + // We always take the full fee + $profit -= $fee; } - $charges += $cost; - $profit += $cost - $fee; // if we're in dry-run, you know... if (!$apply) { @@ -562,7 +563,8 @@ } $charges += $cost; - $profit += $cost - $fee; + $profit += min($cost, $this->balance); + $profit -= $fee; if ($cost == 0) { continue; @@ -619,6 +621,14 @@ $amount = abs($amount) * -1; } + if ($this->balance < 0 && ($this->balance + $amount) > 0) { + // We already took our fee when the wallet went below 0 + // TODO a bonus topup could result in reseller wallet balance, which we don't want I think. + // But I suppose that's a fundamental of paying out a percentage of wallet credit, and not payments. + $negativeBalance = abs($this->balance); + $this->addTenantProfit($negativeBalance); + } + $this->balance += $amount; $this->save();