From 044c35266e80a7421ead8b79d39c7201100f91e3 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 24 Aug 2020 21:53:22 +1000 Subject: [PATCH] Fixes for zapier --- app/Jobs/Util/WebhookHandler.php | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/app/Jobs/Util/WebhookHandler.php b/app/Jobs/Util/WebhookHandler.php index a8f5f4795f..6c6a4ca8c6 100644 --- a/app/Jobs/Util/WebhookHandler.php +++ b/app/Jobs/Util/WebhookHandler.php @@ -2,6 +2,7 @@ namespace App\Jobs\Util; +use GuzzleHttp\RequestOptions; use App\Transformers\ArraySerializer; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; @@ -24,7 +25,7 @@ class WebhookHandler implements ShouldQueue * * @return void */ - + public function __construct($event_id, $entity) { $this->event_id = $event_id; @@ -38,11 +39,9 @@ class WebhookHandler implements ShouldQueue */ public function handle() :bool { - - if(!$this->entity->company || $this->entity->company->company_users->first()->is_migrating) - return true; - //info("i got past the check"); + if(!$this->entity->company || $this->entity->company->company_users->first()->is_migrating == true) + return true; $subscriptions = \App\Models\Webhook::where('company_id', $this->entity->company_id) ->where('event_id', $this->event_id) @@ -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) { @@ -102,6 +96,7 @@ class WebhookHandler implements ShouldQueue { info(print_r($exception->getMessage(),1)); - + } } + \ No newline at end of file