1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Merge pull request #4979 from turbo124/v5-develop

Postmark webhooks
This commit is contained in:
David Bomba 2021-02-24 09:56:47 +11:00 committed by GitHub
commit 309acd1284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 7 deletions

View File

@ -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;
}
}

View File

@ -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;