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\SystemLog;
use App\Notifications\Ninja\EmailSpamNotification;
use Brevo\Client\Configuration;
use Brevo\Client\Model\GetTransacEmailContentEvents;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Brevo\BrevoClient;
use Brevo\Client\Api\TransactionalEmailsApi;
use Turbo124\Beacon\Facades\LightLogs;
class ProcessBrevoWebhook implements ShouldQueue
@ -102,7 +104,8 @@ class ProcessBrevoWebhook implements ShouldQueue
return $this->processBounce();
case 'spam':
return $this->processSpamComplaint();
case 'Open':
case 'unique_opened':
case 'opened':
return $this->processOpen();
default:
# code...
@ -111,40 +114,28 @@ class ProcessBrevoWebhook implements ShouldQueue
}
// {
// "Metadata": {
// "example": "value",
// "example_2": "value"
// },
// "RecordType": "Open",
// "FirstOpen": true,
// "Client": {
// "Name": "Chrome 35.0.1916.153",
// "Company": "Google",
// "Family": "Chrome"
// },
// "OS": {
// "Name": "OS X 10.7 Lion",
// "Company": "Apple Computer, Inc.",
// "Family": "OS X 10"
// },
// "Platform": "WebMail",
// "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",
// "ReadSeconds": 5,
// "Geo": {
// "CountryISOCode": "RS",
// "Country": "Serbia",
// "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"
// "event" : "unique_opened",
// "email" : "example@example.com",
// "id" : 1,
// "date" : "yyyy-m-d h:i:s",
// "message-id" : "<xxx@msgid.domain>",
// "subject" : "Test subject",
// "tag" : "<defined-tag>",
// "sending_ip" : "xxx.xx.xxx.xx",
// "ts_epoch" : 1534486682000,
// "template_id" : 1
// }
// {
// "event" : "opened",
// "email" : "frichris@hotmail.fr",
// "id" : 1,
// "date" : "yyyy-m-d h:i:s",
// "message-id" : "<xxx@msgid.domain>",
// "subject" : "Test subject",
// "tag" : "<defined-tag>",
// "sending_ip" : "xxx.xx.xxx.xx",
// "ts_epoch" : 1534486682000,
// "template_id" : 1
// }
private function processOpen()
@ -251,7 +242,7 @@ class ProcessBrevoWebhook implements ShouldQueue
$bounce = new EmailBounce(
$this->request['tag'],
$this->request['From'],
$this->request['From'], // TODO: @turbo124 is this the recipent?
$this->request['message-id']
);
@ -288,7 +279,7 @@ class ProcessBrevoWebhook implements ShouldQueue
$spam = new EmailSpam(
$this->request['tag'],
$this->request['From'],
$this->request['From'], // TODO
$this->request['message-id']
);
@ -337,8 +328,8 @@ class ProcessBrevoWebhook implements ShouldQueue
public function getRawMessage(string $message_id)
{
$Brevo = new BrevoClient(config('services.brevo.key'));
$messageDetail = $Brevo->getOutboundMessageDetails($message_id);
$Brevo = new TransactionalEmailsApi(null, Configuration::getDefaultConfiguration()->setApiKey('api-key', config('services.brevo.key')));
$messageDetail = $Brevo->getTransacEmailContent($message_id);
return $messageDetail;
}
@ -350,7 +341,7 @@ class ProcessBrevoWebhook implements ShouldQueue
$messageDetail = $this->getRawMessage($message_id);
$event = collect($messageDetail->messageevents)->first(function ($event) {
$event = collect($messageDetail->getEvents())->first(function ($event) {
return $event?->Details?->BounceID ?? false;
@ -360,6 +351,7 @@ class ProcessBrevoWebhook implements ShouldQueue
}
// TODO
private function fetchMessage(): array
{
if (strlen($this->request['message-id']) < 1) {
@ -368,13 +360,12 @@ class ProcessBrevoWebhook implements ShouldQueue
try {
$Brevo = new BrevoClient(config('services.brevo.key'));
$messageDetail = $Brevo->getOutboundMessageDetails($this->request['message-id']);
$messageDetail = $this->getRawMessage($this->request['message-id']);
$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 [
'bounce_id' => $event?->Details?->BounceID ?? '',

View File

@ -47,6 +47,7 @@
"doctrine/dbal": "^3.0",
"eway/eway-rapid-php": "^1.3",
"fakerphp/faker": "^1.14",
"getbrevo/brevo-php": "^1.0",
"gocardless/gocardless-pro": "^4.12",
"google/apiclient": "^2.7",
"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",
"This file is @generated automatically"
],
"content-hash": "ba57ee16621201bac10def4422ba9161",
"content-hash": "b6f77d8c2532d7f443899c5ed77fcf8f",
"packages": [
{
"name": "afosto/yaac",
@ -2431,6 +2431,69 @@
],
"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",
"version": "4.28.0",