1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/Jobs/Payment/EmailPayment.php

90 lines
2.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Jobs\Payment;
use App\Events\Invoice\InvoiceWasEmailed;
use App\Events\Invoice\InvoiceWasEmailedAndFailed;
use App\Events\Payment\PaymentWasEmailed;
use App\Events\Payment\PaymentWasEmailedAndFailed;
use App\Helpers\Email\BuildEmail;
2020-09-14 07:14:37 +02:00
use App\Jobs\Mail\BaseMailerJob;
use App\Jobs\Utils\SystemLogger;
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;
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;
2020-09-14 07:14:37 +02:00
class EmailPayment extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $payment;
public $email_builder;
private $contact;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Payment $payment, $email_builder, $contact)
{
$this->payment = $payment;
$this->email_builder = $email_builder;
$this->contact = $contact;
}
/**
* Execute the job.
*
*
* @return void
*/
public function handle()
{
if ($this->contact->email) {
2020-09-14 07:14:37 +02:00
MultiDB::setDb($this->payment->company->db); //this may fail if we don't pass the serialized object with the company record
//todo fix!!
//if we need to set an email driver do it now
$this->setMailDriver();
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));
if (count(Mail::failures()) > 0) {
2020-07-08 14:02:16 +02:00
event(new PaymentWasEmailedAndFailed($this->payment, Mail::failures(), Ninja::eventVars()));
2020-02-15 12:49:31 +01:00
return $this->logMailError(Mail::failures());
}
//fire any events
2020-07-08 14:02:16 +02:00
event(new PaymentWasEmailed($this->payment, $this->payment->company, Ninja::eventVars()));
//sleep(5);
}
}
private function logMailError($errors)
{
SystemLogger::dispatch(
$errors,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_SEND,
SystemLog::TYPE_FAILURE,
$this->payment->client
);
}
}