diff --git a/src/app/Jobs/PaymentEmail.php b/src/app/Jobs/PaymentEmail.php --- a/src/app/Jobs/PaymentEmail.php +++ b/src/app/Jobs/PaymentEmail.php @@ -62,25 +62,49 @@ $this->controller = $wallet->owner; } - $ext_email = $this->controller->getSetting('external_email'); - $cc = []; - - if ($ext_email && $ext_email != $this->controller->email) { - $cc[] = $ext_email; + if (empty($this->controller)) { + return; } if ($this->payment->status == PaymentProvider::STATUS_PAID) { $mail = new \App\Mail\PaymentSuccess($this->payment, $this->controller); + $label = "Success"; } elseif ( $this->payment->status == PaymentProvider::STATUS_EXPIRED || $this->payment->status == PaymentProvider::STATUS_FAILED ) { $mail = new \App\Mail\PaymentFailure($this->payment, $this->controller); + $label = "Failure"; } else { return; } - Mail::to($this->controller->email)->cc($cc)->send($mail); + list($to, $cc) = \App\Mail\Helper::userEmails($this->controller); + + if (!empty($to)) { + try { + Mail::to($to)->cc($cc)->send($mail); + + $msg = sprintf( + "[Payment] %s mail sent for %s (%s)", + $label, + $wallet->id, + empty($cc) ? $to : implode(', ', array_merge([$to], $cc)) + ); + + \Log::info($msg); + } catch (\Exception $e) { + $msg = sprintf( + "[Payment] Failed to send mail for wallet %s (%s): %s", + $wallet->id, + empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), + $e->getMessage() + ); + + \Log::error($msg); + throw $e; + } + } /* // Send the email to all wallet controllers too diff --git a/src/app/Jobs/PaymentMandateDisabledEmail.php b/src/app/Jobs/PaymentMandateDisabledEmail.php --- a/src/app/Jobs/PaymentMandateDisabledEmail.php +++ b/src/app/Jobs/PaymentMandateDisabledEmail.php @@ -60,16 +60,37 @@ $this->controller = $this->wallet->owner; } - $ext_email = $this->controller->getSetting('external_email'); - $cc = []; - - if ($ext_email && $ext_email != $this->controller->email) { - $cc[] = $ext_email; + if (empty($this->controller)) { + return; } $mail = new PaymentMandateDisabled($this->wallet, $this->controller); - Mail::to($this->controller->email)->cc($cc)->send($mail); + list($to, $cc) = \App\Mail\Helper::userEmails($this->controller); + + if (!empty($to)) { + try { + Mail::to($to)->cc($cc)->send($mail); + + $msg = sprintf( + "[PaymentMandateDisabled] Sent mail for %s (%s)", + $this->wallet->id, + empty($cc) ? $to : implode(', ', array_merge([$to], $cc)) + ); + + \Log::info($msg); + } catch (\Exception $e) { + $msg = sprintf( + "[PaymentMandateDisabled] Failed to send mail for wallet %s (%s): %s", + $this->wallet->id, + empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), + $e->getMessage() + ); + + \Log::error($msg); + throw $e; + } + } /* // Send the email to all controllers too diff --git a/src/app/Jobs/WalletCheck.php b/src/app/Jobs/WalletCheck.php --- a/src/app/Jobs/WalletCheck.php +++ b/src/app/Jobs/WalletCheck.php @@ -104,12 +104,12 @@ // TODO: Should we check if the account is already suspended? - $this->sendMail(\App\Mail\NegativeBalance::class); + $label = "Notification sent for"; + + $this->sendMail(\App\Mail\NegativeBalance::class, false, $label); $now = \Carbon\Carbon::now()->toDateTimeString(); $this->wallet->setSetting('balance_warning_initial', $now); - - \Log::info("[WalletCheck] Notification sent for {$this->wallet->owner->email}"); } /** @@ -123,12 +123,12 @@ // TODO: Should we check if the account is already suspended? - $this->sendMail(\App\Mail\NegativeBalanceReminder::class); + $label = "Reminder sent for"; + + $this->sendMail(\App\Mail\NegativeBalanceReminder::class, false, $label); $now = \Carbon\Carbon::now()->toDateTimeString(); $this->wallet->setSetting('balance_warning_reminder', $now); - - \Log::info("[WalletCheck] Reminder sent for {$this->wallet->owner->email}"); } /** @@ -145,8 +145,6 @@ return; } - \Log::info("[WalletCheck] Suspend account {$this->wallet->owner->email}"); - // Suspend the account $this->wallet->owner->suspend(); foreach ($this->wallet->entitlements as $entitlement) { @@ -158,7 +156,9 @@ } } - $this->sendMail(\App\Mail\NegativeBalanceSuspended::class); + $label = "Account suspended"; + + $this->sendMail(\App\Mail\NegativeBalanceSuspended::class, false, $label); $now = \Carbon\Carbon::now()->toDateTimeString(); $this->wallet->setSetting('balance_warning_suspended', $now); @@ -178,12 +178,12 @@ return; } - $this->sendMail(\App\Mail\NegativeBalanceBeforeDelete::class, true); + $label = "Last warning sent for"; + + $this->sendMail(\App\Mail\NegativeBalanceBeforeDelete::class, true, $label); $now = \Carbon\Carbon::now()->toDateTimeString(); $this->wallet->setSetting('balance_warning_before_delete', $now); - - \Log::info("[WalletCheck] Last warning sent for {$this->wallet->owner->email}"); } /** @@ -196,18 +196,28 @@ // and calculate summarized balance from all wallets. // The dirty work will be done by UserObserver if ($this->wallet->owner) { - \Log::info("[WalletCheck] Delete account {$this->wallet->owner->email}"); + $email = $this->wallet->owner->email; + $this->wallet->owner->delete(); + + \Log::info( + sprintf( + "[WalletCheck] Account deleted %s (%s)", + $this->wallet->id, + $email + ) + ); } } /** * Send the email * - * @param string $class Mailable class name - * @param bool $with_external Use users's external email + * @param string $class Mailable class name + * @param bool $with_external Use users's external email + * @param ?string $log_label Log label */ - protected function sendMail($class, $with_external = false): void + protected function sendMail($class, $with_external = false, $log_label = null): void { // TODO: Send the email to all wallet controllers? @@ -218,11 +228,22 @@ if (!empty($to) || !empty($cc)) { try { Mail::to($to)->cc($cc)->send($mail); + + if ($log_label) { + $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 wallet %s (%s): %s", + "[WalletCheck] Failed to send mail for %s (%s): %s", $this->wallet->id, - json_encode(array_merge([$to], $cc)), + empty($cc) ? $to : implode(', ', array_merge([$to], $cc)), $e->getMessage() ); diff --git a/src/tests/Feature/Jobs/PaymentEmailTest.php b/src/tests/Feature/Jobs/PaymentEmailTest.php --- a/src/tests/Feature/Jobs/PaymentEmailTest.php +++ b/src/tests/Feature/Jobs/PaymentEmailTest.php @@ -69,8 +69,8 @@ Mail::assertSent(PaymentSuccess::class, 1); // Assert the mail was sent to the user's email - Mail::assertSent(PaymentSuccess::class, function ($mail) use ($user) { - return $mail->hasTo($user->email) && $mail->hasCc('ext@email.tld'); + Mail::assertSent(PaymentSuccess::class, function ($mail) { + return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); }); $payment->status = PaymentProvider::STATUS_FAILED; @@ -83,8 +83,8 @@ Mail::assertSent(PaymentFailure::class, 1); // Assert the mail was sent to the user's email - Mail::assertSent(PaymentFailure::class, function ($mail) use ($user) { - return $mail->hasTo($user->email) && $mail->hasCc('ext@email.tld'); + Mail::assertSent(PaymentFailure::class, function ($mail) { + return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); }); $payment->status = PaymentProvider::STATUS_EXPIRED; diff --git a/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php b/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php --- a/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php +++ b/src/tests/Feature/Jobs/PaymentMandateDisabledEmailTest.php @@ -56,8 +56,8 @@ Mail::assertSent(PaymentMandateDisabled::class, 1); // Assert the mail was sent to the user's email - Mail::assertSent(PaymentMandateDisabled::class, function ($mail) use ($user) { - return $mail->hasTo($user->email) && $mail->hasCc('ext@email.tld'); + Mail::assertSent(PaymentMandateDisabled::class, function ($mail) { + return $mail->hasTo('ext@email.tld') && !$mail->hasCc('ext@email.tld'); }); } }