diff --git a/app/Ninja/Mailers/UserMailer.php b/app/Ninja/Mailers/UserMailer.php index c509bd66afcb..8e44295cd726 100644 --- a/app/Ninja/Mailers/UserMailer.php +++ b/app/Ninja/Mailers/UserMailer.php @@ -58,12 +58,7 @@ class UserMailer extends Mailer $view = ($notificationType == 'approved' ? ENTITY_QUOTE : ENTITY_INVOICE) . "_{$notificationType}"; $account = $user->account; $client = $invoice->client; - - if ($account->hasMultipleAccounts()) { - $link = url(sprintf('/account/%s?redirect_to=%s', $account->account_key, $invoice->present()->path)); - } else { - $link = $invoice->present()->url; - } + $link = $invoice->present()->multiAccountLink; $data = [ 'entityType' => $entityType, @@ -116,6 +111,26 @@ class UserMailer extends Mailer $this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data); } + /** + * @param Invitation $invitation + */ + public function sendMessage($user, $subject, $message, $invoice) + { + if (! $user->email) { + return; + } + + $view = 'user_message'; + $data = [ + 'userName' => $user->getDisplayName(), + 'primaryMessage' => $subject, + 'secondaryMessage' => $message, + 'invoiceLink' => $invoice->present()->multiAccountLink, + ]; + + $this->sendTo($user->email, CONTACT_EMAIL, CONTACT_NAME, $subject, $view, $data); + } + public function sendSecurityCode($user, $code) { if (! $user->email) { diff --git a/app/Ninja/Presenters/InvoicePresenter.php b/app/Ninja/Presenters/InvoicePresenter.php index d49708e97b7d..f2a63df788de 100644 --- a/app/Ninja/Presenters/InvoicePresenter.php +++ b/app/Ninja/Presenters/InvoicePresenter.php @@ -279,4 +279,18 @@ class InvoicePresenter extends EntityPresenter return ' - ' . $fee . ' ' . $label; } + + public function multiAccountLink() + { + $invoice = $this->entity; + $account = $invoice->account; + + if ($account->hasMultipleAccounts()) { + $link = url(sprintf('/account/%s?redirect_to=%s', $account->account_key, $invoice->present()->path)); + } else { + $link = $invoice->present()->url; + } + + return $link; + } } diff --git a/app/Services/PaymentService.php b/app/Services/PaymentService.php index 1f1f54e713e6..c6e2829d298f 100644 --- a/app/Services/PaymentService.php +++ b/app/Services/PaymentService.php @@ -136,6 +136,13 @@ class PaymentService extends BaseService try { return $paymentDriver->completeOnsitePurchase(false, $paymentMethod); } catch (Exception $exception) { + if (! Auth::check()) { + $subject = trans('texts.auto_bill_failed', ['invoice_number' => $invoice->invoice_number]); + $message = sprintf('%s: %s', ucwords($paymentDriver->providerName()), $exception->getMessage()); + $mailer = app('App\Ninja\Mailers\UserMailer'); + $mailer->sendMessage($invoice->user, $subject, $message, $invoice); + } + return false; } } diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index ad57af57901a..04ccc4789add 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -2433,6 +2433,7 @@ $LANG = array( 'reset_counter' => 'Reset Counter', 'next_reset' => 'Next Reset', 'reset_counter_help' => 'Automatically reset the invoice and quote counters.', + 'auto_bill_failed' => 'Auto-billing for invoice :invoice_number failed', );