Merge pull request #6526 from turbo124/v5-develop

Stripe - Search for customers by email.
This commit is contained in:
David Bomba 2021-09-01 08:22:56 +10:00 committed by GitHub
commit a4042dde9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 137 additions and 16 deletions

View File

@ -114,9 +114,6 @@ class CreateAccount
$spaa9f78->fresh(); $spaa9f78->fresh();
//todo implement SLACK notifications
//$sp035a66->notification(new NewAccountCreated($spaa9f78, $sp035a66))->ninja();
if(Ninja::isHosted()) if(Ninja::isHosted())
\Modules\Admin\Jobs\Account\NinjaUser::dispatch([], $sp035a66); \Modules\Admin\Jobs\Account\NinjaUser::dispatch([], $sp035a66);

View File

@ -17,7 +17,6 @@ use App\Jobs\Mail\NinjaMailerObject;
use App\Libraries\MultiDB; use App\Libraries\MultiDB;
use App\Mail\Admin\VerifyUserObject; use App\Mail\Admin\VerifyUserObject;
use App\Mail\User\UserAdded; use App\Mail\User\UserAdded;
use App\Notifications\Ninja\VerifyUser;
use App\Utils\Ninja; use App\Utils\Ninja;
use Exception; use Exception;
use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\InteractsWithSockets;

View File

@ -15,11 +15,13 @@ use App\Jobs\Mail\NinjaMailerJob;
use App\Jobs\Mail\NinjaMailerObject; use App\Jobs\Mail\NinjaMailerObject;
use App\Mail\Ninja\EmailQuotaExceeded; use App\Mail\Ninja\EmailQuotaExceeded;
use App\Models\Presenters\AccountPresenter; use App\Models\Presenters\AccountPresenter;
use App\Notifications\Ninja\EmailQuotaNotification;
use App\Utils\Ninja; use App\Utils\Ninja;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Carbon\Carbon; use Carbon\Carbon;
use DateTime; use DateTime;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Laracasts\Presenter\PresentableTrait; use Laracasts\Presenter\PresentableTrait;
@ -384,6 +386,10 @@ class Account extends BaseModel
if(is_null(Cache::get("throttle_notified:{$this->key}"))) { 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 = new NinjaMailerObject;
$nmo->mailable = new EmailQuotaExceeded($this->companies()->first()); $nmo->mailable = new EmailQuotaExceeded($this->companies()->first());
$nmo->company = $this->companies()->first(); $nmo->company = $this->companies()->first();
@ -392,6 +398,9 @@ class Account extends BaseModel
NinjaMailerJob::dispatch($nmo); NinjaMailerJob::dispatch($nmo);
Cache::put("throttle_notified:{$this->key}", true, 60 * 24); Cache::put("throttle_notified:{$this->key}", true, 60 * 24);
if(config('ninja.notification.slack'))
$this->companies()->first()->notification(new EmailQuotaNotification($this))->ninja();
} }
return true; return true;

View File

@ -48,7 +48,15 @@ class ClientPresenter extends EntityPresenter
public function email() 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() public function address()

View File

@ -0,0 +1,88 @@
<?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://www.elastic.co/licensing/elastic-license
*/
namespace App\Notifications\Ninja;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class EmailQuotaNotification extends Notification
{
/**
* Create a new notification instance.
*
* @return void
*/
protected $account;
public function __construct($account)
{
$this->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);
}
}

View File

@ -389,8 +389,9 @@ class BaseDriver extends AbstractPaymentDriver
$invoices->each(function ($invoice) { $invoices->each(function ($invoice) {
if (!$invitation->contact->trashed() && $invitation->contact->send_email && $invitation->contact->email) { if (!$invitation->contact->trashed() && $invitation->contact->send_email && $invitation->contact->email)
$invoice->service()->deletePdf(); $invoice->service()->deletePdf();
}); });
$invoices->first()->invitations->each(function ($invitation) use ($nmo) { $invoices->first()->invitations->each(function ($invitation) use ($nmo) {
@ -400,7 +401,10 @@ class BaseDriver extends AbstractPaymentDriver
$nmo->to_user = $invitation->contact; $nmo->to_user = $invitation->contact;
NinjaMailerJob::dispatch($nmo); NinjaMailerJob::dispatch($nmo);
} }
}); });
} }

View File

@ -315,20 +315,36 @@ class StripePaymentDriver extends BaseDriver
$client_gateway_token = ClientGatewayToken::whereClientId($this->client->id)->whereCompanyGatewayId($this->company_gateway->id)->first(); $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) { if ($client_gateway_token && $client_gateway_token->gateway_customer_reference) {
$customer = Customer::retrieve($client_gateway_token->gateway_customer_reference, $this->stripe_connect_auth); $customer = Customer::retrieve($client_gateway_token->gateway_customer_reference, $this->stripe_connect_auth);
} else {
$data['name'] = $this->client->present()->name(); if($customer)
$data['phone'] = $this->client->present()->phone(); return $customer;
if (filter_var($this->client->present()->email(), FILTER_VALIDATE_EMAIL)) {
$data['email'] = $this->client->present()->email();
}
$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) { if (!$customer) {
throw new Exception('Unable to create gateway customer'); throw new Exception('Unable to create gateway customer');
} }

View File

@ -115,7 +115,7 @@ return [
//'fonts' => 'App\Models\Font', //'fonts' => 'App\Models\Font',
], ],
'notification' => [ 'notification' => [
'slack' => env('SLACK_WEBHOOK_URL', ''), 'slack' => env('SLACK_WEBHOOK_URL', false),
'mail' => env('HOSTED_EMAIL', ''), 'mail' => env('HOSTED_EMAIL', ''),
], ],
'themes' => [ 'themes' => [