Skip to content

Commit

Permalink
feat: Add support for Contact Webhook Events (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
jayanratna committed Jul 18, 2024
1 parent 23aed22 commit 74a5a00
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Events/ContactCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Resend\Laravel\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ContactCreated
{
use Dispatchable, SerializesModels;

/**
* Create a new contact created event instance.
*/
public function __construct(
public array $payload
) {
//
}
}
20 changes: 20 additions & 0 deletions src/Events/ContactDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Resend\Laravel\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ContactDeleted
{
use Dispatchable, SerializesModels;

/**
* Create a new contact deleted event instance.
*/
public function __construct(
public array $payload
) {
//
}
}
20 changes: 20 additions & 0 deletions src/Events/ContactUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Resend\Laravel\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ContactUpdated
{
use Dispatchable, SerializesModels;

/**
* Create a new contact updated event instance.
*/
public function __construct(
public array $payload
) {
//
}
}
33 changes: 33 additions & 0 deletions src/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Str;
use Resend\Laravel\Events\ContactCreated;
use Resend\Laravel\Events\ContactDeleted;
use Resend\Laravel\Events\ContactUpdated;
use Resend\Laravel\Events\EmailBounced;
use Resend\Laravel\Events\EmailClicked;
use Resend\Laravel\Events\EmailComplained;
Expand Down Expand Up @@ -49,6 +52,36 @@ public function handleWebhook(Request $request): Response
return $this->missingMethod($payload);
}

/**
* Handle contact created event.
*/
protected function handleContactCreated(array $payload): Response
{
ContactCreated::dispatch($payload);

return $this->successMethod();
}

/**
* Handle contact deleted event.
*/
protected function handleContactDeleted(array $payload): Response
{
ContactDeleted::dispatch($payload);

return $this->successMethod();
}

/**
* Handle contact updated event.
*/
protected function handleContactUpdated(array $payload): Response
{
ContactUpdated::dispatch($payload);

return $this->successMethod();
}

/**
* Handle email bounced event.
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/Http/Controllers/WebhookController.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

use Illuminate\Support\Facades\Event;
use Resend\Laravel\Events\ContactCreated;
use Resend\Laravel\Events\ContactDeleted;
use Resend\Laravel\Events\ContactUpdated;
use Resend\Laravel\Events\EmailBounced;
use Resend\Laravel\Events\EmailClicked;
use Resend\Laravel\Events\EmailComplained;
Expand All @@ -26,6 +29,9 @@
expect($response->getStatusCode())->toBe(200)
->and($response->getContent())->toBe('Webhook handled');
})->with([
['contact.created', ContactCreated::class],
['contact.deleted', ContactDeleted::class],
['contact.updated', ContactUpdated::class],
['email.bounced', EmailBounced::class],
['email.clicked', EmailClicked::class],
['email.complained', EmailComplained::class],
Expand Down

0 comments on commit 74a5a00

Please sign in to comment.