1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Jobs/Util/WebhookHandler.php

188 lines
5.2 KiB
PHP
Raw Normal View History

<?php
2020-11-11 01:13:39 +01:00
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2020-11-11 01:13:39 +01:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-11-11 01:13:39 +01:00
*/
2021-06-10 03:15:21 +02:00
namespace App\Jobs\Util;
2021-01-13 00:12:01 +01:00
use App\Jobs\Util\SystemLogger;
2020-11-25 15:19:52 +01:00
use App\Libraries\MultiDB;
2021-09-26 12:17:09 +02:00
use App\Models\Client as ClientModel;
2021-01-13 00:12:01 +01:00
use App\Models\SystemLog;
2020-10-28 11:10:49 +01:00
use App\Models\Webhook;
use App\Transformers\ArraySerializer;
2022-12-13 23:25:05 +01:00
use App\Utils\Ninja;
2020-10-28 11:10:49 +01:00
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use League\Fractal\Manager;
use League\Fractal\Resource\Item;
2020-07-06 13:22:36 +02:00
class WebhookHandler implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private $entity;
private $event_id;
2020-11-17 00:04:10 +01:00
private $company;
2020-11-22 12:14:49 +01:00
public $tries = 4; //number of retries
2020-11-22 12:14:49 +01:00
public $deleteWhenMissingModels = true;
2021-10-14 08:54:38 +02:00
private string $includes;
/**
* Create a new job instance.
*
2020-10-28 11:10:49 +01:00
* @param $event_id
* @param $entity
*/
2021-10-14 08:54:38 +02:00
public function __construct($event_id, $entity, $company, $includes = '')
{
$this->event_id = $event_id;
$this->entity = $entity;
2020-11-17 00:04:10 +01:00
$this->company = $company;
2021-10-14 08:54:38 +02:00
$this->includes = $includes;
}
public function backoff()
{
return [10, 30, 60, 180];
}
/**
* Execute the job.
*
2020-10-28 11:10:49 +01:00
* @return bool
*/
2020-11-22 12:14:49 +01:00
public function handle()
2022-12-13 23:25:05 +01:00
{
2020-11-17 00:04:10 +01:00
MultiDB::setDb($this->company->db);
2022-12-13 23:25:05 +01:00
//If the company is disabled, or if on hosted, the user is not a paid hosted user return early
if (! $this->company || $this->company->is_disabled || (Ninja::isHosted() && !$this->company->account->isPaidHostedClient())) {
2020-08-24 13:53:22 +02:00
return true;
2020-11-25 15:19:52 +01:00
}
2020-11-22 12:14:49 +01:00
2020-11-18 10:35:09 +01:00
$subscriptions = Webhook::where('company_id', $this->company->id)
->where('event_id', $this->event_id)
2023-01-29 01:38:36 +01:00
->cursor()
->each(function ($subscription) {
$this->process($subscription);
2023-01-29 01:38:36 +01:00
});
2023-01-29 01:38:36 +01:00
}
private function process($subscription)
{
$this->entity->refresh();
// generate JSON data
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
2021-10-14 08:54:38 +02:00
$manager->parseIncludes($this->includes);
2020-08-24 13:53:22 +02:00
$class = sprintf('App\\Transformers\\%sTransformer', class_basename($this->entity));
$transformer = new $class();
$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 13:53:22 +02:00
'Content-Length' => strlen(json_encode($data)),
'Accept' => 'application/json',
];
2020-10-28 11:10:49 +01:00
$client = new Client(['headers' => array_merge($base_headers, $headers)]);
2020-08-24 13:53:22 +02:00
2021-01-17 05:37:55 +01:00
try {
2021-07-20 00:39:04 +02:00
$response = $client->post($subscription->target_url, [
RequestOptions::JSON => $data, // or 'json' => [...]
]);
2021-01-17 05:37:55 +01:00
SystemLogger::dispatch(
array_merge((array) $response, $data),
2021-01-17 05:37:55 +01:00
SystemLog::CATEGORY_WEBHOOK,
2021-09-16 05:38:16 +02:00
SystemLog::EVENT_WEBHOOK_SUCCESS,
2021-01-17 05:37:55 +01:00
SystemLog::TYPE_WEBHOOK_RESPONSE,
2021-09-20 23:52:21 +02:00
$this->resolveClient(),
$this->company
2021-01-17 05:37:55 +01:00
);
2023-01-29 01:38:36 +01:00
if ($response->getStatusCode() == 429){
nlog("429 retry after 10 seconds internally");
sleep(10);
$response = $client->post($subscription->target_url, [
RequestOptions::JSON => $data, // or 'json' => [...]
]);
SystemLogger::dispatch(
array_merge((array) $response, $data),
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_SUCCESS,
SystemLog::TYPE_WEBHOOK_RESPONSE,
$this->resolveClient(),
$this->company
);
}
} catch (\Exception $e) {
nlog("429 occurred in the exception handler");
2021-09-16 05:38:16 +02:00
nlog($e->getMessage());
2021-01-17 05:37:55 +01:00
SystemLogger::dispatch(
2021-01-13 00:12:01 +01:00
$e->getMessage(),
SystemLog::CATEGORY_WEBHOOK,
SystemLog::EVENT_WEBHOOK_FAILURE,
2021-01-13 00:12:01 +01:00
SystemLog::TYPE_WEBHOOK_RESPONSE,
2021-09-20 23:52:21 +02:00
$this->resolveClient(),
$this->company,
2021-01-13 00:12:01 +01:00
);
2021-01-17 05:37:55 +01:00
}
}
2021-09-20 23:52:21 +02:00
private function resolveClient()
{
2021-09-26 12:17:09 +02:00
//make sure it isn't an instance of the Client Model
if ((! $this->entity instanceof ClientModel) && $this->entity->client()->exists()) {
2021-09-20 23:52:21 +02:00
return $this->entity->client;
}
2021-09-26 12:17:09 +02:00
return $this->company->clients()->first();
2021-09-20 23:52:21 +02:00
}
public function failed($exception)
{
2023-01-29 01:38:36 +01:00
sleep(2);
2020-12-29 22:10:03 +01:00
nlog(print_r($exception->getMessage(), 1));
2023-01-29 01:38:36 +01:00
}
}