From 64c9d8bb24a67f2976adb0028bc7d147ae618634 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 31 Aug 2021 20:21:29 +1000 Subject: [PATCH 1/4] Fixes for basedriver --- app/PaymentDrivers/BaseDriver.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index 219c851c9c37..9cbb365eeb98 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -389,8 +389,9 @@ class BaseDriver extends AbstractPaymentDriver $invoices->each(function ($invoice) { - if (!$invitation->contact->trashed() && $invitation->contact->send_email && $invitation->contact->email) { - $invoice->service()->deletePdf(); + if (!$invitation->contact->trashed() && $invitation->contact->send_email && $invitation->contact->email) + $invoice->service()->deletePdf(); + }); $invoices->first()->invitations->each(function ($invitation) use ($nmo) { @@ -400,7 +401,10 @@ class BaseDriver extends AbstractPaymentDriver $nmo->to_user = $invitation->contact; NinjaMailerJob::dispatch($nmo); } + }); + + } From c075a81326c9de27750c5d143333317385da1af8 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 31 Aug 2021 21:29:18 +1000 Subject: [PATCH 2/4] Slack notifications for email quotas --- app/Jobs/Account/CreateAccount.php | 3 - .../SendVerificationNotification.php | 1 - app/Models/Account.php | 9 ++ .../Ninja/EmailQuotaNotification.php | 88 +++++++++++++++++++ config/ninja.php | 2 +- 5 files changed, 98 insertions(+), 5 deletions(-) create mode 100644 app/Notifications/Ninja/EmailQuotaNotification.php diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index d45b0423fdbe..4dfe7e0bf2a1 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -114,9 +114,6 @@ class CreateAccount $spaa9f78->fresh(); - //todo implement SLACK notifications - //$sp035a66->notification(new NewAccountCreated($spaa9f78, $sp035a66))->ninja(); - if(Ninja::isHosted()) \Modules\Admin\Jobs\Account\NinjaUser::dispatch([], $sp035a66); diff --git a/app/Listeners/SendVerificationNotification.php b/app/Listeners/SendVerificationNotification.php index cec4f916b0cc..4f6beef409ee 100644 --- a/app/Listeners/SendVerificationNotification.php +++ b/app/Listeners/SendVerificationNotification.php @@ -17,7 +17,6 @@ use App\Jobs\Mail\NinjaMailerObject; use App\Libraries\MultiDB; use App\Mail\Admin\VerifyUserObject; use App\Mail\User\UserAdded; -use App\Notifications\Ninja\VerifyUser; use App\Utils\Ninja; use Exception; use Illuminate\Broadcasting\InteractsWithSockets; diff --git a/app/Models/Account.php b/app/Models/Account.php index 54766d341fa9..5301d90cbd7d 100644 --- a/app/Models/Account.php +++ b/app/Models/Account.php @@ -15,11 +15,13 @@ use App\Jobs\Mail\NinjaMailerJob; use App\Jobs\Mail\NinjaMailerObject; use App\Mail\Ninja\EmailQuotaExceeded; use App\Models\Presenters\AccountPresenter; +use App\Notifications\Ninja\EmailQuotaNotification; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use Carbon\Carbon; use DateTime; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Cache; use Laracasts\Presenter\PresentableTrait; @@ -384,6 +386,10 @@ class Account extends BaseModel if(is_null(Cache::get("throttle_notified:{$this->key}"))) { + App::forgetInstance('translator'); + $t = app('translator'); + $t->replace(Ninja::transformTranslations($this->companies()->first()->settings)); + $nmo = new NinjaMailerObject; $nmo->mailable = new EmailQuotaExceeded($this->companies()->first()); $nmo->company = $this->companies()->first(); @@ -392,6 +398,9 @@ class Account extends BaseModel NinjaMailerJob::dispatch($nmo); Cache::put("throttle_notified:{$this->key}", true, 60 * 24); + + if(config('ninja.notification.slack')) + $this->companies()->first()->notification(new EmailQuotaNotification($this))->ninja(); } return true; diff --git a/app/Notifications/Ninja/EmailQuotaNotification.php b/app/Notifications/Ninja/EmailQuotaNotification.php new file mode 100644 index 000000000000..71c4b00644a0 --- /dev/null +++ b/app/Notifications/Ninja/EmailQuotaNotification.php @@ -0,0 +1,88 @@ +account = $account; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * @return array + */ + public function via($notifiable) + { + return ['slack']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * @return MailMessage + */ + public function toMail($notifiable) + { + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + public function toArray($notifiable) + { + return [ + // + ]; + } + + public function toSlack($notifiable) + { + + $content = "Email quota exceeded by Account {$this->account->key} \n"; + + $owner = $this->account->companies()->first()->owner(); + + $content .= "Owner {$owner->present()->name() } | {$owner->email}"; + + return (new SlackMessage) + ->success() + ->from(ctrans('texts.notification_bot')) + ->image('https://app.invoiceninja.com/favicon.png') + ->content($content); + } +} diff --git a/config/ninja.php b/config/ninja.php index 16d0f9c9be73..1bbd2c67b14e 100644 --- a/config/ninja.php +++ b/config/ninja.php @@ -115,7 +115,7 @@ return [ //'fonts' => 'App\Models\Font', ], 'notification' => [ - 'slack' => env('SLACK_WEBHOOK_URL', ''), + 'slack' => env('SLACK_WEBHOOK_URL', false), 'mail' => env('HOSTED_EMAIL', ''), ], 'themes' => [ From 7a9baae85baa53a1b5f5ac419ad9afc3abf94ac1 Mon Sep 17 00:00:00 2001 From: = Date: Tue, 31 Aug 2021 22:19:30 +1000 Subject: [PATCH 3/4] Fixes for client emails --- app/Models/Presenters/ClientPresenter.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/Models/Presenters/ClientPresenter.php b/app/Models/Presenters/ClientPresenter.php index 824393a7ba11..f7a3e7f265ff 100644 --- a/app/Models/Presenters/ClientPresenter.php +++ b/app/Models/Presenters/ClientPresenter.php @@ -48,7 +48,15 @@ class ClientPresenter extends EntityPresenter public function email() { - return $this->entity->primary_contact->first() !== null ? $this->entity->primary_contact->first()->email : 'No Email Set'; + $primary_contact = $this->entity->primary_contact->first(); + + if($primary_contact && strlen($primary_contact->email) > 1) + return $primary_contact->email; + + $contact = $this->entity->contacts->whereNotNull('email')->first(); + + return $contact ? $contact->email : 'No Email Set'; + } public function address() From 03ed1c3aead67e7ff6993b186271aa1e666e1a90 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 1 Sep 2021 08:22:24 +1000 Subject: [PATCH 4/4] Search for stripe customers by email --- app/PaymentDrivers/StripePaymentDriver.php | 32 ++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index 7231cebbffc4..7deb0b433dcb 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -315,20 +315,36 @@ class StripePaymentDriver extends BaseDriver $client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)->whereCompanyGatewayId($this->company_gateway->id)->first(); + //Search by customer reference if ($client_gateway_token && $client_gateway_token->gateway_customer_reference) { + $customer = Customer::retrieve($client_gateway_token->gateway_customer_reference, $this->stripe_connect_auth); - } else { - $data['name'] = $this->client->present()->name(); - $data['phone'] = $this->client->present()->phone(); - - if (filter_var($this->client->present()->email(), FILTER_VALIDATE_EMAIL)) { - $data['email'] = $this->client->present()->email(); - } + if($customer) + return $customer; - $customer = Customer::create($data, $this->stripe_connect_auth); + } + + //Search by email + $searchResults = \Stripe\Customer::all([ + "email" => $this->client->present()->email(), + "limit" => 2, + "starting_after" => null + ],$this->stripe_connect_auth); + + if(count($searchResults) == 1) + return $searchResults->data[0]; + + //Else create a new record + $data['name'] = $this->client->present()->name(); + $data['phone'] = $this->client->present()->phone(); + + if (filter_var($this->client->present()->email(), FILTER_VALIDATE_EMAIL)) { + $data['email'] = $this->client->present()->email(); } + $customer = Customer::create($data, $this->stripe_connect_auth); + if (!$customer) { throw new Exception('Unable to create gateway customer'); }