Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement usage with private apps and access token #1

Merged
merged 4 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ to [Hubspot Email Engagement V3](https://developers.hubspot.com/docs/api/crm/ema

## Contents

- [Installation](#installation)
- [Hubspot Email Notifications Channel for Laravel](#hubspot-email-notifications-channel-for-laravel)
- [Contents](#contents)
- [Installation](#installation)
- [Setting up the HubspotEmail service](#setting-up-the-hubspotemail-service)
- [Usage](#usage)
- [Changelog](#changelog)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)
- [Usage](#usage)
- [Email notification](#email-notification)
- [Example](#example)
- [Notification example](#notification-example)
- [Model example](#model-example)
- [Changelog](#changelog)
- [Testing](#testing)
- [Security](#security)
- [Contributing](#contributing)
- [Credits](#credits)
- [License](#license)

## Installation

Expand All @@ -30,11 +36,13 @@ composer require datomatic/laravel-hubspot-email-notification-channel

### Setting up the HubspotEmail service

Generate API Key from [Hubspot](https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key).
Generate an [API Key](https://knowledge.hubspot.com/integrations/how-do-i-get-my-hubspot-api-key) or a [Private App](https://developers.hubspot.com/docs/api/private-apps) from Hubspot.
**Important!** From November 30th 2022 Hubspot will require you to use only private apps. If you have both API Key and Private App configured, to switch using only Private App just remove `HUBSPOT_API_KEY` from your .env file.

Configure your Hubspot API on .env
```dotenv
HUBSPOT_API_KEY=XXXXXXXX
HUBSPOT_ACCESS_TOKEN=XXXXXXXX
HUBSPOT_OWNER_ID=XXX //an Hubspot owner id to save as email creator
```

Expand All @@ -45,10 +53,11 @@ php artisan vendor:publish --provider="Datomatic\LaravelHubspotEmailNotification
This will publish a file hubspot.php in your config directory with the following contents:

```php
// config/services.php
// config/hubspot.php

return [
'api_key' => env('HUBSPOT_API_KEY'),
'access_token' => env('HUBSPOT_API_KEY'),
'hubspot_owner_id' => env('HUBSPOT_OWNER_ID',null)
];
```
Expand Down
1 change: 1 addition & 0 deletions config/hubspot.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

return [
'api_key' => env('HUBSPOT_API_KEY'),
'access_token' => env('HUBSPOT_ACCESS_TOKEN'),
'hubspot_owner_id' => env('HUBSPOT_OWNER_ID',null)
];
65 changes: 48 additions & 17 deletions src/HubspotEmailChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@

class HubspotEmailChannel
{
// HUBSPOT API CALLS:

// endpoint: POST /crm/v3/objects/emails;
// api ref: https://developers.hubspot.com/docs/api/crm/email
// Standard scope(s) sales-email-read
// Granular scope(s) crm.objects.contacts.write

// endpoint: PUT /crm/v3/objects/emails/{emailId}/associations/{toObjectType}/{toObjectId}/{associationType};
// api ref: https://developers.hubspot.com/docs/api/crm/email
// Standard scope(s) sales-email-read
// Granular scope(s) crm.objects.contacts.write

public const HUBSPOT_URL = 'https://api.hubapi.com/crm/v3/objects/emails';

/**
Expand Down Expand Up @@ -39,27 +51,25 @@ public function send($notifiable, Notification $notification): ?array
}

$message = $notification->toMail($notifiable);
$apiKey = config('hubspot.api_key');
if (is_null($apiKey) || is_null(config('hubspot.hubspot_owner_id'))) {
throw InvalidConfiguration::configurationNotSet();
}

$response = Http::post(
self::HUBSPOT_URL.'?hapikey=' . $apiKey,
[
"properties" => [
"hs_timestamp" => now()->getPreciseTimestamp(3),
"hubspot_owner_id" => config('hubspot.hubspot_owner_id'),
"hs_email_direction" => "EMAIL",
"hs_email_status" => "SENT",
"hs_email_subject" => $message->subject,
"hs_email_text" => (string) $message->render(), ],
]
);
$params = [
"properties" => [
"hs_timestamp" => now()->getPreciseTimestamp(3),
"hubspot_owner_id" => config('hubspot.hubspot_owner_id'),
"hs_email_direction" => "EMAIL",
"hs_email_status" => "SENT",
"hs_email_subject" => $message->subject,
"hs_email_text" => (string) $message->render(), ],
];

$response = $this->callApi(self::HUBSPOT_URL, 'post', $params);

$hubspotEmail = $response->json();

if ($response->status() == 201 && ! empty($hubspotEmail['id'])) {
$newResp = Http::put(self::HUBSPOT_URL.'/'. $hubspotEmail['id'] . '/associations/contacts/' . $hubspotContactId . '/198?hapikey=' . $apiKey);

$url = self::HUBSPOT_URL.'/'.$hubspotEmail['id'].'/associations/contacts/'.$hubspotContactId.'/198';
$newResp = $this->callApi($url, 'put');

if ($newResp->status() != 200) {
throw CouldNotSendNotification::serviceRespondedWithAnError($newResp->body());
Expand All @@ -71,4 +81,25 @@ public function send($notifiable, Notification $notification): ?array

return $hubspotEmail;
}

protected function callApi($baseUrl, $method, $params = [])
{
$apiKey = config('hubspot.api_key');
if (is_null(config('hubspot.hubspot_owner_id'))) {
throw InvalidConfiguration::configurationNotSet();
}

$url = $baseUrl.'?hapikey='.$apiKey;
$http = Http::acceptJson();

if (is_null($apiKey)) {
if (is_null(config('hubspot.access_token'))) {
throw InvalidConfiguration::configurationNotSet();
}
$url = $baseUrl;
$http = $http->withToken(config('hubspot.access_token'));
}

return $http->$method($url, $params);
}
}