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

Merge pull request #4012 from turbo124/v2

Fixes for Webhook handler
This commit is contained in:
David Bomba 2020-08-24 21:54:11 +10:00 committed by GitHub
commit ff2f8c8905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 18 deletions

View File

@ -11,6 +11,7 @@
namespace App\Events\Quote;
use App\Models\ClientContact;
use App\Models\Company;
use App\Models\Quote;
use Illuminate\Broadcasting\Channel;

View File

@ -2,6 +2,7 @@
namespace App\Jobs\Util;
use GuzzleHttp\RequestOptions;
use App\Transformers\ArraySerializer;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
@ -39,11 +40,9 @@ class WebhookHandler implements ShouldQueue
public function handle() :bool
{
if(!$this->entity->company || $this->entity->company->company_users->first()->is_migrating)
if(!$this->entity->company || $this->entity->company->company_users->first()->is_migrating == true)
return true;
//info("i got past the check");
$subscriptions = \App\Models\Webhook::where('company_id', $this->entity->company_id)
->where('event_id', $this->event_id)
->get();
@ -58,14 +57,15 @@ class WebhookHandler implements ShouldQueue
return true;
}
private function process($subscription)
{
// generate JSON data
$manager = new Manager();
$manager->setSerializer(new ArraySerializer());
$transformer = new $this->getTransformerClassName();
$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();
@ -73,24 +73,18 @@ class WebhookHandler implements ShouldQueue
$this->postData($subscription, $data, []);
}
private function getTransformerClassName()
{
return sprintf('App\\Transformers\\%sTransformer', class_basename($this->entity));
}
private function postData($subscription, $data, $headers = [])
{
$base_headers = [
'Content-Length' => strlen($data),
'Content-Length' => strlen(json_encode($data)),
'Accept' => 'application/json'
];
$client = new \GuzzleHttp\Client(['headers' => array_merge($base_headers, $headers)]);
//$response = $client->request('POST', $subscription->target_url, ['form_params' => $data]);
$response = $client->post($subscription->target_url, [
GuzzleHttp\RequestOptions::JSON => $data // or 'json' => [...]
RequestOptions::JSON => $data // or 'json' => [...]
]);
if ($response->getStatusCode() == 410 || $response->getStatusCode() == 200) {
@ -105,3 +99,4 @@ class WebhookHandler implements ShouldQueue
}
}