-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an endpoint for receiving Webhooks from Campaign Monitor. Fixes #225
- Loading branch information
1 parent
9f573f9
commit d841839
Showing
5 changed files
with
162 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,3 +41,4 @@ USPS_PASSWORD= | |
|
||
CAMPAIGN_MONITOR_API_KEY= | ||
CAMPAIGN_MONITOR_DEFAULT_LIST_ID= | ||
CAMPAIGN_MONITOR_WEBHOOK_AUTH_KEY= |
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
parameters: | ||
campaign_monitor.api_key: '%env(CAMPAIGN_MONITOR_API_KEY)%' | ||
campaign_monitor.default_list_id: '%env(CAMPAIGN_MONITOR_DEFAULT_LIST_ID)%' | ||
campaign_monitor.webhook_token: '%env(CAMPAIGN_MONITOR_WEBHOOK_TOKEN)%' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace App\Controller; | ||
|
||
use App\Service\EmailService; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class WebhookController extends AbstractController | ||
{ | ||
/** | ||
* @Route("/webhook", name="webhook") | ||
*/ | ||
public function index(Request $request): Response | ||
{ | ||
return $this->json([ | ||
'status' => 200, | ||
'title' => 'Success', | ||
'message' => 'Webhooks are available.' | ||
]); | ||
} | ||
|
||
/** | ||
* @Route("/webhook/email-service", name="webhook_email_service", methods={"POST"}) | ||
*/ | ||
public function emailServiceWebhook(Request $request, EmailService $emailService): Response | ||
{ | ||
// Fail if not configured | ||
if (!$emailService->isConfigured()) { | ||
return $this->json([ | ||
'status' => 500, | ||
'title' => 'Internal server error', | ||
'details' => 'Email service not configured.' | ||
], 500); | ||
} | ||
|
||
// Fail if token is missing or mismatched | ||
if (!$request->get('token') || | ||
$request->get('token') != $emailService->getWebhookToken() | ||
) { | ||
return $this->json([ | ||
'status' => 403, | ||
'title' => 'Access denied', | ||
'details' => 'Invalid credentials.' | ||
], 403); | ||
} | ||
|
||
// Process payload | ||
try { | ||
$output = $emailService->processWebhookBody($request->getContent()); | ||
return $this->json([ | ||
'status' => 200, | ||
'title' => 'success', | ||
'details' => 'Processed webhook.', | ||
'extra' => $output | ||
]); | ||
} catch (\Exception $e) { | ||
return $this->json([ | ||
'status' => 'error', | ||
'title' => 'Internal server error', | ||
'message' => $e->getMessage() | ||
], 500); | ||
} | ||
} | ||
|
||
|
||
} |
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