forked from symfony/symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Webhook] Add Mailchimp webhook (fixes symfony#50285)
- Loading branch information
Showing
28 changed files
with
801 additions
and
1 deletion.
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
78 changes: 78 additions & 0 deletions
78
src/Symfony/Component/Mailer/Bridge/Mailchimp/RemoteEvent/MailchimpPayloadConverter.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,78 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Mailer\Bridge\Mailchimp\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; | ||
|
||
final class MailchimpPayloadConverter implements PayloadConverterInterface | ||
{ | ||
public function convert(array $payload): AbstractMailerEvent | ||
{ | ||
if (\in_array($payload['event'], ['send', 'deferral', 'soft_bounce', 'hard_bounce', 'delivered', 'reject'], true)) { | ||
$name = match ($payload['event']) { | ||
'send' => MailerDeliveryEvent::RECEIVED, | ||
'deferral', => MailerDeliveryEvent::DEFERRED, | ||
'soft_bounce', 'hard_bounce' => MailerDeliveryEvent::BOUNCE, | ||
'delivered' => MailerDeliveryEvent::DELIVERED, | ||
'reject' => MailerDeliveryEvent::DROPPED, | ||
}; | ||
|
||
$event = new MailerDeliveryEvent($name, $payload['msg']['_id'], $payload); | ||
// reason is only available on failed messages | ||
$event->setReason($this->getReason($payload)); | ||
} else { | ||
$name = match ($payload['event']) { | ||
'click' => MailerEngagementEvent::CLICK, | ||
'open' => MailerEngagementEvent::OPEN, | ||
'spam' => MailerEngagementEvent::SPAM, | ||
'unsub' => MailerEngagementEvent::UNSUBSCRIBE, | ||
default => throw new ParseException(\sprintf('Unsupported event "%s".', $payload['event'])), | ||
}; | ||
$event = new MailerEngagementEvent($name, $payload['msg']['_id'], $payload); | ||
} | ||
|
||
if (!$date = \DateTimeImmutable::createFromFormat('U', $payload['msg']['ts'])) { | ||
throw new ParseException(\sprintf('Invalid date "%s".', $payload['msg']['ts'])); | ||
} | ||
$event->setDate($date); | ||
$event->setRecipientEmail($payload['msg']['email']); | ||
$event->setMetadata($payload['msg']['metadata']); | ||
$event->setTags($payload['msg']['tags']); | ||
|
||
return $event; | ||
} | ||
|
||
private function getReason(array $payload): string | ||
{ | ||
if (null !== $payload['msg']['diag']) { | ||
return $payload['msg']['diag']; | ||
} | ||
if (null !== $payload['msg']['bounce_description']) { | ||
return $payload['msg']['bounce_description']; | ||
} | ||
if (!empty($payload['msg']['smtp_events'])) { | ||
$reasons = []; | ||
foreach ($payload['msg']['smtp_events'] as $event) { | ||
$reasons[] = \sprintf('type: %s diag: %s', $event['type'], $event['diag']); | ||
} | ||
|
||
// Return concatenated reasons or an empty string if no reasons found | ||
return implode(' ', $reasons); | ||
} | ||
|
||
return ''; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/batch.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,70 @@ | ||
{ | ||
"mandrill_events": [ | ||
{ | ||
"ts": 1365109999, | ||
"event": "click", | ||
"msg": { | ||
"ts": 1365109999, | ||
"subject": "Foo bar", | ||
"email": "[email protected]", | ||
"sender": "[email protected]", | ||
"tags": [ | ||
"my_tag_1", | ||
"my_tag_2" | ||
], | ||
"smtp_events": null, | ||
"opens": [ | ||
{ | ||
"ts": 1365110000 | ||
} | ||
], | ||
"clicks": [ | ||
{ | ||
"ts": 1365110000, | ||
"url": "https://www.example.com" | ||
} | ||
], | ||
"state": "sent", | ||
"metadata": { | ||
"mandrill-var-1": "foo", | ||
"mandrill-var-2": "bar" | ||
}, | ||
"_id": "7761630", | ||
"_version": "123", | ||
"subaccount": null, | ||
"diag": null, | ||
"bounce_description": null, | ||
"template": null | ||
} | ||
}, | ||
{ | ||
"ts": 1365109999, | ||
"event": "deferral", | ||
"msg": { | ||
"ts": 1365109999, | ||
"subject": "Foo bar", | ||
"email": "[email protected]", | ||
"sender": "[email protected]", | ||
"tags": [ | ||
"my_tag_1", | ||
"my_tag_2" | ||
], | ||
"smtp_events": null, | ||
"opens": null, | ||
"clicks": null, | ||
"state": "sent", | ||
"metadata": { | ||
"mandrill-var-1": "foo", | ||
"mandrill-var-2": "bar" | ||
}, | ||
"_id": "7761631", | ||
"_version": "123", | ||
"subaccount": null, | ||
"diag": null, | ||
"bounce_description": null, | ||
"template": null | ||
} | ||
} | ||
], | ||
"X-Mandrill-Signature": "Mjask0i0giC/nEAYZrt6sX6JF2M=" | ||
} |
31 changes: 31 additions & 0 deletions
31
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/batch.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,31 @@ | ||
<?php | ||
|
||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent; | ||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent; | ||
|
||
$wh1 = new MailerEngagementEvent( | ||
MailerEngagementEvent::CLICK, '7761630', json_decode( | ||
file_get_contents( | ||
str_replace('.php', '.json', __FILE__) | ||
), true, flags: JSON_THROW_ON_ERROR | ||
)['mandrill_events'][0] | ||
); | ||
$wh1->setRecipientEmail('[email protected]'); | ||
$wh1->setTags(['my_tag_1', 'my_tag_2']); | ||
$wh1->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']); | ||
$wh1->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999)); | ||
|
||
$wh2 = new MailerDeliveryEvent( | ||
MailerDeliveryEvent::DEFERRED, '7761631', json_decode( | ||
file_get_contents( | ||
str_replace('.php', '.json', __FILE__) | ||
), true, flags: JSON_THROW_ON_ERROR | ||
)['mandrill_events'][1] | ||
); | ||
$wh2->setRecipientEmail('[email protected]'); | ||
$wh2->setTags(['my_tag_1', 'my_tag_2']); | ||
$wh2->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']); | ||
$wh2->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999)); | ||
$wh2->setReason(''); | ||
|
||
return [$wh1, $wh2]; |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/click.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,42 @@ | ||
{ | ||
"mandrill_events": [ | ||
{ | ||
"ts": 1365109999, | ||
"event": "click", | ||
"msg": { | ||
"ts": 1365109999, | ||
"subject": "Foo bar", | ||
"email": "[email protected]", | ||
"sender": "[email protected]", | ||
"tags": [ | ||
"my_tag_1", | ||
"my_tag_2" | ||
], | ||
"smtp_events": null, | ||
"opens": [ | ||
{ | ||
"ts": 1365110000 | ||
} | ||
], | ||
"clicks": [ | ||
{ | ||
"ts": 1365110000, | ||
"url": "https://www.example.com" | ||
} | ||
], | ||
"state": "sent", | ||
"metadata": { | ||
"mandrill-var-1": "foo", | ||
"mandrill-var-2": "bar" | ||
}, | ||
"_id": "7761630", | ||
"_version": "123", | ||
"subaccount": null, | ||
"diag": null, | ||
"bounce_description": null, | ||
"template": null | ||
} | ||
} | ||
], | ||
"X-Mandrill-Signature": "5mcP4EaDf9cd1tFLAIY+E13Eqmw=" | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/click.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,11 @@ | ||
<?php | ||
|
||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerEngagementEvent; | ||
|
||
$wh = new MailerEngagementEvent(MailerEngagementEvent::CLICK, '7761630', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events'][0]); | ||
$wh->setRecipientEmail('[email protected]'); | ||
$wh->setTags(['my_tag_1', 'my_tag_2']); | ||
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']); | ||
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999)); | ||
|
||
return [$wh]; |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/deferral.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,33 @@ | ||
{ | ||
"mandrill_events": [ | ||
{ | ||
"ts": 1365109999, | ||
"event": "deferral", | ||
"msg": { | ||
"ts": 1365109999, | ||
"subject": "Foo bar", | ||
"email": "[email protected]", | ||
"sender": "[email protected]", | ||
"tags": [ | ||
"my_tag_1", | ||
"my_tag_2" | ||
], | ||
"smtp_events": null, | ||
"opens": null, | ||
"clicks": null, | ||
"state": "sent", | ||
"metadata": { | ||
"mandrill-var-1": "foo", | ||
"mandrill-var-2": "bar" | ||
}, | ||
"_id": "7761631", | ||
"_version": "123", | ||
"subaccount": null, | ||
"diag": null, | ||
"bounce_description": null, | ||
"template": null | ||
} | ||
} | ||
], | ||
"X-Mandrill-Signature": "VuT14QGTsui1T7B+FqjV6SqACDA=" | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/deferral.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::DEFERRED, '7761631', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events'][0]); | ||
$wh->setRecipientEmail('[email protected]'); | ||
$wh->setTags(['my_tag_1', 'my_tag_2']); | ||
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']); | ||
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999)); | ||
$wh->setReason(''); | ||
|
||
return [$wh]; |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/delivered.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,33 @@ | ||
{ | ||
"mandrill_events": [ | ||
{ | ||
"ts": 1365109999, | ||
"event": "delivered", | ||
"msg": { | ||
"ts": 1365109999, | ||
"subject": "Foo bar", | ||
"email": "[email protected]", | ||
"sender": "[email protected]", | ||
"tags": [ | ||
"my_tag_1", | ||
"my_tag_2" | ||
], | ||
"smtp_events": null, | ||
"opens": null, | ||
"clicks": null, | ||
"state": "sent", | ||
"metadata": { | ||
"mandrill-var-1": "foo", | ||
"mandrill-var-2": "bar" | ||
}, | ||
"_id": "7761632", | ||
"_version": "123", | ||
"subaccount": null, | ||
"diag": null, | ||
"bounce_description": null, | ||
"template": null | ||
} | ||
} | ||
], | ||
"X-Mandrill-Signature": "Q287c9JdfPkKnVE9vRJw5jd+XcE=" | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/delivered.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,11 @@ | ||
<?php | ||
|
||
use Symfony\Component\RemoteEvent\Event\Mailer\MailerDeliveryEvent; | ||
|
||
$wh = new MailerDeliveryEvent(MailerDeliveryEvent::DELIVERED, '7761632', json_decode(file_get_contents(str_replace('.php', '.json', __FILE__)), true, flags: JSON_THROW_ON_ERROR)['mandrill_events'][0]); | ||
$wh->setRecipientEmail('[email protected]'); | ||
$wh->setTags(['my_tag_1', 'my_tag_2']); | ||
$wh->setMetadata(['mandrill-var-1' => 'foo', 'mandrill-var-2' => 'bar']); | ||
$wh->setDate(\DateTimeImmutable::createFromFormat('U', 1365109999)); | ||
|
||
return [$wh]; |
33 changes: 33 additions & 0 deletions
33
src/Symfony/Component/Mailer/Bridge/Mailchimp/Tests/Webhook/Fixtures/hard_bounce.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,33 @@ | ||
{ | ||
"mandrill_events": [ | ||
{ | ||
"ts": 1365109999, | ||
"event": "hard_bounce", | ||
"msg": { | ||
"ts": 1365109999, | ||
"subject": "Foo bar", | ||
"email": "[email protected]", | ||
"sender": "[email protected]", | ||
"tags": [ | ||
"my_tag_1", | ||
"my_tag_2" | ||
], | ||
"smtp_events": null, | ||
"opens": null, | ||
"clicks": null, | ||
"state": "sent", | ||
"metadata": { | ||
"mandrill-var-1": "foo", | ||
"mandrill-var-2": "bar" | ||
}, | ||
"_id": "7761633", | ||
"_version": "123", | ||
"subaccount": null, | ||
"diag": null, | ||
"bounce_description": "hard bounce", | ||
"template": null | ||
} | ||
} | ||
], | ||
"X-Mandrill-Signature": "ip7K8yTxSFakEhIGiJz8tydjVsQ=" | ||
} |
Oops, something went wrong.