|
@@ -0,0 +1,267 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Http\Services;
|
|
|
+
|
|
|
+use App\Models\Invoice;
|
|
|
+use App\Models\FinancialTransaction;
|
|
|
+use Illuminate\Support\Facades\Http;
|
|
|
+use Illuminate\Support\Facades\View;
|
|
|
+
|
|
|
+
|
|
|
+use Barryvdh\DomPDF\Facade\Pdf;
|
|
|
+
|
|
|
+class EmailService
|
|
|
+{
|
|
|
+
|
|
|
+ protected $fromEmailAddress;
|
|
|
+ protected $emailFromName;
|
|
|
+ protected $secret;
|
|
|
+ public $appUrl;
|
|
|
+ public $appInternalName;
|
|
|
+ public $stringMappingConfig;
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->secret = 'b4mC2TeVuZqvcUv';
|
|
|
+ $this->fromEmailAddress = config('app.fromEmailAddress');
|
|
|
+ $this->emailFromName = config('app.emailFromName');
|
|
|
+ $this->appUrl = config('app.url');
|
|
|
+ $this->appInternalName = config('app.internalName');
|
|
|
+ $this->stringMappingConfig = config('constants.' . $this->appInternalName);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function callJava($endPoint, $data)
|
|
|
+ {
|
|
|
+ $data['apiKey'] = $this->secret;
|
|
|
+ $url = config('stag.backendUrl') . $endPoint;
|
|
|
+ $response = Http::asForm()
|
|
|
+ ->withHeaders([
|
|
|
+ 'sessionKey' => request()->cookie('sessionKey')
|
|
|
+ ])
|
|
|
+ ->post($url, $data)
|
|
|
+ ->body();
|
|
|
+
|
|
|
+ return json_decode($response, true);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //Emails
|
|
|
+
|
|
|
+ public function notifyClientOnNewInvoice(Invoice $invoice)
|
|
|
+ {
|
|
|
+ $clientEmailAddress = $invoice->customer->client->email_address;
|
|
|
+ if(!$clientEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $toEmailAddress = $clientEmailAddress;
|
|
|
+ $html = (string) view('emails.templates.invoice-created-email', compact('toEmailAddress', 'invoice', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => 'New Payment Request Added to Your Account',
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $clientEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function notifyClientOnInvoiceDeactivation(Invoice $invoice)
|
|
|
+ {
|
|
|
+ $clientEmailAddress = $invoice->customer->client->email_address;
|
|
|
+ if(!$clientEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $toEmailAddress = $clientEmailAddress;
|
|
|
+ $html = (string) view('emails.templates.invoice-deactivation-email', compact('toEmailAddress', 'invoice', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => 'Payment Request Cancellation Notice',
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $clientEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function notifyClientOnInvoicePayment(Invoice $invoice)
|
|
|
+ {
|
|
|
+ $clientEmailAddress = $invoice->customer->client->email_address;
|
|
|
+ if(!$clientEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $toEmailAddress = $clientEmailAddress;
|
|
|
+ $html = (string) view('emails.templates.invoice-payment-email', compact('toEmailAddress', 'invoice', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => 'Transaction Notification: Service Payment Confirmation',
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $clientEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function notifyClientOnFailedInvoicePayment(Invoice $invoice)
|
|
|
+ {
|
|
|
+ $clientEmailAddress = $invoice->customer->client->email_address;
|
|
|
+ if(!$clientEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $toEmailAddress = $clientEmailAddress;
|
|
|
+ $html = (string) view('emails.templates.invoice-failed-payment-email', compact('toEmailAddress', 'invoice', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => 'Payment Declined: Action Required',
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $clientEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public function notifyClientOnRefundTransaction(FinancialTransaction $transaction)
|
|
|
+ {
|
|
|
+ $clientEmailAddress = $transaction->customer->client->email_address;
|
|
|
+ if(!$clientEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $toEmailAddress = $clientEmailAddress;
|
|
|
+ $html = (string) view('emails.templates.transaction-refund-email', compact('toEmailAddress', 'transaction', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => 'Refund Issued for Payment',
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $clientEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function notifyEmailOnClientChange($client, $message, $toEmailAddres, $toName)
|
|
|
+ {
|
|
|
+ if(config('app.env') !== 'production'){
|
|
|
+ $toEmailAddress = config('app.devEmailAddress');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!$toEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $html = (string) view('emails.templates.notify-on-company-client-change-email', compact('toEmailAddress', 'message', 'toName', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => $client->displayName() . ' Information Update',
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $toEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function notifyOnCompanyClientChange($companyClient, $message, $toEmailAddres, $toName)
|
|
|
+ {
|
|
|
+ if(config('app.env') !== 'production'){
|
|
|
+ $toEmailAddress = config('app.devEmailAddress');
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!$toEmailAddress) return;
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $html = (string) view('emails.templates.notify-on-company-client-change-email', compact('toEmailAddress', 'companyClient', 'message', 'toName', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ $params = [
|
|
|
+ 'subject' => $companyClient->client->displayName() . ' Information Update | ' . $companyClient->company->name,
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $toEmailAddress,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function sendServerError($code)
|
|
|
+ {
|
|
|
+ $toEmailAddress = config('app.devEmailAddress');
|
|
|
+ if(!$toEmailAddress) return;
|
|
|
+
|
|
|
+ $emails = explode(',', $toEmailAddress);
|
|
|
+
|
|
|
+ $appInternalName = $this->appInternalName;
|
|
|
+ $stringMappingConfig = $this->stringMappingConfig;
|
|
|
+ $appUrl = $this->appUrl;
|
|
|
+ $emailFromName = $this->emailFromName;
|
|
|
+ $html = (string) view('emails.templates.error-email', compact('toEmailAddress', 'code', 'appUrl', 'emailFromName', 'appInternalName', 'stringMappingConfig'));
|
|
|
+ $plainText = '';
|
|
|
+
|
|
|
+ foreach($emails as $email){
|
|
|
+ $email = preg_replace('/\s+/', '', $email);
|
|
|
+ $params = [
|
|
|
+ 'subject' => 'Server Error - ' . $code,
|
|
|
+ 'htmlBody' => $html,
|
|
|
+ 'plainTextBody' => $plainText,
|
|
|
+ 'toAddress' => $email,
|
|
|
+ 'fromEmail' => $this->fromEmailAddress,
|
|
|
+ 'fromName' => $this->emailFromName,
|
|
|
+ ];
|
|
|
+
|
|
|
+ $response = $this->callJava('/email/send', $params);
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|