From 055596cfe69f3ce625a401b206082259b4c513e8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 7 Jun 2023 16:25:41 +1000 Subject: [PATCH] Fixes for admin URLs --- app/Jobs/Inventory/AdjustProductInventory.php | 4 +-- app/Jobs/Mail/PaymentFailedMailer.php | 2 +- .../Invoice/InvoiceEmailedNotification.php | 2 +- app/Listeners/Payment/PaymentNotification.php | 13 ++++---- .../PurchaseOrderEmailedNotification.php | 2 +- .../Quote/QuoteEmailedNotification.php | 2 +- app/Mail/Admin/EntityPaidObject.php | 5 +-- .../Admin/InventoryNotificationObject.php | 33 +++++++------------ app/Mail/Admin/PaymentFailureObject.php | 28 ++-------------- app/Models/Client.php | 5 +++ app/Models/Payment.php | 6 ++++ app/Models/Product.php | 5 +++ app/PaymentDrivers/StripePaymentDriver.php | 2 +- 13 files changed, 47 insertions(+), 62 deletions(-) diff --git a/app/Jobs/Inventory/AdjustProductInventory.php b/app/Jobs/Inventory/AdjustProductInventory.php index 34d7a6192a9d..8463a10ad7cc 100644 --- a/app/Jobs/Inventory/AdjustProductInventory.php +++ b/app/Jobs/Inventory/AdjustProductInventory.php @@ -144,12 +144,12 @@ class AdjustProductInventory implements ShouldQueue private function notifyStocklevels(Product $product, string $notification_level) { $nmo = new NinjaMailerObject; - $nmo->mailable = new NinjaMailer((new InventoryNotificationObject($product, $notification_level))->build()); $nmo->company = $this->company; $nmo->settings = $this->company->settings; - $this->company->company_users->each(function ($cu) use ($product, $nmo) { + $this->company->company_users->each(function ($cu) use ($product, $nmo, $notification_level) { if ($this->checkNotificationExists($cu, $product, ['inventory_all', 'inventory_user', 'inventory_threshold_all', 'inventory_threshold_user'])) { + $nmo->mailable = new NinjaMailer((new InventoryNotificationObject($product, $notification_level, $cu->portalType()))->build()); $nmo->to_user = $cu->user; NinjaMailerJob::dispatch($nmo); } diff --git a/app/Jobs/Mail/PaymentFailedMailer.php b/app/Jobs/Mail/PaymentFailedMailer.php index 77c70fede9d6..12f3bc6f02b5 100644 --- a/app/Jobs/Mail/PaymentFailedMailer.php +++ b/app/Jobs/Mail/PaymentFailedMailer.php @@ -92,7 +92,7 @@ class PaymentFailedMailer implements ShouldQueue if (($key = array_search('mail', $methods)) !== false) { unset($methods[$key]); - $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $amount, $this->payment_hash))->build(); + $mail_obj = (new PaymentFailureObject($this->client, $this->error, $this->company, $amount, $this->payment_hash, $company_user->portalType()))->build(); $nmo = new NinjaMailerObject; $nmo->mailable = new NinjaMailer($mail_obj); diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 1f37e7938dcb..8221e926dc5e 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -64,7 +64,7 @@ class InvoiceEmailedNotification implements ShouldQueue unset($methods[$key]); $nmo = new NinjaMailerObject; - $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'invoice', $event->template))->build()); + $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'invoice', $event->template, $company_user->portalType()))->build()); $nmo->company = $invoice->company; $nmo->settings = $invoice->company->settings; $nmo->to_user = $user; diff --git a/app/Listeners/Payment/PaymentNotification.php b/app/Listeners/Payment/PaymentNotification.php index 8b1a64d2977e..37623bf8dc75 100644 --- a/app/Listeners/Payment/PaymentNotification.php +++ b/app/Listeners/Payment/PaymentNotification.php @@ -76,7 +76,7 @@ class PaymentNotification implements ShouldQueue unset($methods[$key]); $nmo = new NinjaMailerObject; - $nmo->mailable = new NinjaMailer((new EntityPaidObject($payment))->build()); + $nmo->mailable = new NinjaMailer((new EntityPaidObject($payment, $company_user->portalType()))->build()); $nmo->company = $event->company; $nmo->settings = $event->company->settings; $nmo->to_user = $user; @@ -108,7 +108,7 @@ class PaymentNotification implements ShouldQueue unset($methods[$key]); $nmo = new NinjaMailerObject; - $nmo->mailable = new NinjaMailer((new EntityPaidObject($payment))->build()); + $nmo->mailable = new NinjaMailer((new EntityPaidObject($payment, $company_user->portalType()))->build()); $nmo->company = $event->company; $nmo->settings = $event->company->settings; $nmo->to_user = $user; @@ -161,11 +161,12 @@ class PaymentNotification implements ShouldQueue } /** - * @param $data + * @param string $url */ - private function sendAnalytics($data) - { - $data = utf8_encode($data); + private function sendAnalytics($url) + { + $data = mb_convert_encoding($url, 'UTF-8'); + // $data = utf8_encode($data); $curl = curl_init(); $opts = [ diff --git a/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php b/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php index 61aa8787c917..2130a62cd51f 100644 --- a/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php +++ b/app/Listeners/PurchaseOrder/PurchaseOrderEmailedNotification.php @@ -62,7 +62,7 @@ class PurchaseOrderEmailedNotification implements ShouldQueue unset($methods[$key]); $nmo = new NinjaMailerObject; - $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'purchase_order', 'purchase_order'))->build()); + $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'purchase_order', 'purchase_order', $company_user->portalType()))->build()); $nmo->company = $purchase_order->company; $nmo->settings = $purchase_order->company->settings; $nmo->to_user = $user; diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php index 2a1c3788feb0..68b84d75806d 100644 --- a/app/Listeners/Quote/QuoteEmailedNotification.php +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -54,7 +54,7 @@ class QuoteEmailedNotification implements ShouldQueue unset($methods[$key]); $nmo = new NinjaMailerObject; - $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'quote', $event->template))->build()); + $nmo->mailable = new NinjaMailer((new EntitySentObject($event->invitation, 'quote', $event->template, $company_user->portalType()))->build()); $nmo->company = $quote->company; $nmo->settings = $quote->company->settings; $nmo->to_user = $user; diff --git a/app/Mail/Admin/EntityPaidObject.php b/app/Mail/Admin/EntityPaidObject.php index ef7edd284e33..99863d1b1df0 100644 --- a/app/Mail/Admin/EntityPaidObject.php +++ b/app/Mail/Admin/EntityPaidObject.php @@ -30,7 +30,7 @@ class EntityPaidObject public $settings; - public function __construct(public Payment $payment) + public function __construct(public Payment $payment, protected bool $use_react_url) { $this->payment = $payment; $this->company = $payment->company; @@ -98,7 +98,7 @@ class EntityPaidObject 'invoice' => $invoice_texts, ] ), - 'url' => config('ninja.app_url'), + 'url' => $this->payment->portalUrl($this->use_react_url), 'button' => ctrans('texts.view_payment'), 'signature' => $settings->email_signature, 'logo' => $this->company->present()->logo(), @@ -117,4 +117,5 @@ class EntityPaidObject return $signature; } + } diff --git a/app/Mail/Admin/InventoryNotificationObject.php b/app/Mail/Admin/InventoryNotificationObject.php index 3a414c8dacdb..2bdbc9ef916b 100644 --- a/app/Mail/Admin/InventoryNotificationObject.php +++ b/app/Mail/Admin/InventoryNotificationObject.php @@ -12,7 +12,6 @@ namespace App\Mail\Admin; -use App\Models\Company; use App\Models\Product; use App\Utils\Ninja; use Illuminate\Support\Facades\App; @@ -20,19 +19,9 @@ use stdClass; class InventoryNotificationObject { - public Product $product; - - public Company $company; - - public $settings; - - public string $notification_level; - - public function __construct(Product $product, string $notification_level) + + public function __construct(protected Product $product, public string $notification_level, protected bool $use_react_url) { - $this->product = $product; - $this->company = $product->company; - $this->settings = $this->company->settings; } public function build() @@ -41,16 +30,16 @@ class InventoryNotificationObject /* Init a new copy of the translator*/ $t = app('translator'); /* Set the locale*/ - App::setLocale($this->company->getLocale()); + App::setLocale($this->product->company->getLocale()); /* Set customized translations _NOW_ */ - $t->replace(Ninja::transformTranslations($this->company->settings)); + $t->replace(Ninja::transformTranslations($this->product->company->settings)); $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; + $mail_obj->tag = $this->product->company->company_key; return $mail_obj; } @@ -79,12 +68,12 @@ class InventoryNotificationObject 'product' => $this->product->product_key.': '.$this->product->notes, ] ), - 'url' => config('ninja.app_url'), - 'button' => ctrans('texts.login'), - 'signature' => $this->settings->email_signature, - 'logo' => $this->company->present()->logo(), - 'settings' => $this->settings, - 'whitelabel' => $this->company->account->isPaid() ? true : false, + 'url' => $this->product->portalUrl($this->use_react_url), + 'button' => $this->use_react_url ? ctrans('texts.product_library') : ctrans('ninja.app_url'), + 'signature' => $this->product->company->settings->email_signature, + 'logo' => $this->product->company->present()->logo(), + 'settings' => $this->product->company->settings, + 'whitelabel' => $this->product->company->account->isPaid() ? true : false, ]; return $data; diff --git a/app/Mail/Admin/PaymentFailureObject.php b/app/Mail/Admin/PaymentFailureObject.php index 3d0297cd212f..313a0fb70293 100644 --- a/app/Mail/Admin/PaymentFailureObject.php +++ b/app/Mail/Admin/PaymentFailureObject.php @@ -24,17 +24,6 @@ class PaymentFailureObject { use MakesHash; - public Client $client; - - public string $error; - - public Company $company; - - public $amount; - - public ?PaymentHash $payment_hash; - // private $invoices; - /** * Create a new job instance. * @@ -43,19 +32,8 @@ class PaymentFailureObject * @param $company * @param $amount */ - public function __construct(Client $client, string $error, Company $company, $amount, ?PaymentHash $payment_hash) + public function __construct(public Client $client, public string $error, public Company $company, public float $amount, public ?PaymentHash $payment_hash, protected bool $use_react_url) { - $this->client = $client; - - $this->error = $error; - - $this->company = $company; - - $this->amount = $amount; - - $this->company = $company; - - $this->payment_hash = $payment_hash; } public function build() @@ -115,8 +93,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'), + 'url' => $this->client->portalUrl($this->use_react_url), + 'button' => $this->use_react_url ? ctrans('texts.view_client') : ctrans('texts.login'), 'additional_info' => $this->error, ]; diff --git a/app/Models/Client.php b/app/Models/Client.php index da032e1c8724..9d5fb64bfa2c 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -865,4 +865,9 @@ class Client extends BaseModel implements HasLocalePreference { return ctrans('texts.client'); } + + public function portalUrl(bool $use_react_url): string + { + return $use_react_url ? config('ninja.react_url'). "/clients/{$this->hashed_id}": config('ninja.app_url'); + } } diff --git a/app/Models/Payment.php b/app/Models/Payment.php index 27cd529483ac..346de07b85b9 100644 --- a/app/Models/Payment.php +++ b/app/Models/Payment.php @@ -557,4 +557,10 @@ class Payment extends BaseModel { return ctrans('texts.payment'); } + + public function portalUrl($use_react_url) + { + return $use_react_url ? config('ninja.react_url')."/payments/{$this->hashed_id}/edit" : config('ninja.app_url'); + } + } diff --git a/app/Models/Product.php b/app/Models/Product.php index 98c7af855c30..fa9cc1f7a970 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -200,4 +200,9 @@ class Product extends BaseModel return $converter->convert($this->notes); } + + public function portalUrl($use_react_url): string + { + return $use_react_url ? config('ninja.react_url') . "/products/{$this->hashed_id}/edit" : config('ninja.app_url'); + } } diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 70c2f009730a..f575378da66b 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -664,7 +664,7 @@ class StripePaymentDriver extends BaseDriver } public function processWebhookRequest(PaymentWebhookRequest $request) - { + {nlog($request->all()); if ($request->type === 'customer.source.updated') { $ach = new ACH($this); $ach->updateBankAccount($request->all());