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\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; | |||||
class WalletCheck implements ShouldQueue | class WalletCheck implements ShouldQueue | ||||
{ | { | ||||
use Dispatchable; | use Dispatchable; | ||||
use InteractsWithQueue; | use InteractsWithQueue; | ||||
use Queueable; | use Queueable; | ||||
use SerializesModels; | use SerializesModels; | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | class WalletCheck implements ShouldQueue | ||||
protected function initialReminder() | protected function initialReminder() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_initial')) { | if ($this->wallet->getSetting('balance_warning_initial')) { | ||||
return; | return; | ||||
} | } | ||||
// TODO: Should we check if the account is already suspended? | // TODO: Should we check if the account is already suspended? | ||||
$label = "Notification sent for"; | $this->sendMail(\App\Mail\NegativeBalance::class, false); | ||||
$this->sendMail(\App\Mail\NegativeBalance::class, false, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_initial', $now); | $this->wallet->setSetting('balance_warning_initial', $now); | ||||
} | } | ||||
/** | /** | ||||
* Send the second reminder | * Send the second reminder | ||||
*/ | */ | ||||
protected function secondReminder() | protected function secondReminder() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_reminder')) { | if ($this->wallet->getSetting('balance_warning_reminder')) { | ||||
return; | return; | ||||
} | } | ||||
// TODO: Should we check if the account is already suspended? | // TODO: Should we check if the account is already suspended? | ||||
$label = "Reminder sent for"; | $this->sendMail(\App\Mail\NegativeBalanceReminder::class, false); | ||||
$this->sendMail(\App\Mail\NegativeBalanceReminder::class, false, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_reminder', $now); | $this->wallet->setSetting('balance_warning_reminder', $now); | ||||
} | } | ||||
/** | /** | ||||
* Suspend the account (and send the warning) | * Suspend the account (and send the warning) | ||||
*/ | */ | ||||
Show All 14 Lines | protected function suspendAccount() | ||||
if ( | if ( | ||||
$entitlement->entitleable_type == \App\Domain::class | $entitlement->entitleable_type == \App\Domain::class | ||||
|| $entitlement->entitleable_type == \App\User::class | || $entitlement->entitleable_type == \App\User::class | ||||
) { | ) { | ||||
$entitlement->entitleable->suspend(); | $entitlement->entitleable->suspend(); | ||||
} | } | ||||
} | } | ||||
$label = "Account suspended"; | $this->sendMail(\App\Mail\NegativeBalanceSuspended::class, true); | ||||
$this->sendMail(\App\Mail\NegativeBalanceSuspended::class, true, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_suspended', $now); | $this->wallet->setSetting('balance_warning_suspended', $now); | ||||
} | } | ||||
/** | /** | ||||
* Send the last warning before delete | * Send the last warning before delete | ||||
*/ | */ | ||||
protected function warnBeforeDelete() | protected function warnBeforeDelete() | ||||
{ | { | ||||
if ($this->wallet->getSetting('balance_warning_before_delete')) { | if ($this->wallet->getSetting('balance_warning_before_delete')) { | ||||
return; | return; | ||||
} | } | ||||
// Sanity check, already deleted | // Sanity check, already deleted | ||||
if (!$this->wallet->owner) { | if (!$this->wallet->owner) { | ||||
return; | return; | ||||
} | } | ||||
$label = "Last warning sent for"; | $this->sendMail(\App\Mail\NegativeBalanceBeforeDelete::class, true); | ||||
$this->sendMail(\App\Mail\NegativeBalanceBeforeDelete::class, true, $label); | |||||
$now = \Carbon\Carbon::now()->toDateTimeString(); | $now = \Carbon\Carbon::now()->toDateTimeString(); | ||||
$this->wallet->setSetting('balance_warning_before_delete', $now); | $this->wallet->setSetting('balance_warning_before_delete', $now); | ||||
} | } | ||||
/** | /** | ||||
* Delete the account | * Delete the account | ||||
*/ | */ | ||||
Show All 18 Lines | protected function deleteAccount() | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Send the email | * Send the email | ||||
* | * | ||||
* @param string $class Mailable class name | * @param string $class Mailable class name | ||||
* @param bool $with_external Use users's external email | * @param bool $with_external Use users's external email | ||||
* @param ?string $log_label Log label | |||||
*/ | */ | ||||
protected function sendMail($class, $with_external = false, $log_label = null): void | protected function sendMail($class, $with_external = false): void | ||||
{ | { | ||||
// TODO: Send the email to all wallet controllers? | // TODO: Send the email to all wallet controllers? | ||||
$mail = new $class($this->wallet, $this->wallet->owner); | $mail = new $class($this->wallet, $this->wallet->owner); | ||||
list($to, $cc) = \App\Mail\Helper::userEmails($this->wallet->owner, $with_external); | list($to, $cc) = \App\Mail\Helper::userEmails($this->wallet->owner, $with_external); | ||||
if (!empty($to) || !empty($cc)) { | if (!empty($to) || !empty($cc)) { | ||||
try { | $params = [ | ||||
Mail::to($to)->cc($cc)->send($mail); | 'to' => $to, | ||||
'cc' => $cc, | |||||
if ($log_label) { | 'add' => " for {$this->wallet->id}", | ||||
$msg = sprintf( | ]; | ||||
"[WalletCheck] %s %s (%s)", | |||||
$log_label, | |||||
$this->wallet->id, | |||||
empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), | |||||
); | |||||
\Log::info($msg); | |||||
} | |||||
} catch (\Exception $e) { | |||||
$msg = sprintf( | |||||
"[WalletCheck] Failed to send mail for %s (%s): %s", | |||||
$this->wallet->id, | |||||
empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), | |||||
$e->getMessage() | |||||
); | |||||
\Log::error($msg); | \App\Mail\Helper::sendMail($mail, $this->wallet->owner->tenant_id, $params); | ||||
throw $e; | |||||
} | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Get the date-time for an action threshold. Calculated using | * Get the date-time for an action threshold. Calculated using | ||||
* the date when a wallet balance turned negative. | * the date when a wallet balance turned negative. | ||||
* | * | ||||
* @param \App\Wallet $wallet A wallet | * @param \App\Wallet $wallet A wallet | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |