2023-12-14 16:40:43 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
2024-03-18 08:04:54 +01:00
|
|
|
namespace App\Jobs\Mailgun;
|
2023-12-14 16:40:43 +01:00
|
|
|
|
2024-03-18 08:04:54 +01:00
|
|
|
use App\Helpers\IngresMail\Transformer\MailgunInboundWebhookTransformer;
|
2023-12-14 16:40:43 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2024-03-18 08:04:54 +01:00
|
|
|
use App\Services\IngresEmail\IngresEmailEngine;
|
2023-12-14 16:40:43 +01:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2024-03-18 08:04:54 +01:00
|
|
|
use Log;
|
2023-12-14 16:40:43 +01:00
|
|
|
|
|
|
|
class ProcessMailgunInboundWebhook implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new job instance.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct(private array $request)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2024-03-18 08:04:54 +01:00
|
|
|
if (!array_key_exists('To', $this->request) || !array_key_exists('attachments', $this->request) || !array_key_exists('timestamp', $this->request) || !array_key_exists('Subject', $this->request) || !(array_key_exists('body-html', $this->request) || array_key_exists('body-plain', $this->request)))
|
2023-12-14 16:40:43 +01:00
|
|
|
throw new \Exception('invalid body');
|
|
|
|
|
2024-03-18 08:04:54 +01:00
|
|
|
// match company
|
|
|
|
$company = MultiDB::findAndSetDbByExpenseMailbox($this->request["To"]);
|
|
|
|
if (!$company) {
|
|
|
|
Log::info('unknown Expense Mailbox occured while handling an inbound email from mailgun: ' . $this->request["To"]);
|
|
|
|
return;
|
|
|
|
}
|
2023-12-14 16:40:43 +01:00
|
|
|
|
2024-03-18 08:04:54 +01:00
|
|
|
// prepare
|
|
|
|
$ingresMail = (new MailgunInboundWebhookTransformer())->transform($this->request);
|
|
|
|
Log::info(json_encode($ingresMail));
|
2023-12-14 16:40:43 +01:00
|
|
|
|
2024-03-18 08:04:54 +01:00
|
|
|
// perform
|
|
|
|
(new IngresEmailEngine($ingresMail))->handle();
|
2023-12-14 16:40:43 +01:00
|
|
|
}
|
|
|
|
}
|