Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ksvirkou-hubspot committed Apr 23, 2020
2 parents 7dc10a3 + 5c6664b commit 2a50d42
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
2 changes: 2 additions & 0 deletions sample-apps/webhooks-contacts-app/.env.template
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
HUBSPOT_APPLICATION_ID=
HUBSPOT_CLIENT_ID=
HUBSPOT_CLIENT_SECRET=
HUBSPOT_DEVELOPER_API_KEY=
1 change: 1 addition & 0 deletions sample-apps/webhooks-contacts-app/composer.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"minimum-stability": "dev",
"repositories": [
{
"type": "path",
Expand Down
54 changes: 31 additions & 23 deletions sample-apps/webhooks-contacts-app/src/actions/webhooks/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

use Helpers\HubspotClientHelper;
use Helpers\UrlHelper;
use HubSpot\Client\Webhooks\Model\BatchInputSubscriptionBatchUpdateRequest;
use HubSpot\Client\Webhooks\Model\SubscriptionBatchUpdateRequest;
use HubSpot\Client\Webhooks\Model\SettingsChangeRequest;
use HubSpot\Client\Webhooks\Model\SettingsResponse;
use HubSpot\Client\Webhooks\Model\SubscriptionCreateRequest;
use HubSpot\Client\Webhooks\Model\SubscriptionPatchRequest;

$appUrl = UrlHelper::generateServerUri().'/webhooks/handle.php';

Expand All @@ -18,7 +19,26 @@
->webhooks()
;

$appId = getEnvOrException('HUBSPOT_APP_ID');
$appId = getEnvOrException('HUBSPOT_APPLICATION_ID');

$updateSubscriptions = function (array $ids, bool $activity) use ($appId, $webhooksClient) {
if (count($ids) > 0) {
$subscriptionRequests = [];

foreach ($ids as $id) {
$subscriptionRequest = new SubscriptionBatchUpdateRequest();
$subscriptionRequest->setId($id);
$subscriptionRequest->setActive($activity);

$subscriptionRequests[] = $subscriptionRequest;
}

$subscriptionsRequest = new BatchInputSubscriptionBatchUpdateRequest();
$subscriptionsRequest->setInputs($subscriptionRequests);

$webhooksClient->subscriptionsApi()->updateBatch($appId, $subscriptionsRequest);
}
};

$necessarySubscriptions = [
'contact.creation' => null,
Expand All @@ -28,7 +48,7 @@

$activeSubscriptions = [];

$subscriptions = $webhooksClient->subscriptionsApi()->getSubscriptions($appId);
$subscriptions = $webhooksClient->subscriptionsApi()->getAll($appId);

foreach ($subscriptions->getResults() as $subscription) {
if (
Expand All @@ -39,24 +59,19 @@
)
) {
$necessarySubscriptions[$subscription->getEventType()] = $subscription->getId();
} elseif ($subscription->getActive()) {
$activeSubscriptions[] = $subscription->getId();
}

if ($subscription->getActive()) {
$subscriptionRequest = new SubscriptionPatchRequest();
$subscriptionRequest->setEnabled(false);
$webhooksClient->subscriptionsApi()
->updateSubscription($subscription->getId(), $appId, $subscriptionRequest)
;
$activeSubscriptions[] = $subscription->getId();
}
}

$updateSubscriptions($activeSubscriptions, false);

$request = new SettingsChangeRequest();
$request->setTargetUrl($appUrl);

$response = $webhooksClient->settingsApi()
->configureSettings($appId, $request)
->configure($appId, $request)
;

foreach ($necessarySubscriptions as $eventType => $subscriptionId) {
Expand All @@ -69,20 +84,13 @@
$request->setPropertyName('firstname');
}

$webhooksClient->subscriptionsApi()->subscribe($appId, $request);
} else {
$activeSubscriptions[] = $subscriptionId;
$webhooksClient->subscriptionsApi()->create($appId, $request);
}
}

foreach ($activeSubscriptions as $subscriptionId) {
$subscriptionRequest = new SubscriptionPatchRequest();
$subscriptionRequest->setEnabled(true);
$webhooksClient->subscriptionsApi()
->updateSubscription($subscriptionId, $appId, $subscriptionRequest)
;
}
$settings = $webhooksClient->settingsApi()->getSettings($appId);
$updateSubscriptions($activeSubscriptions, true);

$settings = $webhooksClient->settingsApi()->getAll($appId);

if (($settings instanceof SettingsResponse) && ($settings->getTargetUrl() == $appUrl)) {
$_SESSION['init'] = true;
Expand Down

0 comments on commit 2a50d42

Please sign in to comment.