AutoBilling failure mailer

This commit is contained in:
David Bomba 2021-02-01 22:26:42 +11:00
parent 5bc8f4d1dc
commit 2ab99e8132
6 changed files with 223 additions and 5 deletions

View File

@ -0,0 +1,109 @@
<?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\AutoBillingFailureObject;
use App\Mail\Admin\EntityNotificationMailer;
use App\Mail\Admin\PaymentFailureObject;
use App\Models\User;
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 AutoBillingFailureMailer extends BaseMailerJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels, UserNotifies;
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->client = $client;
$this->error = $error;
$this->company = $company;
$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();
//iterate through company_users
$this->company->company_users->each(function ($company_user) {
//determine if this user has the right permissions
$methods = $this->findCompanyUserNotificationType($company_user, ['payment_failure','all_notifications']);
//if mail is a method type -fire mail!!
if (($key = array_search('mail', $methods)) !== false) {
unset($methods[$key]);
$mail_obj = (new AutoBillingFailureObject($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($company_user->user->email)
->send(new EntityNotificationMailer($mail_obj));
} catch (\Exception $e) {
//$this->failed($e);
$this->logMailError($e->getMessage(), $this->client);
}
}
});
}
}

View File

@ -69,7 +69,6 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
*/
public function handle()
{
nlog("payment failure mailer ");
/*If we are migrating data we don't want to fire these notification*/
if ($this->company->is_disabled) {
@ -86,7 +85,7 @@ class PaymentFailureMailer extends BaseMailerJob implements ShouldQueue
$this->company->company_users->each(function ($company_user) {
//determine if this user has the right permissions
$methods = $this->findCompanyUserNotificationType($company_user, ['payment_failure']);
$methods = $this->findCompanyUserNotificationType($company_user, ['payment_failure','all_notifications']);
//if mail is a method type -fire mail!!

View File

@ -0,0 +1,105 @@
<?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 AutoBillingFailureObject
{
use MakesHash;
public $client;
public $error;
public $company;
public $payment_hash;
private $invoice;
/**
* 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->invoice = Invoice::where('id', $this->decodePrimarykey($this->payment_hash->invoices()[0]->invoice_id))->first();
$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.auto_bill_failed',
['invoice_number' => $this->invoice->number]
);
}
private function getData()
{
$signature = $this->client->getSetting('email_signature');
$data = [
'title' => ctrans(
'texts.auto_bill_failed',
['invoice_number' => $this->invoice->number]
),
'message' => $this->error,
'signature' => $signature,
'logo' => $this->company->present()->logo(),
'settings' => $this->client->getMergedSettings(),
'whitelabel' => $this->company->account->isPaid() ? true : false,
'url' => config('ninja.app_url'),
'button' => ctrans('texts.login'),
];
return $data;
}
}

View File

@ -54,7 +54,7 @@ class PaymentFailureObject
return
ctrans(
'texts.payment_failed_subject',
['client' => $this->payment->client->present()->name()]
['client' => $this->client->present()->name()]
);
}
@ -78,6 +78,8 @@ class PaymentFailureObject
'logo' => $this->company->present()->logo(),
'settings' => $this->client->getMergedSettings(),
'whitelabel' => $this->company->account->isPaid() ? true : false,
'url' => config('ninja.app_url'),
'button' => ctrans('texts.login'),
];
return $data;

View File

@ -16,6 +16,7 @@ use App\Events\Payment\PaymentWasCreated;
use App\Exceptions\PaymentFailed;
use App\Factory\PaymentFactory;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Mail\AutoBillingFailureMailer;
use App\Jobs\Mail\PaymentFailureMailer;
use App\Jobs\Util\SystemLogger;
use App\Models\Client;
@ -345,11 +346,11 @@ class BaseDriver extends AbstractPaymentDriver
$amount = optional($this->payment_hash->data)->value ?? optional($this->payment_hash->data)->amount;
PaymentFailureMailer::dispatch(
AutoBillingFailureMailer::dispatch(
$gateway->client,
$error,
$gateway->client->company,
$amount
$this->payment_hash
);
SystemLogger::dispatch(

View File

@ -66,6 +66,8 @@ trait UserNotifies
public function findCompanyUserNotificationType($company_user, $required_permissions) :array
{
nlog("find company user notification type");
if ($company_user->company->is_disabled) {
return [];
}