From 2ab99e8132fa4f140a8ccae724dc8be19a6901d7 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 1 Feb 2021 22:26:42 +1100 Subject: [PATCH] AutoBilling failure mailer --- app/Jobs/Mail/AutoBillingFailureMailer.php | 109 ++++++++++++++++++ app/Jobs/Mail/PaymentFailureMailer.php | 3 +- app/Mail/Admin/AutoBillingFailureObject.php | 105 +++++++++++++++++ app/Mail/Admin/PaymentFailureObject.php | 4 +- app/PaymentDrivers/BaseDriver.php | 5 +- .../Traits/Notifications/UserNotifies.php | 2 + 6 files changed, 223 insertions(+), 5 deletions(-) create mode 100644 app/Jobs/Mail/AutoBillingFailureMailer.php create mode 100644 app/Mail/Admin/AutoBillingFailureObject.php diff --git a/app/Jobs/Mail/AutoBillingFailureMailer.php b/app/Jobs/Mail/AutoBillingFailureMailer.php new file mode 100644 index 000000000000..3ccd0c420005 --- /dev/null +++ b/app/Jobs/Mail/AutoBillingFailureMailer.php @@ -0,0 +1,109 @@ +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); + } + } + }); + } +} diff --git a/app/Jobs/Mail/PaymentFailureMailer.php b/app/Jobs/Mail/PaymentFailureMailer.php index 27543a98c1d4..e51ebe468719 100644 --- a/app/Jobs/Mail/PaymentFailureMailer.php +++ b/app/Jobs/Mail/PaymentFailureMailer.php @@ -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!! diff --git a/app/Mail/Admin/AutoBillingFailureObject.php b/app/Mail/Admin/AutoBillingFailureObject.php new file mode 100644 index 000000000000..e6cfd14103b5 --- /dev/null +++ b/app/Mail/Admin/AutoBillingFailureObject.php @@ -0,0 +1,105 @@ +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; + } +} diff --git a/app/Mail/Admin/PaymentFailureObject.php b/app/Mail/Admin/PaymentFailureObject.php index e0b58025edf9..b7e693d00bfe 100644 --- a/app/Mail/Admin/PaymentFailureObject.php +++ b/app/Mail/Admin/PaymentFailureObject.php @@ -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; diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index f2c48d6760af..11ff29606185 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -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( diff --git a/app/Utils/Traits/Notifications/UserNotifies.php b/app/Utils/Traits/Notifications/UserNotifies.php index 96a955fc691d..e05689359ee1 100644 --- a/app/Utils/Traits/Notifications/UserNotifies.php +++ b/app/Utils/Traits/Notifications/UserNotifies.php @@ -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 []; }