Refactoring email

This commit is contained in:
David Bomba 2021-02-02 16:11:33 +11:00
parent 2474507790
commit 1797cac1a9
13 changed files with 281 additions and 36 deletions

View File

@ -111,7 +111,7 @@ class EmailEntity extends BaseMailerJob implements ShouldQueue
->send( ->send(
new TemplateEmail( new TemplateEmail(
$this->email_entity_builder, $this->email_entity_builder,
$this->invitation->contact->client $this->invitation->contact
) )
); );
} catch (\Exception $e) { } catch (\Exception $e) {

View File

@ -0,0 +1,111 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Jobs\Mail;
use App\Libraries\MultiDB;
use App\Mail\Admin\ClientPaymentFailureObject;
use App\Mail\Admin\EntityNotificationMailer;
use App\Mail\Admin\PaymentFailureObject;
use App\Models\Invoice;
use App\Models\User;
use App\Utils\Traits\MakesHash;
use App\Utils\Traits\Notifications\UserNotifies;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;
/*Multi Mailer implemented*/
class ClientPaymentFailureMailer extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies, MakesHash;
public $client;
public $error;
public $company;
public $payment_hash;
public $settings;
/**
* Create a new job instance.
*
* @param $client
* @param $message
* @param $company
* @param $amount
*/
public function __construct($client, $error, $company, $payment_hash)
{
$this->company = $company;
$this->error = $error;
$this->client = $client;
$this->payment_hash = $payment_hash;
$this->company = $company;
$this->settings = $client->getMergedSettings();
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
/*If we are migrating data we don't want to fire these notification*/
if ($this->company->is_disabled) {
return true;
}
//Set DB
MultiDB::setDb($this->company->db);
//if we need to set an email driver do it now
$this->setMailDriver();
$this->invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get();
$this->invoices->first()->invitations->each(function ($invitation) {
if ($invitation->contact->send_email && $invitation->contact->email) {
$mail_obj = (new ClientPaymentFailureObject($this->client, $this->error, $this->company, $this->payment_hash))->build();
$mail_obj->from = [config('mail.from.address'), config('mail.from.name')];
//send email
try {
Mail::to($invitation->contact->email)
->send(new EntityNotificationMailer($mail_obj));
} catch (\Exception $e) {
$this->logMailError($e->getMessage(), $this->client);
}
}
});
}
}

View File

@ -80,7 +80,7 @@ class EmailPayment extends BaseMailerJob implements ShouldQueue
try { try {
$mail = Mail::to($this->contact->email, $this->contact->present()->name()); $mail = Mail::to($this->contact->email, $this->contact->present()->name());
$mail->send(new TemplateEmail($email_builder, $this->contact->client)); $mail->send(new TemplateEmail($email_builder, $this->contact));
} catch (\Exception $e) { } catch (\Exception $e) {
nlog("mailing failed with message " . $e->getMessage()); nlog("mailing failed with message " . $e->getMessage());
event(new PaymentWasEmailedAndFailed($this->payment, $this->company, Mail::failures(), Ninja::eventVars())); event(new PaymentWasEmailedAndFailed($this->payment, $this->company, Mail::failures(), Ninja::eventVars()));

View File

@ -0,0 +1,114 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Mail\Admin;
use App\Models\Invoice;
use App\Utils\Number;
use App\Utils\Traits\MakesHash;
use stdClass;
class ClientPaymentFailureObject
{
use MakesHash;
public $client;
public $error;
public $company;
public $payment_hash;
private $invoices;
/**
* Create a new job instance.
*
* @param $client
* @param $message
* @param $company
* @param $amount
*/
public function __construct($client, $error, $company, $payment_hash)
{
$this->client = $client;
$this->error = $error;
$this->company = $company;
$this->payment_hash = $payment_hash;
$this->company = $company;
}
public function build()
{
$this->invoices = Invoice::whereIn('id', $this->transformKeys(array_column($this->payment_hash->invoices(), 'invoice_id')))->get();
$mail_obj = new stdClass;
$mail_obj->amount = $this->getAmount();
$mail_obj->subject = $this->getSubject();
$mail_obj->data = $this->getData();
$mail_obj->markdown = 'email.admin.generic';
$mail_obj->tag = $this->company->company_key;
return $mail_obj;
}
private function getAmount()
{
return array_sum(array_column($this->payment_hash->invoices(), 'amount')) + $this->payment_hash->fee_total;
}
private function getSubject()
{
return
ctrans(
'texts.notification_invoice_payment_failed_subject',
['invoice' => $this->client->present()->name()]
);
}
private function getData()
{
$signature = $this->client->getSetting('email_signature');
$data = [
'title' => ctrans(
'texts.notification_invoice_payment_failed_subject',
[
'invoice' => $this->invoices->first()->number
]
),
'greeting' => ctrans('texts.email_salutation', ['name' => $this->client->present()->name]),
'message' => $this->error,
'signature' => $signature,
'logo' => $this->company->present()->logo(),
'settings' => $this->client->getMergedSettings(),
'whitelabel' => $this->company->account->isPaid() ? true : false,
'url' => route('client.login'),
'button' => ctrans('texts.login'),
'additional_info' => false
];
return $data;
}
}

View File

@ -31,13 +31,13 @@ class DownloadInvoices extends Mailable
public function build() public function build()
{ {
return $this->from(config('mail.from.address'), config('mail.from.name')) return $this->from(config('mail.from.address'), config('mail.from.name'))
->subject(ctrans('texts.download_files')) ->subject(ctrans('texts.download_files'))
->markdown( ->markdown(
'email.admin.download_files', 'email.admin.download_files',
[ [
'url' => $this->file_path, 'url' => $this->file_path,
'logo' => $this->company->present()->logo, 'logo' => $this->company->present()->logo,
'whitelabel' => $this->company->account->isPaid() ? true : false,
] ]
); );
} }

View File

@ -32,6 +32,7 @@ class MigrationCompleted extends Mailable
{ {
$data['settings'] = $this->company->settings; $data['settings'] = $this->company->settings;
$data['company'] = $this->company; $data['company'] = $this->company;
$data['whitelabel'] = $this->company->account->isPaid() ? true : false;
return $this->from(config('mail.from.address'), config('mail.from.name')) return $this->from(config('mail.from.address'), config('mail.from.name'))
->view('email.import.completed', $data) ->view('email.import.completed', $data)

View File

@ -32,7 +32,6 @@ class MigrationFailed extends Mailable
public function build() public function build()
{ {
return $this->from(config('mail.from.address'), config('mail.from.name')) return $this->from(config('mail.from.address'), config('mail.from.name'))
->view('email.migration.failed'); ->view('email.migration.failed');
} }
} }

View File

@ -25,11 +25,15 @@ class TemplateEmail extends Mailable
private $client; private $client;
public function __construct($build_email, Client $client) private $contact;
public function __construct($build_email, ClientContact $contact)
{ {
$this->build_email = $build_email; $this->build_email = $build_email;
$this->client = $client; $this->contact = $contact;
$this->client = $contact->client;
} }
/** /**
@ -64,12 +68,12 @@ class TemplateEmail extends Mailable
'settings' => $settings, 'settings' => $settings,
]) ])
->view($template_name, [ ->view($template_name, [
'greeting' => ctrans('texts.email_salutation', ['name' => $this->contact->present()->name()]),
'body' => $this->build_email->getBody(), 'body' => $this->build_email->getBody(),
'footer' => $this->build_email->getFooter(), 'footer' => $this->build_email->getFooter(),
'view_link' => $this->build_email->getViewLink(), 'view_link' => $this->build_email->getViewLink(),
'view_text' => $this->build_email->getViewText(), 'view_text' => $this->build_email->getViewText(),
'title' => '', 'title' => '',
// 'title' => $this->build_email->getSubject(),
'signature' => $settings->email_signature, 'signature' => $settings->email_signature,
'settings' => $settings, 'settings' => $settings,
'company' => $company, 'company' => $company,

View File

@ -17,6 +17,7 @@ use App\Exceptions\PaymentFailed;
use App\Factory\PaymentFactory; use App\Factory\PaymentFactory;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Mail\AutoBillingFailureMailer; use App\Jobs\Mail\AutoBillingFailureMailer;
use App\Jobs\Mail\ClientPaymentFailureMailer;
use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Models\Client; use App\Models\Client;
@ -353,6 +354,13 @@ class BaseDriver extends AbstractPaymentDriver
$this->payment_hash $this->payment_hash
); );
ClientPaymentFailureMailer::dispatch(
$gateway->client,
$error,
$gateway->client->company,
$this->payment_hash
);
SystemLogger::dispatch( SystemLogger::dispatch(
$gateway->payment_hash, $gateway->payment_hash,
SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::CATEGORY_GATEWAY_RESPONSE,
@ -364,12 +372,6 @@ class BaseDriver extends AbstractPaymentDriver
throw new PaymentFailed($error, $e->getCode()); throw new PaymentFailed($error, $e->getCode());
} }
public function tokenBillingFailed($gateway, $e)
{
}
/** /**
* Wrapper method for checking if resource is good. * Wrapper method for checking if resource is good.
* *

View File

@ -16,10 +16,11 @@
InvoiceNinja (contact@invoiceninja.com) InvoiceNinja (contact@invoiceninja.com)
@endslot @endslot
@if(!$whitelabel)
@slot('footer') @slot('footer')
@component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '&copy; InvoiceNinja']) @component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '&copy; InvoiceNinja'])
For any info, please visit InvoiceNinja. For any info, please visit InvoiceNinja.
@endcomponent @endcomponent
@endslot @endslot
@endif
@endcomponent @endcomponent

View File

@ -1,10 +1,13 @@
@component('email.template.master', ['design' => 'light', 'settings' => $settings]) @component('email.template.master', ['design' => 'light', 'settings' => $settings])
@slot('header') @slot('header')
@include('email.components.header', ['logo' => $logo]) @include('email.components.header', ['logo' => $logo])
@endslot @endslot
@if($greeting)
<p>{{ $greeting }}</p>
@endif
<p>{{ $title }}</p> <p>{{ $title }}</p>
<p>{{ $message }}</p> <p>{{ $message }}</p>
@ -23,9 +26,11 @@
{{ $signature }} {{ $signature }}
@endslot @endslot
@if(!$whitelabel)
@slot('footer') @slot('footer')
@component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '&copy; InvoiceNinja']) @component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '&copy; InvoiceNinja'])
For any info, please visit InvoiceNinja. For any info, please visit InvoiceNinja.
@endcomponent @endcomponent
@endslot @endslot
@endif
@endcomponent @endcomponent

View File

@ -76,4 +76,12 @@
<a href="{{ url('/') }}" target="_blank" class="button">{{ ctrans('texts.account_login')}}</a> <a href="{{ url('/') }}" target="_blank" class="button">{{ ctrans('texts.account_login')}}</a>
<p>{{ ctrans('texts.email_signature')}}<br/> {{ ctrans('texts.email_from') }}</p> <p>{{ ctrans('texts.email_signature')}}<br/> {{ ctrans('texts.email_from') }}</p>
@if(!$whitelabel)
@slot('footer')
@component('email.components.footer', ['url' => 'https://invoiceninja.com', 'url_text' => '&copy; InvoiceNinja'])
For any info, please visit InvoiceNinja.
@endcomponent
@endslot
@endif
@endcomponent @endcomponent