1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/Jobs/PostMark/ProcessPostmarkWebhook.php

446 lines
14 KiB
PHP
Raw Normal View History

2022-01-16 04:11:48 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-01-16 04:11:48 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Jobs\PostMark;
2023-12-16 23:50:35 +01:00
use App\Models\Company;
use App\Models\SystemLog;
2023-10-26 04:57:44 +02:00
use App\Libraries\MultiDB;
2023-12-16 23:50:35 +01:00
use Postmark\PostmarkClient;
use Illuminate\Bus\Queueable;
use App\Jobs\Util\SystemLogger;
use App\Models\QuoteInvitation;
2022-01-16 04:11:48 +01:00
use App\Models\CreditInvitation;
use App\Models\InvoiceInvitation;
2023-12-16 23:50:35 +01:00
use Illuminate\Queue\SerializesModels;
use Turbo124\Beacon\Facades\LightLogs;
2022-08-17 09:18:30 +02:00
use App\Models\PurchaseOrderInvitation;
2023-12-16 23:50:35 +01:00
use Illuminate\Queue\InteractsWithQueue;
2022-01-16 04:11:48 +01:00
use App\Models\RecurringInvoiceInvitation;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2023-12-16 23:50:35 +01:00
use App\DataMapper\Analytics\Mail\EmailSpam;
use App\DataMapper\Analytics\Mail\EmailBounce;
use App\Notifications\Ninja\EmailSpamNotification;
use App\Notifications\Ninja\EmailBounceNotification;
2022-01-16 04:11:48 +01:00
class ProcessPostmarkWebhook implements ShouldQueue
{
2024-01-14 05:05:00 +01:00
use Dispatchable;
use InteractsWithQueue;
use Queueable;
use SerializesModels;
2022-01-16 04:11:48 +01:00
public $tries = 1;
public $invitation;
2023-08-07 07:30:34 +02:00
2023-08-23 06:36:55 +02:00
private $entity;
2024-02-18 17:50:33 +01:00
private array $default_response = [
2023-08-23 06:36:55 +02:00
'recipients' => '',
'subject' => 'Message not found.',
'entity' => '',
'entity_id' => '',
'events' => [],
];
2023-08-22 12:42:01 +02:00
2024-02-18 17:50:33 +01:00
private ?Company $company = null;
2022-01-16 04:11:48 +01:00
/**
* Create a new job instance.
*
*/
2023-08-07 07:30:34 +02:00
public function __construct(private array $request)
2022-01-16 04:11:48 +01:00
{
}
private function getSystemLog(string $message_id): ?SystemLog
{
return SystemLog::query()
2024-02-18 17:50:33 +01:00
->where('company_id', $this->invitation->company_id)
->where('type_id', SystemLog::TYPE_WEBHOOK_RESPONSE)
->whereJsonContains('log', ['MessageID' => $message_id])
->orderBy('id', 'desc')
->first();
}
private function updateSystemLog(SystemLog $system_log, array $data): void
{
$system_log->log = $data;
$system_log->save();
}
2022-01-16 04:11:48 +01:00
/**
* Execute the job.
*
*/
public function handle()
{
MultiDB::findAndSetDbByCompanyKey($this->request['Tag']);
2024-07-14 13:28:54 +02:00
$this->company = Company::query()->where('company_key', $this->request['Tag'])->first(); /** @phpstan-ignore-line */
2024-01-14 05:05:00 +01:00
2022-01-16 04:11:48 +01:00
$this->invitation = $this->discoverInvitation($this->request['MessageID']);
2024-02-18 17:50:33 +01:00
if ($this->company && $this->request['RecordType'] == 'SpamComplaint' && config('ninja.notification.slack')) {
$this->company->notification(new EmailSpamNotification($this->company))->ninja();
2023-12-16 23:50:35 +01:00
}
2023-02-16 02:36:09 +01:00
if (!$this->invitation) {
2022-01-16 04:11:48 +01:00
return;
2023-02-16 02:36:09 +01:00
}
2022-01-16 04:11:48 +01:00
2023-02-16 02:36:09 +01:00
if (array_key_exists('Details', $this->request)) {
2022-02-13 11:06:40 +01:00
$this->invitation->email_error = $this->request['Details'];
2023-02-16 02:36:09 +01:00
}
2024-01-14 05:05:00 +01:00
2023-02-16 02:36:09 +01:00
switch ($this->request['RecordType']) {
2022-01-16 04:11:48 +01:00
case 'Delivery':
return $this->processDelivery();
case 'Bounce':
2024-02-18 17:50:33 +01:00
if ($this->request['Subject'] == ctrans('texts.confirmation_subject')) {
$this->company->notification(new EmailBounceNotification($this->request['Email']))->ninja();
}
2022-01-16 04:11:48 +01:00
return $this->processBounce();
case 'SpamComplaint':
return $this->processSpamComplaint();
2022-02-07 09:14:54 +01:00
case 'Open':
return $this->processOpen();
2022-01-16 04:11:48 +01:00
default:
# code...
break;
}
}
2023-10-26 04:57:44 +02:00
// {
// "Metadata": {
// "example": "value",
// "example_2": "value"
// },
// "RecordType": "Open",
// "FirstOpen": true,
// "Client": {
// "Name": "Chrome 35.0.1916.153",
// "Company": "Google",
// "Family": "Chrome"
// },
// "OS": {
// "Name": "OS X 10.7 Lion",
// "Company": "Apple Computer, Inc.",
// "Family": "OS X 10"
// },
// "Platform": "WebMail",
// "UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36",
// "ReadSeconds": 5,
// "Geo": {
// "CountryISOCode": "RS",
// "Country": "Serbia",
// "RegionISOCode": "VO",
// "Region": "Autonomna Pokrajina Vojvodina",
// "City": "Novi Sad",
// "Zip": "21000",
// "Coords": "45.2517,19.8369",
// "IP": "188.2.95.4"
// },
// "MessageID": "00000000-0000-0000-0000-000000000000",
// "MessageStream": "outbound",
// "ReceivedAt": "2022-02-06T06:37:48Z",
// "Tag": "welcome-email",
// "Recipient": "john@example.com"
// }
2022-02-07 09:14:54 +01:00
private function processOpen()
{
$this->invitation->opened_date = now();
$this->invitation->save();
2023-08-22 12:42:01 +02:00
$data = array_merge($this->request, ['history' => $this->fetchMessage()]);
$sl = $this->getSystemLog($this->request['MessageID']);
2024-02-18 17:50:33 +01:00
if ($sl) {
$this->updateSystemLog($sl, $data);
return;
}
2024-02-18 17:50:33 +01:00
(
new SystemLogger(
$data,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_OPENED,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->invitation->contact->client,
$this->invitation->company
)
)->handle();
2022-02-07 09:14:54 +01:00
}
2023-10-26 04:57:44 +02:00
// {
// "RecordType": "Delivery",
// "ServerID": 23,
// "MessageStream": "outbound",
// "MessageID": "00000000-0000-0000-0000-000000000000",
// "Recipient": "john@example.com",
// "Tag": "welcome-email",
// "DeliveredAt": "2021-02-21T16:34:52Z",
// "Details": "Test delivery webhook details",
// "Metadata": {
// "example": "value",
// "example_2": "value"
// }
// }
2022-01-16 04:11:48 +01:00
private function processDelivery()
{
$this->invitation->email_status = 'delivered';
$this->invitation->save();
2023-08-22 12:42:01 +02:00
$data = array_merge($this->request, ['history' => $this->fetchMessage()]);
$sl = $this->getSystemLog($this->request['MessageID']);
2024-02-18 17:50:33 +01:00
if ($sl) {
$this->updateSystemLog($sl, $data);
return;
}
2024-02-18 17:50:33 +01:00
(
new SystemLogger(
$data,
SystemLog::CATEGORY_MAIL,
SystemLog::EVENT_MAIL_DELIVERY,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->invitation->contact->client,
$this->invitation->company
)
)->handle();
2022-01-16 04:11:48 +01:00
}
2023-10-26 04:57:44 +02:00
// {
// "Metadata": {
// "example": "value",
// "example_2": "value"
// },
// "RecordType": "Bounce",
// "ID": 42,
// "Type": "HardBounce",
// "TypeCode": 1,
// "Name": "Hard bounce",
// "Tag": "Test",
// "MessageID": "00000000-0000-0000-0000-000000000000",
// "ServerID": 1234,
// "MessageStream": "outbound",
// "Description": "The server was unable to deliver your message (ex: unknown user, mailbox not found).",
// "Details": "Test bounce details",
// "Email": "john@example.com",
// "From": "sender@example.com",
// "BouncedAt": "2021-02-21T16:34:52Z",
// "DumpAvailable": true,
// "Inactive": true,
// "CanActivate": true,
// "Subject": "Test subject",
// "Content": "Test content"
// }
2022-01-16 04:11:48 +01:00
private function processBounce()
{
$this->invitation->email_status = 'bounced';
$this->invitation->save();
$bounce = new EmailBounce(
$this->request['Tag'],
$this->request['From'],
$this->request['MessageID']
);
LightLogs::create($bounce)->send();
2022-01-16 04:11:48 +01:00
2023-08-22 12:42:01 +02:00
$data = array_merge($this->request, ['history' => $this->fetchMessage()]);
$sl = $this->getSystemLog($this->request['MessageID']);
2024-02-18 17:50:33 +01:00
if ($sl) {
$this->updateSystemLog($sl, $data);
return;
}
2023-08-22 12:42:01 +02:00
(new SystemLogger($data, SystemLog::CATEGORY_MAIL, SystemLog::EVENT_MAIL_BOUNCED, SystemLog::TYPE_WEBHOOK_RESPONSE, $this->invitation->contact->client, $this->invitation->company))->handle();
2022-01-16 04:11:48 +01:00
}
2023-10-26 04:57:44 +02:00
// {
// "Metadata": {
// "example": "value",
// "example_2": "value"
// },
// "RecordType": "SpamComplaint",
// "ID": 42,
// "Type": "SpamComplaint",
// "TypeCode": 100001,
// "Name": "Spam complaint",
// "Tag": "Test",
// "MessageID": "00000000-0000-0000-0000-000000000000",
// "ServerID": 1234,
// "MessageStream": "outbound",
// "Description": "The subscriber explicitly marked this message as spam.",
// "Details": "Test spam complaint details",
// "Email": "john@example.com",
// "From": "sender@example.com",
// "BouncedAt": "2021-02-21T16:34:52Z",
// "DumpAvailable": true,
// "Inactive": true,
// "CanActivate": false,
// "Subject": "Test subject",
// "Content": "Test content"
// }
2022-01-16 04:11:48 +01:00
private function processSpamComplaint()
{
$this->invitation->email_status = 'spam';
$this->invitation->save();
$spam = new EmailSpam(
$this->request['Tag'],
$this->request['From'],
$this->request['MessageID']
);
LightLogs::create($spam)->send();
2022-01-16 04:11:48 +01:00
2023-08-22 12:42:01 +02:00
$data = array_merge($this->request, ['history' => $this->fetchMessage()]);
$sl = $this->getSystemLog($this->request['MessageID']);
2024-02-18 17:50:33 +01:00
if ($sl) {
$this->updateSystemLog($sl, $data);
}
2022-01-16 04:11:48 +01:00
}
private function discoverInvitation($message_id)
{
$invitation = false;
2023-02-16 02:36:09 +01:00
if ($invitation = InvoiceInvitation::where('message_id', $message_id)->first()) {
2023-08-23 06:36:55 +02:00
$this->entity = 'invoice';
2022-01-16 04:11:48 +01:00
return $invitation;
2023-02-16 02:36:09 +01:00
} elseif ($invitation = QuoteInvitation::where('message_id', $message_id)->first()) {
2023-08-23 06:36:55 +02:00
$this->entity = 'quote';
2022-01-16 04:11:48 +01:00
return $invitation;
2023-02-16 02:36:09 +01:00
} elseif ($invitation = RecurringInvoiceInvitation::where('message_id', $message_id)->first()) {
2023-08-23 06:36:55 +02:00
$this->entity = 'recurring_invoice';
2022-01-16 04:11:48 +01:00
return $invitation;
2023-02-16 02:36:09 +01:00
} elseif ($invitation = CreditInvitation::where('message_id', $message_id)->first()) {
2023-08-23 06:36:55 +02:00
$this->entity = 'credit';
2022-01-16 04:11:48 +01:00
return $invitation;
2023-02-16 02:36:09 +01:00
} elseif ($invitation = PurchaseOrderInvitation::where('message_id', $message_id)->first()) {
2023-08-23 06:36:55 +02:00
$this->entity = 'purchase_order';
2022-08-17 09:18:30 +02:00
return $invitation;
2023-02-16 02:36:09 +01:00
} else {
2022-01-16 04:11:48 +01:00
return $invitation;
2023-02-16 02:36:09 +01:00
}
2022-01-16 04:11:48 +01:00
}
2023-08-22 12:42:01 +02:00
public function getRawMessage(string $message_id)
{
2024-03-03 08:33:17 +01:00
$postmark_secret = !empty($this->company->settings->postmark_secret) ? $this->company->settings->postmark_secret : config('services.postmark.token');
2024-02-27 07:36:32 +01:00
$postmark = new PostmarkClient($postmark_secret);
$messageDetail = $postmark->getOutboundMessageDetails($message_id);
try {
$messageDetail = $postmark->getOutboundMessageDetails($message_id);
} catch(\Exception $e) {
$postmark_secret = config('services.postmark-outlook.token');
$postmark = new PostmarkClient($postmark_secret);
$messageDetail = $postmark->getOutboundMessageDetails($message_id);
}
return $messageDetail;
2024-01-14 05:05:00 +01:00
}
public function getBounceId(string $message_id): ?int
{
$messageDetail = $this->getRawMessage($message_id);
2024-01-14 05:05:00 +01:00
2024-02-18 17:50:33 +01:00
$event = collect($messageDetail->messageevents)->first(function ($event) {
return $event?->Details?->BounceID ?? false;
});
return $event?->Details?->BounceID ?? null;
}
2023-08-22 12:42:01 +02:00
private function fetchMessage(): array
{
2024-02-18 17:50:33 +01:00
if (strlen($this->request['MessageID']) < 1) {
2023-08-22 12:42:01 +02:00
return $this->default_response;
2023-10-26 04:57:44 +02:00
}
2024-01-14 05:05:00 +01:00
2023-08-22 12:42:01 +02:00
try {
2024-03-03 08:33:17 +01:00
$postmark_secret = !empty($this->company->settings->postmark_secret) ? $this->company->settings->postmark_secret : config('services.postmark.token');
2024-02-18 17:50:33 +01:00
$postmark = new PostmarkClient($postmark_secret);
try {
$messageDetail = $postmark->getOutboundMessageDetails($this->request['MessageID']);
2024-06-14 09:09:44 +02:00
} catch(\Exception $e) {
$postmark_secret = config('services.postmark-outlook.token');
$postmark = new PostmarkClient($postmark_secret);
$messageDetail = $postmark->getOutboundMessageDetails($this->request['MessageID']);
}
2023-08-23 06:36:55 +02:00
$recipients = collect($messageDetail['recipients'])->flatten()->implode(',');
$subject = $messageDetail->subject ?? '';
2024-02-18 17:50:33 +01:00
$events = collect($messageDetail->messageevents)->map(function ($event) {
2023-08-23 06:36:55 +02:00
return [
2024-02-18 17:50:33 +01:00
'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') ?? '',
];
2023-08-23 06:36:55 +02:00
})->toArray();
2023-08-22 12:42:01 +02:00
return [
2023-08-23 06:36:55 +02:00
'recipients' => $recipients,
'subject' => $subject,
'entity' => $this->entity ?? '',
'entity_id' => $this->invitation->{$this->entity}->hashed_id ?? '',
'events' => $events,
2023-08-22 12:42:01 +02:00
];
2023-08-23 06:36:55 +02:00
2023-10-26 04:57:44 +02:00
} catch (\Exception $e) {
2023-08-22 12:42:01 +02:00
return $this->default_response;
}
}
2023-02-16 02:36:09 +01:00
}