-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Webhook][RemoteEvent] Add Sendgrid #50704
- Loading branch information
1 parent
52a9292
commit bb57ad0
Showing
14 changed files
with
453 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
CHANGELOG | ||
========= | ||
|
||
6.4 | ||
--- | ||
|
||
* Add support for webhooks | ||
|
||
5.4 | ||
--- | ||
|
||
|
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
58 changes: 58 additions & 0 deletions
58
src/Symfony/Component/Mailer/Bridge/Sendgrid/RemoteEvent/SendgridPayloadConverter.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,58 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent; | ||
|
||
use Symfony\Component\RemoteEvent\Event\Mailer\AbstractMailerEvent; | ||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent; | ||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent; | ||
use Symfony\Component\RemoteEvent\Exception\ParseException; | ||
use Symfony\Component\RemoteEvent\PayloadConverterInterface; | ||
|
||
/** | ||
* @author WoutervanderLoop.nl <info@woutervanderloop.nl> | ||
*/ | ||
final class SendgridPayloadConverter implements PayloadConverterInterface | ||
{ | ||
public function convert(array $payload): AbstractMailerEvent | ||
{ | ||
if (\in_array($payload['event'], ['processed', 'delivered', 'bounce', 'dropped', 'deferred'], true)) { | ||
$name = match ($payload['event']) { | ||
'processed', 'delivered' => MailerDeliveryEvent::DELIVERED, | ||
'dropped' => MailerDeliveryEvent::DROPPED, | ||
'deferred' => MailerDeliveryEvent::DEFERRED, | ||
'bounce' => MailerDeliveryEvent::BOUNCE, | ||
}; | ||
$event = new MailerDeliveryEvent($name, $payload['sg_message_id'], $payload); | ||
$event->setReason($payload['reason'] ?? ''); | ||
} else { | ||
$name = match ($payload['event']) { | ||
'click' => MailerEngagementEvent::CLICK, | ||
'unsubscribe' => MailerEngagementEvent::UNSUBSCRIBE, | ||
'open' => MailerEngagementEvent::OPEN, | ||
'spamreport' => MailerEngagementEvent::SPAM, | ||
default => throw new ParseException(sprintf('Unsupported event "%s".', $payload['unsubscribe'])), | ||
}; | ||
$event = new MailerEngagementEvent($name, $payload['sg_message_id'], $payload); | ||
} | ||
|
||
if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['timestamp'])) { | ||
throw new ParseException(sprintf('Invalid date "%s".', $payload['timestamp'])); | ||
} | ||
|
||
$event->setDate($date); | ||
$event->setRecipientEmail($payload['email']); | ||
$event->setMetadata([]); | ||
$event->setTags($payload['category'] ?? []); | ||
|
||
return $event; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
src/Symfony/Component/Mailer/Bridge/Sendgrid/Tests/Webhook/Fixtures/webhook.json
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 @@ | ||
[{"email":"hello@world.com","event":"dropped","reason":"Bounced Address","sg_event_id":"ZHJvcC0xMDk5NDkxOS1MUnpYbF9OSFN0T0doUTRrb2ZTbV9BLTA","sg_message_id":"LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0","smtp-id":"<LRzXl_NHStOGhQ4kofSm_A@ismtpd0039p1iad1.sendgrid.net>","timestamp":1600112492}] |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Component/Mailer/Bridge/Sendgrid/Tests/Webhook/Fixtures/webhook.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,12 @@ | ||
<?php | ||
|
||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent; | ||
|
||
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DROPPED, 'LRzXl_NHStOGhQ4kofSm_A.filterdrecv-p3mdw1-756b745b58-kmzbl-18-5F5FC76C-9.0', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true)[0]); | ||
$wh->setRecipientEmail('hello@world.com'); | ||
$wh->setTags([]); | ||
$wh->setMetadata([]); | ||
$wh->setReason('Bounced Address'); | ||
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1600112492)); | ||
|
||
return $wh; |
48 changes: 48 additions & 0 deletions
48
...Component/Mailer/Bridge/Sendgrid/Tests/Webhook/SendgridMissingSignedRequestParserTest.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,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\Webhook; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\Webhook\SendgridRequestParser; | ||
use Symfony\Component\Webhook\Client\RequestParserInterface; | ||
use Symfony\Component\Webhook\Exception\RejectWebhookException; | ||
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase; | ||
|
||
/** | ||
* @author WoutervanderLoop.nl <info@woutervanderloop.nl> | ||
*/ | ||
class SendgridMissingSignedRequestParserTest extends AbstractRequestParserTestCase | ||
{ | ||
protected function createRequestParser(): RequestParserInterface | ||
{ | ||
$this->expectException(RejectWebhookException::class); | ||
$this->expectExceptionMessage('Signature is required.'); | ||
|
||
return new SendgridRequestParser(new SendgridPayloadConverter()); | ||
} | ||
|
||
/** | ||
* @see https://github.com/sendgrid/sendgrid-php/blob/9335dca98bc64456a72db73469d1dd67db72f6ea/test/unit/EventWebhookTest.php#L20 | ||
*/ | ||
protected function createRequest(string $payload): Request | ||
{ | ||
return Request::create('/', 'POST', [], [], [], [ | ||
'Content-Type' => 'application/json', | ||
], str_replace("\n", "\r\n", $payload)); | ||
} | ||
|
||
protected function getSecret(): string | ||
{ | ||
return 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=='; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...ymfony/Component/Mailer/Bridge/Sendgrid/Tests/Webhook/SendgridSignedRequestParserTest.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,46 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\Webhook; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\Webhook\SendgridRequestParser; | ||
use Symfony\Component\Webhook\Client\RequestParserInterface; | ||
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase; | ||
|
||
/** | ||
* @author WoutervanderLoop.nl <info@woutervanderloop.nl> | ||
*/ | ||
class SendgridSignedRequestParserTest extends AbstractRequestParserTestCase | ||
{ | ||
protected function createRequestParser(): RequestParserInterface | ||
{ | ||
return new SendgridRequestParser(new SendgridPayloadConverter(), true); | ||
} | ||
|
||
/** | ||
* @see https://github.com/sendgrid/sendgrid-php/blob/9335dca98bc64456a72db73469d1dd67db72f6ea/test/unit/EventWebhookTest.php#L20 | ||
*/ | ||
protected function createRequest(string $payload): Request | ||
{ | ||
return Request::create('/', 'POST', [], [], [], [ | ||
'Content-Type' => 'application/json', | ||
'HTTP_X-Twilio-Email-Event-Webhook-Signature' => 'MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=', | ||
'HTTP_X-Twilio-Email-Event-Webhook-Timestamp' => '1600112502', | ||
], str_replace("\n", "\r\n", $payload)); | ||
} | ||
|
||
protected function getSecret(): string | ||
{ | ||
return 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=='; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...fony/Component/Mailer/Bridge/Sendgrid/Tests/Webhook/SendgridUnsignedRequestParserTest.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,39 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\Webhook; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\Webhook\SendgridRequestParser; | ||
use Symfony\Component\Webhook\Client\RequestParserInterface; | ||
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase; | ||
|
||
/** | ||
* @author WoutervanderLoop.nl <info@woutervanderloop.nl> | ||
*/ | ||
class SendgridUnsignedRequestParserTest extends AbstractRequestParserTestCase | ||
{ | ||
protected function createRequestParser(): RequestParserInterface | ||
{ | ||
return new SendgridRequestParser(new SendgridPayloadConverter()); | ||
} | ||
|
||
/** | ||
* @see https://github.com/sendgrid/sendgrid-php/blob/9335dca98bc64456a72db73469d1dd67db72f6ea/test/unit/EventWebhookTest.php#L20 | ||
*/ | ||
protected function createRequest(string $payload): Request | ||
{ | ||
return Request::create('/', 'POST', [], [], [], [ | ||
'Content-Type' => 'application/json', | ||
], str_replace("\n", "\r\n", $payload)); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...y/Component/Mailer/Bridge/Sendgrid/Tests/Webhook/SendgridWrongSecretRequestParserTest.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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\Webhook; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\Webhook\SendgridRequestParser; | ||
use Symfony\Component\Webhook\Client\RequestParserInterface; | ||
use Symfony\Component\Webhook\Exception\RejectWebhookException; | ||
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase; | ||
|
||
/** | ||
* @author WoutervanderLoop.nl <info@woutervanderloop.nl> | ||
*/ | ||
class SendgridWrongSecretRequestParserTest extends AbstractRequestParserTestCase | ||
{ | ||
protected function createRequestParser(): RequestParserInterface | ||
{ | ||
$this->expectException(RejectWebhookException::class); | ||
$this->expectExceptionMessage('Public key is wrong.'); | ||
|
||
return new SendgridRequestParser(new SendgridPayloadConverter()); | ||
} | ||
|
||
/** | ||
* @see https://github.com/sendgrid/sendgrid-php/blob/9335dca98bc64456a72db73469d1dd67db72f6ea/test/unit/EventWebhookTest.php#L20 | ||
*/ | ||
protected function createRequest(string $payload): Request | ||
{ | ||
return Request::create('/', 'POST', [], [], [], [ | ||
'Content-Type' => 'application/json', | ||
'HTTP_X-Twilio-Email-Event-Webhook-Signature' => 'MEUCIGHQVtGj+Y3LkG9fLcxf3qfI10QysgDWmMOVmxG0u6ZUAiEAyBiXDWzM+uOe5W0JuG+luQAbPIqHh89M15TluLtEZtM=', | ||
'HTTP_X-Twilio-Email-Event-Webhook-Timestamp' => '1600112502', | ||
], str_replace("\n", "\r\n", $payload)); | ||
} | ||
|
||
protected function getSecret(): string | ||
{ | ||
return 'incorrect'; | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...omponent/Mailer/Bridge/Sendgrid/Tests/Webhook/SendgridWrongSignatureRequestParserTest.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,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Sendgrid\Tests\Webhook; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\RemoteEvent\SendgridPayloadConverter; | ||
use Symfony\Component\Mailer\Bridge\Sendgrid\Webhook\SendgridRequestParser; | ||
use Symfony\Component\Webhook\Client\RequestParserInterface; | ||
use Symfony\Component\Webhook\Exception\RejectWebhookException; | ||
use Symfony\Component\Webhook\Test\AbstractRequestParserTestCase; | ||
|
||
/** | ||
* @author WoutervanderLoop.nl <info@woutervanderloop.nl> | ||
*/ | ||
class SendgridWrongSignatureRequestParserTest extends AbstractRequestParserTestCase | ||
{ | ||
protected function createRequestParser(): RequestParserInterface | ||
{ | ||
$this->expectException(RejectWebhookException::class); | ||
$this->expectExceptionMessage('Signature is wrong.'); | ||
|
||
return new SendgridRequestParser(new SendgridPayloadConverter()); | ||
} | ||
|
||
/** | ||
* @see https://github.com/sendgrid/sendgrid-php/blob/9335dca98bc64456a72db73469d1dd67db72f6ea/test/unit/EventWebhookTest.php#L20 | ||
*/ | ||
protected function createRequest(string $payload): Request | ||
{ | ||
return Request::create('/', 'POST', [], [], [], [ | ||
'Content-Type' => 'application/json', | ||
'HTTP_X-Twilio-Email-Event-Webhook-Signature' => 'incorrect', | ||
'HTTP_X-Twilio-Email-Event-Webhook-Timestamp' => '1600112502', | ||
], str_replace("\n", "\r\n", $payload)); | ||
} | ||
|
||
protected function getSecret(): string | ||
{ | ||
return 'MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE83T4O/n84iotIvIW4mdBgQ/7dAfSmpqIM8kF9mN1flpVKS3GRqe62gw+2fNNRaINXvVpiglSI8eNEc6wEA3F+g=='; | ||
} | ||
} |
Oops, something went wrong.