|
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/v4/objects/{fromObjectType}/{fromObjectId}/associations/default/{toObjectType}/{toObjectId}; |
|
20
|
|
|
// api ref: https://developers.hubspot.com/docs/api/crm/associations |
|
21
|
|
|
|
|
22
|
|
|
public const HUBSPOT_URL_V3 = 'https://api.hubapi.com/crm/v3/objects/'; |
|
23
|
|
|
public const HUBSPOT_URL_V4 = 'https://api.hubapi.com/crm/v4/objects/'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* HubspotEngagementChannel constructor. |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Send the given notification. |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $notifiable |
|
36
|
|
|
* @param \Illuminate\Notifications\Notification $notification |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \Datomatic\LaravelHubspotEmailNotificationChannel\Exceptions\CouldNotSendNotification|InvalidConfiguration |
|
39
|
|
|
*/ |
|
40
|
|
|
public function send($notifiable, Notification $notification): ?array |
|
41
|
|
|
{ |
|
42
|
|
|
$hubspotContactId = $notifiable->getHubspotContactId($notification); |
|
43
|
|
|
|
|
44
|
|
|
if (empty($hubspotContactId)) { |
|
45
|
|
|
return null; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
if (! method_exists($notification, 'toMail')) { |
|
49
|
|
|
return null; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$message = $notification->toMail($notifiable); |
|
53
|
|
|
|
|
54
|
|
|
$params = [ |
|
55
|
|
|
"properties" => [ |
|
56
|
|
|
"hs_timestamp" => round(microtime(true) * 1000), |
|
57
|
|
|
"hubspot_owner_id" => $message->metadata['hubspot_owner_id'] ?? config('hubspot.hubspot_owner_id'), |
|
58
|
|
|
"hs_email_direction" => "EMAIL", |
|
59
|
|
|
"hs_email_status" => "SENT", |
|
60
|
|
|
"hs_email_subject" => $message->subject, |
|
61
|
|
|
"hs_email_text" => (string)$message->render(), |
|
62
|
|
|
], |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
$hubspotEmail = $this->callApi(self::HUBSPOT_URL_V3 . 'emails', 'post', $params); |
|
66
|
|
|
|
|
67
|
|
|
if (! empty($hubspotEmail['id'])) { |
|
68
|
|
|
|
|
69
|
|
|
$this->callApi( |
|
70
|
|
|
self::HUBSPOT_URL_V4 . 'contact/'. $hubspotContactId .'/associations/default/email/' . $hubspotEmail['id'], |
|
71
|
|
|
'put', |
|
72
|
|
|
["associationTypeId" => 197] |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
if(config('hubspot.company_email_associations')) { |
|
76
|
|
|
$contactResp = $this->callApi( |
|
77
|
|
|
self::HUBSPOT_URL_V3 . 'contacts/' . $hubspotContactId, |
|
78
|
|
|
'get', |
|
79
|
|
|
['properties' => 'associatedcompanyid'] |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$hubspotCompanyId = $contactResp['properties']['associatedcompanyid'] ?? null; |
|
83
|
|
|
|
|
84
|
|
|
if ($hubspotCompanyId) { |
|
85
|
|
|
$this->callApi( |
|
86
|
|
|
self::HUBSPOT_URL_V4 . 'company/' . $hubspotCompanyId . '/associations/default/email/' . $hubspotEmail['id'], |
|
87
|
|
|
'put', |
|
88
|
|
|
["associationTypeId" => 185] |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $hubspotEmail; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function callApi(string $baseUrl, string $method, array $params = []): array |
|
98
|
|
|
{ |
|
99
|
|
|
if (is_null(config('hubspot.hubspot_owner_id'))) { |
|
100
|
|
|
throw InvalidConfiguration::configurationNotSet(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$apiKey = config('hubspot.api_key'); |
|
104
|
|
|
if($apiKey) { |
|
105
|
|
|
$params['hapikey'] = $apiKey; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$http = Http::acceptJson()->retry(3, 11 * 1000); |
|
109
|
|
|
|
|
110
|
|
|
if (is_null($apiKey)) { |
|
111
|
|
|
if (is_null(config('hubspot.access_token'))) { |
|
112
|
|
|
throw InvalidConfiguration::configurationNotSet(); |
|
113
|
|
|
} |
|
114
|
|
|
$http = $http->withToken(config('hubspot.access_token')); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$response = $http->$method($baseUrl, $params); |
|
118
|
|
|
|
|
119
|
|
|
if ($response->failed()) { |
|
120
|
|
|
throw CouldNotSendNotification::serviceRespondedWithAnError($baseUrl.' '.$response->status().' '.$response->body()); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
return $response->json(); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|