diff --git a/src/Controllers/Webhook.php b/src/Controllers/Webhook.php new file mode 100644 index 0000000..76c9c98 --- /dev/null +++ b/src/Controllers/Webhook.php @@ -0,0 +1,69 @@ +header('Intuit-Signature'); + // Load the secret, you also can load it from env(YOUR_OWN_SLACK_SECRET) + $secret = config(sprintf('services.provider.%s.verifytoken',self::name)); + + Log::channel('webhook')->debug('Payload: '.$request->getContent()); + Log::channel('webhook')->debug('Signature: '.$signature); + + $payloadHash = hash_hmac('sha256',$request->getContent(),$secret); + $singatureHash = bin2hex(base64_decode($signature)); + + if ($payloadHash == $singatureHash) { + Log::channel('webhook')->debug('Signature OK'); + } else { + Log::channel('webhook')->alert('Signature NOT ok.'); + } + + $payload = new Payload(json_decode($request->getContent(),TRUE)); + + foreach ($payload->types() as $type) { + + switch ($type) { + case 'eventNotifications': + + foreach ($payload->event($type) as $dataObject) { + Log::info('Event for realm: '.$dataObject['realmId'],['data'=>$dataObject]); + + foreach ($dataObject['dataChangeEvent'] as $object => $data) { + switch ($object) { + case 'entities': + foreach ($data as $eventData) { + switch ($x=$eventData['name']) { + default: + Log::alert(sprintf('We dont know how to handle [%s:%s] yet',$object,$x),['data'=>$eventData]); + } + } + break; + + default: + Log::error(sprintf('Unknown object: %s in %s',$object,$type),['data'=>$data]); + } + } + } + + break; + + default: + Log::error('Unknown webhook payload type: '.$type,['data'=>$data]); + } + } + } +} \ No newline at end of file diff --git a/src/Response/Webhook.php b/src/Response/Webhook.php new file mode 100644 index 0000000..4bb23ef --- /dev/null +++ b/src/Response/Webhook.php @@ -0,0 +1,143 @@ +data = collect($key ? ['payload'=>$data ] : $data); + } + + /** + * @return string + */ + public function __toString() + { + return $this->toJson(); + } + + /** + * Creates a response object from a JSON message. + * + * @param string $json A JSON string. + * @return Webhook The parsed response. + */ + public static function fromJson($json): self + { + $data = json_decode((string)$json,true); + + if (json_last_error() !== JSON_ERROR_NONE || (! is_array($data))) { + throw new \UnexpectedValueException('Invalid JSON message:'.serialize($data)); + } + + return new static($data); + } + + /* INTERFACES */ + + /** + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + if (is_null($offset)) { + $this->data[] = $value; + + } else { + $this->data[$offset] = $value; + } + } + + /** + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset) + { + return isset($this->data[$offset]); + } + + /** + * @param mixed $offset + */ + public function offsetUnset($offset) + { + unset($this->data[$offset]); + } + + /** + * @param mixed $offset + * @return null + */ + public function offsetGet($offset) + { + return $this->data[$offset] ?? NULL; + } + + /** + * @return array + */ + public function jsonSerialize() + { + return $this->data; + } + + /* METHODS */ + + /** + * Return the events of a specific type + * @param string $type + * @return Collection + */ + public function event(string $type): Collection + { + return collect($this->data->get($type)); + } + + /** + * Gets the payload data. + * + * @return array The payload data. + */ + public function getData() + { + return $this->data; + } + + /** + * Serializes the payload to a JSON message. + * + * @return string A JSON message. + */ + public function toJson(): string + { + return json_encode($this->data,true); + } + + /** + * Return the event types + * + * @return Collection + */ + public function types(): Collection + { + return $this->data->keys(); + } +} \ No newline at end of file