1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Datomatic\LaravelHubspotEmailNotificationChannel; |
4
|
|
|
|
5
|
|
|
use Datomatic\LaravelHubspotEmailNotificationChannel\Exceptions\CouldNotSendNotification; |
6
|
|
|
use Datomatic\LaravelHubspotEmailNotificationChannel\Exceptions\InvalidConfiguration; |
7
|
|
|
use Illuminate\Notifications\Notification; |
8
|
|
|
use Illuminate\Support\Facades\Http; |
9
|
|
|
|
10
|
|
|
class HubspotEmailChannel |
11
|
|
|
{ |
12
|
|
|
// HUBSPOT API CALLS: |
13
|
|
|
|
14
|
|
|
// endpoint: POST /crm/v3/objects/emails; |
15
|
|
|
// api ref: https://developers.hubspot.com/docs/api/crm/email |
16
|
|
|
// Standard scope(s) sales-email-read |
17
|
|
|
// Granular scope(s) crm.objects.contacts.write |
18
|
|
|
|
19
|
|
|
// endpoint: PUT /crm/v3/objects/emails/{emailId}/associations/{toObjectType}/{toObjectId}/{associationType}; |
20
|
|
|
// api ref: https://developers.hubspot.com/docs/api/crm/email |
21
|
|
|
// Standard scope(s) sales-email-read |
22
|
|
|
// Granular scope(s) crm.objects.contacts.write |
23
|
|
|
|
24
|
|
|
public const HUBSPOT_URL = 'https://api.hubapi.com/crm/v3/objects/emails'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* HubspotEngagementChannel constructor. |
28
|
|
|
*/ |
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Send the given notification. |
35
|
|
|
* |
36
|
|
|
* @param mixed $notifiable |
37
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
38
|
|
|
* |
39
|
|
|
* @throws \Datomatic\LaravelHubspotEmailNotificationChannel\Exceptions\CouldNotSendNotification|InvalidConfiguration |
40
|
|
|
*/ |
41
|
|
|
public function send($notifiable, Notification $notification): ?array |
42
|
|
|
{ |
43
|
|
|
$hubspotContactId = $notifiable->getHubspotContactId($notification); |
44
|
|
|
|
45
|
|
|
if (empty($hubspotContactId)) { |
46
|
|
|
return null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (! method_exists($notification, 'toMail')) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$message = $notification->toMail($notifiable); |
54
|
|
|
|
55
|
|
|
$params = [ |
56
|
|
|
"properties" => [ |
57
|
|
|
"hs_timestamp" => now()->getPreciseTimestamp(3), |
58
|
|
|
"hubspot_owner_id" => config('hubspot.hubspot_owner_id'), |
59
|
|
|
"hs_email_direction" => "EMAIL", |
60
|
|
|
"hs_email_status" => "SENT", |
61
|
|
|
"hs_email_subject" => $message->subject, |
62
|
|
|
"hs_email_text" => (string) $message->render(), ], |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
$response = $this->callApi(self::HUBSPOT_URL, 'post', $params); |
66
|
|
|
|
67
|
|
|
$hubspotEmail = $response->json(); |
68
|
|
|
|
69
|
|
|
if ($response->status() == 201 && ! empty($hubspotEmail['id'])) { |
70
|
|
|
|
71
|
|
|
$url = self::HUBSPOT_URL.'/'.$hubspotEmail['id'].'/associations/contacts/'.$hubspotContactId.'/198'; |
72
|
|
|
$newResp = $this->callApi($url, 'put'); |
73
|
|
|
|
74
|
|
|
if ($newResp->status() != 200) { |
75
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($newResp->body()); |
76
|
|
|
} |
77
|
|
|
$hubspotEmail['associations'] = $newResp['associations']; |
78
|
|
|
} else { |
79
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($response->body()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $hubspotEmail; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function callApi($baseUrl, $method, $params = []) |
86
|
|
|
{ |
87
|
|
|
$apiKey = config('hubspot.api_key'); |
88
|
|
|
if (is_null(config('hubspot.hubspot_owner_id'))) { |
89
|
|
|
throw InvalidConfiguration::configurationNotSet(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$url = $baseUrl.'?hapikey='.$apiKey; |
93
|
|
|
$http = Http::acceptJson(); |
94
|
|
|
|
95
|
|
|
if (is_null($apiKey)) { |
96
|
|
|
if (is_null(config('hubspot.access_token'))) { |
97
|
|
|
throw InvalidConfiguration::configurationNotSet(); |
98
|
|
|
} |
99
|
|
|
$url = $baseUrl; |
100
|
|
|
$http = $http->withToken(config('hubspot.access_token')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $http->$method($url, $params); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|