From b4411b36a8495991478daaa86a1bb236b9fd09ec Mon Sep 17 00:00:00 2001 From: Erik Bonder Date: Wed, 21 Oct 2020 15:27:12 +0300 Subject: [PATCH 1/4] Move base and contact lists to V3 --- src/Ctct/Components/Contacts/ContactList.php | 2 +- src/Ctct/Services/ListService.php | 4 ++-- src/Ctct/Util/Config.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Ctct/Components/Contacts/ContactList.php b/src/Ctct/Components/Contacts/ContactList.php index 19ea4a7..39c7db1 100755 --- a/src/Ctct/Components/Contacts/ContactList.php +++ b/src/Ctct/Components/Contacts/ContactList.php @@ -62,7 +62,7 @@ public function __construct($list_id = null) { */ public static function create(array $props) { $contact_list = new ContactList(); - $contact_list->id = parent::getValue($props, "id"); + $contact_list->id = parent::getValue($props, "list_id"); $contact_list->name = parent::getValue($props, "name"); $contact_list->status = parent::getValue($props, "status"); $contact_list->contact_count = parent::getValue($props, "contact_count"); diff --git a/src/Ctct/Services/ListService.php b/src/Ctct/Services/ListService.php index b442f81..c648680 100755 --- a/src/Ctct/Services/ListService.php +++ b/src/Ctct/Services/ListService.php @@ -32,8 +32,8 @@ public function getLists($accessToken, Array $params = array()) { } $lists = array(); - foreach (json_decode($response->getBody(), true) as $contact) { - $lists[] = ContactList::create($contact); + foreach (json_decode($response->getBody(), true)['lists'] as $list) { + $lists[] = ContactList::create($list); } return $lists; diff --git a/src/Ctct/Util/Config.php b/src/Ctct/Util/Config.php index a989f1c..ffd34fd 100755 --- a/src/Ctct/Util/Config.php +++ b/src/Ctct/Util/Config.php @@ -16,7 +16,7 @@ class Config { * REST endpoints */ 'endpoints' => array( - 'base_url' => 'https://api.constantcontact.com/v2/', + 'base_url' => 'https://api.cc.email/v3/', 'account_verified_addresses' => 'account/verifiedemailaddresses', 'account_info' => 'account/info', 'activity' => 'activities/%s', @@ -27,7 +27,7 @@ class Config { 'add_contacts_activity' => 'activities/addcontacts', 'contact' => 'contacts/%s', 'contacts' => 'contacts', - 'lists' => 'lists', + 'lists' => 'contact_lists', 'list' => 'lists/%s', 'list_contacts' => 'lists/%s/contacts', 'contact_lists' => 'contacts/%s/lists', From e415b11abf59c6b057e3bd2107967a1c6ea61eaf Mon Sep 17 00:00:00 2001 From: Erik Bonder Date: Thu, 22 Oct 2020 14:20:40 +0300 Subject: [PATCH 2/4] Token refresh method --- src/Ctct/Auth/CtctOAuth2.php | 24 ++++++++++++++++++++++++ src/Ctct/Util/Config.php | 6 +++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/Ctct/Auth/CtctOAuth2.php b/src/Ctct/Auth/CtctOAuth2.php index eb62138..6ef352e 100755 --- a/src/Ctct/Auth/CtctOAuth2.php +++ b/src/Ctct/Auth/CtctOAuth2.php @@ -106,4 +106,28 @@ public function getTokenInfo($accessToken) { } return $response; } + + public function refreshTokens($refreshToken) + { + $query = array( + 'grant_type' => 'refresh_token', + 'refresh_token' => $refreshToken, + ); + + $headers = [ + 'Authorization' => 'Basic ' . base64_encode($this->clientId . ':' . $this->clientSecret), + ]; + + $baseUrl = Config::get('auth.base_url') . Config::get('auth.token_endpoint'); + try { + $response = json_decode( + $this->client->request('POST', $baseUrl, compact('query', 'headers'))->getBody(), + true + ); + } catch (ClientException $e) { + throw $this->convertException($e); + } + + return $response; + } } diff --git a/src/Ctct/Util/Config.php b/src/Ctct/Util/Config.php index ffd34fd..8adff05 100755 --- a/src/Ctct/Util/Config.php +++ b/src/Ctct/Util/Config.php @@ -116,13 +116,13 @@ class Config { * OAuth2 Authorization related configuration options */ 'auth' => array( - 'base_url' => 'https://oauth2.constantcontact.com/oauth2/', + 'base_url' => 'https://idfed.constantcontact.com/as/', 'response_type_code' => 'code', 'response_type_token' => 'token', 'authorization_code_grant_type' => 'authorization_code', 'authorization_endpoint' => 'oauth/siteowner/authorize', - 'token_endpoint' => 'oauth/token', - 'token_info' => 'tokeninfo.htm' + 'token_endpoint' => 'token.oauth2', + 'token_info' => 'tokeninfo.htm', ), /** * Errors to be returned for various exceptions From 28a12b9676aef49e088dba5777898cce8ad49123 Mon Sep 17 00:00:00 2001 From: Erik Bonder Date: Thu, 22 Oct 2020 14:20:58 +0300 Subject: [PATCH 3/4] Contact V3 changes --- src/Ctct/Components/Contacts/Contact.php | 26 ++++++++++++++----- src/Ctct/Components/Contacts/EmailAddress.php | 10 +++---- src/Ctct/Services/ContactService.php | 3 +-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/Ctct/Components/Contacts/Contact.php b/src/Ctct/Components/Contacts/Contact.php index 9fa9efd..e2e2b13 100755 --- a/src/Ctct/Components/Contacts/Contact.php +++ b/src/Ctct/Components/Contacts/Contact.php @@ -48,10 +48,16 @@ class Contact extends Component { public $source; /** - * Array of email addresses associated with this contact - * @var EmailAddress[] + * Contact source information + * @var string + */ + public $create_source; + + /** + * Email address associated with this contact + * @var EmailAddress */ - public $email_addresses = array(); + public $email_address; /** * The prefix name of the contact @@ -119,6 +125,12 @@ class Contact extends Component { */ public $lists = array(); + /** + * Array of contact lists IDs this contact belongs to + * @var [] + */ + public $list_memberships = array(); + /** * Date the contact was created * @var string @@ -150,11 +162,11 @@ public static function create(array $props) { $contact->last_name = parent::getValue($props, "last_name"); $contact->confirmed = parent::getValue($props, "confirmed"); $contact->source = parent::getValue($props, "source"); + $contact->create_source = parent::getValue($props, "create_source"); + $contact->list_memberships = parent::getValue($props, "list_memberships"); - if (isset($props['email_addresses'])) { - foreach ($props['email_addresses'] as $email_address) { - $contact->email_addresses[] = EmailAddress::create($email_address); - } + if (isset($props['email_address'])) { + $contact->email_address = EmailAddress::create(parent::getValue($props, "email_address")); } $contact->prefix_name = parent::getValue($props, "prefix_name"); diff --git a/src/Ctct/Components/Contacts/EmailAddress.php b/src/Ctct/Components/Contacts/EmailAddress.php index 1506b5d..15af6bf 100755 --- a/src/Ctct/Components/Contacts/EmailAddress.php +++ b/src/Ctct/Components/Contacts/EmailAddress.php @@ -52,11 +52,11 @@ class EmailAddress extends Component { * Email address associated with the contact * @var string */ - public $email_address; + public $address; - public function __construct($email_address = null) { - if (!is_null($email_address)) { - $this->email_address = $email_address; + public function __construct($address = null) { + if (!is_null($address)) { + $this->address = $address; } return $this; @@ -75,7 +75,7 @@ public static function create(array $props) { $email_address->opt_in_source = parent::getValue($props, "opt_in_source"); $email_address->opt_in_date = parent::getValue($props, "opt_in_date"); $email_address->opt_out_date = parent::getValue($props, "opt_out_date"); - $email_address->email_address = parent::getValue($props, "email_address"); + $email_address->address = parent::getValue($props, "address"); return $email_address; } } diff --git a/src/Ctct/Services/ContactService.php b/src/Ctct/Services/ContactService.php index ae3fb57..1572d4b 100755 --- a/src/Ctct/Services/ContactService.php +++ b/src/Ctct/Services/ContactService.php @@ -123,10 +123,9 @@ public function unsubscribeContact($accessToken, $contactId) { */ public function addContact($accessToken, Contact $contact, $actionByContact) { $baseUrl = Config::get('endpoints.base_url') . Config::get('endpoints.contacts'); - $params["action_by"] = ($actionByContact ? "ACTION_BY_VISITOR" : "ACTION_BY_OWNER"); try { - $response = parent::sendRequestWithBody($accessToken, 'POST', $baseUrl, $contact, $params); + $response = parent::sendRequestWithBody($accessToken, 'POST', $baseUrl, $contact); } catch (TransferException $e) { throw parent::convertException($e); } From 16154786314e7ec33a1fe4ac2a2f0065f43daaba Mon Sep 17 00:00:00 2001 From: Erik Bonder Date: Wed, 23 Nov 2022 13:51:51 +0200 Subject: [PATCH 4/4] Add support for Guzzle 7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2ef5179..5ff6dd1 100755 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "license": "MIT", "require": { "php": ">=5.5.0", - "guzzlehttp/guzzle": "^6.1.1" + "guzzlehttp/guzzle": "^6.1.1 || ^7.4.1" }, "require-dev": { "phpunit/phpunit": "4.8.21"