1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 16:31:33 +02: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' => [],
];
private ?Company $company = null;
/**
* Create a new job instance.
*
@ -86,12 +88,12 @@ class ProcessBrevoWebhook implements ShouldQueue
public function handle()
{
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']);
if ($company && $this->request['event'] == 'spam' && config('ninja.notification.slack')) {
$company->notification(new EmailSpamNotification($company))->ninja();
if ($this->company && $this->request['event'] == 'spam' && config('ninja.notification.slack')) {
$this->company->notification(new EmailSpamNotification($this->company))->ninja();
}
if (!$this->invitation) {
@ -111,7 +113,7 @@ class ProcessBrevoWebhook implements ShouldQueue
case 'blocked':
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();
@ -419,8 +421,10 @@ class ProcessBrevoWebhook implements ShouldQueue
public function getRawMessage(string $message_id)
{
$Brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', config('services.brevo.key')));
$messageDetail = $Brevo->getTransacEmailContent($message_id);
$brevo_secret = $this->company->settings->brevo_secret ?? config('services.brevo.key');
$brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', $brevo_secret));
$messageDetail = $brevo->getTransacEmailContent($message_id);
return $messageDetail;
}
@ -431,7 +435,6 @@ class ProcessBrevoWebhook implements ShouldQueue
$messageDetail = $this->getRawMessage($message_id);
$event = collect($messageDetail->getEvents())->first(function ($event) {
return $event?->Details?->BounceID ?? false;

View File

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