-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
326 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
database/migrations/2020_07_24_044003_create_webhook_requests_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateWebhookRequestsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('webhook_requests', function (Blueprint $table) { | ||
$table->bigIncrements('id'); | ||
$table->string('uuid'); | ||
$table->string('httpVerb'); | ||
$table->string('webhookUrl'); | ||
$table->string('status'); | ||
$table->string('payload')->nullable(); | ||
$table->string('meta')->nullable(); | ||
$table->string('responseBody')->nullable(); | ||
$table->integer('attempt'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('webhook_requests'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace CustomD\WebhookRegistry\Contracts; | ||
|
||
interface ShouldDeliverWebhooks { | ||
|
||
/** | ||
* Get the webhook payload | ||
* | ||
* @return array | ||
*/ | ||
public function getWebhookPayload(): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace CustomD\WebhookRegistry\Listeners; | ||
|
||
use CustomD\WebhookRegistry\Facades\WebhookRegistry; | ||
|
||
class GeneralEventSubscriber | ||
{ | ||
|
||
/** | ||
* Register the listeners for the subscriber. | ||
* | ||
* @param \Illuminate\Events\Dispatcher $events | ||
*/ | ||
public function subscribe($events) | ||
{ | ||
$events->listen('App\Events*', function ($eventName, array $payloads) { | ||
foreach($payloads as $payload){ | ||
if(WebhookRegistry::has($eventName)){ | ||
WebhookRegistry::trigger($eventName, $payload->getWebhookPayload()); | ||
} | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace CustomD\WebhookRegistry\Listeners; | ||
|
||
use Spatie\WebhookServer\Events\WebhookCallEvent; | ||
use CustomD\WebhookRegistry\Model\WebhookRequest; | ||
use Spatie\WebhookServer\Events\WebhookCallFailedEvent; | ||
use Spatie\WebhookServer\Events\WebhookCallSucceededEvent; | ||
use Spatie\WebhookServer\Events\FinalWebhookCallFailedEvent; | ||
|
||
class LoggingEventSubscriber | ||
{ | ||
|
||
public function recordWebhookSuccess(WebhookCallSucceededEvent $event) | ||
{ | ||
WebhookRequest::create( | ||
$this->makeLogEntryFromEvent($event, 'success') | ||
); | ||
} | ||
|
||
public function recordWebhookFailed(WebhookCallFailedEvent $event) | ||
{ | ||
WebhookRequest::create( | ||
$this->makeLogEntryFromEvent($event, 'failed') | ||
); | ||
} | ||
|
||
public function recordWebhookFailedFinal(FinalWebhookCallFailedEvent $event) | ||
{ | ||
WebhookRequest::create( | ||
$this->makeLogEntryFromEvent($event, 'failed') | ||
); | ||
} | ||
|
||
public function makeLogEntryFromEvent(WebhookCallEvent $event, string $status) | ||
{ | ||
return [ | ||
'uuid' => $event->uuid, | ||
'httpVerb' => $event->httpVerb, | ||
'webhookUrl' => $event->webhookUrl, | ||
'meta' => $event->meta ? json_encode($event->meta) : null, | ||
'payload' => json_encode($event->payload), | ||
'responseBody' => $event->response ? $event->response->getBody() : null, | ||
'attempt' => $event->attempt, | ||
'status' => $status, | ||
]; | ||
} | ||
|
||
/** | ||
* Register the listeners for the subscriber. | ||
* | ||
* @param \Illuminate\Events\Dispatcher $events | ||
*/ | ||
public function subscribe($events) | ||
{ | ||
$className = self::class; | ||
|
||
$events->listen( | ||
WebhookCallSucceededEvent::class, | ||
"$className@recordWebhookSuccess" | ||
); | ||
$events->listen( | ||
WebhookCallFailedEvent::class, | ||
"$className@recordWebhookFailed" | ||
); | ||
$events->listen( | ||
FinalWebhookCallFailedEvent::class, | ||
"$className@recordWebhookFailedFinal" | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace CustomD\WebhookRegistry\Model; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
|
||
class WebhookEndpoint extends Model | ||
{ | ||
use SoftDeletes; | ||
|
||
protected $fillable = [ | ||
'description', | ||
'base_url', | ||
'verify_ssl', | ||
]; | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
static::creating(function ($endpoint) { | ||
$endpoint->secret = bin2hex(random_bytes(8)); | ||
}); | ||
} | ||
|
||
public function events() | ||
{ | ||
return $this->hasMany(WebhookEvent::class); | ||
} | ||
} |
Oops, something went wrong.