2020-02-12 10:21:06 +01:00
|
|
|
<?php
|
2020-02-15 10:01:15 +01:00
|
|
|
|
2020-02-12 10:21:06 +01:00
|
|
|
namespace App\Jobs\Payment;
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Events\Invoice\InvoiceWasEmailed;
|
|
|
|
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
|
2020-02-12 10:21:06 +01:00
|
|
|
use App\Events\Payment\PaymentWasEmailed;
|
|
|
|
use App\Events\Payment\PaymentWasEmailedAndFailed;
|
2020-02-15 10:01:15 +01:00
|
|
|
use App\Helpers\Email\BuildEmail;
|
|
|
|
use App\Jobs\Utils\SystemLogger;
|
2020-02-12 10:21:06 +01:00
|
|
|
use App\Libraries\MultiDB;
|
|
|
|
use App\Mail\TemplateEmail;
|
|
|
|
use App\Models\Company;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\SystemLog;
|
2020-07-08 14:02:16 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-02-12 10:21:06 +01:00
|
|
|
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;
|
|
|
|
|
|
|
|
class EmailPayment implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $payment;
|
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
public $email_builder;
|
|
|
|
|
|
|
|
private $contact;
|
2020-02-12 10:21:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-03-21 06:37:30 +01:00
|
|
|
public function __construct(Payment $payment, $email_builder, $contact)
|
|
|
|
{
|
2020-02-12 10:21:06 +01:00
|
|
|
$this->payment = $payment;
|
2020-02-15 10:01:15 +01:00
|
|
|
$this->email_builder = $email_builder;
|
|
|
|
$this->contact = $contact;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-02-12 10:21:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2020-02-15 10:01:15 +01:00
|
|
|
if ($this->contact->email) {
|
|
|
|
Mail::to($this->contact->email, $this->contact->present()->name())
|
2020-02-15 12:49:31 +01:00
|
|
|
->send(new TemplateEmail($this->email_builder, $this->contact->user, $this->contact->customer));
|
2020-02-12 10:21:06 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
if (count(Mail::failures()) > 0) {
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new PaymentWasEmailedAndFailed($this->payment, Mail::failures(), Ninja::eventVars()));
|
2020-02-12 10:21:06 +01:00
|
|
|
|
2020-02-15 12:49:31 +01:00
|
|
|
return $this->logMailError(Mail::failures());
|
2020-02-15 10:01:15 +01:00
|
|
|
}
|
2020-02-12 10:21:06 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
//fire any events
|
2020-07-08 14:02:16 +02:00
|
|
|
event(new PaymentWasEmailed($this->payment, $this->payment->company, Ninja::eventVars()));
|
2020-02-12 10:21:06 +01:00
|
|
|
|
2020-02-15 10:01:15 +01:00
|
|
|
//sleep(5);
|
|
|
|
}
|
2020-02-12 10:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function logMailError($errors)
|
|
|
|
{
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$errors,
|
|
|
|
SystemLog::CATEGORY_MAIL,
|
|
|
|
SystemLog::EVENT_MAIL_SEND,
|
|
|
|
SystemLog::TYPE_FAILURE,
|
|
|
|
$this->payment->client
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|