Changeset View
Changeset View
Standalone View
Standalone View
src/app/Console/Commands/Wallet/ChargeCommand.php
Show All 38 Lines | public function handle() | ||||
if (!$wallet->owner) { | if (!$wallet->owner) { | ||||
$this->error("Wallet's owner is deleted."); | $this->error("Wallet's owner is deleted."); | ||||
return 1; | return 1; | ||||
} | } | ||||
$wallets = [$wallet]; | $wallets = [$wallet]; | ||||
} else { | } else { | ||||
// Get all wallets, excluding deleted accounts | // Get all wallets, excluding deleted accounts | ||||
$wallets = \App\Wallet::select('wallets.*') | $wallets = \App\Wallet::select('wallets.id') | ||||
->join('users', 'users.id', '=', 'wallets.user_id') | ->join('users', 'users.id', '=', 'wallets.user_id') | ||||
->withEnvTenantContext('users') | ->withEnvTenantContext('users') | ||||
->whereNull('users.deleted_at') | ->whereNull('users.deleted_at') | ||||
->cursor(); | ->cursor(); | ||||
} | } | ||||
foreach ($wallets as $wallet) { | foreach ($wallets as $wallet) { | ||||
// This is a long-running process. Because another process might have modified | |||||
// the wallet balance in meantime we have to refresh it. | |||||
// Note: This is needed despite the use of cursor() above. | |||||
$wallet->refresh(); | |||||
// Sanity check after refresh (owner deleted in meantime) | |||||
if (!$wallet->owner) { | |||||
continue; | |||||
} | |||||
$charge = $wallet->chargeEntitlements(); | $charge = $wallet->chargeEntitlements(); | ||||
if ($charge > 0) { | if ($charge > 0) { | ||||
$this->info( | $this->info( | ||||
"Charged wallet {$wallet->id} for user {$wallet->owner->email} with {$charge}" | "Charged wallet {$wallet->id} for user {$wallet->owner->email} with {$charge}" | ||||
); | ); | ||||
// Top-up the wallet if auto-payment enabled for the wallet | // Top-up the wallet if auto-payment enabled for the wallet | ||||
Show All 11 Lines |