diff --git a/app/Libraries/ExactTargetService.php b/app/Libraries/ExactTargetService.php index 7254b7dc8e..2e3f8e6411 100644 --- a/app/Libraries/ExactTargetService.php +++ b/app/Libraries/ExactTargetService.php @@ -2,11 +2,9 @@ namespace App\Libraries; +use Illuminate\Support\Facades\Http; use Illuminate\Support\Str; use App\Models\ExactTargetList; -use FuelSdk\ET_Client; -use FuelSdk\ET_DataExtension_Row; -use FuelSdk\ET_Subscriber; class ExactTargetService { @@ -42,14 +40,9 @@ public function __construct( */ public function subscribe($alsoRemove = true) { - $client = $this->getEtClient(); - - // Add the user to a data extension - $deRow = new ET_DataExtension_Row(); - - $deRow->authStub = $client; - $deRow->props = [ - 'Email' => $this->email, + $accessToken = $this->getAccessToken(); + $dataExtensionKey = config('exact-target.customer_key'); + $props = [ 'OptMuseum' => 'True', ]; @@ -62,65 +55,35 @@ public function subscribe($alsoRemove = true) foreach ($allLists as $list) { if (in_array($list, $this->list)) { - $deRow->props[$list] = 'True'; + $props[$list] = 'True'; } elseif ($alsoRemove) { - $deRow->props[$list] = 'False'; + $props[$list] = 'False'; } } } if ($this->wasFormPrefilled || $this->firstName) { - $deRow->props['FirstName'] = $this->firstName; + $props['FirstName'] = $this->firstName; } if ($this->wasFormPrefilled || $this->lastName) { - $deRow->props['LastName'] = $this->lastName; - } - - $deRow->CustomerKey = config('exact-target.customer_key'); - $deRow->Name = config('exact-target.name'); - - $response = $deRow->post(); - - // If it fails, try patch - if (!$response->status) { - $response = $deRow->patch(); - - if (!$response->status) { - return $response; - } - } - - // Add the subscriber - $subscriber = new ET_Subscriber(); - $subscriber->authStub = $client; - $subscriber->props = [ - 'EmailAddress' => $this->email, - 'SubscriberKey' => $this->email, - ]; - $response = $subscriber->post(); - - if (!$response->status) { - $error = $response->results[0]->ErrorMessage ?? ''; - $status = $response->results[0]->StatusMessage ?? ''; - - if ( - Str::startsWith($error, 'Violation of PRIMARY KEY constraint') - || Str::startsWith($status, 'The subscriber is already on the list') - ) { - // Email has been previously subscribed, so proceed - } else { - return $response; - } + $props['LastName'] = $this->lastName; } - // Then patch it with some additional properties - $subscriber->props['Status'] = 'Active'; - $subscriber->Name = 'Museum Business Unit'; - $response = $subscriber->patch(); + $response = Http::withToken($accessToken)->post( + config('exact-target.client.baseUrl') . "hub/v1/dataeventsasync/key:$dataExtensionKey/rowset", + [ + [ + 'keys' => [ + 'Email' => $this->email + ], + 'values' => $props + ] + ] + ); - if (!$response->status) { - return $response; + if ($response->failed()) { + return $response->json(); } return true; @@ -131,38 +94,21 @@ public function subscribe($alsoRemove = true) */ public function unsubscribe() { - $client = $this->getEtClient(); + $accessToken = $this->getAccessToken(); + $dataExtensionKey = config('exact-target.customer_key'); // Delete the user from the data extension - $deRow = new ET_DataExtension_Row(); - - $deRow->authStub = $client; - $deRow->props = [ - 'Email' => $this->email, - ]; - - $deRow->CustomerKey = config('exact-target.customer_key'); - $deRow->Name = config('exact-target.name'); - - $response = $deRow->delete(); - - if (!$response->status) { - return $response; - } - - // Set the subscriber to Unsubscribed - $subscriber = new ET_Subscriber(); - $subscriber->authStub = $client; - $subscriber->props = [ - 'EmailAddress' => $this->email, - 'SubscriberKey' => $this->email, - 'Status' => 'Unsubscribed' - ]; - $subscriber->Name = 'Museum Business Unit'; - $response = $subscriber->patch(); + $response = Http::withToken($accessToken)->delete( + config('exact-target.client.baseUrl') . "hub/v1/dataevents/key:$dataExtensionKey/rowset", + [ + 'keys' => [ + 'Email' => $this->email + ] + ] + ); - if (!$response->status) { - return $response; + if ($response->failed()) { + return $response->json(); } return true; @@ -170,69 +116,44 @@ public function unsubscribe() public function get() { - $client = new ET_Client(false, config('app.debug'), config('exact-target.client')); - - $deRow = new ET_DataExtension_Row(); - $deRow->authStub = $client; - - // Select - $allLists = ExactTargetList::getList()->keys()->all(); - $fields = array_merge( + $accessToken = $this->getAccessToken(); + $dataExtensionKey = config('exact-target.customer_key'); + $fields = array_merge([ + 'Email', + 'FirstName', + 'LastName', + ], ExactTargetList::getList()->keys()->all()); + + $response = Http::withToken($accessToken)->get( + config('exact-target.client.baseUrl') . "data/v1/customobjectdata/key:$dataExtensionKey/rowset", [ - 'Email', - 'FirstName', - 'LastName', - ], - $allLists + 'fields' => implode(',', $fields), + 'filter' => [ + 'Property' => 'Email', + 'SimpleOperator' => 'equals', + 'Value' => $this->email + ] + ] ); - $deRow->props = $fields; - - // From (e.g. "All Subscribers Master") - $deRow->Name = config('exact-target.customer_key'); - - // Where - $deRow->filter = [ - 'Property' => 'Email', - 'SimpleOperator' => 'equals', - 'Value' => $this->email, - ]; - - return $deRow->get(); + return $response->json(); } - protected function getEtClient() + protected function getAccessToken() { $auth_url = config('exact-target.client.baseAuthUrl'); - $clientId = config('exact-target.client.clientid'); - $clientSecret = config('exact-target.client.clientsecret'); - $api = new \GuzzleHttp\Client(); + $response = Http::asForm()->post($auth_url . "v2/token", [ + 'grant_type' => 'client_credentials', + 'client_id' => config('exact-target.client.clientid'), + 'client_secret' => config('exact-target.client.clientsecret'), + ]); - $result = $api->request( - 'POST', - $auth_url . '/v2/token', - [ - 'json' => [ - 'client_id' => $clientId, - 'client_secret' => $clientSecret, - 'grant_type' => 'client_credentials', - ] - ] - ); - $tokenInfo = json_decode($result->getBody()->getContents(), true); + if ($response->failed()) { + throw new \Exception('Unable to retrieve access token'); + } - return new ET_Client( - true, - config('app.debug'), - array_merge( - config('exact-target.client'), - [ - 'authorizationCode' => $tokenInfo['access_token'], - 'scope' => $tokenInfo['scope'], - ] - ) - ); + return $response->json()['access_token']; } } diff --git a/composer.json b/composer.json index d9bb649926..375896ba32 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,6 @@ "type": "vcs", "url": "/~https://github.com/art-institute-of-chicago/data-hub-foundation.git" }, - { - "type": "vcs", - "url": "/~https://github.com/axsweet/FuelSDK-PHP.git" - }, { "type": "package", "package": { @@ -61,7 +57,6 @@ "michelf/php-smartypants": "^1.8", "ramsey/uuid": "^4.0", "rlanvin/php-rrule": "^2.0", - "salesforce-mc/fuel-sdk-php": "dev-master", "sendgrid/sendgrid": "^8.1", "sentry/sentry-laravel": "^4.9", "spatie/calendar-links": "^1.0", diff --git a/composer.lock b/composer.lock index 466248822c..ba56c05c1d 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "9061b70c83062135f3beada78a896b01", + "content-hash": "69dd7545eae675260459358169e381ed", "packages": [ { "name": "aic/data-hub-foundation", @@ -6772,154 +6772,6 @@ }, "time": "2023-06-07T13:15:59+00:00" }, - { - "name": "robrichards/wse-php", - "version": "2.0.3", - "source": { - "type": "git", - "url": "/~https://github.com/robrichards/wse-php.git", - "reference": "f705f0242ab5f9ea69570955d2ebc64a902b12eb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/robrichards/wse-php/zipball/f705f0242ab5f9ea69570955d2ebc64a902b12eb", - "reference": "f705f0242ab5f9ea69570955d2ebc64a902b12eb", - "shasum": "" - }, - "require": { - "php": ">= 5.3", - "robrichards/xmlseclibs": ">=3.0.2" - }, - "type": "library", - "autoload": { - "psr-4": { - "RobRichards\\WsePhp\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "authors": [ - { - "name": "Rob Richards", - "homepage": "http://www.cdatazone.org/", - "role": "Main developer" - } - ], - "description": "Libraries for adding WS-* support to ext/soap in PHP.", - "homepage": "/~https://github.com/robrichards/wse-php", - "keywords": [ - "WS-Security", - "soap", - "ws-addressing" - ], - "support": { - "issues": "/~https://github.com/robrichards/wse-php/issues", - "source": "/~https://github.com/robrichards/wse-php/tree/master" - }, - "time": "2019-01-03T15:37:08+00:00" - }, - { - "name": "robrichards/xmlseclibs", - "version": "3.1.1", - "source": { - "type": "git", - "url": "/~https://github.com/robrichards/xmlseclibs.git", - "reference": "f8f19e58f26cdb42c54b214ff8a820760292f8df" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/robrichards/xmlseclibs/zipball/f8f19e58f26cdb42c54b214ff8a820760292f8df", - "reference": "f8f19e58f26cdb42c54b214ff8a820760292f8df", - "shasum": "" - }, - "require": { - "ext-openssl": "*", - "php": ">= 5.4" - }, - "type": "library", - "autoload": { - "psr-4": { - "RobRichards\\XMLSecLibs\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "BSD-3-Clause" - ], - "description": "A PHP library for XML Security", - "homepage": "/~https://github.com/robrichards/xmlseclibs", - "keywords": [ - "security", - "signature", - "xml", - "xmldsig" - ], - "support": { - "issues": "/~https://github.com/robrichards/xmlseclibs/issues", - "source": "/~https://github.com/robrichards/xmlseclibs/tree/3.1.1" - }, - "time": "2020-09-05T13:00:25+00:00" - }, - { - "name": "salesforce-mc/fuel-sdk-php", - "version": "dev-master", - "source": { - "type": "git", - "url": "/~https://github.com/axsweet/FuelSDK-PHP.git", - "reference": "a5be7ca56420b9cbc6114faff4f6d1846e5d9c53" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/axsweet/FuelSDK-PHP/zipball/a5be7ca56420b9cbc6114faff4f6d1846e5d9c53", - "reference": "a5be7ca56420b9cbc6114faff4f6d1846e5d9c53", - "shasum": "" - }, - "require": { - "ext-curl": "*", - "ext-openssl": "*", - "ext-soap": "*", - "firebase/php-jwt": ">=5.0.0", - "php": ">=5.6.24", - "robrichards/wse-php": "2.0.3" - }, - "require-dev": { - "dompdf/dompdf": "0.6", - "phpdocumentor/phpdocumentor": "2.8.0", - "phpunit/phpunit": ">=5.7.20" - }, - "default-branch": true, - "type": "library", - "autoload": { - "psr-4": { - "FuelSdk\\": "src/" - } - }, - "autoload-dev": { - "psr-4": { - "FuelSdk\\Test\\": "tests/" - } - }, - "license": [ - "MIT" - ], - "description": "Salesforce Marketing Cloud Fuel SDK for PHP", - "homepage": "https://developer.salesforce.com/docs/atlas.en-us.mc-sdks.meta/mc-sdks/index-sdk.htm", - "keywords": [ - "exacttarget", - "fuel sdk", - "fuel-sdk", - "marketing cloud", - "php sdk", - "salesforce", - "sdk" - ], - "support": { - "source": "/~https://github.com/axsweet/FuelSDK-PHP/tree/master" - }, - "time": "2021-10-26T20:46:29+00:00" - }, { "name": "sebastian/diff", "version": "5.0.3", @@ -14816,14 +14668,12 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": { - "salesforce-mc/fuel-sdk-php": 20 - }, + "stability-flags": [], "prefer-stable": false, "prefer-lowest": false, "platform": { "php": "^8.1" }, - "platform-dev": {}, - "plugin-api-version": "2.6.0" + "platform-dev": [], + "plugin-api-version": "2.3.0" }