adding brevo client and first changes to webhook data

This commit is contained in:
paulwer 2023-12-21 19:05:21 +01:00
parent 6a62be3715
commit c9178a6a01
3 changed files with 102 additions and 47 deletions

View File

@ -22,12 +22,14 @@ use App\Models\QuoteInvitation;
use App\Models\RecurringInvoiceInvitation; use App\Models\RecurringInvoiceInvitation;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\Notifications\Ninja\EmailSpamNotification; use App\Notifications\Ninja\EmailSpamNotification;
use Brevo\Client\Configuration;
use Brevo\Client\Model\GetTransacEmailContentEvents;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Brevo\BrevoClient; use Brevo\Client\Api\TransactionalEmailsApi;
use Turbo124\Beacon\Facades\LightLogs; use Turbo124\Beacon\Facades\LightLogs;
class ProcessBrevoWebhook implements ShouldQueue class ProcessBrevoWebhook implements ShouldQueue
@ -102,7 +104,8 @@ class ProcessBrevoWebhook implements ShouldQueue
return $this->processBounce(); return $this->processBounce();
case 'spam': case 'spam':
return $this->processSpamComplaint(); return $this->processSpamComplaint();
case 'Open': case 'unique_opened':
case 'opened':
return $this->processOpen(); return $this->processOpen();
default: default:
# code... # code...
@ -111,41 +114,29 @@ class ProcessBrevoWebhook implements ShouldQueue
} }
// { // {
// "Metadata": { // "event" : "unique_opened",
// "example": "value", // "email" : "example@example.com",
// "example_2": "value" // "id" : 1,
// }, // "date" : "yyyy-m-d h:i:s",
// "RecordType": "Open", // "message-id" : "<xxx@msgid.domain>",
// "FirstOpen": true, // "subject" : "Test subject",
// "Client": { // "tag" : "<defined-tag>",
// "Name": "Chrome 35.0.1916.153", // "sending_ip" : "xxx.xx.xxx.xx",
// "Company": "Google", // "ts_epoch" : 1534486682000,
// "Family": "Chrome" // "template_id" : 1
// }, // }
// "OS": { // {
// "Name": "OS X 10.7 Lion", // "event" : "opened",
// "Company": "Apple Computer, Inc.", // "email" : "frichris@hotmail.fr",
// "Family": "OS X 10" // "id" : 1,
// }, // "date" : "yyyy-m-d h:i:s",
// "Platform": "WebMail", // "message-id" : "<xxx@msgid.domain>",
// "UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36", // "subject" : "Test subject",
// "ReadSeconds": 5, // "tag" : "<defined-tag>",
// "Geo": { // "sending_ip" : "xxx.xx.xxx.xx",
// "CountryISOCode": "RS", // "ts_epoch" : 1534486682000,
// "Country": "Serbia", // "template_id" : 1
// "RegionISOCode": "VO", // }
// "Region": "Autonomna Pokrajina Vojvodina",
// "City": "Novi Sad",
// "Zip": "21000",
// "Coords": "45.2517,19.8369",
// "IP": "188.2.95.4"
// },
// "MessageID": "00000000-0000-0000-0000-000000000000",
// "MessageStream": "outbound",
// "ReceivedAt": "2022-02-06T06:37:48Z",
// "Tag": "welcome-email",
// "Recipient": "john@example.com"
// }
private function processOpen() private function processOpen()
{ {
@ -251,7 +242,7 @@ class ProcessBrevoWebhook implements ShouldQueue
$bounce = new EmailBounce( $bounce = new EmailBounce(
$this->request['tag'], $this->request['tag'],
$this->request['From'], $this->request['From'], // TODO: @turbo124 is this the recipent?
$this->request['message-id'] $this->request['message-id']
); );
@ -288,7 +279,7 @@ class ProcessBrevoWebhook implements ShouldQueue
$spam = new EmailSpam( $spam = new EmailSpam(
$this->request['tag'], $this->request['tag'],
$this->request['From'], $this->request['From'], // TODO
$this->request['message-id'] $this->request['message-id']
); );
@ -337,8 +328,8 @@ class ProcessBrevoWebhook implements ShouldQueue
public function getRawMessage(string $message_id) public function getRawMessage(string $message_id)
{ {
$Brevo = new BrevoClient(config('services.brevo.key')); $Brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', config('services.brevo.key')));
$messageDetail = $Brevo->getOutboundMessageDetails($message_id); $messageDetail = $Brevo->getTransacEmailContent($message_id);
return $messageDetail; return $messageDetail;
} }
@ -350,7 +341,7 @@ class ProcessBrevoWebhook implements ShouldQueue
$messageDetail = $this->getRawMessage($message_id); $messageDetail = $this->getRawMessage($message_id);
$event = collect($messageDetail->messageevents)->first(function ($event) { $event = collect($messageDetail->getEvents())->first(function ($event) {
return $event?->Details?->BounceID ?? false; return $event?->Details?->BounceID ?? false;
@ -360,6 +351,7 @@ class ProcessBrevoWebhook implements ShouldQueue
} }
// TODO
private function fetchMessage(): array private function fetchMessage(): array
{ {
if (strlen($this->request['message-id']) < 1) { if (strlen($this->request['message-id']) < 1) {
@ -368,13 +360,12 @@ class ProcessBrevoWebhook implements ShouldQueue
try { try {
$Brevo = new BrevoClient(config('services.brevo.key')); $messageDetail = $this->getRawMessage($this->request['message-id']);
$messageDetail = $Brevo->getOutboundMessageDetails($this->request['message-id']);
$recipients = collect($messageDetail['recipients'])->flatten()->implode(','); $recipients = collect($messageDetail['recipients'])->flatten()->implode(',');
$subject = $messageDetail->subject ?? ''; $subject = $messageDetail->getSubject() ?? '';
$events = collect($messageDetail->messageevents)->map(function ($event) { $events = collect($messageDetail->getEvents())->map(function (GetTransacEmailContentEvents $event) {
return [ return [
'bounce_id' => $event?->Details?->BounceID ?? '', 'bounce_id' => $event?->Details?->BounceID ?? '',

View File

@ -47,6 +47,7 @@
"doctrine/dbal": "^3.0", "doctrine/dbal": "^3.0",
"eway/eway-rapid-php": "^1.3", "eway/eway-rapid-php": "^1.3",
"fakerphp/faker": "^1.14", "fakerphp/faker": "^1.14",
"getbrevo/brevo-php": "^1.0",
"gocardless/gocardless-pro": "^4.12", "gocardless/gocardless-pro": "^4.12",
"google/apiclient": "^2.7", "google/apiclient": "^2.7",
"guzzlehttp/guzzle": "^7.2", "guzzlehttp/guzzle": "^7.2",

65
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "ba57ee16621201bac10def4422ba9161", "content-hash": "b6f77d8c2532d7f443899c5ed77fcf8f",
"packages": [ "packages": [
{ {
"name": "afosto/yaac", "name": "afosto/yaac",
@ -2431,6 +2431,69 @@
], ],
"time": "2023-10-12T05:21:21+00:00" "time": "2023-10-12T05:21:21+00:00"
}, },
{
"name": "getbrevo/brevo-php",
"version": "v1.0.2",
"source": {
"type": "git",
"url": "https://github.com/getbrevo/brevo-php.git",
"reference": "6c3286e62327277fd8445cddb057d44e850722c0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/getbrevo/brevo-php/zipball/6c3286e62327277fd8445cddb057d44e850722c0",
"reference": "6c3286e62327277fd8445cddb057d44e850722c0",
"shasum": ""
},
"require": {
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"guzzlehttp/guzzle": "^7.4.0",
"php": ">=5.6"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~1.12",
"phpunit/phpunit": "^4.8",
"squizlabs/php_codesniffer": "~2.6"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.x.x-dev"
}
},
"autoload": {
"psr-4": {
"Brevo\\Client\\": "lib/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Brevo Developers",
"email": "contact@brevo.com",
"homepage": "https://www.brevo.com/"
}
],
"description": "Official Brevo provided RESTFul API V3 php library",
"homepage": "https://github.com/getbrevo/brevo-php",
"keywords": [
"api",
"brevo",
"php",
"sdk",
"swagger"
],
"support": {
"issues": "https://github.com/getbrevo/brevo-php/issues",
"source": "https://github.com/getbrevo/brevo-php/tree/v1.0.2"
},
"time": "2023-07-14T10:00:50+00:00"
},
{ {
"name": "gocardless/gocardless-pro", "name": "gocardless/gocardless-pro",
"version": "4.28.0", "version": "4.28.0",