diff --git a/app/Http/Controllers/PostMarkController.php b/app/Http/Controllers/PostMarkController.php index 74067176b919..253a31e72d94 100644 --- a/app/Http/Controllers/PostMarkController.php +++ b/app/Http/Controllers/PostMarkController.php @@ -11,6 +11,13 @@ namespace App\Http\Controllers; +use App\Jobs\Util\SystemLogger; +use App\Libraries\MultiDB; +use App\Models\CreditInvitation; +use App\Models\InvoiceInvitation; +use App\Models\QuoteInvitation; +use App\Models\RecurringInvoiceInvitation; +use App\Models\SystemLog; use Illuminate\Http\Request; /** @@ -18,6 +25,7 @@ use Illuminate\Http\Request; */ class PostMarkController extends BaseController { + private $invitation; public function __construct() { @@ -64,14 +72,38 @@ class PostMarkController extends BaseController if($request->header('X-API-SECURITY') && $request->header('X-API-SECURITY') == config('postmark.secret')) { + nlog($request->all()); + + MultiDB::findAndSetDbByCompanyKey($request->input('Tag')); + + $this->invitation = $this->discoverInvitation($request->input('MessageID')); + + if($this->invitation){ + $this->invitation->email_error = $request->input('Details'); + $this->invitation->save(); + } + + switch ($request->input('RecordType')) + { + case 'Delivery': + return $this->processDelivery($request); + case 'Bounce': + return $this->processBounce($request); + case 'SpamComplaint': + return $this->processSpamComplaint($request); + default: + # code... + break; + } + + return response()->json(['message' => 'Success'], 200); + } + return response()->json(['message' => 'Unauthorized'], 403); + } - - - - // { // "RecordType": "Delivery", // "ServerID": 23, @@ -88,7 +120,7 @@ class PostMarkController extends BaseController // } private function processDelivery($request) { - + SystemLogger::dispatch($request->all(), SystemLog::CATEGORY_MAIL, SystemLog::EVENT_MAIL_DELIVERY, SystemLog::TYPE_WEBHOOK_RESPONSE, $this->invitation->contact->client); } // { @@ -119,7 +151,7 @@ class PostMarkController extends BaseController private function processBounce($request) { - + SystemLogger::dispatch($request->all(), SystemLog::CATEGORY_MAIL, SystemLog::EVENT_MAIL_BOUNCED, SystemLog::TYPE_WEBHOOK_RESPONSE, $this->invitation->contact->client); } // { @@ -149,8 +181,22 @@ class PostMarkController extends BaseController // } private function processSpamComplaint($request) { - + SystemLogger::dispatch($request->all(), SystemLog::CATEGORY_MAIL, SystemLog::EVENT_MAIL_SPAM_COMPLAINT, SystemLog::TYPE_WEBHOOK_RESPONSE, $this->invitation->contact->client); } + private function discoverInvitation($message_id) + { + $invitation = false; + if($invitation = InvoiceInvitation::whereRaw('BINARY `message_id`= ?', [$message_id])->first()) + return $invitation; + elseif($invitation = QuoteInvitation::whereRaw('BINARY `message_id`= ?', [$message_id])->first()) + return $invitation; + elseif($invitation = RecurringInvoiceInvitation::whereRaw('BINARY `message_id`= ?', [$message_id])->first()) + return $invitation; + elseif($invitation = CreditInvitation::whereRaw('BINARY `message_id`= ?', [$message_id])->first()) + return $invitation; + else + return $invitation; + } } diff --git a/app/Models/SystemLog.php b/app/Models/SystemLog.php index 335c530a271a..816846ba255f 100644 --- a/app/Models/SystemLog.php +++ b/app/Models/SystemLog.php @@ -47,6 +47,9 @@ class SystemLog extends Model const EVENT_MAIL_SEND = 30; const EVENT_MAIL_RETRY_QUEUE = 31; //we use this to queue emails that are spooled and not sent due to the email queue quota being exceeded. + const EVENT_MAIL_BOUNCED = 32; + const EVENT_MAIL_SPAM_COMPLAINT = 33; + const EVENT_MAIL_DELIVERY = 34; const EVENT_WEBHOOK_RESPONSE = 40; const EVENT_PDF_RESPONSE = 50;