2020-05-04 21:13:46 +10:00
|
|
|
<?php
|
2020-11-11 11:13:39 +11:00
|
|
|
/**
|
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
|
*
|
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
|
*
|
2022-04-27 13:20:41 +10:00
|
|
|
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-11-11 11:13:39 +11:00
|
|
|
*
|
2021-06-16 16:58:16 +10:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-11-11 11:13:39 +11:00
|
|
|
*/
|
2021-06-10 11:15:21 +10:00
|
|
|
|
2020-05-04 21:13:46 +10:00
|
|
|
namespace App\Jobs\Util;
|
|
|
|
|
|
2021-01-13 10:12:01 +11:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2020-11-25 15:19:52 +01:00
|
|
|
use App\Libraries\MultiDB;
|
2021-09-26 20:17:09 +10:00
|
|
|
use App\Models\Client as ClientModel;
|
2021-01-13 10:12:01 +11:00
|
|
|
use App\Models\SystemLog;
|
2020-10-28 21:10:49 +11:00
|
|
|
use App\Models\Webhook;
|
2020-05-06 21:49:42 +10:00
|
|
|
use App\Transformers\ArraySerializer;
|
2020-10-28 21:10:49 +11:00
|
|
|
use GuzzleHttp\Client;
|
2020-09-06 19:38:10 +10:00
|
|
|
use GuzzleHttp\RequestOptions;
|
2020-05-04 21:13:46 +10:00
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
2020-05-06 21:49:42 +10:00
|
|
|
use League\Fractal\Manager;
|
|
|
|
|
use League\Fractal\Resource\Item;
|
2020-05-04 21:13:46 +10:00
|
|
|
|
2020-07-06 21:22:36 +10:00
|
|
|
class WebhookHandler implements ShouldQueue
|
2020-05-04 21:13:46 +10:00
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
2020-05-06 21:49:42 +10:00
|
|
|
private $entity;
|
|
|
|
|
|
|
|
|
|
private $event_id;
|
|
|
|
|
|
2020-11-17 10:04:10 +11:00
|
|
|
private $company;
|
2020-11-22 22:14:49 +11:00
|
|
|
|
2021-09-05 20:31:08 +10:00
|
|
|
public $tries = 3; //number of retries
|
2020-11-22 22:14:49 +11:00
|
|
|
|
2021-05-31 15:27:26 +10:00
|
|
|
public $backoff = 10; //seconds to wait until retry
|
2020-11-22 22:14:49 +11:00
|
|
|
|
|
|
|
|
public $deleteWhenMissingModels = true;
|
|
|
|
|
|
2021-10-14 17:54:38 +11:00
|
|
|
private string $includes;
|
2022-06-21 09:57:17 +00:00
|
|
|
|
2020-05-04 21:13:46 +10:00
|
|
|
/**
|
|
|
|
|
* Create a new job instance.
|
|
|
|
|
*
|
2020-10-28 21:10:49 +11:00
|
|
|
* @param $event_id
|
|
|
|
|
* @param $entity
|
2020-05-04 21:13:46 +10:00
|
|
|
*/
|
2021-10-14 17:54:38 +11:00
|
|
|
public function __construct($event_id, $entity, $company, $includes = '')
|
2020-05-04 21:13:46 +10:00
|
|
|
{
|
2020-05-06 21:49:42 +10:00
|
|
|
$this->event_id = $event_id;
|
|
|
|
|
$this->entity = $entity;
|
2020-11-17 10:04:10 +11:00
|
|
|
$this->company = $company;
|
2021-10-14 17:54:38 +11:00
|
|
|
$this->includes = $includes;
|
2020-05-04 21:13:46 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Execute the job.
|
|
|
|
|
*
|
2020-10-28 21:10:49 +11:00
|
|
|
* @return bool
|
2020-05-04 21:13:46 +10:00
|
|
|
*/
|
2020-11-22 22:14:49 +11:00
|
|
|
public function handle()
|
2020-11-11 11:13:39 +11:00
|
|
|
{//todo set multidb here
|
2020-11-17 10:04:10 +11:00
|
|
|
|
|
|
|
|
MultiDB::setDb($this->company->db);
|
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (! $this->company || $this->company->is_disabled) {
|
2020-08-24 21:53:22 +10:00
|
|
|
return true;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-11-22 22:14:49 +11:00
|
|
|
|
2020-11-18 20:35:09 +11:00
|
|
|
$subscriptions = Webhook::where('company_id', $this->company->id)
|
2020-05-06 21:49:42 +10:00
|
|
|
->where('event_id', $this->event_id)
|
|
|
|
|
->get();
|
|
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
if (! $subscriptions || $subscriptions->count() == 0) {
|
2020-11-22 22:14:49 +11:00
|
|
|
return;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2022-06-21 09:57:17 +00:00
|
|
|
|
2020-09-06 19:38:10 +10:00
|
|
|
$subscriptions->each(function ($subscription) {
|
2020-05-06 21:49:42 +10:00
|
|
|
$this->process($subscription);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-09-06 19:38:10 +10:00
|
|
|
|
2020-05-06 21:49:42 +10:00
|
|
|
private function process($subscription)
|
|
|
|
|
{
|
2022-05-05 10:40:43 +10:00
|
|
|
$this->entity->refresh();
|
2022-06-21 09:57:17 +00:00
|
|
|
|
2020-05-06 21:49:42 +10:00
|
|
|
// generate JSON data
|
|
|
|
|
$manager = new Manager();
|
|
|
|
|
$manager->setSerializer(new ArraySerializer());
|
2021-10-14 17:54:38 +11:00
|
|
|
$manager->parseIncludes($this->includes);
|
2020-05-06 21:49:42 +10:00
|
|
|
|
2020-08-24 21:53:22 +10:00
|
|
|
$class = sprintf('App\\Transformers\\%sTransformer', class_basename($this->entity));
|
|
|
|
|
|
|
|
|
|
$transformer = new $class();
|
2020-05-06 21:49:42 +10:00
|
|
|
|
|
|
|
|
$resource = new Item($this->entity, $transformer, $this->entity->getEntityType());
|
|
|
|
|
$data = $manager->createData($resource)->toArray();
|
|
|
|
|
|
|
|
|
|
$this->postData($subscription, $data, []);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function postData($subscription, $data, $headers = [])
|
|
|
|
|
{
|
|
|
|
|
$base_headers = [
|
2020-08-24 21:53:22 +10:00
|
|
|
'Content-Length' => strlen(json_encode($data)),
|
2020-09-06 19:38:10 +10:00
|
|
|
'Accept' => 'application/json',
|
2020-05-06 21:49:42 +10:00
|
|
|
];
|
|
|
|
|
|
2020-10-28 21:10:49 +11:00
|
|
|
$client = new Client(['headers' => array_merge($base_headers, $headers)]);
|
2020-08-24 21:53:22 +10:00
|
|
|
|
2021-01-17 15:37:55 +11:00
|
|
|
try {
|
2021-07-20 08:39:04 +10:00
|
|
|
$response = $client->post($subscription->target_url, [
|
|
|
|
|
RequestOptions::JSON => $data, // or 'json' => [...]
|
|
|
|
|
]);
|
2021-01-17 15:37:55 +11:00
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
2022-06-21 09:57:17 +00:00
|
|
|
array_merge((array) $response, $data),
|
2021-01-17 15:37:55 +11:00
|
|
|
SystemLog::CATEGORY_WEBHOOK,
|
2021-09-16 13:38:16 +10:00
|
|
|
SystemLog::EVENT_WEBHOOK_SUCCESS,
|
2021-01-17 15:37:55 +11:00
|
|
|
SystemLog::TYPE_WEBHOOK_RESPONSE,
|
2021-09-21 07:52:21 +10:00
|
|
|
$this->resolveClient(),
|
2021-05-19 11:12:23 +10:00
|
|
|
$this->company
|
2021-01-17 15:37:55 +11:00
|
|
|
);
|
|
|
|
|
|
2021-11-07 21:23:44 +11:00
|
|
|
// if ($response->getStatusCode() == 410)
|
|
|
|
|
// return true; $subscription->delete();
|
2022-06-21 09:57:17 +00:00
|
|
|
} catch (\Exception $e) {
|
2021-09-16 13:38:16 +10:00
|
|
|
nlog($e->getMessage());
|
2021-01-17 15:37:55 +11:00
|
|
|
|
2022-06-21 09:57:17 +00:00
|
|
|
SystemLogger::dispatch(
|
2021-01-13 10:12:01 +11:00
|
|
|
$e->getMessage(),
|
|
|
|
|
SystemLog::CATEGORY_WEBHOOK,
|
|
|
|
|
SystemLog::EVENT_WEBHOOK_RESPONSE,
|
|
|
|
|
SystemLog::TYPE_WEBHOOK_RESPONSE,
|
2021-09-21 07:52:21 +10:00
|
|
|
$this->resolveClient(),
|
2021-05-19 11:12:23 +10:00
|
|
|
$this->company,
|
2021-01-13 10:12:01 +11:00
|
|
|
);
|
2021-01-17 15:37:55 +11:00
|
|
|
}
|
2020-05-04 21:13:46 +10:00
|
|
|
}
|
2020-07-01 16:03:46 +10:00
|
|
|
|
2021-09-21 07:52:21 +10:00
|
|
|
private function resolveClient()
|
|
|
|
|
{
|
2021-09-26 20:17:09 +10:00
|
|
|
//make sure it isn't an instance of the Client Model
|
2022-06-21 09:57:17 +00:00
|
|
|
if ((! $this->entity instanceof ClientModel) && $this->entity->client()->exists()) {
|
2021-09-21 07:52:21 +10:00
|
|
|
return $this->entity->client;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-26 20:17:09 +10:00
|
|
|
return $this->company->clients()->first();
|
2021-09-21 07:52:21 +10:00
|
|
|
}
|
|
|
|
|
|
2020-07-01 16:03:46 +10:00
|
|
|
public function failed($exception)
|
|
|
|
|
{
|
2020-12-30 08:10:03 +11:00
|
|
|
nlog(print_r($exception->getMessage(), 1));
|
2020-07-01 16:03:46 +10:00
|
|
|
}
|
2020-05-04 21:13:46 +10:00
|
|
|
}
|