Skip to content

Commit

Permalink
Fix process callback with content type text plain
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo J. Veintimilla Vargas committed Mar 31, 2024
1 parent 5e13ecf commit 8d40bff
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
'name' => 'AmazonSESBundle',
'description' => 'Provides Amazon SES as an email transport and callback to process bounces',
'author' => 'Pablo Veintimilla Vargas',
'version' => '1.0.0',
'version' => '1.0.1',
'routes' => array()
);
32 changes: 24 additions & 8 deletions EventSubscriber/CallbackSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ public static function getSubscribedEvents(): array
public function processCallbackRequest(TransportWebhookEvent $webhookEvent): void
{
$this->webhookEvent = $webhookEvent;

$this->parseRequest();
if (!$this->validateCallbackRequest()) {
return;
}

$this->payload = $this->webhookEvent->getRequest()->request->all();
$type = $this->parseType();

try {
Expand All @@ -68,7 +68,24 @@ public function processCallbackRequest(TransportWebhookEvent $webhookEvent): voi
return;
}

$webhookEvent->setResponse(new Response("Callback processed: $type"));
$this->webhookEvent->setResponse(new Response("Callback processed: $type"));
}

/**
* Parse request to correct content type
*/
protected function parseRequest()
{
$request = $this->webhookEvent->getRequest();
$contentType = $request->getContentType();
switch ($contentType) {
case 'json':
$this->payload = $request->request->all();
break;
default:
$this->payload = json_decode($request->getContent(), true, 512, JSON_THROW_ON_ERROR);
break;
}
}

/**
Expand All @@ -83,18 +100,17 @@ protected function validateCallbackRequest(): bool
return false;
}

// Check post data
$postData = $this->webhookEvent->getRequest()->request->all();
if (empty($postData)) {
// Check data
if (!is_array($this->payload)) {
$message = 'There is no data to process.';
$this->logger->error($message);
$this->logger->error($message . $this->webhookEvent->getRequest()->getContent());
$this->webhookEvent->setResponse(new Response($message, Response::HTTP_BAD_REQUEST));
return false;
}

//Check type
if (
!$this->arrayKeysExists($this->allowdTypes, $postData)
!$this->arrayKeysExists($this->allowdTypes, $this->payload)
) {
$message = "Type of request is invalid";
$this->webhookEvent->setResponse(new Response($message, Response::HTTP_BAD_REQUEST));
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ composer require pabloveintimilla/mautic-amazon-ses
2. Install plugin

```
php bin\console mautic:plugins:reload
php bin/console mautic:plugins:reload
```

## CONFIGURATION MAUTIC
Expand Down

0 comments on commit 8d40bff

Please sign in to comment.