Added webhook controller
This commit is contained in:
parent
2f1b34a806
commit
ffe70058ba
69
src/Controllers/Webhook.php
Normal file
69
src/Controllers/Webhook.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Intuit\Controllers;
|
||||
|
||||
use Illuminate\Foundation\Bus\DispatchesJobs;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Routing\Controller as BaseController;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Intuit\Response\Webhook as Payload;
|
||||
|
||||
class Webhook extends BaseController
|
||||
{
|
||||
use DispatchesJobs;
|
||||
|
||||
private const name = 'intuit';
|
||||
|
||||
public function webhook(Request $request)
|
||||
{
|
||||
$signature = $request->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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
143
src/Response/Webhook.php
Normal file
143
src/Response/Webhook.php
Normal file
@ -0,0 +1,143 @@
|
||||
<?php
|
||||
|
||||
namespace Intuit\Response;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Stores incoming or outgoing message data for a Slack API call.
|
||||
*/
|
||||
class Webhook implements \ArrayAccess, \JsonSerializable
|
||||
{
|
||||
/**
|
||||
* @var Collection The response data.
|
||||
*/
|
||||
protected Collection $data;
|
||||
|
||||
/**
|
||||
* Creates a new payload object.
|
||||
*
|
||||
* @param array $data The payload data.
|
||||
*/
|
||||
public function __construct(array $data,bool $key=FALSE)
|
||||
{
|
||||
$this->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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user