Changeset View
Changeset View
Standalone View
Standalone View
src/app/Jobs/WalletCheck.php
<?php | <?php | ||||
namespace App\Jobs; | namespace App\Jobs; | ||||
use App\Http\Controllers\API\V4\PaymentsController; | |||||
use App\Wallet; | use App\Wallet; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Illuminate\Bus\Queueable; | use Illuminate\Bus\Queueable; | ||||
use Illuminate\Contracts\Queue\ShouldQueue; | use Illuminate\Contracts\Queue\ShouldQueue; | ||||
use Illuminate\Foundation\Bus\Dispatchable; | use Illuminate\Foundation\Bus\Dispatchable; | ||||
use Illuminate\Queue\InteractsWithQueue; | use Illuminate\Queue\InteractsWithQueue; | ||||
use Illuminate\Queue\SerializesModels; | use Illuminate\Queue\SerializesModels; | ||||
use Illuminate\Support\Facades\Mail; | use Illuminate\Support\Facades\Mail; | ||||
class WalletCheck implements ShouldQueue | class WalletCheck implements ShouldQueue | ||||
{ | { | ||||
use Dispatchable; | use Dispatchable; | ||||
use InteractsWithQueue; | use InteractsWithQueue; | ||||
use Queueable; | use Queueable; | ||||
use SerializesModels; | use SerializesModels; | ||||
public const THRESHOLD_DELETE = 'delete'; | public const THRESHOLD_DELETE = 'delete'; | ||||
public const THRESHOLD_BEFORE_DELETE = 'before_delete'; | public const THRESHOLD_BEFORE_DELETE = 'before_delete'; | ||||
public const THRESHOLD_SUSPEND = 'suspend'; | public const THRESHOLD_SUSPEND = 'suspend'; | ||||
public const THRESHOLD_BEFORE_SUSPEND = 'before_suspend'; | |||||
public const THRESHOLD_REMINDER = 'reminder'; | public const THRESHOLD_REMINDER = 'reminder'; | ||||
public const THRESHOLD_BEFORE_REMINDER = 'before_reminder'; | |||||
public const THRESHOLD_INITIAL = 'initial'; | public const THRESHOLD_INITIAL = 'initial'; | ||||
/** @var int The number of seconds to wait before retrying the job. */ | /** @var int The number of seconds to wait before retrying the job. */ | ||||
public $retryAfter = 10; | public $retryAfter = 10; | ||||
/** @var int How many times retry the job if it fails. */ | /** @var int How many times retry the job if it fails. */ | ||||
public $tries = 5; | public $tries = 5; | ||||
Show All 14 Lines | class WalletCheck implements ShouldQueue | ||||
public function __construct(Wallet $wallet) | public function __construct(Wallet $wallet) | ||||
{ | { | ||||
$this->wallet = $wallet; | $this->wallet = $wallet; | ||||
} | } | ||||
/** | /** | ||||
* Execute the job. | * Execute the job. | ||||
* | * | ||||
* @return void | * @return ?string Executed action (THRESHOLD_*) | ||||
*/ | */ | ||||
public function handle() | public function handle() | ||||
{ | { | ||||
if ($this->wallet->balance >= 0) { | if ($this->wallet->balance >= 0) { | ||||
return; | return null; | ||||
} | } | ||||
$now = Carbon::now(); | $now = Carbon::now(); | ||||
// Delete the account | // Delete the account | ||||
if (self::threshold($this->wallet, self::THRESHOLD_DELETE) < $now) { | if (self::threshold($this->wallet, self::THRESHOLD_DELETE) < $now) { | ||||
$this->deleteAccount(); | $this->deleteAccount(); | ||||
return; | return self::THRESHOLD_DELETE; | ||||
} | } | ||||
// Warn about the upcomming account deletion | // Warn about the upcomming account deletion | ||||
if (self::threshold($this->wallet, self::THRESHOLD_BEFORE_DELETE) < $now) { | if (self::threshold($this->wallet, self::THRESHOLD_BEFORE_DELETE) < $now) { | ||||
$this->warnBeforeDelete(); | $this->warnBeforeDelete(); | ||||
return; | return self::THRESHOLD_BEFORE_DELETE; | ||||
} | } | ||||
// Suspend the account | // Suspend the account | ||||
if (self::threshold($this->wallet, self::THRESHOLD_SUSPEND) < $now) { | if (self::threshold($this->wallet, self::THRESHOLD_SUSPEND) < $now) { | ||||
$this->suspendAccount(); | $this->suspendAccount(); | ||||
return; | return self::THRESHOLD_SUSPEND; | ||||
} | |||||
// Try to top-up the wallet before suspending the account | |||||
if (self::threshold($this->wallet, self::THRESHOLD_BEFORE_SUSPEND) < $now) { | |||||
PaymentsController::topUpWallet($this->wallet); | |||||
return self::THRESHOLD_BEFORE_SUSPEND; | |||||
} | } | ||||
// Send the second reminder | // Send the second reminder | ||||
if (self::threshold($this->wallet, self::THRESHOLD_REMINDER) < $now) { | if (self::threshold($this->wallet, self::THRESHOLD_REMINDER) < $now) { | ||||
$this->secondReminder(); | $this->secondReminder(); | ||||
return; | return self::THRESHOLD_REMINDER; | ||||
} | |||||
// Try to top-up the wallet before the second reminder | |||||
if (self::threshold($this->wallet, self::THRESHOLD_BEFORE_REMINDER) < $now) { | |||||
PaymentsController::topUpWallet($this->wallet); | |||||
return self::THRESHOLD_BEFORE_REMINDER; | |||||
} | } | ||||
// Send the initial reminder | // Send the initial reminder | ||||
if (self::threshold($this->wallet, self::THRESHOLD_INITIAL) < $now) { | if (self::threshold($this->wallet, self::THRESHOLD_INITIAL) < $now) { | ||||
$this->initialReminder(); | $this->initialReminder(); | ||||
return; | return self::THRESHOLD_INITIAL; | ||||
} | } | ||||
return null; | |||||
} | } | ||||
/** | /** | ||||
* Send the initial reminder | * Send the initial reminder | ||||
*/ | */ | ||||
protected function initialReminder() | protected function initialReminder() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_initial')) { | if ($this->wallet->getSetting('balance_warning_initial')) { | ||||
▲ Show 20 Lines • Show All 188 Lines • ▼ Show 20 Lines | public static function threshold(Wallet $wallet, string $type): ?Carbon | ||||
return $negative_since->addDays($delete + $suspend + $remind - $warn); | return $negative_since->addDays($delete + $suspend + $remind - $warn); | ||||
} | } | ||||
// Account suspension | // Account suspension | ||||
if ($type == self::THRESHOLD_SUSPEND) { | if ($type == self::THRESHOLD_SUSPEND) { | ||||
return $negative_since->addDays($suspend + $remind); | return $negative_since->addDays($suspend + $remind); | ||||
} | } | ||||
// A day before account suspension | |||||
if ($type == self::THRESHOLD_BEFORE_SUSPEND) { | |||||
return $negative_since->addDays($suspend + $remind - 1); | |||||
} | |||||
// Second notification | // Second notification | ||||
if ($type == self::THRESHOLD_REMINDER) { | if ($type == self::THRESHOLD_REMINDER) { | ||||
return $negative_since->addDays($remind); | return $negative_since->addDays($remind); | ||||
} | } | ||||
// A day before the second reminder | |||||
if ($type == self::THRESHOLD_BEFORE_REMINDER) { | |||||
return $negative_since->addDays($remind - 1); | |||||
} | |||||
// Initial notification | // Initial notification | ||||
// Give it an hour so the async recurring payment has a chance to be finished | // Give it an hour so the async recurring payment has a chance to be finished | ||||
if ($type == self::THRESHOLD_INITIAL) { | if ($type == self::THRESHOLD_INITIAL) { | ||||
return $negative_since->addHours(1); | return $negative_since->addHours(1); | ||||
} | } | ||||
return null; | return null; | ||||
} | } | ||||
} | } |