1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00

fixes for webhook-handlers

This commit is contained in:
paulwer 2024-02-18 17:50:33 +01:00
parent 4e180db731
commit 33560bd6a6
2 changed files with 61 additions and 50 deletions

View File

@ -52,6 +52,8 @@ class ProcessBrevoWebhook implements ShouldQueue
'events' => [], 'events' => [],
]; ];
private ?Company $company = null;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -86,12 +88,12 @@ class ProcessBrevoWebhook implements ShouldQueue
public function handle() public function handle()
{ {
MultiDB::findAndSetDbByCompanyKey($this->request['tags'][0]); MultiDB::findAndSetDbByCompanyKey($this->request['tags'][0]);
$company = Company::where('company_key', $this->request['tags'][0])->first(); $this->company = Company::where('company_key', $this->request['tags'][0])->first();
$this->invitation = $this->discoverInvitation($this->request['message-id']); $this->invitation = $this->discoverInvitation($this->request['message-id']);
if ($company && $this->request['event'] == 'spam' && config('ninja.notification.slack')) { if ($this->company && $this->request['event'] == 'spam' && config('ninja.notification.slack')) {
$company->notification(new EmailSpamNotification($company))->ninja(); $this->company->notification(new EmailSpamNotification($this->company))->ninja();
} }
if (!$this->invitation) { if (!$this->invitation) {
@ -111,7 +113,7 @@ class ProcessBrevoWebhook implements ShouldQueue
case 'blocked': case 'blocked':
if ($this->request['subject'] == ctrans('texts.confirmation_subject')) { if ($this->request['subject'] == ctrans('texts.confirmation_subject')) {
$company->notification(new EmailBounceNotification($this->request['email']))->ninja(); $this->company->notification(new EmailBounceNotification($this->request['email']))->ninja();
} }
return $this->processBounce(); return $this->processBounce();
@ -419,8 +421,10 @@ class ProcessBrevoWebhook implements ShouldQueue
public function getRawMessage(string $message_id) public function getRawMessage(string $message_id)
{ {
$Brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', config('services.brevo.key'))); $brevo_secret = $this->company->settings->brevo_secret ?? config('services.brevo.key');
$messageDetail = $Brevo->getTransacEmailContent($message_id);
$brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', $brevo_secret));
$messageDetail = $brevo->getTransacEmailContent($message_id);
return $messageDetail; return $messageDetail;
} }
@ -431,7 +435,6 @@ class ProcessBrevoWebhook implements ShouldQueue
$messageDetail = $this->getRawMessage($message_id); $messageDetail = $this->getRawMessage($message_id);
$event = collect($messageDetail->getEvents())->first(function ($event) { $event = collect($messageDetail->getEvents())->first(function ($event) {
return $event?->Details?->BounceID ?? false; return $event?->Details?->BounceID ?? false;

View File

@ -45,7 +45,7 @@ class ProcessPostmarkWebhook implements ShouldQueue
private $entity; private $entity;
private array $default_response = [ private array $default_response = [
'recipients' => '', 'recipients' => '',
'subject' => 'Message not found.', 'subject' => 'Message not found.',
'entity' => '', 'entity' => '',
@ -53,6 +53,8 @@ class ProcessPostmarkWebhook implements ShouldQueue
'events' => [], 'events' => [],
]; ];
private ?Company $company = null;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -64,11 +66,11 @@ class ProcessPostmarkWebhook implements ShouldQueue
private function getSystemLog(string $message_id): ?SystemLog private function getSystemLog(string $message_id): ?SystemLog
{ {
return SystemLog::query() return SystemLog::query()
->where('company_id', $this->invitation->company_id) ->where('company_id', $this->invitation->company_id)
->where('type_id', SystemLog::TYPE_WEBHOOK_RESPONSE) ->where('type_id', SystemLog::TYPE_WEBHOOK_RESPONSE)
->whereJsonContains('log', ['MessageID' => $message_id]) ->whereJsonContains('log', ['MessageID' => $message_id])
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->first(); ->first();
} }
@ -87,12 +89,12 @@ class ProcessPostmarkWebhook implements ShouldQueue
public function handle() public function handle()
{ {
MultiDB::findAndSetDbByCompanyKey($this->request['Tag']); MultiDB::findAndSetDbByCompanyKey($this->request['Tag']);
$company = Company::where('company_key', $this->request['Tag'])->first(); $this->company = Company::where('company_key', $this->request['Tag'])->first();
$this->invitation = $this->discoverInvitation($this->request['MessageID']); $this->invitation = $this->discoverInvitation($this->request['MessageID']);
if ($company && $this->request['RecordType'] == 'SpamComplaint' && config('ninja.notification.slack')) { if ($this->company && $this->request['RecordType'] == 'SpamComplaint' && config('ninja.notification.slack')) {
$company->notification(new EmailSpamNotification($company))->ninja(); $this->company->notification(new EmailSpamNotification($this->company))->ninja();
} }
if (!$this->invitation) { if (!$this->invitation) {
@ -108,8 +110,8 @@ class ProcessPostmarkWebhook implements ShouldQueue
return $this->processDelivery(); return $this->processDelivery();
case 'Bounce': case 'Bounce':
if($this->request['Subject'] == ctrans('texts.confirmation_subject')) { if ($this->request['Subject'] == ctrans('texts.confirmation_subject')) {
$company->notification(new EmailBounceNotification($this->request['Email']))->ninja(); $this->company->notification(new EmailBounceNotification($this->request['Email']))->ninja();
} }
return $this->processBounce(); return $this->processBounce();
@ -169,19 +171,21 @@ class ProcessPostmarkWebhook implements ShouldQueue
$sl = $this->getSystemLog($this->request['MessageID']); $sl = $this->getSystemLog($this->request['MessageID']);
if($sl) { if ($sl) {
$this->updateSystemLog($sl, $data); $this->updateSystemLog($sl, $data);
return; return;
} }
(new SystemLogger( (
$data, new SystemLogger(
SystemLog::CATEGORY_MAIL, $data,
SystemLog::EVENT_MAIL_OPENED, SystemLog::CATEGORY_MAIL,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::EVENT_MAIL_OPENED,
$this->invitation->contact->client, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->invitation->company $this->invitation->contact->client,
))->handle(); $this->invitation->company
)
)->handle();
} }
// { // {
@ -207,19 +211,21 @@ class ProcessPostmarkWebhook implements ShouldQueue
$sl = $this->getSystemLog($this->request['MessageID']); $sl = $this->getSystemLog($this->request['MessageID']);
if($sl) { if ($sl) {
$this->updateSystemLog($sl, $data); $this->updateSystemLog($sl, $data);
return; return;
} }
(new SystemLogger( (
$data, new SystemLogger(
SystemLog::CATEGORY_MAIL, $data,
SystemLog::EVENT_MAIL_DELIVERY, SystemLog::CATEGORY_MAIL,
SystemLog::TYPE_WEBHOOK_RESPONSE, SystemLog::EVENT_MAIL_DELIVERY,
$this->invitation->contact->client, SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->invitation->company $this->invitation->contact->client,
))->handle(); $this->invitation->company
)
)->handle();
} }
// { // {
@ -265,7 +271,7 @@ class ProcessPostmarkWebhook implements ShouldQueue
$sl = $this->getSystemLog($this->request['MessageID']); $sl = $this->getSystemLog($this->request['MessageID']);
if($sl) { if ($sl) {
$this->updateSystemLog($sl, $data); $this->updateSystemLog($sl, $data);
return; return;
} }
@ -316,7 +322,7 @@ class ProcessPostmarkWebhook implements ShouldQueue
$sl = $this->getSystemLog($this->request['MessageID']); $sl = $this->getSystemLog($this->request['MessageID']);
if($sl) { if ($sl) {
$this->updateSystemLog($sl, $data); $this->updateSystemLog($sl, $data);
} }
@ -362,7 +368,7 @@ class ProcessPostmarkWebhook implements ShouldQueue
$messageDetail = $this->getRawMessage($message_id); $messageDetail = $this->getRawMessage($message_id);
$event = collect($messageDetail->messageevents)->first(function ($event) { $event = collect($messageDetail->messageevents)->first(function ($event) {
return $event?->Details?->BounceID ?? false; return $event?->Details?->BounceID ?? false;
@ -374,29 +380,31 @@ class ProcessPostmarkWebhook implements ShouldQueue
private function fetchMessage(): array private function fetchMessage(): array
{ {
if(strlen($this->request['MessageID']) < 1) { if (strlen($this->request['MessageID']) < 1) {
return $this->default_response; return $this->default_response;
} }
try { try {
$postmark = new PostmarkClient(config('services.postmark.token')); $postmark_secret = $this->company->settings->postmark_secret ?? config('services.postmark.token');
$postmark = new PostmarkClient($postmark_secret);
$messageDetail = $postmark->getOutboundMessageDetails($this->request['MessageID']); $messageDetail = $postmark->getOutboundMessageDetails($this->request['MessageID']);
$recipients = collect($messageDetail['recipients'])->flatten()->implode(','); $recipients = collect($messageDetail['recipients'])->flatten()->implode(',');
$subject = $messageDetail->subject ?? ''; $subject = $messageDetail->subject ?? '';
$events = collect($messageDetail->messageevents)->map(function ($event) { $events = collect($messageDetail->messageevents)->map(function ($event) {
return [ return [
'bounce_id' => $event?->Details?->BounceID ?? '', 'bounce_id' => $event?->Details?->BounceID ?? '',
'recipient' => $event->Recipient ?? '', 'recipient' => $event->Recipient ?? '',
'status' => $event->Type ?? '', 'status' => $event->Type ?? '',
'delivery_message' => $event->Details->DeliveryMessage ?? $event->Details->Summary ?? '', 'delivery_message' => $event->Details->DeliveryMessage ?? $event->Details->Summary ?? '',
'server' => $event->Details->DestinationServer ?? '', 'server' => $event->Details->DestinationServer ?? '',
'server_ip' => $event->Details->DestinationIP ?? '', 'server_ip' => $event->Details->DestinationIP ?? '',
'date' => \Carbon\Carbon::parse($event->ReceivedAt)->format('Y-m-d H:i:s') ?? '', 'date' => \Carbon\Carbon::parse($event->ReceivedAt)->format('Y-m-d H:i:s') ?? '',
]; ];
})->toArray(); })->toArray();