├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── README.md ├── composer.json ├── phpunit.xml ├── src ├── Mailchimp.php ├── Mailchimp2.php ├── MailchimpAPIException.php ├── MailchimpApiInterface.php ├── MailchimpApiUser.php ├── MailchimpAutomations.php ├── MailchimpCampaigns.php ├── MailchimpConnectedSites.php ├── MailchimpEcommerce.php ├── MailchimpLists.php ├── MailchimpReports.php ├── MailchimpTemplates.php └── http │ ├── MailchimpCurlHttpClient.php │ ├── MailchimpGuzzleHttpClient.php │ └── MailchimpHttpClientInterface.php └── tests ├── MailchimpAutomationsTest.php ├── MailchimpCampaignsTest.php ├── MailchimpConnectedSitesTest.php ├── MailchimpEcommerceTest.php ├── MailchimpListsTest.php ├── MailchimpReportsTest.php ├── MailchimpTemplatesTest.php ├── MailchimpTest.php └── src ├── Client.php ├── Mailchimp.php ├── MailchimpApiUser.php ├── MailchimpAutomations.php ├── MailchimpCampaigns.php ├── MailchimpConnectedSites.php ├── MailchimpEcommerce.php ├── MailchimpLists.php ├── MailchimpReports.php ├── MailchimpTemplates.php ├── MailchimpTestHttpClient.php └── MailchimpTestHttpResponse.php /.gitignore: -------------------------------------------------------------------------------- 1 | /composer.lock 2 | /vendor 3 | .idea -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | before_script: 3 | - composer self-update 4 | - composer install --dev --no-interaction 5 | script: 6 | - vendor/bin/phpunit --coverage-clover clover.xml 7 | after_script: 8 | php: 9 | - 5.4 10 | - 5.5 11 | - 5.6 12 | - 7.0 13 | notifications: 14 | email: false 15 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Thank you for taking the time to help us develop the MailChimp library! 4 | 5 | We do all our work on GitHub. If you'd like to help, you can create a 6 | [free GitHub account here](https://github.com/join). 7 | 8 | ## Reporting an issue 9 | 10 | For bug reports or feature requests, please [create a new issue](https://github.com/thinkshout/mailchimp-api-php/issues). 11 | 12 | If reporting a bug, please: 13 | 14 | * Let us know what steps we can take to reproduce the bug 15 | * Include any error messages you received 16 | 17 | ## Submitting changes 18 | 19 | The best way to submit a bug fix or improvement is through a [pull request](https://help.github.com/articles/creating-a-pull-request-from-a-fork/). 20 | 21 | * [Fork](https://guides.github.com/activities/forking/), then clone the repository: 22 | 23 | `git clone git@github.com:YOUR-GITHUB-USERNAME/mailchimp-api-php.git` 24 | 25 | * Install the library's dependencies using [Composer](https://getcomposer.org/): 26 | 27 | ```shell 28 | cd mailchimp-api-php 29 | composer install 30 | ``` 31 | 32 | * If you are adding new functionality, please add a corresponding test. 33 | See [Testing](#testing) for more information. 34 | 35 | * After making your changes, ensure all tests pass. 36 | 37 | * Commit and push your changes to your fork of the repository. 38 | 39 | * [Submit a pull request](https://github.com/thinkshout/mailchimp-api-php/pulls). 40 | 41 | ## Testing 42 | 43 | This library includes a [PHPUnit](https://phpunit.de/) test suite. Keeping these 44 | tests up to date helps us ensure the library is reliable. 45 | 46 | ### Running PHPUnit tests 47 | 48 | Add Composer's vendor directory to your PATH by adding the following line to 49 | your profile. This is dependent on your system, but on a Linux or Mac OSX system 50 | using Bash, you'll typically find the file at *~/.bash_profile*. 51 | 52 | `export PATH="./vendor/bin:$PATH"` 53 | 54 | Bash example: 55 | 56 | ```shell 57 | echo 'export PATH="./vendor/bin:$PATH"' >> ~/.bash_profile 58 | source ~/.bash_profile 59 | ``` 60 | 61 | Then run PHPUnit: 62 | 63 | `phpunit` 64 | 65 | ### Creating new tests 66 | 67 | Tests are located in the *tests* directory and are grouped into PHP files named 68 | after the library component they are testing. For example, 69 | *MailchimpCampaignsTest.php* contains tests for MailChimp Campaigns. 70 | 71 | New tests should contain at least the functionality in this simple test: 72 | 73 | ```php 74 | public function testGetCampaigns() { 75 | $mc = new MailchimpCampaigns(); 76 | $mc->getCampaigns(); 77 | 78 | $this->assertEquals('GET', $mc->getClient()->method); 79 | $this->assertEquals($mc->getEndpoint() . '/campaigns', $mc->getClient()->uri); 80 | } 81 | ``` 82 | 83 | This test checks the request type and request URI are both correct. 84 | 85 | More advanced examples can be found in the *tests* directory. 86 | 87 | ## Additional resources 88 | 89 | * [MailChimp API documentation](http://developer.mailchimp.com/documentation/mailchimp/) 90 | * [MailChimp Drupal module](https://www.drupal.org/project/mailchimp), MailChimp integration for Drupal using this library. 91 | * [MailChimp E-Commerce Drupal module](https://www.drupal.org/project/mailchimp_ecommerce), MailChimp integration for Drupal Commerce using this library. 92 | * [ThinkShout](https://thinkshout.com), the library maintainer. 93 | 94 | ## Want to help build this and other open source projects? 95 | 96 | We're hiring software engineers! Check out our [careers page](https://thinkshout.com/careers/). 97 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PHP library for v3 of the Mailchimp API 2 | 3 | This library provides convenient wrapper functions for Mailchimp's REST API. 4 | The API is [documented here](http://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/). 5 | 6 | ## Requirements 7 | 8 | - PHP 5.4.0 or greater (7.0 or greater if you wish to use phpunit) 9 | - [Composer](https://getcomposer.org/) 10 | - [Guzzle](https://github.com/guzzle/guzzle) 11 | 12 | ## Installation 13 | 14 | Dependencies are managed by [Composer](https://getcomposer.org/). After 15 | installing Composer, run the following command from the library root: 16 | 17 | `composer install --no-dev --ignore-platform-reqs` 18 | 19 | Or to install with phpunit: 20 | 21 | `composer install` 22 | 23 | ## Usage 24 | 25 | ### Get your account information 26 | 27 | A basic test to confirm the library is set up and functional. 28 | 29 | ### With OAuth Access token 30 | ```php 31 | 'YOUR_ACCESS_TOKEN', 37 | 'data_center' => 'YOUR_DATA_CENTER', 38 | 'api_user' => 'oauth', 39 | ]; 40 | 41 | // Use Mailchimp2 class for OAuth access_token. 42 | $api_class = new Mailchimp2($authentication_settings); 43 | 44 | // Instantiate a MailchimpApiUser or a class that extends it (ie. MailchimpLists, MailchimpSignups, etc..) 45 | $mailchimp = new MailchimpApiUser($api_class); 46 | 47 | // Get the account details of the authenticated user. 48 | $response = $mailchimp->getAccount(); 49 | 50 | // Output the account details. 51 | if (!empty($response) && isset($response->account_id)) { 52 | echo "ID: {$response->account_id}\n" 53 | . "Name: {$response->account_name}\n"; 54 | } 55 | 56 | ``` 57 | 58 | ### With API Key 59 | ```php 60 | require 'PATH_TO_LIBRARY/mailchimp-api-php/vendor/autoload.php'; 61 | 62 | // Instantiate MailchimpApiInterface 63 | $authentication_settings = [ 64 | 'api_key' => 'YOUR_API_KEY', 65 | 'api_user' => 'api_key', 66 | ]; 67 | 68 | // Use Mailchimp class for api_key. 69 | $api_class = new Mailchimp($authentication_settings); 70 | 71 | // Instantiate a MailchimpApiUser or a class that extends it (ie. MailchimpLists, MailchimpSignups, etc..) 72 | $mailchimp = new MailchimpApiUser($api_class); 73 | 74 | // Get the account details of the authenticated user. 75 | $response = $mailchimp->getAccount(); 76 | 77 | // Output the account details. 78 | if (!empty($response) && isset($response->account_id)) { 79 | echo "ID: {$response->account_id}\n" 80 | . "Name: {$response->account_name}\n"; 81 | }ject = new MailchimpApiUser($api_class, $http_options); 82 | ``` 83 | 84 | ### Get lists and their interest categories 85 | 86 | A more complicated example that takes the response from one API call and 87 | uses that data to make another. 88 | 89 | ```php 90 | 'YOUR_ACCESS_TOKEN', 95 | 'data_center' => 'YOUR_DATA_CENTER', 96 | 'api_user' => 'oauth', 97 | ]; 98 | $api_class = new Mailchimp2($authentication_settings); 99 | 100 | $mailchimp_lists = new Mailchimp\MailchimpLists($api_class); 101 | 102 | // Get all lists. 103 | $response = $mailchimp_lists->getLists(); 104 | 105 | if (!empty($response) && isset($response->lists)) { 106 | foreach ($response->lists as $list) { 107 | // Output the list name. 108 | echo "List name: {$list->name}\n"; 109 | 110 | // Get the list's interest categories. 111 | $interests = $mailchimp_lists->getInterestCategories($list->id); 112 | 113 | // Output the names of the list's interest categories. 114 | if (!empty($interests) && isset($interests->categories)) { 115 | foreach ($interests->categories as $category) { 116 | echo "Interest category: {$category->title}\n"; 117 | } 118 | } 119 | } 120 | } 121 | ``` 122 | 123 | ## Testing 124 | 125 | This library includes a [PHPUnit](https://phpunit.de/) test suite. 126 | 127 | ### Running PHPUnit tests 128 | 129 | Add Composer's vendor directory to your PATH by adding the following line to 130 | your profile. This is dependent on your system, but on a Linux or Mac OSX system 131 | using Bash, you'll typically find the file at *~/.bash_profile*. 132 | 133 | `export PATH="./vendor/bin:$PATH"` 134 | 135 | Bash example: 136 | 137 | ```shell 138 | echo 'export PATH="./vendor/bin:$PATH"' >> ~/.bash_profile 139 | source ~/.bash_profile 140 | ``` 141 | 142 | Then run PHPUnit: 143 | 144 | `phpunit` 145 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "thinkshout/mailchimp-api-php", 3 | "type": "library", 4 | "description": "PHP library for v3 of the MailChimp API", 5 | "keywords": ["mailchimp", "mail"], 6 | "homepage": "https://github.com/thinkshout/mailchimp-api-php", 7 | "license": "GPL-2.0-or-later", 8 | "require": { 9 | "php": ">=5.4.0", 10 | "guzzlehttp/guzzle": "^6.5.8|^7.4.5" 11 | }, 12 | "require-dev": { 13 | "phpunit/phpunit": "^6.2.2|^7.0|^8.0|^9.0" 14 | }, 15 | "autoload": { 16 | "psr-4": { 17 | "Mailchimp\\": "src/", 18 | "Mailchimp\\http\\": "src/http/" 19 | } 20 | }, 21 | "autoload-dev": { 22 | "psr-4": { 23 | "Mailchimp\\Tests\\": "tests/src/" 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ./tests/ 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /src/Mailchimp.php: -------------------------------------------------------------------------------- 1 | api_key = $authentication_settings['api_key']; 111 | $this->api_user = $authentication_settings['api_user']; 112 | 113 | $dc = $this->getDataCenter($this->api_key); 114 | 115 | $this->endpoint = str_replace(Mailchimp::DEFAULT_DATA_CENTER, (string) $dc, $this->endpoint); 116 | 117 | if (!empty($client)) { 118 | $this->client = $client; 119 | } 120 | else { 121 | $this->client = $this->getDefaultHttpClient($http_options); 122 | } 123 | } 124 | 125 | /** 126 | * Adds a pending batch operation. 127 | * 128 | * @param string $method 129 | * The HTTP method. 130 | * @param string $path 131 | * The request path, relative to the API endpoint. 132 | * @param array $parameters 133 | * Associative array of optional request parameters. 134 | * 135 | * @return object 136 | * The new batch operation object. 137 | * 138 | * @throws MailchimpAPIException 139 | * 140 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#create-post_batches 141 | */ 142 | protected function addBatchOperation($method, $path, $parameters = []) { 143 | if (empty($method) || empty($path)) { 144 | throw new MailchimpAPIException('Cannot add batch operation without a method and path.'); 145 | } 146 | 147 | $op = (object) [ 148 | 'method' => $method, 149 | 'path' => $path, 150 | ]; 151 | 152 | if (!empty($parameters)) { 153 | if ($method == 'GET') { 154 | $op->params = (object) $parameters; 155 | } 156 | else { 157 | $op->body = json_encode($parameters); 158 | } 159 | } 160 | 161 | if (empty($this->batch_operations)) { 162 | $this->batch_operations = []; 163 | } 164 | 165 | $this->batch_operations[] = $op; 166 | 167 | return $op; 168 | } 169 | 170 | /** 171 | * Makes a request to the Mailchimp API. 172 | * 173 | * @param string $method 174 | * The REST method to use when making the request. 175 | * @param string $path 176 | * The API path to request. 177 | * @param array $tokens 178 | * Associative array of tokens and values to replace in the path. 179 | * @param array $parameters 180 | * Associative array of parameters to send in the request body. 181 | * @param bool $batch 182 | * TRUE if this request should be added to pending batch operations. 183 | * @param bool $returnAssoc 184 | * TRUE to return Mailchimp API response as an associative array. 185 | * 186 | * @return mixed 187 | * Object or Array if $returnAssoc is TRUE. 188 | * 189 | * @throws MailchimpAPIException 190 | */ 191 | public function request($method, $path, $tokens = NULL, $parameters = [], $batch = FALSE, $returnAssoc = FALSE) { 192 | if (!empty($tokens)) { 193 | foreach ($tokens as $key => $value) { 194 | $path = str_replace('{' . $key . '}', $value, $path); 195 | } 196 | } 197 | 198 | if ($batch) { 199 | return $this->addBatchOperation($method, $path, $parameters); 200 | } 201 | 202 | // Set default request options with auth header. 203 | $options = [ 204 | 'headers' => [ 205 | 'Authorization' => $this->api_user . ' ' . $this->api_key, 206 | ], 207 | ]; 208 | 209 | // Add trigger error header if a debug error code has been set. 210 | if (!empty($this->debug_error_code)) { 211 | $options['headers']['X-Trigger-Error'] = $this->debug_error_code; 212 | } 213 | 214 | return $this->client->handleRequest($method, $this->endpoint . $path, $options, $parameters, $returnAssoc); 215 | } 216 | 217 | /** 218 | * {@inheritdoc} 219 | */ 220 | public function hasApiAccess() { 221 | return isset($this->api_key); 222 | } 223 | 224 | /** 225 | * Gets the ID of the data center associated with an API key. 226 | * 227 | * @param string $api_key 228 | * The Mailchimp API key. 229 | * 230 | * @return string 231 | * The data center ID. 232 | */ 233 | private function getDataCenter($api_key) { 234 | $api_key_parts = explode('-', (string) $api_key); 235 | 236 | return (isset($api_key_parts[1])) ? $api_key_parts[1] : Mailchimp::DEFAULT_DATA_CENTER; 237 | } 238 | 239 | /** 240 | * Instantiates a default HTTP client based on the local environment. 241 | * 242 | * @param array $http_options 243 | * HTTP client options. 244 | * 245 | * @return MailchimpHttpClientInterface 246 | * The HTTP client. 247 | */ 248 | private function getDefaultHttpClient($http_options) { 249 | // Process HTTP options. 250 | // Handle deprecated 'timeout' argument. 251 | if (is_int($http_options)) { 252 | $http_options = [ 253 | 'timeout' => $http_options, 254 | ]; 255 | } 256 | 257 | // Default timeout is 10 seconds. 258 | $http_options += [ 259 | 'timeout' => 10, 260 | ]; 261 | 262 | $client = NULL; 263 | 264 | // Use cURL HTTP client if PHP version is below 5.5.0. 265 | // Use Guzzle client otherwise. 266 | if (version_compare(phpversion(), '5.5.0', '<')) { 267 | $client = new MailchimpCurlHttpClient($http_options); 268 | } 269 | else { 270 | $client = new MailchimpGuzzleHttpClient($http_options); 271 | } 272 | 273 | return $client; 274 | } 275 | 276 | } 277 | -------------------------------------------------------------------------------- /src/Mailchimp2.php: -------------------------------------------------------------------------------- 1 | access_token = $authentication_settings['access_token']; 111 | $this->api_user = $authentication_settings['api_user']; 112 | $this->endpoint = str_replace(Mailchimp::DEFAULT_DATA_CENTER, (string) $authentication_settings['data_center'], $this->endpoint); 113 | 114 | if (!empty($client)) { 115 | $this->client = $client; 116 | } 117 | else { 118 | $this->client = $this->getDefaultHttpClient($http_options); 119 | } 120 | } 121 | 122 | /** 123 | * Adds a pending batch operation. 124 | * 125 | * @param string $method 126 | * The HTTP method. 127 | * @param string $path 128 | * The request path, relative to the API endpoint. 129 | * @param array $parameters 130 | * Associative array of optional request parameters. 131 | * 132 | * @return object 133 | * The new batch operation object. 134 | * 135 | * @throws MailchimpAPIException 136 | * 137 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#create-post_batches 138 | */ 139 | protected function addBatchOperation($method, $path, $parameters = []) { 140 | if (empty($method) || empty($path)) { 141 | throw new MailchimpAPIException('Cannot add batch operation without a method and path.'); 142 | } 143 | 144 | $op = (object) [ 145 | 'method' => $method, 146 | 'path' => $path, 147 | ]; 148 | 149 | if (!empty($parameters)) { 150 | if ($method == 'GET') { 151 | $op->params = (object) $parameters; 152 | } 153 | else { 154 | $op->body = json_encode($parameters); 155 | } 156 | } 157 | 158 | if (empty($this->batch_operations)) { 159 | $this->batch_operations = []; 160 | } 161 | 162 | $this->batch_operations[] = $op; 163 | 164 | return $op; 165 | } 166 | 167 | /** 168 | * Makes a request to the Mailchimp API using OAuth. 169 | * 170 | * @param string $method 171 | * The REST method to use when making the request. 172 | * @param string $path 173 | * The API path to request. 174 | * @param array $tokens 175 | * Associative array of tokens and values to replace in the path. 176 | * @param array $parameters 177 | * Associative array of parameters to send in the request body. 178 | * @param bool $batch 179 | * TRUE if this request should be added to pending batch operations. 180 | * @param bool $returnAssoc 181 | * TRUE to return Mailchimp API response as an associative array. 182 | * 183 | * @return mixed 184 | * Object or Array if $returnAssoc is TRUE. 185 | * 186 | * @throws MailchimpAPIException 187 | */ 188 | public function request($method, $path, $tokens = NULL, $parameters = [], $batch = FALSE, $returnAssoc = FALSE) { 189 | if (!empty($tokens)) { 190 | foreach ($tokens as $key => $value) { 191 | $path = str_replace('{' . $key . '}', $value, $path); 192 | } 193 | } 194 | 195 | if ($batch) { 196 | return $this->addBatchOperation($method, $path, $parameters); 197 | } 198 | 199 | // Set default request options with auth header. 200 | $options = [ 201 | 'headers' => [ 202 | 'Authorization' => $this->api_user . ' ' . $this->access_token, 203 | ], 204 | ]; 205 | 206 | // Add trigger error header if a debug error code has been set. 207 | if (!empty($this->debug_error_code)) { 208 | $options['headers']['X-Trigger-Error'] = $this->debug_error_code; 209 | } 210 | 211 | return $this->client->handleRequest($method, $this->endpoint . $path, $options, $parameters, $returnAssoc); 212 | } 213 | 214 | /** 215 | * {@inheritdoc} 216 | */ 217 | public function hasApiAccess() { 218 | return isset($this->access_token); 219 | } 220 | 221 | /** 222 | * Instantiates a default HTTP client based on the local environment. 223 | * 224 | * @param array $http_options 225 | * HTTP client options. 226 | * 227 | * @return MailchimpHttpClientInterface 228 | * The HTTP client. 229 | */ 230 | private function getDefaultHttpClient($http_options) { 231 | // Process HTTP options. 232 | // Handle deprecated 'timeout' argument. 233 | if (is_int($http_options)) { 234 | $http_options = [ 235 | 'timeout' => $http_options, 236 | ]; 237 | } 238 | 239 | // Default timeout is 10 seconds. 240 | $http_options += [ 241 | 'timeout' => 10, 242 | ]; 243 | 244 | $client = NULL; 245 | 246 | // Use cURL HTTP client if PHP version is below 5.5.0. 247 | // Use Guzzle client otherwise. 248 | if (version_compare(phpversion(), '5.5.0', '<')) { 249 | $client = new MailchimpCurlHttpClient($http_options); 250 | } 251 | else { 252 | $client = new MailchimpGuzzleHttpClient($http_options); 253 | } 254 | 255 | return $client; 256 | } 257 | 258 | } 259 | -------------------------------------------------------------------------------- /src/MailchimpAPIException.php: -------------------------------------------------------------------------------- 1 | status . ': ' . $message_obj->title; 22 | if (!empty($message_obj->detail)) { 23 | $message .= ' - ' . $message_obj->detail; 24 | } 25 | if (!empty($message_obj->errors)) { 26 | $message .= ' ' . serialize($message_obj->errors); 27 | } 28 | } 29 | 30 | parent::__construct($message, $code, $previous); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/MailchimpApiInterface.php: -------------------------------------------------------------------------------- 1 | api_class = $api_class; 33 | } 34 | 35 | /** 36 | * Sets a custom HTTP client to be used for all API requests. 37 | * 38 | * @param \Mailchimp\http\MailchimpHttpClientInterface $client 39 | * The HTTP client. 40 | */ 41 | public function setClient(MailchimpHttpClientInterface $client) { 42 | $this->api_class->client = $client; 43 | } 44 | 45 | /** 46 | * Sets a Mailchimp error code to be returned by all requests. 47 | * 48 | * Used to test and debug error handling. 49 | * 50 | * @param string $error_code 51 | * The Mailchimp error code. 52 | */ 53 | public function setDebugErrorCode($error_code) { 54 | $this->api_class->debug_error_code = $error_code; 55 | } 56 | 57 | /** 58 | * Gets Mailchimp account information for the authenticated account. 59 | * 60 | * @param array $parameters 61 | * Associative array of optional request parameters. 62 | * 63 | * @return object 64 | * 65 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/root/#read-get_root 66 | */ 67 | public function getAccount($parameters = []) { 68 | return $this->api_class->request('GET', '/', NULL, $parameters); 69 | } 70 | 71 | /** 72 | * Gets the status of a batch request. 73 | * 74 | * @param string $batch_id 75 | * The ID of the batch operation. 76 | * 77 | * @return object 78 | * 79 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#read-get_batches_batch_id 80 | */ 81 | public function getBatchOperation($batch_id) { 82 | $tokens = [ 83 | 'batch_id' => $batch_id, 84 | ]; 85 | 86 | return $this->api_class->request('GET', '/batches/{batch_id}', $tokens); 87 | } 88 | 89 | /** 90 | * Processes all pending batch operations. 91 | * 92 | * @throws MailchimpAPIException 93 | * 94 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/batches/#create-post_batches 95 | */ 96 | public function processBatchOperations() { 97 | $parameters = [ 98 | 'operations' => $this->api_class->batch_operations, 99 | ]; 100 | 101 | try { 102 | $response = $this->api_class->request('POST', '/batches', NULL, $parameters); 103 | 104 | // Reset batch operations. 105 | $this->api_class->batch_operations = []; 106 | 107 | return $response; 108 | 109 | } 110 | catch (MailchimpAPIException $e) { 111 | $message = 'Failed to process batch operations: ' . $e->getMessage(); 112 | throw new MailchimpAPIException($message, $e->getCode(), $e); 113 | } 114 | } 115 | 116 | /** 117 | * Check if key or token is in place. 118 | * 119 | * @return bool 120 | * If the access_token or api_key is set. 121 | */ 122 | public function hasApiAccess() { 123 | return $this->api_class->hasApiAccess(); 124 | } 125 | 126 | /** 127 | * Passes on request to Mailchimp Api Interface. 128 | * This allows backwards compatibilty with existing calls. 129 | * 130 | * @param string $method 131 | * The REST method to use when making the request. 132 | * @param string $path 133 | * The API path to request. 134 | * @param array $tokens 135 | * Associative array of tokens and values to replace in the path. 136 | * @param array $parameters 137 | * Associative array of parameters to send in the request body. 138 | * @param bool $batch 139 | * TRUE if this request should be added to pending batch operations. 140 | * @param bool $returnAssoc 141 | * TRUE to return Mailchimp API response as an associative array. 142 | * 143 | * @return mixed 144 | * Object or Array if $returnAssoc is TRUE. 145 | * 146 | * @throws MailchimpAPIException 147 | */ 148 | public function request($method, $path, $tokens = NULL, $parameters = [], $batch = FALSE, $returnAssoc = FALSE) { 149 | return $this->api_class->request($method, $path, $tokens, $parameters, $batch, $returnAssoc); 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /src/MailchimpAutomations.php: -------------------------------------------------------------------------------- 1 | api_class->request('GET', '/automations', NULL, $parameters); 19 | } 20 | 21 | /** 22 | * Get information about a specific Automation workflow. 23 | * 24 | * @param string $workflow_id 25 | * The unique id for the Automation workflow. 26 | * 27 | * @return object 28 | * 29 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/automations/#read-get_automations_workflow_id 30 | */ 31 | public function getWorkflow($workflow_id) { 32 | $tokens = [ 33 | 'workflow_id' => $workflow_id, 34 | ]; 35 | 36 | return $this->api_class->request('GET', '/automations/{workflow_id}', $tokens); 37 | } 38 | 39 | /** 40 | * Gets a list of automated emails in a workflow. 41 | * 42 | * @param string $workflow_id 43 | * The unique id for the Automation workflow. 44 | * 45 | * @return object 46 | * 47 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/automations/emails/#read-get_automations_workflow_id_emails 48 | */ 49 | public function getWorkflowEmails($workflow_id) { 50 | $tokens = [ 51 | 'workflow_id' => $workflow_id, 52 | ]; 53 | 54 | return $this->api_class->request('GET', '/automations/{workflow_id}/emails', $tokens); 55 | } 56 | 57 | /** 58 | * Gets information about a specific workflow email. 59 | * 60 | * @param string $workflow_id 61 | * The unique id for the Automation workflow. 62 | * @param string $workflow_email_id 63 | * The unique id for the Automation workflow email. 64 | * 65 | * @return object 66 | * 67 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/automations/emails/#read-get_automations_workflow_id_emails 68 | */ 69 | public function getWorkflowEmail($workflow_id, $workflow_email_id) { 70 | $tokens = [ 71 | 'workflow_id' => $workflow_id, 72 | 'workflow_email_id' => $workflow_email_id, 73 | ]; 74 | 75 | return $this->api_class->request('GET', '/automations/{workflow_id}/emails/{workflow_email_id}', $tokens); 76 | } 77 | 78 | /** 79 | * Gets queued subscribers from a Mailchimp workflow automation. 80 | * 81 | * @param string $workflow_id 82 | * The unique id for the Automation workflow. 83 | * @param string $workflow_email_id 84 | * The unique id for the Automation workflow email. 85 | * 86 | * @return object 87 | * 88 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/automations/emails/queue/#read-get_automations_workflow_id_emails_workflow_email_id_queue 89 | */ 90 | public function getWorkflowEmailSubscribers($workflow_id, $workflow_email_id) { 91 | $tokens = [ 92 | 'workflow_id' => $workflow_id, 93 | 'workflow_email_id' => $workflow_email_id, 94 | ]; 95 | 96 | return $this->api_class->request('GET', '/automations/{workflow_id}/emails/{workflow_email_id}/queue', $tokens); 97 | } 98 | 99 | /** 100 | * Gets a subscriber from a Mailchimp workflow automation email queue. 101 | * 102 | * @param string $workflow_id 103 | * The unique id for the Automation workflow. 104 | * @param string $workflow_email_id 105 | * The unique id for the Automation workflow email. 106 | * @param string $email 107 | * The email address of the subscriber. 108 | * 109 | * @return object 110 | * 111 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/automations/emails/queue/#read-get_automations_workflow_id_emails_workflow_email_id_queue_subscriber_hash 112 | */ 113 | public function getWorkflowEmailSubscriber($workflow_id, $workflow_email_id, $email) { 114 | $tokens = [ 115 | 'workflow_id' => $workflow_id, 116 | 'workflow_email_id' => $workflow_email_id, 117 | 'subscriber_hash' => md5(strtolower($email)), 118 | ]; 119 | 120 | return $this->api_class->request('GET', '/automations/{workflow_id}/emails/{workflow_email_id}/queue/{subscriber_hash}', $tokens); 121 | } 122 | 123 | /** 124 | * Adds a subscriber to a Mailchimp workflow automation email queue. 125 | * 126 | * @param string $workflow_id 127 | * The unique id for the Automation workflow. 128 | * @param string $workflow_email_id 129 | * The unique id for the Automation workflow email. 130 | * @param string $email 131 | * The email address of the subscriber. 132 | * @param array $parameters 133 | * Associative array of optional request parameters. 134 | * 135 | * @return object 136 | * 137 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/automations/emails/queue/#create-post_automations_workflow_id_emails_workflow_email_id_queue 138 | */ 139 | public function addWorkflowEmailSubscriber($workflow_id, $workflow_email_id, $email, $parameters = []) { 140 | $tokens = [ 141 | 'workflow_id' => $workflow_id, 142 | 'workflow_email_id' => $workflow_email_id, 143 | ]; 144 | 145 | $parameters += [ 146 | 'email_address' => $email, 147 | ]; 148 | 149 | return $this->api_class->request('POST', '/automations/{workflow_id}/emails/{workflow_email_id}/queue', $tokens, $parameters); 150 | } 151 | 152 | } 153 | -------------------------------------------------------------------------------- /src/MailchimpCampaigns.php: -------------------------------------------------------------------------------- 1 | api_class->request('GET', '/campaigns', NULL, $parameters); 33 | } 34 | 35 | /** 36 | * Gets information about a specific campaign. 37 | * 38 | * @param string $campaign_id 39 | * The ID of the campaign. 40 | * @param array $parameters 41 | * Associative array of optional request parameters. 42 | * 43 | * @return object 44 | * 45 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#read-get_campaigns_campaign_id 46 | */ 47 | public function getCampaign($campaign_id, $parameters = []) { 48 | $tokens = [ 49 | 'campaign_id' => $campaign_id, 50 | ]; 51 | 52 | return $this->api_class->request('GET', '/campaigns/{campaign_id}', $tokens, $parameters); 53 | } 54 | 55 | /** 56 | * Adds a new campaign to the authenticated account. 57 | * 58 | * @param string $type 59 | * The campaign type. See CAMPAIGN_TYPE_* constants. 60 | * @param object $recipients 61 | * List settings for the campaign. 62 | * @param object $settings 63 | * The subject, from name, reply-to, etc settings for the campaign. 64 | * @param array $parameters 65 | * Associative array of optional request parameters. 66 | * @param bool $batch 67 | * TRUE to create a new pending batch operation. 68 | * 69 | * @return object 70 | * 71 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#create-post_campaigns 72 | */ 73 | public function addCampaign($type, $recipients, $settings, $parameters = [], $batch = FALSE) { 74 | $parameters += [ 75 | 'type' => $type, 76 | 'recipients' => $recipients, 77 | 'settings' => $settings, 78 | ]; 79 | 80 | return $this->api_class->request('POST', '/campaigns', NULL, $parameters, $batch); 81 | } 82 | 83 | /** 84 | * Gets the HTML, plain-text, and template content for a Mailchimp campaign. 85 | * 86 | * @param string $campaign_id 87 | * The ID of the campaign. 88 | * @param array $parameters 89 | * Associative array of optional request parameters. 90 | * 91 | * @return object 92 | * 93 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#read-get_campaigns_campaign_id_content 94 | */ 95 | public function getCampaignContent($campaign_id, $parameters = []) { 96 | $tokens = [ 97 | 'campaign_id' => $campaign_id, 98 | ]; 99 | 100 | return $this->api_class->request('GET', '/campaigns/{campaign_id}/content', $tokens, $parameters); 101 | } 102 | 103 | /** 104 | * Sets the HTML, plain-text, and template content for a Mailchimp campaign. 105 | * 106 | * @param string $campaign_id 107 | * The ID of the campaign. 108 | * @param array $parameters 109 | * Associative array of optional request parameters. 110 | * 111 | * @return object 112 | * 113 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/content/#edit-put_campaigns_campaign_id_content 114 | */ 115 | public function setCampaignContent($campaign_id, $parameters = []) { 116 | $tokens = [ 117 | 'campaign_id' => $campaign_id, 118 | ]; 119 | 120 | return $this->api_class->request('PUT', '/campaigns/{campaign_id}/content', $tokens, $parameters); 121 | } 122 | 123 | /** 124 | * Get the send checklist for a Mailchimp campaign. 125 | * 126 | * @param string $campaign_id 127 | * The ID of the campaign. 128 | * 129 | * @return object 130 | * 131 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/send-checklist 132 | */ 133 | public function getSendChecklist($campaign_id) { 134 | $tokens = [ 135 | 'campaign_id' => $campaign_id, 136 | ]; 137 | 138 | return $this->api_class->request('GET', '/campaigns/{campaign_id}/send-checklist', $tokens, []); 139 | } 140 | 141 | /** 142 | * Updates a campaign. 143 | * 144 | * @param string $campaign_id 145 | * The ID of the campaign. 146 | * @param string $type 147 | * The campaign type. See CAMPAIGN_TYPE_* constants. 148 | * @param object $recipients 149 | * List settings for the campaign. 150 | * @param object $settings 151 | * The subject, from name, reply-to, etc settings for the campaign. 152 | * @param array $parameters 153 | * Associative array of optional request parameters. 154 | * @param bool $batch 155 | * TRUE to create a new pending batch operation. 156 | * 157 | * @return object 158 | * 159 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#edit-patch_campaigns_campaign_id 160 | */ 161 | public function updateCampaign($campaign_id, $type, $recipients, $settings, $parameters = [], $batch = FALSE) { 162 | $tokens = [ 163 | 'campaign_id' => $campaign_id, 164 | ]; 165 | 166 | $parameters += [ 167 | 'type' => $type, 168 | 'recipients' => $recipients, 169 | 'settings' => $settings, 170 | ]; 171 | 172 | return $this->api_class->request('PATCH', '/campaigns/{campaign_id}', $tokens, $parameters, $batch); 173 | } 174 | 175 | /** 176 | * Sends a test email. 177 | * 178 | * @param string $campaign_id 179 | * The ID of the campaign. 180 | * @param array $test_emails 181 | * Email addresses to send the test email to. 182 | * @param string $send_type 183 | * The type of test email to send. 184 | * @param array $parameters 185 | * Associative array of optional request parameters. 186 | * @param bool $batch 187 | * TRUE to create a new pending batch operation. 188 | * 189 | * @return object 190 | * 191 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_test 192 | */ 193 | public function sendTest($campaign_id, $test_emails, $send_type, $parameters = [], $batch = FALSE) { 194 | $tokens = [ 195 | 'campaign_id' => $campaign_id, 196 | ]; 197 | 198 | $parameters += [ 199 | 'test_emails' => $test_emails, 200 | 'send_type' => $send_type, 201 | ]; 202 | 203 | return $this->api_class->request('POST', '/campaigns/{campaign_id}/actions/test', $tokens, $parameters, $batch); 204 | } 205 | 206 | /** 207 | * Schedule a Mailchimp campaign. 208 | * 209 | * @param string $campaign_id 210 | * The ID of the campaign. 211 | * @param schedule_time $schedule_time 212 | * The date and time in UTC to schedule the campaign for delivery. 213 | * @param bool $timewarp 214 | * Choose whether the campaign should use Timewarp when sending. 215 | * @param object $batch_delivery 216 | * Choose whether the campaign should use Batch Delivery. 217 | * Cannot be set to true for campaigns using Timewarp. 218 | * @param bool $batch 219 | * TRUE to create a new pending batch operation. 220 | * 221 | * @return object 222 | * 223 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_schedule 224 | */ 225 | public function schedule($campaign_id, $schedule_time, $timewarp = FALSE, $batch_delivery = FALSE, $parameters = [], $batch = FALSE) { 226 | $tokens = [ 227 | 'campaign_id' => $campaign_id, 228 | ]; 229 | 230 | $parameters += [ 231 | 'schedule_time' => $schedule_time, 232 | 'timewarp' => $timewarp, 233 | 'batch_delivery' => $batch_delivery, 234 | ]; 235 | 236 | return $this->api_class->request('POST', '/campaigns/{campaign_id}/actions/schedule', $tokens, $parameters, $batch); 237 | } 238 | 239 | /** 240 | * Unschedule a Mailchimp campaign. 241 | * 242 | * @param string $campaign_id 243 | * The ID of the campaign. 244 | * 245 | * @return object 246 | * 247 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_unschedule 248 | */ 249 | public function unschedule($campaign_id) { 250 | $tokens = [ 251 | 'campaign_id' => $campaign_id, 252 | ]; 253 | 254 | return $this->api_class->request('POST', '/campaigns/{campaign_id}/actions/unschedule', $tokens, []); 255 | } 256 | 257 | /** 258 | * Send a Mailchimp campaign. 259 | * 260 | * @param string $campaign_id 261 | * The ID of the campaign. 262 | * @param bool $batch 263 | * TRUE to create a new pending batch operation. 264 | * 265 | * @return object 266 | * 267 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#action-post_campaigns_campaign_id_actions_send 268 | */ 269 | public function send($campaign_id, $batch = FALSE) { 270 | $tokens = [ 271 | 'campaign_id' => $campaign_id, 272 | ]; 273 | 274 | return $this->api_class->request('POST', '/campaigns/{campaign_id}/actions/send', $tokens, [], $batch); 275 | } 276 | 277 | /** 278 | * Deletes a Mailchimp campaign. 279 | * 280 | * @param string $campaign_id 281 | * The ID of the campaign. 282 | * 283 | * @return object 284 | * 285 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/#delete-delete_campaigns_campaign_id 286 | */ 287 | public function delete($campaign_id) { 288 | $tokens = [ 289 | 'campaign_id' => $campaign_id, 290 | ]; 291 | 292 | return $this->api_class->request('DELETE', '/campaigns/{campaign_id}', $tokens); 293 | } 294 | 295 | } 296 | -------------------------------------------------------------------------------- /src/MailchimpConnectedSites.php: -------------------------------------------------------------------------------- 1 | api_class->request('GET', '/connected-sites', NULL, $parameters); 24 | } 25 | 26 | /** 27 | * Gets a connected site. 28 | * 29 | * @param string $connected_site_id 30 | * The ID of the connected site. 31 | * @param array $parameters 32 | * Associative array of optional request parameters. 33 | * 34 | * @return object 35 | */ 36 | public function getConnectedSite($connected_site_id, $parameters = []) { 37 | $tokens = [ 38 | 'connected_site_id' => $connected_site_id, 39 | ]; 40 | 41 | return $this->api_class->request('GET', '/connected-sites/{connected_site_id}', $tokens, $parameters); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/MailchimpLists.php: -------------------------------------------------------------------------------- 1 | api_class->request('GET', '/lists', NULL, $parameters); 29 | } 30 | 31 | /** 32 | * Gets a Mailchimp list. 33 | * 34 | * @param string $list_id 35 | * The ID of the list. 36 | * @param array $parameters 37 | * Associative array of optional request parameters. 38 | * 39 | * @return object 40 | * 41 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/#read-get_lists_list_id 42 | */ 43 | public function getList($list_id, $parameters = []) { 44 | $tokens = [ 45 | 'list_id' => $list_id, 46 | ]; 47 | 48 | return $this->api_class->request('GET', '/lists/{list_id}', $tokens, $parameters); 49 | } 50 | 51 | /** 52 | * Gets information about all interest categories associated with a list. 53 | * 54 | * @param string $list_id 55 | * The ID of the list. 56 | * @param array $parameters 57 | * Associative array of optional request parameters. 58 | * 59 | * @return object 60 | * 61 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#read-get_lists_list_id_interest_categories 62 | */ 63 | public function getInterestCategories($list_id, $parameters = []) { 64 | $tokens = [ 65 | 'list_id' => $list_id, 66 | ]; 67 | 68 | return $this->api_class->request('GET', '/lists/{list_id}/interest-categories', $tokens, $parameters); 69 | } 70 | 71 | /** 72 | * Create new interest category associated with a list. 73 | * 74 | * @param string $list_id 75 | * The ID of the list. 76 | * @param string $title 77 | * The title of interest category. 78 | * @param string $type 79 | * The type of interest category. 80 | * @param array $parameters 81 | * Associative array of optional request parameters. 82 | * 83 | * @return object 84 | * 85 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories 86 | */ 87 | public function addInterestCategories($list_id, $title, $type, $parameters = [], $batch = FALSE) { 88 | $tokens = [ 89 | 'list_id' => $list_id, 90 | ]; 91 | 92 | $parameters += [ 93 | 'title' => $title, 94 | 'type' => $type, 95 | ]; 96 | 97 | return $this->api_class->request('POST', '/lists/{list_id}/interest-categories', $tokens, $parameters); 98 | } 99 | 100 | /** 101 | * Update interest category associated with a list. 102 | * 103 | * @param string $list_id 104 | * The ID of the list. 105 | * @param string $interest_category_id 106 | * The ID of the interest category. 107 | * @param string $title 108 | * The title of interest category. 109 | * @param string $type 110 | * The type of interest category. 111 | * @param array $parameters 112 | * Associative array of optional request parameters. 113 | * 114 | * @return object 115 | * 116 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories 117 | */ 118 | public function updateInterestCategories($list_id, $interest_category_id, $title, $type, $parameters = [], $batch = FALSE) { 119 | $tokens = [ 120 | 'list_id' => $list_id, 121 | 'interest_category_id' => $interest_category_id, 122 | ]; 123 | 124 | $parameters += [ 125 | 'title' => $title, 126 | 'type' => $type, 127 | ]; 128 | 129 | return $this->api_class->request('PATCH', '/lists/{list_id}/interest-categories/{interest_category_id}', $tokens, $parameters); 130 | } 131 | 132 | /** 133 | * Update interest category associated with a list. 134 | * 135 | * @param string $list_id 136 | * The ID of the list. 137 | * @param string $interest_category_id 138 | * The ID of the interest category. 139 | * @param array $parameters 140 | * Associative array of optional request parameters. 141 | * 142 | * @return object 143 | * 144 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories 145 | */ 146 | public function deleteInterestCategories($list_id, $interest_category_id, $parameters = [], $batch = FALSE) { 147 | $tokens = [ 148 | 'list_id' => $list_id, 149 | 'interest_category_id' => $interest_category_id, 150 | ]; 151 | 152 | return $this->api_class->request('DELETE', '/lists/{list_id}/interest-categories/{interest_category_id}', $tokens, $parameters); 153 | } 154 | 155 | /** 156 | * Gets information about all interests associated with an interest category. 157 | * 158 | * @param string $list_id 159 | * The ID of the list. 160 | * @param string $interest_category_id 161 | * The ID of the interest category. 162 | * @param array $parameters 163 | * Associative array of optional request parameters. 164 | * 165 | * @return object 166 | * 167 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/interests/#read-get_lists_list_id_interest_categories_interest_category_id_interests 168 | */ 169 | public function getInterests($list_id, $interest_category_id, $parameters = []) { 170 | $tokens = [ 171 | 'list_id' => $list_id, 172 | 'interest_category_id' => $interest_category_id, 173 | ]; 174 | 175 | return $this->api_class->request('GET', '/lists/{list_id}/interest-categories/{interest_category_id}/interests', $tokens, $parameters); 176 | } 177 | 178 | /** 179 | * Create new interest in category. 180 | * 181 | * @param string $list_id 182 | * The ID of the list. 183 | * @param string $interest_category_id 184 | * The ID of interest category. 185 | * @param string $name 186 | * The name of interest. 187 | * @param array $parameters 188 | * Associative array of optional request parameters. 189 | * 190 | * @return object 191 | * 192 | * @see https://mailchimp.com/developer/reference/lists/interest-categories/interests/ 193 | */ 194 | public function addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE) { 195 | $tokens = [ 196 | 'list_id' => $list_id, 197 | 'interest_category_id' => $interest_category_id, 198 | ]; 199 | 200 | $parameters += [ 201 | 'name' => $name, 202 | ]; 203 | 204 | return $this->api_class->request('POST', '/lists/{list_id}/interest-categories/{interest_category_id}/interests', $tokens, $parameters); 205 | } 206 | 207 | /** 208 | * Edit interest in category. 209 | * 210 | * @param string $list_id 211 | * The ID of the list. 212 | * @param string $interest_category_id 213 | * The ID of interest category. 214 | * @param string $interest_id 215 | * The ID of interest. 216 | * @param string $name 217 | * The name of interest. 218 | * @param array $parameters 219 | * Associative array of optional request parameters. 220 | * 221 | * @return object 222 | * 223 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories 224 | */ 225 | public function updateInterests($list_id, $interest_category_id, $interest_id, $name, $parameters = [], $batch = FALSE) { 226 | $tokens = [ 227 | 'list_id' => $list_id, 228 | 'interest_category_id' => $interest_category_id, 229 | 'interest_id' => $interest_id, 230 | ]; 231 | 232 | $parameters += [ 233 | 'name' => $name, 234 | ]; 235 | 236 | return $this->api_class->request('PATCH', '/lists/{list_id}/interest-categories/{interest_category_id}/interests/{interest_id}', $tokens, $parameters); 237 | } 238 | 239 | /** 240 | * Delete interest in category. 241 | * 242 | * @param string $list_id 243 | * The ID of the list. 244 | * @param string $interest_category_id 245 | * The ID of interest category. 246 | * @param string $interest_id 247 | * The ID of interest. 248 | * @param array $parameters 249 | * Associative array of optional request parameters. 250 | * 251 | * @return object 252 | * 253 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/interest-categories/#create-post_lists_list_id_interest_categories 254 | */ 255 | public function deleteInterests($list_id, $interest_category_id, $interest_id, $parameters = [], $batch = FALSE) { 256 | $tokens = [ 257 | 'list_id' => $list_id, 258 | 'interest_category_id' => $interest_category_id, 259 | 'interest_id' => $interest_id, 260 | ]; 261 | 262 | return $this->api_class->request('DELETE', '/lists/{list_id}/interest-categories/{interest_category_id}/interests/{interest_id}', $tokens, $parameters); 263 | } 264 | 265 | /** 266 | * Gets merge fields associated with a Mailchimp list. 267 | * 268 | * @param string $list_id 269 | * The ID of the list. 270 | * @param array $parameters 271 | * Associative array of optional request parameters. 272 | * 273 | * @return object 274 | * 275 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/merge-fields/#read-get_lists_list_id_merge_fields 276 | */ 277 | public function getMergeFields($list_id, $parameters = []) { 278 | $tokens = [ 279 | 'list_id' => $list_id, 280 | ]; 281 | 282 | return $this->api_class->request('GET', '/lists/{list_id}/merge-fields', $tokens, $parameters); 283 | } 284 | 285 | /** 286 | * Add merge field associated with a Mailchimp list. 287 | * 288 | * @param string $list_id 289 | * The ID of the list. 290 | * @param string $name 291 | * The name of the merge field. 292 | * @param string $type 293 | * The type for the merge field. 294 | * @param array $parameters 295 | * Associative array of optional request parameters. 296 | * 297 | * @return object 298 | * 299 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/merge-fields/#create-post_lists_list_id_merge_fields 300 | */ 301 | public function addMergeField($list_id, $name, $type, $parameters = []) { 302 | $tokens = [ 303 | 'list_id' => $list_id, 304 | ]; 305 | 306 | $parameters += [ 307 | 'name' => $name, 308 | 'type' => $type, 309 | ]; 310 | 311 | return $this->api_class->request('POST', '/lists/{list_id}/merge-fields', $tokens, $parameters); 312 | } 313 | 314 | /** 315 | * Gets information about all members of a Mailchimp list. 316 | * 317 | * @param string $list_id 318 | * The ID of the list. 319 | * @param array $parameters 320 | * Associative array of optional request parameters. 321 | * 322 | * @return object 323 | * 324 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members 325 | */ 326 | public function getMembers($list_id, $parameters = []) { 327 | $tokens = [ 328 | 'list_id' => $list_id, 329 | ]; 330 | 331 | return $this->api_class->request('GET', '/lists/{list_id}/members', $tokens, $parameters); 332 | } 333 | 334 | /** 335 | * Gets information about a member of a Mailchimp list. 336 | * 337 | * @param string $list_id 338 | * The ID of the list. 339 | * @param string $email 340 | * The member's email address. 341 | * @param array $parameters 342 | * Associative array of optional request parameters. 343 | * 344 | * @return object 345 | * 346 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#read-get_lists_list_id_members_subscriber_hash 347 | */ 348 | public function getMemberInfo($list_id, $email, $parameters = []) { 349 | $tokens = [ 350 | 'list_id' => $list_id, 351 | 'subscriber_hash' => md5(strtolower($email)), 352 | ]; 353 | 354 | return $this->api_class->request('GET', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters); 355 | } 356 | 357 | /** 358 | * Add an event for a Mailchimp list member. 359 | * 360 | * @param string $list_id 361 | * The ID of the list. 362 | * @param string $email 363 | * The member's email address. 364 | * @param array $parameters 365 | * Associative array of optional request parameters. 366 | * 367 | * @return object 368 | * 369 | * @see https://mailchimp.com/developer/marketing/api/list-member-events/add-event/ 370 | */ 371 | public function addMemberEvent($list_id, $email, $parameters = []) { 372 | $tokens = [ 373 | 'list_id' => $list_id, 374 | 'subscriber_hash' => md5(strtolower($email)), 375 | ]; 376 | 377 | return $this->api_class->request('POST', '/lists/{list_id}/members/{subscriber_hash}/events', $tokens, $parameters); 378 | } 379 | 380 | /** 381 | * Gets information about a member of a Mailchimp list. 382 | * 383 | * @param string $list_id 384 | * The ID of the list. 385 | * @param string $mc_eid 386 | * The member's unique ID. 387 | * @param array $parameters 388 | * Associative array of optional request parameters. 389 | * 390 | * @return object 391 | * 392 | * @see https://developer.mailchimp.com/documentation/mailchimp/guides/getting-started-with-ecommerce/ 393 | */ 394 | public function getMemberInfoById($list_id, $mc_eid, $parameters = []) { 395 | $tokens = [ 396 | 'list_id' => $list_id, 397 | ]; 398 | 399 | $parameters = [ 400 | 'unique_email_id' => $mc_eid, 401 | ]; 402 | 403 | return $this->api_class->request('GET', '/lists/{list_id}/members/', $tokens, $parameters); 404 | } 405 | 406 | /** 407 | * Gets activity related to a member of a Mailchimp list. 408 | * 409 | * @param string $list_id 410 | * The ID of the list. 411 | * @param string $email 412 | * The member's email address. 413 | * @param array $parameters 414 | * Associative array of optional request parameters. 415 | * 416 | * @return object 417 | * 418 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/activity/#read-get_lists_list_id_members_subscriber_hash_activity 419 | */ 420 | public function getMemberActivity($list_id, $email, $parameters = []) { 421 | $tokens = [ 422 | 'list_id' => $list_id, 423 | 'subscriber_hash' => md5(strtolower($email)), 424 | ]; 425 | 426 | return $this->api_class->request('GET', '/lists/{list_id}/members/{subscriber_hash}/activity', $tokens, $parameters); 427 | } 428 | 429 | /** 430 | * Get events for a Mailchimp contact. 431 | * 432 | * @param string $list_id 433 | * The ID of the list. 434 | * @param string $email 435 | * The member's email address. 436 | * @param array $parameters 437 | * Associative array of optional request parameters. 438 | * 439 | * @return object 440 | * 441 | * @see https://mailchimp.com/developer/marketing/api/list-member-events/list-member-events/ 442 | */ 443 | public function getMemberEvents($list_id, $email, $parameters = []) { 444 | $tokens = [ 445 | 'list_id' => $list_id, 446 | 'subscriber_hash' => md5(strtolower($email)), 447 | ]; 448 | 449 | return $this->api_class->request('GET', '/lists/{list_id}/members/{subscriber_hash}/events', $tokens, $parameters); 450 | } 451 | 452 | /** 453 | * Adds a new member to a Mailchimp list. 454 | * 455 | * @param string $list_id 456 | * The ID of the list. 457 | * @param string $email 458 | * The email address to add. 459 | * @param array $parameters 460 | * Associative array of optional request parameters. 461 | * @param bool $batch 462 | * TRUE to create a new pending batch operation. 463 | * 464 | * @return object 465 | * 466 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#create-post_lists_list_id_members 467 | */ 468 | public function addMember($list_id, $email, $parameters = [], $batch = FALSE) { 469 | $tokens = [ 470 | 'list_id' => $list_id, 471 | ]; 472 | 473 | $parameters += [ 474 | 'email_address' => $email, 475 | ]; 476 | 477 | return $this->api_class->request('POST', '/lists/{list_id}/members', $tokens, $parameters, $batch); 478 | } 479 | 480 | /** 481 | * Removes a member from a Mailchimp list. 482 | * 483 | * @param string $list_id 484 | * The ID of the list. 485 | * @param string $email 486 | * The member's email address. 487 | * 488 | * @return object 489 | * 490 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#delete-delete_lists_list_id_members_subscriber_hash 491 | */ 492 | public function removeMember($list_id, $email) { 493 | $tokens = [ 494 | 'list_id' => $list_id, 495 | 'subscriber_hash' => md5(strtolower($email)), 496 | ]; 497 | 498 | return $this->api_class->request('DELETE', '/lists/{list_id}/members/{subscriber_hash}', $tokens); 499 | } 500 | 501 | /** 502 | * Updates a member of a Mailchimp list. 503 | * 504 | * @param string $list_id 505 | * The ID of the list. 506 | * @param string $email 507 | * The member's email address. 508 | * @param array $parameters 509 | * Associative array of optional request parameters. 510 | * @param bool $batch 511 | * TRUE to create a new pending batch operation. 512 | * 513 | * @return object 514 | * 515 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-patch_lists_list_id_members_subscriber_hash 516 | */ 517 | public function updateMember($list_id, $email, $parameters = [], $batch = FALSE) { 518 | $tokens = [ 519 | 'list_id' => $list_id, 520 | 'subscriber_hash' => md5(strtolower($email)), 521 | ]; 522 | 523 | return $this->api_class->request('PATCH', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters, $batch); 524 | } 525 | 526 | /** 527 | * Adds a new or update an existing member of a Mailchimp list. 528 | * 529 | * @param string $list_id 530 | * The ID of the list. 531 | * @param string $email 532 | * The member's email address. 533 | * @param array $parameters 534 | * Associative array of optional request parameters. 535 | * @param bool $batch 536 | * TRUE to create a new pending batch operation. 537 | * 538 | * @return object 539 | * 540 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/#edit-put_lists_list_id_members_subscriber_hash 541 | */ 542 | public function addOrUpdateMember($list_id, $email, $parameters = [], $batch = FALSE) { 543 | $tokens = [ 544 | 'list_id' => $list_id, 545 | 'subscriber_hash' => md5(strtolower($email)), 546 | ]; 547 | 548 | $parameters += [ 549 | 'email_address' => $email, 550 | ]; 551 | 552 | return $this->api_class->request('PUT', '/lists/{list_id}/members/{subscriber_hash}', $tokens, $parameters, $batch); 553 | } 554 | 555 | /** 556 | * Gets tags related to a member of a MailChimp list. 557 | * 558 | * @param string $list_id 559 | * The ID of the list. 560 | * @param string $email 561 | * The member's email address. 562 | * @param array $parameters 563 | * Associative array of optional request parameters. 564 | * 565 | * @return object 566 | * 567 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/#read-get_lists_list_id_members_subscriber_hash_tags 568 | */ 569 | public function getMemberTags($list_id, $email, $parameters = []) { 570 | $tokens = [ 571 | 'list_id' => $list_id, 572 | 'subscriber_hash' => md5(strtolower($email)), 573 | ]; 574 | 575 | return $this->api_class->request('GET', '/lists/{list_id}/members/{subscriber_hash}/tags', $tokens, $parameters); 576 | } 577 | 578 | /** 579 | * Gets information about segments associated with a Mailchimp list. 580 | * 581 | * @param string $list_id 582 | * The ID of the list. 583 | * @param array $parameters 584 | * Associative array of optional request parameters. 585 | * 586 | * @return object 587 | * 588 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#read-get_lists_list_id_segments 589 | */ 590 | public function getSegments($list_id, $parameters = []) { 591 | $tokens = [ 592 | 'list_id' => $list_id, 593 | ]; 594 | 595 | return $this->api_class->request('GET', '/lists/{list_id}/segments', $tokens, $parameters); 596 | } 597 | 598 | /** 599 | * Gets a Mailchimp list segment. 600 | * 601 | * @param string $list_id 602 | * The ID of the list. 603 | * @param string $segment_id 604 | * The ID of the list segment. 605 | * @param array $parameters 606 | * Associative array of optional request parameters. 607 | * 608 | * @return object 609 | * 610 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#read-get_lists_list_id_segments_segment_id 611 | */ 612 | public function getSegment($list_id, $segment_id, $parameters = []) { 613 | $tokens = [ 614 | 'list_id' => $list_id, 615 | 'segment_id' => $segment_id, 616 | ]; 617 | 618 | return $this->api_class->request('GET', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters); 619 | } 620 | 621 | /** 622 | * Adds a new segment to a Mailchimp list. 623 | * 624 | * @param string $list_id 625 | * The ID of the list. 626 | * @param string $name 627 | * The name of the segment. 628 | * @param array $parameters 629 | * Associative array of optional request parameters. 630 | * @param bool $batch 631 | * TRUE to create a new pending batch operation. 632 | * 633 | * @return object 634 | * 635 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#create-post_lists_list_id_segments 636 | */ 637 | public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) { 638 | $tokens = [ 639 | 'list_id' => $list_id, 640 | ]; 641 | 642 | $parameters += [ 643 | 'name' => $name, 644 | ]; 645 | 646 | return $this->api_class->request('POST', '/lists/{list_id}/segments', $tokens, $parameters, $batch); 647 | } 648 | 649 | /** 650 | * Updates a segment associated with a Mailchimp list. 651 | * 652 | * @param string $list_id 653 | * The ID of the list. 654 | * @param int $segment_id 655 | * The ID of the segment. 656 | * @param string $name 657 | * The name of the segment. 658 | * @param array $parameters 659 | * Associative array of optional request parameters. 660 | * @param bool $batch 661 | * TRUE to create a new pending batch operation. 662 | * 663 | * @return object 664 | * 665 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/#edit-patch_lists_list_id_segments_segment_id 666 | */ 667 | public function updateSegment($list_id, $segment_id, $name, $parameters = [], $batch = FALSE) { 668 | $tokens = [ 669 | 'list_id' => $list_id, 670 | 'segment_id' => $segment_id, 671 | ]; 672 | 673 | $parameters += [ 674 | 'name' => $name, 675 | ]; 676 | 677 | return $this->api_class->request('PATCH', '/lists/{list_id}/segments/{segment_id}', $tokens, $parameters, $batch); 678 | } 679 | 680 | /** 681 | * Gets information about members of a list segment. 682 | * 683 | * @param string $list_id 684 | * The ID of the list. 685 | * @param string $segment_id 686 | * The ID of the segment. 687 | * @param array $parameters 688 | * Associative array of optional request parameters. 689 | * 690 | * @return object 691 | * 692 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/members/#read-get_lists_list_id_segments_segment_id_members 693 | */ 694 | public function getSegmentMembers($list_id, $segment_id, $parameters = []) { 695 | $tokens = [ 696 | 'list_id' => $list_id, 697 | 'segment_id' => $segment_id, 698 | ]; 699 | 700 | return $this->api_class->request('GET', '/lists/{list_id}/segments/{segment_id}/members', $tokens, $parameters); 701 | } 702 | 703 | /** 704 | * Adds a member to a list segment. 705 | * 706 | * @param string $list_id 707 | * The ID of the list. 708 | * @param string $segment_id 709 | * The ID of the segment. 710 | * @param string $email 711 | * The email address to add to the segment. 712 | * @param array $parameters 713 | * Associative array of optional request parameters. 714 | * 715 | * @return object 716 | * 717 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/members/ 718 | */ 719 | public function addSegmentMember($list_id, $segment_id, $email, $parameters = []) { 720 | $tokens = [ 721 | 'list_id' => $list_id, 722 | 'segment_id' => $segment_id, 723 | ]; 724 | 725 | $parameters += [ 726 | 'email_address' => $email, 727 | ]; 728 | 729 | return $this->api_class->request('POST', '/lists/{list_id}/segments/{segment_id}/members', $tokens, $parameters); 730 | } 731 | 732 | /** 733 | * Removes a member from a list segment. 734 | * 735 | * @param string $list_id 736 | * The ID of the list. 737 | * @param string $segment_id 738 | * The ID of the segment. 739 | * @param string $email 740 | * The email address to remove from the segment. 741 | * 742 | * @return object 743 | * 744 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/segments/members/#delete-delete_lists_list_id_segments_segment_id_members_subscriber_hash 745 | */ 746 | public function removeSegmentMember($list_id, $segment_id, $email) { 747 | $tokens = [ 748 | 'list_id' => $list_id, 749 | 'segment_id' => $segment_id, 750 | 'subscriber_hash' => md5(strtolower($email)), 751 | ]; 752 | 753 | return $this->api_class->request('DELETE', '/lists/{list_id}/segments/{segment_id}/members/{subscriber_hash}', $tokens); 754 | } 755 | 756 | /** 757 | * Adds tags to a member. 758 | * 759 | * @param string $list_id 760 | * The ID of the list. 761 | * @param string[] $tags 762 | * A list of tags to add. 763 | * @param string $email 764 | * The email address to add the tag to. 765 | * @param array $parameters 766 | * Associative array of optional request parameters. 767 | * 768 | * @return object 769 | * 770 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/ 771 | */ 772 | public function addTagsMember($list_id, array $tags, $email, array $parameters = []) { 773 | $tokens = [ 774 | 'list_id' => $list_id, 775 | 'subscriber_hash' => md5(strtolower($email)), 776 | ]; 777 | 778 | foreach ($tags as $tag) { 779 | $parameters['tags'][] = [ 780 | 'name' => $tag, 781 | 'status' => 'active', 782 | ]; 783 | } 784 | 785 | return $this->api_class->request('POST', '/lists/{list_id}/members/{subscriber_hash}/tags', $tokens, $parameters); 786 | } 787 | 788 | /** 789 | * Removes tags from a member. 790 | * 791 | * @param string $list_id 792 | * The ID of the list. 793 | * @param string[] $tags 794 | * A list of tags to remove. 795 | * @param string $email 796 | * The email address to remove the tag from. 797 | * @param array $parameters 798 | * Associative array of optional request parameters. 799 | * 800 | * @return object 801 | * 802 | * @see https://developer.mailchimp.com/documentation/mailchimp/reference/lists/members/tags/ 803 | */ 804 | public function removeTagsMember($list_id, array $tags, $email, array $parameters = []) { 805 | $tokens = [ 806 | 'list_id' => $list_id, 807 | 'subscriber_hash' => md5(strtolower($email)), 808 | ]; 809 | 810 | foreach ($tags as $tag) { 811 | $parameters['tags'][] = [ 812 | 'name' => $tag, 813 | 'status' => 'inactive', 814 | ]; 815 | } 816 | 817 | return $this->api_class->request('POST', '/lists/{list_id}/members/{subscriber_hash}/tags', $tokens, $parameters); 818 | } 819 | 820 | /** 821 | * Gets information about webhooks associated with a list. 822 | * 823 | * @param string $list_id 824 | * The ID of the list. 825 | * @param array $parameters 826 | * Associative array of optional request parameters. 827 | * 828 | * @return object 829 | * 830 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/webhooks/#read-get_lists_list_id_webhooks 831 | */ 832 | public function getWebhooks($list_id, $parameters = []) { 833 | $tokens = [ 834 | 'list_id' => $list_id, 835 | ]; 836 | 837 | return $this->api_class->request('GET', '/lists/{list_id}/webhooks', $tokens, $parameters); 838 | } 839 | 840 | /** 841 | * Gets information about a specific webhook associated with a list. 842 | * 843 | * @param string $list_id 844 | * The ID of the list. 845 | * @param string $webhook_id 846 | * The ID of the webhook. 847 | * @param array $parameters 848 | * Associative array of optional request parameters. 849 | * 850 | * @return object 851 | * 852 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/lists/webhooks/#read-get_lists_list_id_webhooks_webhook_id 853 | */ 854 | public function getWebhook($list_id, $webhook_id, $parameters = []) { 855 | $tokens = [ 856 | 'list_id' => $list_id, 857 | 'webhook_id' => $webhook_id, 858 | ]; 859 | 860 | return $this->api_class->request('GET', '/lists/{list_id}/webhooks/{webhook_id}', $tokens, $parameters); 861 | } 862 | 863 | /** 864 | * Adds a new webhook to a list. 865 | * 866 | * @param string $list_id 867 | * The ID of the list. 868 | * @param string $url 869 | * The callback URL the webhook should notify of events. 870 | * @param array $parameters 871 | * Associative array of optional request parameters. 872 | * @param bool $batch 873 | * TRUE to create a new pending batch operation. 874 | * 875 | * @return object 876 | */ 877 | public function addWebhook($list_id, $url, $parameters = [], $batch = FALSE) { 878 | $tokens = [ 879 | 'list_id' => $list_id, 880 | ]; 881 | 882 | $parameters += [ 883 | 'url' => $url, 884 | ]; 885 | 886 | return $this->api_class->request('POST', '/lists/{list_id}/webhooks', $tokens, $parameters, $batch); 887 | } 888 | 889 | /** 890 | * Deletes a webhook. 891 | * 892 | * @param string $list_id 893 | * The ID of the list. 894 | * @param string $webhook_id 895 | * The ID of the webhook. 896 | * @param array $parameters 897 | * Associative array of optional request parameters. 898 | * 899 | * @return object 900 | */ 901 | public function deleteWebhook($list_id, $webhook_id, $parameters = []) { 902 | $tokens = [ 903 | 'list_id' => $list_id, 904 | 'webhook_id' => $webhook_id, 905 | ]; 906 | 907 | return $this->api_class->request('DELETE', '/lists/{list_id}/webhooks/{webhook_id}', $tokens, $parameters); 908 | } 909 | 910 | /** 911 | * Gets all lists an email address is subscribed to. 912 | * 913 | * @param string $email 914 | * The email address to get lists for. 915 | * 916 | * @return array 917 | * Array of subscribed list objects. 918 | * 919 | * @throws MailchimpAPIException 920 | */ 921 | public function getListsForEmail($email) { 922 | $list_data = $this->getLists(); 923 | 924 | $subscribed_lists = []; 925 | 926 | // Check each list for a subscriber matching the email address. 927 | if ($list_data->total_items > 0) { 928 | foreach ($list_data->lists as $list) { 929 | try { 930 | $member_data = $this->getMemberInfo($list->id, $email); 931 | 932 | if (isset($member_data->id)) { 933 | $subscribed_lists[] = $list; 934 | } 935 | } 936 | catch (MailchimpAPIException $e) { 937 | if ($e->getCode() !== 404) { 938 | // 404 indicates the email address is not subscribed to this list 939 | // and can be safely ignored. Surface all other exceptions. 940 | throw new MailchimpAPIException($e->getMessage(), $e->getCode(), $e); 941 | } 942 | } 943 | } 944 | } 945 | 946 | return $subscribed_lists; 947 | } 948 | 949 | } 950 | -------------------------------------------------------------------------------- /src/MailchimpReports.php: -------------------------------------------------------------------------------- 1 | api_class->request('GET', '/reports', NULL, $parameters); 24 | } 25 | 26 | /** 27 | * Gets a report summary for a specific campaign. 28 | * 29 | * @param string $campaign_id 30 | * The ID of the campaign. 31 | * @param array $parameters 32 | * Associative array of optional request parameters. 33 | * 34 | * @return object 35 | * 36 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/#read-get_reports_campaign_id 37 | */ 38 | public function getCampaignSummary($campaign_id, $parameters = []) { 39 | $tokens = [ 40 | 'campaign_id' => $campaign_id, 41 | ]; 42 | 43 | return $this->api_class->request('GET', '/reports/{campaign_id}', $tokens, $parameters); 44 | } 45 | 46 | /** 47 | * Gets a specific report for a specific campaign. 48 | * 49 | * @param string $campaign_id 50 | * The ID of the campaign. 51 | * @param string $type 52 | * The type of report to generate path url. 53 | * - abuse-reports @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/abuse-reports 54 | * - advice @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/advice 55 | * - click-details @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/click-details 56 | * - domain-performance @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/domain-performance 57 | * - eepurl @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/eepurl 58 | * - email-activity @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/email-activity 59 | * - locations @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/locations 60 | * - sent-to @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/sent-to 61 | * - sub-reports @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/sub-reports 62 | * - unsubscribed @see http://developer.mailchimp.com/documentation/mailchimp/reference/reports/unsubscribed 63 | * @param array $parameters 64 | * Associative array of optional request parameters. 65 | * 66 | * @return object 67 | */ 68 | public function getCampaignReport($campaign_id, $type, $parameters = []) { 69 | $tokens = [ 70 | 'campaign_id' => $campaign_id, 71 | 'type' => $type, 72 | ]; 73 | 74 | return $this->api_class->request('GET', '/reports/{campaign_id}/{type}', $tokens, $parameters); 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/MailchimpTemplates.php: -------------------------------------------------------------------------------- 1 | api_class->request('GET', '/templates', NULL, $parameters); 24 | } 25 | 26 | /** 27 | * Gets information a specific template. 28 | * 29 | * @param string $template_id 30 | * The ID of the template. 31 | * @param array $parameters 32 | * Associative array of optional request parameters. 33 | * 34 | * @return object 35 | * 36 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/templates/#read-get_templates_template_id 37 | */ 38 | public function getTemplate($template_id, $parameters = []) { 39 | $tokens = [ 40 | 'template_id' => $template_id, 41 | ]; 42 | 43 | return $this->api_class->request('GET', '/templates/{template_id}', $tokens, $parameters); 44 | } 45 | 46 | /** 47 | * Gets the default content of a specific template. 48 | * 49 | * @param string $template_id 50 | * The ID of the template. 51 | * @param array $parameters 52 | * Associative array of optional request parameters. 53 | * 54 | * @return object 55 | * 56 | * @see http://developer.mailchimp.com/documentation/mailchimp/reference/templates/default-content/#read-get_templates_template_id_default_content 57 | */ 58 | public function getTemplateContent($template_id, $parameters = []) { 59 | $tokens = [ 60 | 'template_id' => $template_id, 61 | ]; 62 | 63 | return $this->api_class->request('GET', '/templates/{template_id}/default-content', $tokens, $parameters); 64 | } 65 | 66 | } 67 | -------------------------------------------------------------------------------- /src/http/MailchimpCurlHttpClient.php: -------------------------------------------------------------------------------- 1 | config = $config; 23 | } 24 | 25 | /** 26 | * @inheritdoc 27 | */ 28 | public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE) { 29 | $ch = curl_init(); 30 | 31 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->config['timeout']); 32 | 33 | // Set request headers. 34 | $headers = []; 35 | foreach ($options['headers'] as $header_name => $header_value) { 36 | $headers[] = $header_name . ': ' . $header_value; 37 | } 38 | 39 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 40 | 41 | // Set request content. 42 | switch ($method) { 43 | case 'POST': 44 | curl_setopt($ch, CURLOPT_POST, TRUE); 45 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object) $parameters)); 46 | break; 47 | 48 | case 'GET': 49 | $uri .= '?' . http_build_query($parameters); 50 | break; 51 | 52 | case 'PUT': 53 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); 54 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object) $parameters)); 55 | break; 56 | 57 | case 'PATCH': 58 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); 59 | curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode((object) $parameters)); 60 | break; 61 | 62 | case 'DELETE': 63 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); 64 | break; 65 | 66 | default: 67 | // Throw exception for unsupported request method. 68 | throw new \Exception('Unsupported HTTP request method: ' . $method); 69 | } 70 | 71 | // Set proxy to use. 72 | if (isset($this->config['proxy'])) { 73 | if (!is_array($this->config['proxy'])) { 74 | curl_setopt($ch, CURLOPT_PROXY, $this->config['proxy']); 75 | } 76 | else { 77 | $scheme = isset($_SERVER["HTTPS"]) ? 'https' : 'http'; 78 | if (isset($this->config['proxy'][$scheme])) { 79 | curl_setopt($ch, CURLOPT_PROXY, $this->config['proxy'][$scheme]); 80 | } 81 | } 82 | } 83 | 84 | curl_setopt($ch, CURLOPT_URL, $uri); 85 | 86 | // Get response as a string. 87 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 88 | $response = curl_exec($ch); 89 | 90 | $http_code = 0; 91 | $error = NULL; 92 | 93 | // Check for cURL error before checking HTTP response code. 94 | if (curl_errno($ch)) { 95 | $error = curl_error($ch); 96 | } 97 | else { 98 | // Check the HTTP response code. 99 | $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 100 | if ($http_code != 200) { 101 | $response_data = json_decode($response); 102 | $error = $response_data->detail; 103 | } 104 | } 105 | 106 | // Close cURL connection. 107 | curl_close($ch); 108 | 109 | if (!empty($error)) { 110 | throw new \Exception($error, $http_code); 111 | } 112 | 113 | return json_decode($response, $returnAssoc); 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /src/http/MailchimpGuzzleHttpClient.php: -------------------------------------------------------------------------------- 1 | guzzle = new Client($config); 32 | } 33 | 34 | /** 35 | * @inheritdoc 36 | */ 37 | public function handleRequest($method, $uri = '', $options = [], $parameters = [], $returnAssoc = FALSE) { 38 | if (!empty($parameters)) { 39 | if ($method == 'GET') { 40 | // Send parameters as query string parameters. 41 | $options['query'] = $parameters; 42 | } 43 | else { 44 | // Send parameters as JSON in request body. 45 | $options['json'] = (object) $parameters; 46 | } 47 | } 48 | 49 | try { 50 | $response = $this->guzzle->request($method, $uri, $options); 51 | $data = json_decode($response->getBody(), $returnAssoc); 52 | 53 | return $data; 54 | } 55 | catch (RequestException $e) { 56 | $response = $e->getResponse(); 57 | if (!empty($response)) { 58 | $message = $e->getResponse()->getBody(); 59 | } 60 | else { 61 | $message = $e->getMessage(); 62 | } 63 | 64 | throw new MailchimpAPIException($message, $e->getCode(), $e); 65 | } 66 | } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /src/http/MailchimpHttpClientInterface.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 14 | $mc = new MailchimpAutomations($api_user); 15 | $mc->getAutomations(); 16 | 17 | $this->assertEquals('GET', $mc->getClient()->method); 18 | $this->assertEquals($mc->getEndpoint() . '/automations', $mc->getClient()->uri); 19 | } 20 | 21 | /** 22 | * Tests library functionality for automation workflows. 23 | */ 24 | public function testGetWorkflow() { 25 | $workflow_id = '57afe96172'; 26 | 27 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 28 | $mc = new MailchimpAutomations($api_user); 29 | $mc->getWorkflow($workflow_id); 30 | 31 | $this->assertEquals('GET', $mc->getClient()->method); 32 | $this->assertEquals($mc->getEndpoint() . '/automations/' . $workflow_id, $mc->getClient()->uri); 33 | } 34 | 35 | /** 36 | * Tests library functionality for workflow automation emails. 37 | */ 38 | public function testGetWorkflowEmails() { 39 | $workflow_id = '57afe96172'; 40 | 41 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 42 | $mc = new MailchimpAutomations($api_user); 43 | $mc->getWorkflowEmails($workflow_id); 44 | 45 | $this->assertEquals('GET', $mc->getClient()->method); 46 | $this->assertEquals($mc->getEndpoint() . '/automations/' . $workflow_id . '/emails', $mc->getClient()->uri); 47 | } 48 | 49 | /** 50 | * Tests library functionality for a workflow automation email. 51 | */ 52 | public function testGetWorkflowEmail() { 53 | $workflow_id = '57afe96172'; 54 | $workflow_email_id = 'a87de7d1e5'; 55 | 56 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 57 | $mc = new MailchimpAutomations($api_user); 58 | $mc->getWorkflowEmail($workflow_id, $workflow_email_id); 59 | 60 | $this->assertEquals('GET', $mc->getClient()->method); 61 | $this->assertEquals($mc->getEndpoint() . '/automations/' . $workflow_id . '/emails/' . $workflow_email_id, $mc->getClient()->uri); 62 | } 63 | 64 | /** 65 | * Tests library functionality for workflow automation email queues. 66 | */ 67 | public function testGetWorkflowEmailSubscribers() { 68 | $workflow_id = '57afe96172'; 69 | $workflow_email_id = 'a87de7d1e5'; 70 | 71 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 72 | $mc = new MailchimpAutomations($api_user); 73 | $mc->getWorkflowEmailSubscribers($workflow_id, $workflow_email_id); 74 | 75 | $this->assertEquals('GET', $mc->getClient()->method); 76 | $this->assertEquals($mc->getEndpoint() . '/automations/' . $workflow_id . '/emails/' . $workflow_email_id . '/queue', $mc->getClient()->uri); 77 | } 78 | 79 | /** 80 | * Tests library functionality for a user in a workflow automation queue. 81 | */ 82 | public function testGetWorkflowEmailSubscriber() { 83 | $workflow_id = '57afe96172'; 84 | $workflow_email_id = 'a87de7d1e5'; 85 | $email = 'test@example.com'; 86 | 87 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 88 | $mc = new MailchimpAutomations($api_user); 89 | $mc->getWorkflowEmailSubscriber($workflow_id, $workflow_email_id, $email); 90 | 91 | $this->assertEquals('GET', $mc->getClient()->method); 92 | $this->assertEquals($mc->getEndpoint() . '/automations/' . $workflow_id . '/emails/' . $workflow_email_id . '/queue/' . md5($email), $mc->getClient()->uri); 93 | } 94 | 95 | /** 96 | * Tests library functionality for a user in a workflow automation queue. 97 | */ 98 | public function testAddWorkflowEmailSubscriber() { 99 | $workflow_id = '57afe96172'; 100 | $workflow_email_id = 'a87de7d1e5'; 101 | $email = 'test@example.com'; 102 | 103 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 104 | $mc = new MailchimpAutomations($api_user); 105 | $mc->addWorkflowEmailSubscriber($workflow_id, $workflow_email_id, $email); 106 | 107 | $this->assertEquals('POST', $mc->getClient()->method); 108 | $this->assertEquals($mc->getEndpoint() . '/automations/' . $workflow_id . '/emails/' . $workflow_email_id . '/queue', $mc->getClient()->uri); 109 | 110 | $this->assertNotEmpty($mc->getClient()->options['json']); 111 | 112 | $request_body = $mc->getClient()->options['json']; 113 | 114 | $this->assertEquals($email, $request_body->email_address); 115 | } 116 | 117 | } 118 | -------------------------------------------------------------------------------- /tests/MailchimpCampaignsTest.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 19 | $mc = new MailchimpCampaigns($api_user); 20 | $mc->getCampaigns(); 21 | 22 | $this->assertEquals('GET', $mc->getClient()->method); 23 | $this->assertEquals($mc->getEndpoint() . '/campaigns', $mc->getClient()->uri); 24 | } 25 | 26 | /** 27 | * Tests library functionality for campaign information. 28 | */ 29 | public function testGetCampaign() { 30 | $campaign_id = '42694e9e57'; 31 | 32 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 33 | $mc = new MailchimpCampaigns($api_user); 34 | $mc->getCampaign($campaign_id); 35 | 36 | $this->assertEquals('GET', $mc->getClient()->method); 37 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id, $mc->getClient()->uri); 38 | } 39 | 40 | /** 41 | * Tests library functionality for adding a new campaign. 42 | */ 43 | public function testAddCampaign() { 44 | $type = 'regular'; 45 | $recipients = (object) [ 46 | 'list_id' => '3c307a9f3f', 47 | ]; 48 | $settings = (object) [ 49 | 'subject_line' => 'Your Purchase Receipt', 50 | 'from_name' => 'Customer Service', 51 | ]; 52 | 53 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 54 | $mc = new MailchimpCampaigns($api_user); 55 | $mc->addCampaign($type, $recipients, $settings); 56 | 57 | $this->assertEquals('POST', $mc->getClient()->method); 58 | $this->assertEquals($mc->getEndpoint() . '/campaigns', $mc->getClient()->uri); 59 | 60 | $this->assertNotEmpty($mc->getClient()->options['json']); 61 | 62 | $request_body = $mc->getClient()->options['json']; 63 | 64 | $this->assertEquals($type, $request_body->type); 65 | 66 | $this->assertEquals($recipients->list_id, $request_body->recipients->list_id); 67 | $this->assertEquals($settings->subject_line, $request_body->settings->subject_line); 68 | $this->assertEquals($settings->from_name, $request_body->settings->from_name); 69 | } 70 | 71 | /** 72 | * Tests library functionality for getting campaign content. 73 | */ 74 | public function testGetCampaignContent() { 75 | $campaign_id = '42694e9e57'; 76 | 77 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 78 | $mc = new MailchimpCampaigns($api_user); 79 | $mc->getCampaignContent($campaign_id); 80 | 81 | $this->assertEquals('GET', $mc->getClient()->method); 82 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/content', $mc->getClient()->uri); 83 | } 84 | 85 | /** 86 | * Tests library functionality for setting campaign content. 87 | */ 88 | public function testSetCampaignContent() { 89 | $campaign_id = '42694e9e57'; 90 | $parameters = [ 91 | 'html' => '

The HTML to use for the saved campaign.

', 92 | ]; 93 | 94 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 95 | $mc = new MailchimpCampaigns($api_user); 96 | $mc->setCampaignContent($campaign_id, $parameters); 97 | 98 | $this->assertEquals('PUT', $mc->getClient()->method); 99 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/content', $mc->getClient()->uri); 100 | 101 | $this->assertNotEmpty($mc->getClient()->options['json']); 102 | 103 | $request_body = $mc->getClient()->options['json']; 104 | 105 | $this->assertEquals($parameters['html'], $request_body->html); 106 | } 107 | 108 | /** 109 | * Tests library functionality for getting a campaign send checklist. 110 | */ 111 | public function testGetSendChecklist() { 112 | $campaign_id = '42694e9e57'; 113 | 114 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 115 | $mc = new MailchimpCampaigns($api_user); 116 | $mc->getSendChecklist($campaign_id); 117 | 118 | $this->assertEquals('GET', $mc->getClient()->method); 119 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/send-checklist', $mc->getClient()->uri); 120 | } 121 | 122 | /** 123 | * Tests library functionality for updating a campaign. 124 | */ 125 | public function testUpdateCampaign() { 126 | $campaign_id = '3e06f4ec92'; 127 | $type = 'regular'; 128 | $recipients = (object) [ 129 | 'list_id' => '3c307a9f3f', 130 | ]; 131 | $settings = (object) [ 132 | 'subject_line' => 'This is an updated subject line', 133 | 'from_name' => 'Customer Service', 134 | ]; 135 | 136 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 137 | $mc = new MailchimpCampaigns($api_user); 138 | $mc->updateCampaign($campaign_id, $type, $recipients, $settings); 139 | 140 | $this->assertEquals('PATCH', $mc->getClient()->method); 141 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id, $mc->getClient()->uri); 142 | 143 | $this->assertNotEmpty($mc->getClient()->options['json']); 144 | 145 | $request_body = $mc->getClient()->options['json']; 146 | 147 | $this->assertEquals($type, $request_body->type); 148 | 149 | $this->assertEquals($recipients->list_id, $request_body->recipients->list_id); 150 | $this->assertEquals($settings->subject_line, $request_body->settings->subject_line); 151 | $this->assertEquals($settings->from_name, $request_body->settings->from_name); 152 | } 153 | 154 | /** 155 | * Tests library functionality for sending a test campaign. 156 | */ 157 | public function testSendTest() { 158 | $campaign_id = 'b03bfc273a'; 159 | $emails = [ 160 | 'test@example.com', 161 | ]; 162 | $send_type = 'html'; 163 | 164 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 165 | $mc = new MailchimpCampaigns($api_user); 166 | $mc->sendTest($campaign_id, $emails, $send_type); 167 | 168 | $this->assertEquals('POST', $mc->getClient()->method); 169 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/actions/test', $mc->getClient()->uri); 170 | } 171 | 172 | /** 173 | * Tests library functionality for scheduling a campaign. 174 | */ 175 | public function testSchedule() { 176 | $campaign_id = 'b03bfc273a'; 177 | $schedule_time = '2017-02-04T19:13:00+00:00'; 178 | $timewarp = FALSE; 179 | $batch_delivery = (object) [ 180 | 'batch_delay' => 5, 181 | 'batch_count' => 100, 182 | ]; 183 | 184 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 185 | $mc = new MailchimpCampaigns($api_user); 186 | $mc->schedule($campaign_id, $schedule_time, $timewarp, $batch_delivery); 187 | 188 | $this->assertEquals('POST', $mc->getClient()->method); 189 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/actions/schedule', $mc->getClient()->uri); 190 | 191 | $request_body = $mc->getClient()->options['json']; 192 | 193 | $this->assertEquals($schedule_time, $request_body->schedule_time); 194 | $this->assertEquals($timewarp, $request_body->timewarp); 195 | $this->assertEquals($batch_delivery->batch_delay, $request_body->batch_delivery->batch_delay); 196 | $this->assertEquals($batch_delivery->batch_count, $request_body->batch_delivery->batch_count); 197 | } 198 | 199 | /** 200 | * Tests library functionality for unscheduling a campaign. 201 | */ 202 | public function testUnschedule() { 203 | $campaign_id = 'b03bfc273a'; 204 | 205 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 206 | $mc = new MailchimpCampaigns($api_user); 207 | $mc->unschedule($campaign_id); 208 | 209 | $this->assertEquals('POST', $mc->getClient()->method); 210 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/actions/unschedule', $mc->getClient()->uri); 211 | } 212 | 213 | /** 214 | * Tests library functionality for sending a campaign. 215 | */ 216 | public function testSend() { 217 | $campaign_id = 'b03bfc273a'; 218 | 219 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 220 | $mc = new MailchimpCampaigns($api_user); 221 | $mc->send($campaign_id); 222 | 223 | $this->assertEquals('POST', $mc->getClient()->method); 224 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id . '/actions/send', $mc->getClient()->uri); 225 | } 226 | 227 | /** 228 | * Tests library functionality for campaigns information. 229 | */ 230 | public function testDelete() { 231 | $campaign_id = '42694e9e57'; 232 | 233 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 234 | $mc = new MailchimpCampaigns($api_user); 235 | $mc->delete($campaign_id); 236 | 237 | $this->assertEquals('DELETE', $mc->getClient()->method); 238 | $this->assertEquals($mc->getEndpoint() . '/campaigns/' . $campaign_id, $mc->getClient()->uri); 239 | } 240 | 241 | } 242 | -------------------------------------------------------------------------------- /tests/MailchimpConnectedSitesTest.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 19 | $mc = new MailchimpEcommerce($api_user); 20 | $mc->getStores(); 21 | 22 | $this->assertEquals('GET', $mc->getClient()->method); 23 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores', $mc->getClient()->uri); 24 | } 25 | 26 | /** 27 | * Tests library functionality for store information. 28 | */ 29 | public function testGetStore() { 30 | $store_id = 'MC002'; 31 | 32 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 33 | $mc = new MailchimpEcommerce($api_user); 34 | $mc->getStore($store_id); 35 | 36 | $this->assertEquals('GET', $mc->getClient()->method); 37 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id, $mc->getClient()->uri); 38 | } 39 | 40 | /** 41 | * Tests library function for adding a new store. 42 | */ 43 | public function testAddStore() { 44 | $id = 'MC001'; 45 | $store = [ 46 | 'list_id' => '205d96e6b4', 47 | 'name' => "Freddie's Merchandise", 48 | 'currency_code' => 'USD', 49 | ]; 50 | 51 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 52 | $mc = new MailchimpEcommerce($api_user); 53 | $mc->addStore($id, $store); 54 | 55 | $this->assertEquals('POST', $mc->getClient()->method); 56 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores', $mc->getClient()->uri); 57 | 58 | $this->assertNotEmpty($mc->getClient()->options['json']); 59 | 60 | $request_body = $mc->getClient()->options['json']; 61 | 62 | $this->assertEquals($id, $request_body->id); 63 | $this->assertEquals($store['list_id'], $request_body->list_id); 64 | $this->assertEquals($store['name'], $request_body->name); 65 | $this->assertEquals($store['currency_code'], $request_body->currency_code); 66 | } 67 | 68 | /** 69 | * Tests library functionality for updating a store. 70 | */ 71 | public function testUpdateStore() { 72 | $store_id = 'MC001'; 73 | $name = "Freddie's Merchandise"; 74 | $currency_code = 'USD'; 75 | 76 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 77 | $mc = new MailchimpEcommerce($api_user); 78 | $mc->updateStore($store_id, $name, $currency_code); 79 | 80 | $this->assertEquals('PATCH', $mc->getClient()->method); 81 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id, $mc->getClient()->uri); 82 | 83 | $this->assertNotEmpty($mc->getClient()->options['json']); 84 | 85 | $request_body = $mc->getClient()->options['json']; 86 | 87 | $this->assertEquals($name, $request_body->name); 88 | $this->assertEquals($currency_code, $request_body->currency_code); 89 | } 90 | 91 | /** 92 | * Tests library functionality for deleting stores. 93 | */ 94 | public function testDeleteStore() { 95 | $store_id = 'MC002'; 96 | 97 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 98 | $mc = new MailchimpEcommerce($api_user); 99 | $mc->deleteStore($store_id); 100 | 101 | $this->assertEquals('DELETE', $mc->getClient()->method); 102 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id, $mc->getClient()->uri); 103 | } 104 | 105 | /** 106 | * Tests library functionality for getting information on a store's carts. 107 | */ 108 | public function testGetCarts() { 109 | $store_id = 'MC001'; 110 | 111 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 112 | $mc = new MailchimpEcommerce($api_user); 113 | $mc->getCarts($store_id); 114 | 115 | $this->assertEquals('GET', $mc->getClient()->method); 116 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts', $mc->getClient()->uri); 117 | } 118 | 119 | /** 120 | * Tests library functionality for getting information on a specific cart. 121 | */ 122 | public function testGetCart() { 123 | $store_id = 'MC001'; 124 | $cart_id = 'cart0001'; 125 | 126 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 127 | $mc = new MailchimpEcommerce($api_user); 128 | $mc->getCart($store_id, $cart_id); 129 | 130 | $this->assertEquals('GET', $mc->getClient()->method); 131 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id, $mc->getClient()->uri); 132 | } 133 | 134 | /** 135 | * Tests library function for adding a new cart. 136 | */ 137 | public function testAddCart() { 138 | $store_id = 'MC001'; 139 | $id = 'cart0001'; 140 | $customer = [ 141 | 'id' => 'cust0005', 142 | 'email_address' => 'freddy@freddiesjokes.com', 143 | 'opt_in_status' => TRUE, 144 | ]; 145 | $cart = [ 146 | 'currency_code' => 'USD', 147 | 'order_total' => 12.45, 148 | 'lines' => [ 149 | 'id' => 'LINE001', 150 | 'product_id' => 'PROD001', 151 | 'product_title' => "Freddie's Jokes", 152 | 'product_variant_id' => 'PROD001A', 153 | 'product_variant_title' => "Freddie's Jokes Volume 1", 154 | 'quantity' => 2, 155 | 'price' => 10, 156 | ], 157 | ]; 158 | 159 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 160 | $mc = new MailchimpEcommerce($api_user); 161 | $mc->addCart($store_id, $id, $customer, $cart); 162 | 163 | $this->assertEquals('POST', $mc->getClient()->method); 164 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts', $mc->getClient()->uri); 165 | 166 | $this->assertNotEmpty($mc->getClient()->options['json']); 167 | 168 | $request_body = $mc->getClient()->options['json']; 169 | 170 | $this->assertEquals($id, $request_body->id); 171 | $this->assertEquals($customer['id'], $request_body->customer->id); 172 | $this->assertEquals($customer['email_address'], $request_body->customer->email_address); 173 | $this->assertEquals($customer['opt_in_status'], $request_body->customer->opt_in_status); 174 | $this->assertEquals($cart['currency_code'], $request_body->currency_code); 175 | $this->assertEquals($cart['order_total'], $request_body->order_total); 176 | $this->assertEquals($cart['lines'], $request_body->lines); 177 | $this->assertEquals($cart['lines']['id'], $request_body->lines['id']); 178 | $this->assertEquals($cart['lines']['product_id'], $request_body->lines['product_id']); 179 | $this->assertEquals($cart['lines']['product_title'], $request_body->lines['product_title']); 180 | $this->assertEquals($cart['lines']['product_variant_id'], $request_body->lines['product_variant_id']); 181 | $this->assertEquals($cart['lines']['product_variant_title'], $request_body->lines['product_variant_title']); 182 | $this->assertEquals($cart['lines']['quantity'], $request_body->lines['quantity']); 183 | $this->assertEquals($cart['lines']['price'], $request_body->lines['price']); 184 | } 185 | 186 | /** 187 | * Tests library function for updating an existing cart. 188 | */ 189 | public function testUpdateCart() { 190 | $store_id = 'MC001'; 191 | $cart_id = 'cart0001'; 192 | 193 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 194 | $mc = new MailchimpEcommerce($api_user); 195 | $mc->updateCart($store_id, $cart_id); 196 | 197 | $this->assertEquals('PATCH', $mc->getClient()->method); 198 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id, $mc->getClient()->uri); 199 | } 200 | 201 | /** 202 | * Tests library function for deleting a cart. 203 | */ 204 | public function testDeleteCart() { 205 | $store_id = 'MC001'; 206 | $cart_id = 'cart0001'; 207 | 208 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 209 | $mc = new MailchimpEcommerce($api_user); 210 | $mc->deleteCart($store_id, $cart_id); 211 | 212 | $this->assertEquals('DELETE', $mc->getClient()->method); 213 | $this->assertEquals($mc->getEndPoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id, $mc->getClient()->uri); 214 | } 215 | 216 | /** 217 | * Tests library function for getting cart lines. 218 | */ 219 | public function testGetCartLines() { 220 | $store_id = 'MC001'; 221 | $cart_id = 'cart0001'; 222 | 223 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 224 | $mc = new MailchimpEcommerce($api_user); 225 | $mc->getCartLines($store_id, $cart_id); 226 | 227 | $this->assertEquals('GET', $mc->getClient()->method); 228 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id . '/lines', $mc->getClient()->uri); 229 | } 230 | 231 | /** 232 | * Tests library function for getting a specific cart's line item. 233 | */ 234 | public function testGetCartLine() { 235 | $store_id = 'MC001'; 236 | $cart_id = 'cart0001'; 237 | $line_id = 'line002'; 238 | 239 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 240 | $mc = new MailchimpEcommerce($api_user); 241 | $mc->getCartLine($store_id, $cart_id, $line_id); 242 | 243 | $this->assertEquals('GET', $mc->getClient()->method); 244 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id . '/lines/' . $line_id, $mc->getClient()->uri); 245 | } 246 | 247 | /** 248 | * Tests library function for adding a line item to a cart. 249 | */ 250 | public function testAddCartLine() { 251 | $store_id = 'MC001'; 252 | $cart_id = 'cart0001'; 253 | $id = 'L001'; 254 | $product = [ 255 | 'product_id' => 'PROD001', 256 | 'product_variant_id' => "Freddie's Jokes", 257 | 'quantity' => 1, 258 | 'price' => 5, 259 | ]; 260 | 261 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 262 | $mc = new MailchimpEcommerce($api_user); 263 | $mc->addCartLine($store_id, $cart_id, $id, $product); 264 | 265 | $this->assertEquals('POST', $mc->getClient()->method); 266 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id . '/lines', $mc->getClient()->uri); 267 | 268 | $this->assertNotEmpty($mc->getClient()->options['json']); 269 | 270 | $request_body = $mc->getClient()->options['json']; 271 | 272 | $this->assertEquals($id, $request_body->id); 273 | $this->assertEquals($product['product_id'], $request_body->product_id); 274 | $this->assertEquals($product['product_variant_id'], $request_body->product_variant_id); 275 | $this->assertEquals($product['quantity'], $request_body->quantity); 276 | $this->assertEquals($product['price'], $request_body->price); 277 | } 278 | 279 | /** 280 | * Tests library function for updating a cart line item. 281 | */ 282 | public function testUpdateCartLine() { 283 | $store_id = 'MC001'; 284 | $cart_id = 'cart0001'; 285 | $line_id = 'L001'; 286 | 287 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 288 | $mc = new MailchimpEcommerce($api_user); 289 | $mc->updateCartLine($store_id, $cart_id, $line_id); 290 | 291 | $this->assertEquals('PATCH', $mc->getClient()->method); 292 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id . '/lines/' . $line_id, $mc->getClient()->uri); 293 | } 294 | 295 | /** 296 | * Tests library function for deleting a cart line item. 297 | */ 298 | public function testDeleteCartLine() { 299 | $store_id = 'MC001'; 300 | $cart_id = 'cart0001'; 301 | $line_id = 'L001'; 302 | 303 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 304 | $mc = new MailchimpEcommerce($api_user); 305 | $mc->deleteCartLine($store_id, $cart_id, $line_id); 306 | 307 | $this->assertEquals('DELETE', $mc->getClient()->method); 308 | $this->assertEquals($mc->getEndPoint() . '/ecommerce/stores/' . $store_id . '/carts/' . $cart_id . '/lines/' . $line_id, $mc->getClient()->uri); 309 | } 310 | 311 | /** 312 | * Tests library function for getting customers. 313 | */ 314 | public function testGetCustomers() { 315 | $store_id = 'MC001'; 316 | 317 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 318 | $mc = new MailchimpEcommerce($api_user); 319 | $mc->getCustomers($store_id); 320 | 321 | $this->assertEquals('GET', $mc->getClient()->method); 322 | $this->assertEquals($mc->getEndPoint() . '/ecommerce/stores/' . $store_id . '/customers', $mc->getClient()->uri); 323 | } 324 | 325 | /** 326 | * Tests library function for getting a customer. 327 | */ 328 | public function testGetCustomer() { 329 | $store_id = 'MC001'; 330 | $customer_id = 'cust0001'; 331 | 332 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 333 | $mc = new MailchimpEcommerce($api_user); 334 | $mc->getCustomer($store_id, $customer_id); 335 | 336 | $this->assertEquals('GET', $mc->getClient()->method); 337 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/customers/' . $customer_id, $mc->getClient()->uri); 338 | } 339 | 340 | /** 341 | * Tests library function for adding a customer. 342 | */ 343 | public function testAddCustomer() { 344 | $store_id = 'MC001'; 345 | $customer = [ 346 | 'id' => 'cust0001', 347 | 'email_address' => 'freddie@freddiesjokes.com', 348 | 'opt_in_status' => TRUE, 349 | ]; 350 | 351 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 352 | $mc = new MailchimpEcommerce($api_user); 353 | $mc->addCustomer($store_id, $customer); 354 | 355 | $this->assertEquals('POST', $mc->getClient()->method); 356 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/customers', $mc->getClient()->uri); 357 | 358 | $this->assertNotEmpty($mc->getClient()->options['json']); 359 | 360 | $request_body = $mc->getClient()->options['json']; 361 | 362 | $this->assertEquals($customer['id'], $request_body->id); 363 | $this->assertEquals($customer['email_address'], $request_body->email_address); 364 | $this->assertEquals($customer['opt_in_status'], $request_body->opt_in_status); 365 | } 366 | 367 | /** 368 | * Tests library function for updating a customer. 369 | */ 370 | public function testUpdateCustomer() { 371 | $store_id = 'MC001'; 372 | $customer = [ 373 | 'id' => 'cust0001', 374 | 'email_address' => 'freddie@freddiesjokes.com', 375 | 'opt_in_status' => TRUE, 376 | ]; 377 | 378 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 379 | $mc = new MailchimpEcommerce($api_user); 380 | $mc->updateCustomer($store_id, $customer); 381 | 382 | $this->assertEquals('PATCH', $mc->getClient()->method); 383 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/customers/' . $customer['id'], $mc->getClient()->uri); 384 | 385 | $this->assertNotEmpty($mc->getClient()->options['json']); 386 | 387 | $request_body = $mc->getClient()->options['json']; 388 | 389 | $this->assertEquals($customer['id'], $request_body->id); 390 | $this->assertEquals($customer['email_address'], $request_body->email_address); 391 | $this->assertEquals($customer['opt_in_status'], $request_body->opt_in_status); 392 | } 393 | 394 | /** 395 | * Tests library function for deleting a customer. 396 | */ 397 | public function testDeleteCustomer() { 398 | $store_id = 'MC001'; 399 | $customer_id = 'cust0003'; 400 | 401 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 402 | $mc = new MailchimpEcommerce($api_user); 403 | $mc->deleteCustomer($store_id, $customer_id); 404 | 405 | $this->assertEquals('DELETE', $mc->getClient()->method); 406 | $this->assertEquals($mc->getEndPoint() . '/ecommerce/stores/' . $store_id . '/customers/' . $customer_id, $mc->getClient()->uri); 407 | } 408 | 409 | /** 410 | * Tests library function for getting orders. 411 | */ 412 | public function testGetOrders() { 413 | $store_id = 'MC001'; 414 | 415 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 416 | $mc = new MailchimpEcommerce($api_user); 417 | $mc->getOrders($store_id); 418 | 419 | $this->assertEquals('GET', $mc->getClient()->method); 420 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/orders', $mc->getClient()->uri); 421 | } 422 | 423 | /** 424 | * Tests library function for getting an order. 425 | */ 426 | public function testGetOrder() { 427 | $store_id = 'MC001'; 428 | $order_id = 'ord0001'; 429 | 430 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 431 | $mc = new MailchimpEcommerce($api_user); 432 | $mc->getOrder($store_id, $order_id); 433 | 434 | $this->assertEquals('GET', $mc->getClient()->method); 435 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/orders/' . $order_id, $mc->getClient()->uri); 436 | } 437 | 438 | /** 439 | * Tests library function for adding an order. 440 | */ 441 | public function testAddOrder() { 442 | $store_id = 'MC001'; 443 | $order_id = 'ord0001'; 444 | $customer = [ 445 | 'id' => 'cust0005', 446 | 'email_address' => 'freddy@freddiesjokes.com', 447 | 'opt_in_status' => TRUE, 448 | ]; 449 | $order = [ 450 | 'currency_code' => 'USD', 451 | 'order_total' => 12.45, 452 | 'lines' => [ 453 | 'id' => 'LINE001', 454 | 'product_id' => 'PROD001', 455 | 'product_title' => "Freddie's Jokes", 456 | 'product_variant_id' => 'PROD001A', 457 | 'product_variant_title' => "Freddie's Jokes Volume 1", 458 | 'quantity' => 2, 459 | 'price' => 10, 460 | ], 461 | ]; 462 | 463 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 464 | $mc = new MailchimpEcommerce($api_user); 465 | $mc->addOrder($store_id, $order_id, $customer, $order); 466 | 467 | $this->assertEquals('POST', $mc->getClient()->method); 468 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/orders', $mc->getClient()->uri); 469 | 470 | $this->assertNotEmpty($mc->getClient()->options['json']); 471 | 472 | $request_body = $mc->getClient()->options['json']; 473 | 474 | $this->assertEquals($order_id, $request_body->id); 475 | $this->assertEquals($customer['id'], $request_body->customer->id); 476 | $this->assertEquals($customer['email_address'], $request_body->customer->email_address); 477 | $this->assertEquals($customer['opt_in_status'], $request_body->customer->opt_in_status); 478 | $this->assertEquals($order['currency_code'], $request_body->currency_code); 479 | $this->assertEquals($order['order_total'], $request_body->order_total); 480 | $this->assertEquals($order['lines'], $request_body->lines); 481 | $this->assertEquals($order['lines']['id'], $request_body->lines['id']); 482 | $this->assertEquals($order['lines']['product_id'], $request_body->lines['product_id']); 483 | $this->assertEquals($order['lines']['product_title'], $request_body->lines['product_title']); 484 | $this->assertEquals($order['lines']['product_variant_id'], $request_body->lines['product_variant_id']); 485 | $this->assertEquals($order['lines']['product_variant_title'], $request_body->lines['product_variant_title']); 486 | $this->assertEquals($order['lines']['quantity'], $request_body->lines['quantity']); 487 | $this->assertEquals($order['lines']['price'], $request_body->lines['price']); 488 | } 489 | 490 | /** 491 | * Tests library function for updating an order. 492 | */ 493 | public function testUpdateOrder() { 494 | $store_id = 'MC001'; 495 | $order_id = 'ord0001'; 496 | 497 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 498 | $mc = new MailchimpEcommerce($api_user); 499 | $mc->updateOrder($store_id, $order_id); 500 | 501 | $this->assertEquals('PATCH', $mc->getClient()->method); 502 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/orders/' . $order_id, $mc->getClient()->uri); 503 | } 504 | 505 | /** 506 | * Tests library functionality for deleting an order. 507 | */ 508 | public function testDeleteOrder() { 509 | $store_id = 'MC002'; 510 | $order_id = 'ord0001'; 511 | 512 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 513 | $mc = new MailchimpEcommerce($api_user); 514 | $mc->deleteOrder($store_id, $order_id); 515 | 516 | $this->assertEquals('DELETE', $mc->getClient()->method); 517 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/orders/' . $order_id, $mc->getClient()->uri); 518 | } 519 | 520 | /** 521 | * Tests library function for getting order lines. 522 | */ 523 | public function testGetOrderLines() { 524 | $store_id = 'MC001'; 525 | $order_id = 'ord0001'; 526 | 527 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 528 | $mc = new MailchimpEcommerce($api_user); 529 | $mc->getOrderLines($store_id, $order_id); 530 | 531 | $this->assertEquals('GET', $mc->getClient()->method); 532 | $this->assertEquals($mc->getEndPoint() . '/ecommerce/stores/' . $store_id . '/orders/' . $order_id . '/lines', $mc->getClient()->uri); 533 | } 534 | 535 | /** 536 | * Tests library function for getting order lines. 537 | */ 538 | public function testGetOrderLine() { 539 | $store_id = 'MC001'; 540 | $order_id = 'ord0001'; 541 | $line_id = 'L001'; 542 | 543 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 544 | $mc = new MailchimpEcommerce($api_user); 545 | $mc->getOrderLine($store_id, $order_id, $line_id); 546 | 547 | $this->assertEquals('GET', $mc->getClient()->method); 548 | $this->assertEquals($mc->getEndPoint() . '/ecommerce/stores/' . $store_id . '/orders/' . $order_id . '/lines/' . $line_id, $mc->getClient()->uri); 549 | } 550 | 551 | /** 552 | * Tests library function for adding an order line. 553 | */ 554 | public function testAddOrderLine() { 555 | $store_id = 'MC001'; 556 | $order_id = 'ord0001'; 557 | $id = 'L002'; 558 | $product = [ 559 | 'product_id' => 'PROD001', 560 | 'product_variant_id' => "Freddie's Jokes", 561 | 'quantity' => 1, 562 | 'price' => 5, 563 | ]; 564 | 565 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 566 | $mc = new MailchimpEcommerce($api_user); 567 | $mc->addOrderLine($store_id, $order_id, $id, $product); 568 | 569 | $this->assertEquals('POST', $mc->getClient()->method); 570 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/orders/' . $order_id . '/lines', $mc->getClient()->uri); 571 | 572 | $this->assertNotEmpty($mc->getClient()->options['json']); 573 | 574 | $request_body = $mc->getClient()->options['json']; 575 | 576 | $this->assertEquals($id, $request_body->id); 577 | $this->assertEquals($product['product_id'], $request_body->product_id); 578 | $this->assertEquals($product['product_variant_id'], $request_body->product_variant_id); 579 | $this->assertEquals($product['quantity'], $request_body->quantity); 580 | $this->assertEquals($product['price'], $request_body->price); 581 | } 582 | 583 | /** 584 | * Test getting all products. 585 | */ 586 | public function testsGetProducts() { 587 | $store_id = 'MC001'; 588 | 589 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 590 | $mc = new MailchimpEcommerce($api_user); 591 | $mc->getProducts($store_id); 592 | // Method must be GET. 593 | $this->assertEquals('GET', $mc->getClient()->method); 594 | // Confirm the URI being used. 595 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products', $mc->getClient()->uri); 596 | } 597 | 598 | /** 599 | * Test getting information on a single product. 600 | */ 601 | public function testGetProduct() { 602 | $store_id = 'MC001'; 603 | $product_id = 'sku0001'; 604 | 605 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 606 | $mc = new MailchimpEcommerce($api_user); 607 | $mc->getProduct($store_id, $product_id); 608 | // Method must be GET. 609 | $this->assertEquals('GET', $mc->getClient()->method); 610 | // Confirm the URI being used. 611 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id, $mc->getClient()->uri); 612 | } 613 | 614 | /** 615 | * Test adding a product. 616 | */ 617 | public function testAddProduct() { 618 | $store_id = 'MC001'; 619 | $id = 'sku0001'; 620 | $title = 'Test Product 001'; 621 | $url = 'http://example.org/'; 622 | $variant_1 = (object) [ 623 | 'id' => 'PROD001A', 624 | 'title' => "Freddie's Jokes Volume 1", 625 | ]; 626 | $variants = [ 627 | $variant_1, 628 | ]; 629 | 630 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 631 | $mc = new MailchimpEcommerce($api_user); 632 | 633 | $mc->addProduct($store_id, $id, $title, $url, $variants); 634 | $this->assertEquals('POST', $mc->getClient()->method); 635 | 636 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products', $mc->getClient()->uri); 637 | $this->assertNotEmpty($mc->getClient()->options['json']); 638 | 639 | $request_body = $mc->getClient()->options['json']; 640 | 641 | $this->assertEquals($id, $request_body->id); 642 | $this->assertEquals($title, $request_body->title); 643 | $this->assertEquals($variant_1->id, $request_body->variants[0]->id); 644 | $this->assertEquals($variant_1->title, $request_body->variants[0]->title); 645 | } 646 | 647 | /** 648 | * Test updating a product. 649 | */ 650 | public function testUpdateProduct() { 651 | $store_id = 'MC001'; 652 | $id = 'sku0001'; 653 | $variant_1 = (object) [ 654 | 'id' => 'PROD001A', 655 | 'title' => "Freddie's Jokes Volume 1", 656 | ]; 657 | $variant_2 = (object) [ 658 | 'id' => 'PROD002A', 659 | 'title' => "Freddie's Jokes Volume 2", 660 | ]; 661 | $variants = [ 662 | $variant_1, 663 | $variant_2, 664 | ]; 665 | 666 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 667 | $mc = new MailchimpEcommerce($api_user); 668 | 669 | $mc->updateProduct($store_id, $id, $variants); 670 | $this->assertEquals('PATCH', $mc->getClient()->method); 671 | 672 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $id, $mc->getClient()->uri); 673 | $this->assertNotEmpty($mc->getClient()->options['json']); 674 | 675 | $request_body = $mc->getClient()->options['json']; 676 | 677 | $this->assertEquals($variant_1->id, $request_body->variants[0]->id); 678 | $this->assertEquals($variant_1->title, $request_body->variants[0]->title); 679 | $this->assertEquals($variant_2->id, $request_body->variants[1]->id); 680 | $this->assertEquals($variant_2->title, $request_body->variants[1]->title); 681 | } 682 | 683 | /** 684 | * Test deleting a product. 685 | */ 686 | public function testDeleteProduct() { 687 | $store_id = 'MC001'; 688 | $product_id = 'sku0001'; 689 | 690 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 691 | $mc = new MailchimpEcommerce($api_user); 692 | $mc->deleteProduct($store_id, $product_id); 693 | // Method must be DELETE. 694 | $this->assertEquals('DELETE', $mc->getClient()->method); 695 | // Confirm URI being used. 696 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id, $mc->getClient()->uri); 697 | } 698 | 699 | /** 700 | * Test adding a product variant. 701 | */ 702 | public function testAddProductVariant() { 703 | $store_id = 'MC001'; 704 | $product_id = 'sku0001'; 705 | $params = [ 706 | 'id' => 'var001', 707 | 'title' => 'Var Title', 708 | ]; 709 | 710 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 711 | $mc = new MailchimpEcommerce($api_user); 712 | $mc->addProductVariant($store_id, $product_id, $params); 713 | $this->assertEquals('POST', $mc->getClient()->method); 714 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id . '/variants', $mc->getClient()->uri); 715 | $this->assertNotEmpty($mc->getClient()->options['json']); 716 | $request_body = $mc->getClient()->options['json']; 717 | $this->assertEquals($params['id'], $request_body->id); 718 | $this->assertEquals($params['title'], $request_body->title); 719 | } 720 | 721 | /** 722 | * Test deleting a variant. 723 | */ 724 | public function testDeleteVariant() { 725 | $store_id = 'MC001'; 726 | $product_id = 'sku0001'; 727 | $variant_id = 'var001'; 728 | 729 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 730 | $mc = new MailchimpEcommerce($api_user); 731 | $mc->deleteProductVariant($store_id, $product_id, $variant_id); 732 | // Confirm we are using DELETE in the client method. 733 | $this->assertEquals('DELETE', $mc->getClient()->method); 734 | // Confirm URI being used. 735 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id . '/variants/' . $variant_id, $mc->getClient()->uri); 736 | } 737 | 738 | /** 739 | * Test getting a single variant of a single product. 740 | */ 741 | public function testGetVariant() { 742 | $store_id = 'MC001'; 743 | $product_id = 'sku0001'; 744 | $variant_id = 'var001'; 745 | 746 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 747 | $mc = new MailchimpEcommerce($api_user); 748 | $mc->getProductVariant($store_id, $product_id, $variant_id); 749 | // Check method. 750 | $this->assertEquals('GET', $mc->getClient()->method); 751 | // Check URI being used. 752 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id . '/variants/' . $variant_id, $mc->getClient()->uri); 753 | } 754 | 755 | /** 756 | * Test getting all variants for a single product. 757 | */ 758 | public function testGetVariants() { 759 | $store_id = 'MC001'; 760 | $product_id = 'sku0001'; 761 | 762 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 763 | $mc = new MailchimpEcommerce($api_user); 764 | $mc->getProductVariants($store_id, $product_id); 765 | // Check method. 766 | $this->assertEquals('GET', $mc->getClient()->method); 767 | // Check URI being used. 768 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id . '/variants', $mc->getClient()->uri); 769 | 770 | } 771 | 772 | /** 773 | * Test updating a variant. 774 | */ 775 | public function testUpdateVariant() { 776 | $store_id = 'MC001'; 777 | $product_id = 'sku0001'; 778 | $variant_id = 'var001'; 779 | $params = [ 780 | 'title' => 'New Title', 781 | 'url' => 'http://www.example.com', 782 | 'sku' => 'abc0042', 783 | ]; 784 | 785 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 786 | $mc = new MailchimpEcommerce($api_user); 787 | $mc->updateProductVariant($store_id, $product_id, $variant_id, $params); 788 | // Check method. 789 | $this->assertEquals('PATCH', $mc->getClient()->method); 790 | // Check URI being used. 791 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/products/' . $product_id . '/variants/' . $variant_id, $mc->getClient()->uri); 792 | // Test the contents of the body of the request for the params. 793 | $this->assertNotEmpty($mc->getClient()->options['json']); 794 | $request_body = $mc->getClient()->options['json']; 795 | $this->assertEquals($params['url'], $request_body->url); 796 | $this->assertEquals($params['title'], $request_body->title); 797 | $this->assertEquals($params['sku'], $request_body->sku); 798 | } 799 | 800 | 801 | /** 802 | * Tests creating a promo rule 803 | */ 804 | public function testAddPromoRule() { 805 | $store_id = 'MC001'; 806 | $params = [ 807 | 'id' => 'promo0001', 808 | 'description' => 'Test promotion rule', 809 | 'amount' => '10.00', 810 | 'type' => 'fixed', 811 | 'target' => 'total', 812 | ]; 813 | 814 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 815 | $mc = new MailchimpEcommerce($api_user); 816 | $mc->addPromoRule($store_id, $params); 817 | // Check method. 818 | $this->assertEquals('POST', $mc->getClient()->method); 819 | // Check URI being used. 820 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules', $mc->getClient()->uri); 821 | // Test the contents of the body of the request for the params. 822 | $this->assertNotEmpty($mc->getClient()->options['json']); 823 | $request_body = $mc->getClient()->options['json']; 824 | $this->assertEquals($params['id'], $request_body->id); 825 | $this->assertEquals($params['description'], $request_body->description); 826 | $this->assertEquals($params['amount'], $request_body->amount); 827 | $this->assertEquals($params['type'], $request_body->type); 828 | $this->assertEquals($params['target'], $request_body->target); 829 | } 830 | 831 | /** 832 | * Tests updating a promo rule 833 | */ 834 | public function testUpdatePromoRule() { 835 | $store_id = 'MC001'; 836 | $params = [ 837 | 'id' => 'promo0001', 838 | 'description' => 'New description', 839 | 'amount' => '11', 840 | 'type' => 'percentage', 841 | 'target' => 'per_item', 842 | ]; 843 | 844 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 845 | $mc = new MailchimpEcommerce($api_user); 846 | $mc->updatePromoRule($store_id, $params); 847 | // Check method. 848 | $this->assertEquals('PATCH', $mc->getClient()->method); 849 | // Check URI being used. 850 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $params['id'], $mc->getClient()->uri); 851 | // Test the contents of the body of the request for the params. 852 | $this->assertNotEmpty($mc->getClient()->options['json']); 853 | $request_body = $mc->getClient()->options['json']; 854 | $this->assertEquals($params['id'], $request_body->id); 855 | $this->assertEquals($params['description'], $request_body->description); 856 | $this->assertEquals($params['amount'], $request_body->amount); 857 | $this->assertEquals($params['type'], $request_body->type); 858 | $this->assertEquals($params['target'], $request_body->target); 859 | } 860 | 861 | /** 862 | * Tests getting promo rules 863 | */ 864 | public function testGetPromoRules() { 865 | $store_id = 'MC001'; 866 | 867 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 868 | $mc = new MailchimpEcommerce($api_user); 869 | $mc->getPromoRules($store_id); 870 | // Check method. 871 | $this->assertEquals('GET', $mc->getClient()->method); 872 | // Check URI being used. 873 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules', $mc->getClient()->uri); 874 | } 875 | 876 | /** 877 | * Tests getting a promo rule 878 | */ 879 | public function testGetPromoRule() { 880 | $store_id = 'MC001'; 881 | $promo_rule_id = 'promo0001'; 882 | 883 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 884 | $mc = new MailchimpEcommerce($api_user); 885 | $mc->getPromoRule($store_id, $promo_rule_id); 886 | // Check method. 887 | $this->assertEquals('GET', $mc->getClient()->method); 888 | // Check URI being used. 889 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id, $mc->getClient()->uri); 890 | } 891 | 892 | /** 893 | * Tests deleting a promo rule 894 | */ 895 | public function testDeletePromoRule() { 896 | $store_id = 'MC001'; 897 | $promo_rule_id = 'promo0001'; 898 | 899 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 900 | $mc = new MailchimpEcommerce($api_user); 901 | $mc->deletePromoRule($store_id, $promo_rule_id); 902 | // Method must be DELETE. 903 | $this->assertEquals('DELETE', $mc->getClient()->method); 904 | // Confirm URI being used. 905 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id, $mc->getClient()->uri); 906 | } 907 | 908 | /** 909 | * Tests creating a promo code 910 | */ 911 | public function testAddPromoCode() { 912 | $store_id = 'MC001'; 913 | $promo_rule_id = 'promo0001'; 914 | $promo_code_id = 'code001'; 915 | $params = [ 916 | 'id' => 'code001', 917 | 'code' => 'abc0042', 918 | 'redemption_url' => 'http://www.example.com', 919 | ]; 920 | 921 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 922 | $mc = new MailchimpEcommerce($api_user); 923 | $mc->addPromoCode($store_id, $promo_rule_id, $params); 924 | // Check method. 925 | $this->assertEquals('POST', $mc->getClient()->method); 926 | // Check URI being used. 927 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id . '/promo-codes', $mc->getClient()->uri); 928 | // Test the contents of the body of the request for the params. 929 | $this->assertNotEmpty($mc->getClient()->options['json']); 930 | $request_body = $mc->getClient()->options['json']; 931 | $this->assertEquals($params['id'], $request_body->id); 932 | $this->assertEquals($params['code'], $request_body->code); 933 | $this->assertEquals($params['redemption_url'], $request_body->redemption_url); } 934 | 935 | /** 936 | * Tests updating a promo code 937 | */ 938 | public function testUpdatePromoCode() { 939 | $store_id = 'MC001'; 940 | $promo_rule_id = 'promo0001'; 941 | $params = [ 942 | 'id' => 'code001', 943 | 'code' => 'abc0042', 944 | 'redemption_url' => 'http://www.example.com', 945 | ]; 946 | 947 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 948 | $mc = new MailchimpEcommerce($api_user); 949 | $mc->updatePromoCode($store_id, $promo_rule_id, $params); 950 | // Check method. 951 | $this->assertEquals('PATCH', $mc->getClient()->method); 952 | // Check URI being used. 953 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id . '/promo-codes/' . $params['id'], $mc->getClient()->uri); 954 | // Test the contents of the body of the request for the params. 955 | $this->assertNotEmpty($mc->getClient()->options['json']); 956 | $request_body = $mc->getClient()->options['json']; 957 | $this->assertEquals($params['id'], $request_body->id); 958 | $this->assertEquals($params['code'], $request_body->code); 959 | $this->assertEquals($params['redemption_url'], $request_body->redemption_url); 960 | } 961 | 962 | /** 963 | * Tests getting promo codes 964 | */ 965 | public function testGetPromoCodes() { 966 | $store_id = 'MC001'; 967 | $promo_rule_id = 'promo0001'; 968 | 969 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 970 | $mc = new MailchimpEcommerce($api_user); 971 | $mc->getPromoCodes($store_id, $promo_rule_id); 972 | // Check method. 973 | $this->assertEquals('GET', $mc->getClient()->method); 974 | // Check URI being used. 975 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id . '/promo-codes', $mc->getClient()->uri); 976 | } 977 | 978 | /** 979 | * Tests getting a promo code 980 | */ 981 | public function testGetPromoCode() { 982 | $store_id = 'MC001'; 983 | $promo_rule_id = 'promo0001'; 984 | $promo_code_id = 'code001'; 985 | 986 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 987 | $mc = new MailchimpEcommerce($api_user); 988 | $mc->getPromoCode($store_id, $promo_rule_id, $promo_code_id); 989 | // Check method. 990 | $this->assertEquals('GET', $mc->getClient()->method); 991 | // Check URI being used. 992 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id . '/promo-codes/' . $promo_code_id, $mc->getClient()->uri); 993 | } 994 | 995 | /** 996 | * Tests deleting a promo code 997 | */ 998 | public function testDeletePromoCode() { 999 | $store_id = 'MC001'; 1000 | $promo_rule_id = 'promo0001'; 1001 | $promo_code_id = 'code001'; 1002 | 1003 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 1004 | $mc = new MailchimpEcommerce($api_user); 1005 | $mc->deletePromoCode($store_id, $promo_rule_id, $promo_code_id); 1006 | // Method must be DELETE. 1007 | $this->assertEquals('DELETE', $mc->getClient()->method); 1008 | // Confirm URI being used. 1009 | $this->assertEquals($mc->getEndpoint() . '/ecommerce/stores/' . $store_id . '/promo-rules/' . $promo_rule_id . '/promo-codes/' . $promo_code_id, $mc->getClient()->uri); 1010 | } 1011 | 1012 | } 1013 | -------------------------------------------------------------------------------- /tests/MailchimpListsTest.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 19 | $mc = new MailchimpLists($api_user); 20 | $mc->getLists(); 21 | 22 | $this->assertEquals('GET', $mc->getClient()->method); 23 | $this->assertEquals($mc->getEndpoint() . '/lists', $mc->getClient()->uri); 24 | } 25 | 26 | /** 27 | * Tests library functionality for list information. 28 | */ 29 | public function testGetList() { 30 | $list_id = '57afe96172'; 31 | 32 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 33 | $mc = new MailchimpLists($api_user); 34 | $mc->getList($list_id); 35 | 36 | $this->assertEquals('GET', $mc->getClient()->method); 37 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id, $mc->getClient()->uri); 38 | } 39 | 40 | /** 41 | * Tests library functionality for interest categories information. 42 | */ 43 | public function testGetInterestCategories() { 44 | $list_id = '57afe96172'; 45 | 46 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 47 | $mc = new MailchimpLists($api_user); 48 | $mc->getInterestCategories($list_id); 49 | 50 | $this->assertEquals('GET', $mc->getClient()->method); 51 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories', $mc->getClient()->uri); 52 | } 53 | 54 | /** 55 | * Tests library functionality for adding new interest categories. 56 | */ 57 | public function testAddInterestCategories() { 58 | $list_id = '57afe96172'; 59 | $title = 'Test Interest Category'; 60 | $type = 'checkbox'; 61 | 62 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 63 | $mc = new MailchimpLists($api_user); 64 | $mc->addInterestCategories($list_id, $title, $type); 65 | 66 | $this->assertEquals('POST', $mc->getClient()->method); 67 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories', $mc->getClient()->uri); 68 | 69 | $this->assertNotEmpty($mc->getClient()->options['json']); 70 | 71 | $request_body = $mc->getClient()->options['json']; 72 | 73 | $this->assertEquals($title, $request_body->title); 74 | $this->assertEquals($type, $request_body->type); 75 | } 76 | 77 | /** 78 | * Tests library functionality for updating interest categories. 79 | */ 80 | public function testUpdateInterestCategories() { 81 | $list_id = '57afe96172'; 82 | $title = 'Test Interest Category Edited'; 83 | $type = 'dropdown'; 84 | $interest_category_id = '08f0b1d7b2'; 85 | 86 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 87 | $mc = new MailchimpLists($api_user); 88 | $mc->updateInterestCategories($list_id, $interest_category_id, $title, $type); 89 | 90 | $this->assertEquals('PATCH', $mc->getClient()->method); 91 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id, $mc->getClient()->uri); 92 | 93 | $this->assertNotEmpty($mc->getClient()->options['json']); 94 | 95 | $request_body = $mc->getClient()->options['json']; 96 | 97 | $this->assertEquals($title, $request_body->title); 98 | $this->assertEquals($type, $request_body->type); 99 | } 100 | 101 | /** 102 | * Tests library functionality for deleting interest categories. 103 | */ 104 | public function testDeleteInterestCategories() { 105 | $list_id = '57afe96172'; 106 | $interest_category_id = '08f0b1d7b2'; 107 | 108 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 109 | $mc = new MailchimpLists($api_user); 110 | $mc->deleteInterestCategories($list_id, $interest_category_id); 111 | 112 | $this->assertEquals('DELETE', $mc->getClient()->method); 113 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id, $mc->getClient()->uri); 114 | } 115 | 116 | /** 117 | * Tests library functionality for interests information. 118 | */ 119 | public function testGetInterests() { 120 | $list_id = ''; 121 | $interest_category_id = ''; 122 | 123 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 124 | $mc = new MailchimpLists($api_user); 125 | $mc->getInterests($list_id, $interest_category_id); 126 | 127 | $this->assertEquals('GET', $mc->getClient()->method); 128 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests', $mc->getClient()->uri); 129 | } 130 | 131 | /** 132 | * Tests library functionality for adding interests. 133 | */ 134 | public function testAddInterests() { 135 | $list_id = '57afe96172'; 136 | $interest_category_id = '08f0b1d7b2'; 137 | $name = 'Test Interest'; 138 | 139 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 140 | $mc = new MailchimpLists($api_user); 141 | $mc->addInterests($list_id, $interest_category_id, $name); 142 | 143 | $this->assertEquals('POST', $mc->getClient()->method); 144 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests', $mc->getClient()->uri); 145 | 146 | $this->assertNotEmpty($mc->getClient()->options['json']); 147 | 148 | $request_body = $mc->getClient()->options['json']; 149 | 150 | $this->assertEquals($name, $request_body->name); 151 | } 152 | 153 | /** 154 | * Tests library functionality for updating interests. 155 | */ 156 | public function testUpdateInterests() { 157 | $list_id = '57afe96172'; 158 | $interest_category_id = '08f0b1d7b2'; 159 | $interest_id = '9143cf3bd1'; 160 | $name = 'Test Interest Edited'; 161 | 162 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 163 | $mc = new MailchimpLists($api_user); 164 | $mc->updateInterests($list_id, $interest_category_id, $interest_id, $name); 165 | 166 | $this->assertEquals('PATCH', $mc->getClient()->method); 167 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests/' . $interest_id, $mc->getClient()->uri); 168 | 169 | $this->assertNotEmpty($mc->getClient()->options['json']); 170 | 171 | $request_body = $mc->getClient()->options['json']; 172 | 173 | $this->assertEquals($name, $request_body->name); 174 | } 175 | 176 | /** 177 | * Tests library functionality for deleting interests. 178 | */ 179 | public function testDeleteInterests() { 180 | $list_id = '57afe96172'; 181 | $interest_category_id = '08f0b1d7b2'; 182 | $interest_id = '9143cf3bd1'; 183 | 184 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 185 | $mc = new MailchimpLists($api_user); 186 | $mc->deleteInterests($list_id, $interest_category_id, $interest_id); 187 | 188 | $this->assertEquals('DELETE', $mc->getClient()->method); 189 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/interest-categories/' . $interest_category_id . '/interests/' . $interest_id, $mc->getClient()->uri); 190 | } 191 | 192 | /** 193 | * Tests library functionality for merge fields information. 194 | */ 195 | public function testGetMergeFields() { 196 | $list_id = '57afe96172'; 197 | 198 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 199 | $mc = new MailchimpLists($api_user); 200 | $mc->getMergeFields($list_id); 201 | 202 | $this->assertEquals('GET', $mc->getClient()->method); 203 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/merge-fields', $mc->getClient()->uri); 204 | } 205 | 206 | /** 207 | * Tests library functionality for adding a merge field. 208 | */ 209 | public function testAddMergeField() { 210 | $list_id = '57afe96172'; 211 | $name = 'Phone number'; 212 | $type = 'phone'; 213 | 214 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 215 | $mc = new MailchimpLists($api_user); 216 | $mc->addMergeField($list_id, $name, $type); 217 | 218 | $this->assertEquals('POST', $mc->getClient()->method); 219 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/merge-fields', $mc->getClient()->uri); 220 | 221 | $request_body = $mc->getClient()->options['json']; 222 | 223 | $this->assertEquals($name, $request_body->name); 224 | $this->assertEquals($type, $request_body->type); 225 | } 226 | 227 | /** 228 | * Tests library functionality for members information. 229 | */ 230 | public function testGetMembers() { 231 | $list_id = '57afe96172'; 232 | 233 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 234 | $mc = new MailchimpLists($api_user); 235 | $mc->getMembers($list_id); 236 | 237 | $this->assertEquals('GET', $mc->getClient()->method); 238 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members', $mc->getClient()->uri); 239 | } 240 | 241 | /** 242 | * Tests library functionality for member information. 243 | */ 244 | public function testGetMemberInfo() { 245 | $list_id = '57afe96172'; 246 | $email = 'test@example.com'; 247 | 248 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 249 | $mc = new MailchimpLists($api_user); 250 | $mc->getMemberInfo($list_id, $email); 251 | 252 | $this->assertEquals('GET', $mc->getClient()->method); 253 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email), $mc->getClient()->uri); 254 | } 255 | 256 | /** 257 | * Tests library functionality for member activity information. 258 | */ 259 | public function testGetMemberActivity() { 260 | $list_id = '57afe96172'; 261 | $email = 'test@example.com'; 262 | 263 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 264 | $mc = new MailchimpLists($api_user); 265 | $mc->getMemberActivity($list_id, $email); 266 | 267 | $this->assertEquals('GET', $mc->getClient()->method); 268 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/activity', $mc->getClient()->uri); 269 | } 270 | 271 | /** 272 | * Tests library functionality for member events information. 273 | */ 274 | public function testGetMemberEvents() { 275 | $list_id = '57afe96172'; 276 | $email = 'test@example.com'; 277 | 278 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 279 | $mc = new MailchimpLists($api_user); 280 | $mc->getMemberEvents($list_id, $email); 281 | 282 | $this->assertEquals('GET', $mc->getClient()->method); 283 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/events', $mc->getClient()->uri); 284 | } 285 | 286 | /** 287 | * Tests library functionality for adding a list member. 288 | */ 289 | public function testAddMember() { 290 | $list_id = '57afe96172'; 291 | $email = 'test@example.com'; 292 | 293 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 294 | $mc = new MailchimpLists($api_user); 295 | $mc->addMember($list_id, $email); 296 | 297 | $this->assertEquals('POST', $mc->getClient()->method); 298 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members', $mc->getClient()->uri); 299 | 300 | $this->assertNotEmpty($mc->getClient()->options['json']); 301 | 302 | $request_body = $mc->getClient()->options['json']; 303 | 304 | $this->assertEquals($email, $request_body->email_address); 305 | } 306 | 307 | /** 308 | * Tests library functionality for removing a list member. 309 | */ 310 | public function testRemoveMember() { 311 | $list_id = '57afe96172'; 312 | $email = 'test@example.com'; 313 | 314 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 315 | $mc = new MailchimpLists($api_user); 316 | $mc->removeMember($list_id, $email); 317 | 318 | $this->assertEquals('DELETE', $mc->getClient()->method); 319 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email), $mc->getClient()->uri); 320 | } 321 | 322 | /** 323 | * Tests library functionality for updating a list member. 324 | */ 325 | public function testUpdateMember() { 326 | $list_id = '57afe96172'; 327 | $email = 'test@example.com'; 328 | 329 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 330 | $mc = new MailchimpLists($api_user); 331 | $mc->updateMember($list_id, $email); 332 | 333 | $this->assertEquals('PATCH', $mc->getClient()->method); 334 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email), $mc->getClient()->uri); 335 | } 336 | 337 | /** 338 | * Tests library functionality for adding or updating an existing list member. 339 | */ 340 | public function testAddOrUpdateMember() { 341 | $list_id = '57afe96172'; 342 | $email = 'test@example.com'; 343 | 344 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 345 | $mc = new MailchimpLists($api_user); 346 | $mc->addOrUpdateMember($list_id, $email); 347 | 348 | $this->assertEquals('PUT', $mc->getClient()->method); 349 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email), $mc->getClient()->uri); 350 | } 351 | 352 | /** 353 | * Tests library functionality for member tags information. 354 | */ 355 | public function testGetMemberTags() { 356 | $list_id = '57afe96172'; 357 | $email = 'test@example.com'; 358 | 359 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 360 | $mc = new MailchimpLists($api_user); 361 | $mc->getMemberTags($list_id, $email); 362 | 363 | $this->assertEquals('GET', $mc->getClient()->method); 364 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/tags', $mc->getClient()->uri); 365 | } 366 | 367 | /** 368 | * Tests library functionality for list segment information. 369 | */ 370 | public function testGetSegments() { 371 | $list_id = '57afe96172'; 372 | 373 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 374 | $mc = new MailchimpLists($api_user); 375 | $mc->getSegments($list_id); 376 | 377 | $this->assertEquals('GET', $mc->getClient()->method); 378 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments', $mc->getClient()->uri); 379 | } 380 | 381 | /** 382 | * Tests library functionality for list segment information. 383 | */ 384 | public function testGetSegment() { 385 | $list_id = '57afe96172'; 386 | $segment_id = '49377'; 387 | 388 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 389 | $mc = new MailchimpLists($api_user); 390 | $mc->getSegment($list_id, $segment_id); 391 | 392 | $this->assertEquals('GET', $mc->getClient()->method); 393 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id, $mc->getClient()->uri); 394 | } 395 | 396 | /** 397 | * Tests library functionality for adding a list segment. 398 | */ 399 | public function testAddSegment() { 400 | $list_id = '57afe96172'; 401 | $name = 'Test Segment'; 402 | 403 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 404 | $mc = new MailchimpLists($api_user); 405 | $mc->addSegment($list_id, $name); 406 | 407 | $this->assertEquals('POST', $mc->getClient()->method); 408 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments', $mc->getClient()->uri); 409 | 410 | $this->assertNotEmpty($mc->getClient()->options['json']); 411 | 412 | $request_body = $mc->getClient()->options['json']; 413 | 414 | $this->assertEquals($name, $request_body->name); 415 | } 416 | 417 | /** 418 | * Tests library functionality for updating a list segment. 419 | */ 420 | public function testUpdateSegment() { 421 | $list_id = '57afe96172'; 422 | $segment_id = '49381'; 423 | $name = 'Updated Test Segment'; 424 | 425 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 426 | $mc = new MailchimpLists($api_user); 427 | $mc->updateSegment($list_id, $segment_id, $name); 428 | 429 | $this->assertEquals('PATCH', $mc->getClient()->method); 430 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id, $mc->getClient()->uri); 431 | 432 | $this->assertNotEmpty($mc->getClient()->options['json']); 433 | 434 | $request_body = $mc->getClient()->options['json']; 435 | 436 | $this->assertEquals($name, $request_body->name); 437 | } 438 | 439 | /** 440 | * Tests library functionality for segment member information. 441 | */ 442 | public function testGetSegmentMembers() { 443 | $list_id = '205d96e6b4'; 444 | $segment_id = '457'; 445 | 446 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 447 | $mc = new MailchimpLists($api_user); 448 | $mc->getSegmentMembers($list_id, $segment_id); 449 | 450 | $this->assertEquals('GET', $mc->getClient()->method); 451 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id . '/members', $mc->getClient()->uri); 452 | } 453 | 454 | /** 455 | * Tests library functionality for adding a segment member. 456 | */ 457 | public function testAddSegmentMember() { 458 | $list_id = '205d96e6b4'; 459 | $segment_id = '457'; 460 | $email = 'test@example.com'; 461 | 462 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 463 | $mc = new MailchimpLists($api_user); 464 | $mc->addSegmentMember($list_id, $segment_id, $email); 465 | 466 | $this->assertEquals('POST', $mc->getClient()->method); 467 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id . '/members', $mc->getClient()->uri); 468 | 469 | $this->assertNotEmpty($mc->getClient()->options['json']); 470 | 471 | $request_body = $mc->getClient()->options['json']; 472 | 473 | $this->assertEquals($email, $request_body->email_address); 474 | } 475 | 476 | /** 477 | * Tests library functionality for removing a segment member. 478 | */ 479 | public function testRemoveSegmentMember() { 480 | $list_id = '205d96e6b4'; 481 | $segment_id = '457'; 482 | $email = 'test@example.com'; 483 | 484 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 485 | $mc = new MailchimpLists($api_user); 486 | $mc->removeSegmentMember($list_id, $segment_id, $email); 487 | 488 | $this->assertEquals('DELETE', $mc->getClient()->method); 489 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/segments/' . $segment_id . '/members/' . md5($email), $mc->getClient()->uri); 490 | } 491 | 492 | /** 493 | * Tests library functionality for adding tags to a member. 494 | */ 495 | public function testAddTagsMember() { 496 | $list_id = '205d96e6b4'; 497 | $tags = ['Foo', 'Bar']; 498 | $email = 'test@example.com'; 499 | 500 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 501 | $mc = new MailchimpLists($api_user); 502 | $mc->addTagsMember($list_id, $tags, $email); 503 | 504 | $this->assertEquals('POST', $mc->getClient()->method); 505 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/tags', $mc->getClient()->uri); 506 | 507 | $this->assertNotEmpty($mc->getClient()->options['json']); 508 | 509 | $request_body = $mc->getClient()->options['json']; 510 | 511 | $expected = [ 512 | [ 513 | 'name' => 'Foo', 514 | 'status' => 'active', 515 | ], 516 | [ 517 | 'name' => 'Bar', 518 | 'status' => 'active', 519 | ], 520 | ]; 521 | $this->assertEquals($expected, $request_body->tags); 522 | } 523 | 524 | /** 525 | * Tests library functionality for removing tags from a member. 526 | */ 527 | public function testRemoveTagsMember() { 528 | $list_id = '205d96e6b4'; 529 | $tags = ['Foo', 'Bar']; 530 | $email = 'test@example.com'; 531 | 532 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 533 | $mc = new MailchimpLists($api_user); 534 | $mc->removeTagsMember($list_id, $tags, $email); 535 | 536 | $this->assertEquals('POST', $mc->getClient()->method); 537 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/tags', $mc->getClient()->uri); 538 | 539 | $this->assertNotEmpty($mc->getClient()->options['json']); 540 | 541 | $request_body = $mc->getClient()->options['json']; 542 | 543 | $expected = [ 544 | [ 545 | 'name' => 'Foo', 546 | 'status' => 'inactive', 547 | ], 548 | [ 549 | 'name' => 'Bar', 550 | 'status' => 'inactive', 551 | ], 552 | ]; 553 | $this->assertEquals($expected, $request_body->tags); 554 | } 555 | 556 | /** 557 | * Tests library functionality for adding a member event. 558 | */ 559 | public function testAddMemberEvent() { 560 | $list_id = '205d96e6b4'; 561 | $email = 'test@example.com'; 562 | $event = [ 563 | "name" => "registered_referral", 564 | "properties" => [ 565 | "ref-code-123456" 566 | ], 567 | ]; 568 | 569 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 570 | $mc = new MailchimpLists($api_user); 571 | $mc->AddMemberEvent($list_id, $email, $event); 572 | 573 | $this->assertEquals('POST', $mc->getClient()->method); 574 | $this->assertEquals($mc->getEndpoint() . '/lists/' . $list_id . '/members/' . md5($email) . '/events', $mc->getClient()->uri); 575 | 576 | $this->assertNotEmpty($mc->getClient()->options['json']); 577 | 578 | $request_body = $mc->getClient()->options['json']; 579 | 580 | $this->assertEquals($event, (array) $request_body); 581 | } 582 | } 583 | -------------------------------------------------------------------------------- /tests/MailchimpReportsTest.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 19 | $mc = new MailchimpReports($api_user); 20 | $mc->getSummary(); 21 | 22 | $this->assertEquals('GET', $mc->getClient()->method); 23 | $this->assertEquals($mc->getEndpoint() . '/reports', $mc->getClient()->uri); 24 | } 25 | 26 | /** 27 | * Tests library functionality for campaign report information. 28 | */ 29 | public function testCampaignSummary() { 30 | $campaign_id = '42694e9e57'; 31 | 32 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 33 | $mc = new MailchimpReports($api_user); 34 | $mc->getCampaignSummary($campaign_id); 35 | 36 | $this->assertEquals('GET', $mc->getClient()->method); 37 | $this->assertEquals($mc->getEndpoint() . '/reports/' . $campaign_id, $mc->getClient()->uri); 38 | } 39 | 40 | /** 41 | * Tests library functionality for campaign report information. 42 | */ 43 | public function testCampaignReport() { 44 | $campaign_id = '42694e9e57'; 45 | $type = 'email-activity'; 46 | 47 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 48 | $mc = new MailchimpReports($api_user); 49 | $mc->getCampaignReport($campaign_id, $type); 50 | 51 | $this->assertEquals('GET', $mc->getClient()->method); 52 | $this->assertEquals($mc->getEndpoint() . '/reports/' . $campaign_id . '/' . $type, $mc->getClient()->uri); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /tests/MailchimpTemplatesTest.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 19 | $mc = new MailchimpTemplates($api_user); 20 | $mc->getTemplates(); 21 | 22 | $this->assertEquals('GET', $mc->getClient()->method); 23 | $this->assertEquals($mc->getEndpoint() . '/templates', $mc->getClient()->uri); 24 | } 25 | 26 | /** 27 | * Tests library functionality for template information. 28 | */ 29 | public function testGetTemplate() { 30 | $template_id = '2000094'; 31 | 32 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 33 | $mc = new MailchimpTemplates($api_user); 34 | $mc->getTemplate($template_id); 35 | 36 | $this->assertEquals('GET', $mc->getClient()->method); 37 | $this->assertEquals($mc->getEndpoint() . '/templates/' . $template_id, $mc->getClient()->uri); 38 | } 39 | 40 | /** 41 | * Tests library functionality for template content information. 42 | */ 43 | public function testGetTemplateContent() { 44 | $template_id = '2000094'; 45 | 46 | $api_user = new Mailchimp(['api_user' => null, 'api_key' => null]); 47 | $mc = new MailchimpTemplates($api_user); 48 | $mc->getTemplateContent($template_id); 49 | 50 | $this->assertEquals('GET', $mc->getClient()->method); 51 | $this->assertEquals($mc->getEndpoint() . '/templates/' . $template_id . '/default-content', $mc->getClient()->uri); 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /tests/MailchimpTest.php: -------------------------------------------------------------------------------- 1 | null, 'api_key' => null]); 19 | $mc = new MailchimpApiUser($api_user); 20 | $mc->getAccount(); 21 | 22 | $this->assertEquals('GET', $mc->getClient()->method); 23 | $this->assertEquals($mc->getEndpoint() . '/', $mc->getClient()->uri); 24 | } 25 | 26 | /** 27 | * Test the version number. 28 | */ 29 | public function testVersion() { 30 | $mc = new Mailchimp(['api_user' => null, 'api_key' => null]); 31 | $this->assertEquals(json_decode(file_get_contents('composer.json'))->version, $mc::VERSION); 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /tests/src/Client.php: -------------------------------------------------------------------------------- 1 | method = $method; 23 | $this->uri = $uri; 24 | $this->options = $options; 25 | 26 | return new Response(); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /tests/src/Mailchimp.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 17 | 18 | parent::__construct($authentication_settings, $http_options, $this->client); 19 | } 20 | 21 | public function getClient() { 22 | return $this->api_class->client; 23 | } 24 | 25 | public function getEndpoint() { 26 | return 'https://us1.api.mailchimp.com/3.0'; 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /tests/src/MailchimpApiUser.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 19 | 20 | parent::__construct($api_class); 21 | } 22 | 23 | public function getClient() { 24 | return $this->api_class->client; 25 | } 26 | 27 | public function getEndpoint() { 28 | return 'https://us1.api.mailchimp.com/3.0'; 29 | } 30 | 31 | } -------------------------------------------------------------------------------- /tests/src/MailchimpAutomations.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 19 | 20 | parent::__construct($api_class); 21 | } 22 | 23 | public function getClient() { 24 | return $this->api_class->client; 25 | } 26 | 27 | public function getEndpoint() { 28 | return 'https://us1.api.mailchimp.com/3.0'; 29 | } 30 | 31 | /** 32 | * @inheritdoc 33 | */ 34 | public function getAutomations($parameters = []) { 35 | parent::getAutomations($parameters); 36 | 37 | $response = (object) [ 38 | 'automations' => [ 39 | (object) [ 40 | 'id' => '57afe96172', 41 | 'name' => 'Test Automation One', 42 | ], 43 | (object) [ 44 | 'id' => 'f4b7b26b2e', 45 | 'name' => 'Test Automation Two', 46 | ], 47 | (object) [ 48 | 'id' => '587693d673', 49 | 'name' => 'Test Automation Three', 50 | ], 51 | ], 52 | 'total_items' => 3, 53 | ]; 54 | 55 | return $response; 56 | } 57 | 58 | /** 59 | * @inheritdoc 60 | */ 61 | public function getWorkflow($workflow_id) { 62 | parent::getWorkflow($workflow_id); 63 | $response = (object) [ 64 | 'id' => 'eb4c82c9d2', 65 | 'create_time' => '2015-07-23T15:15:00+00:00', 66 | 'start_time' => '', 67 | 'status' => 'save', 68 | 'emails_sent' => 0, 69 | ]; 70 | 71 | return $response; 72 | } 73 | 74 | /** 75 | * @inheritdoc 76 | */ 77 | public function getWorkflowEmails($workflow_id) { 78 | parent::getWorkflowEmails($workflow_id); 79 | 80 | $response = (object) [ 81 | 'emails' => [ 82 | (object) [ 83 | 'id' => 'a87de7d1e5', 84 | 'workflow_id' => '57afe96172', 85 | 'position' => 1, 86 | 'status' => 'sending', 87 | 'emails_sent' => 1, 88 | 'send_time' => '2016-07-20T15:48:04+00:00', 89 | 'content_type' => 'template', 90 | ], 91 | ], 92 | 'total_items' => 1, 93 | ]; 94 | 95 | return $response; 96 | } 97 | 98 | /** 99 | * @inheritdoc 100 | */ 101 | public function getWorkflowEmail($workflow_id,$workflow_email_id) { 102 | parent::getWorkflowEmail($workflow_id, $workflow_email_id); 103 | 104 | $response = (object) [ 105 | 'id' => 'a87de7d1e5', 106 | 'workflow_id' => '57afe96172', 107 | 'position' => 1, 108 | 'status' => 'sending', 109 | 'emails_sent' => 1, 110 | 'send_time' => '2016-07-20T15:48:04+00:00', 111 | 'content_type' => 'template', 112 | ]; 113 | 114 | return $response; 115 | } 116 | 117 | /** 118 | * @inheritdoc 119 | */ 120 | public function getWorkflowEmailSubscribers($workflow_id, $workflow_email_id) { 121 | parent::getWorkflowEmailSubscribers($workflow_id, $workflow_email_id); 122 | 123 | $response = (object) [ 124 | 'workflow_id' => '4e3da78a41', 125 | 'email_id' => 'a87de7d1e5', 126 | 'queue' => [], 127 | 'total_items' => 0, 128 | ]; 129 | 130 | return $response; 131 | } 132 | 133 | /** 134 | * @inheritdoc 135 | */ 136 | public function getWorkflowEmailSubscriber($workflow_id, $workflow_email_id, $email) { 137 | parent::getWorkflowEmailSubscriber($workflow_id, $workflow_email_id, $email); 138 | 139 | $response = (object) [ 140 | 'id' => md5(strtolower($email)), 141 | 'workflow_id' => '4e3da78a41', 142 | 'email_id' => 'a87de7d1e5', 143 | 'list_id' => '57afe96172', 144 | 'email_address' => $email, 145 | 'next_send' => '2017-04-28T15:48:04+00:00', 146 | ]; 147 | 148 | return $response; 149 | } 150 | 151 | } 152 | -------------------------------------------------------------------------------- /tests/src/MailchimpCampaigns.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 24 | 25 | parent::__construct($api_class); 26 | } 27 | 28 | public function getClient() { 29 | return $this->api_class->client; 30 | } 31 | 32 | public function getEndpoint() { 33 | return 'https://us1.api.mailchimp.com/3.0'; 34 | } 35 | 36 | /** 37 | * @inheritdoc 38 | */ 39 | public function getCampaign($campaign_id, $parameters = []) { 40 | parent::getCampaign($campaign_id, $parameters); 41 | 42 | $response = (object) [ 43 | 'id' => $campaign_id, 44 | 'type' => 'regular', 45 | 'recipients' => (object) [ 46 | 'list_id' => '57afe96172', 47 | ], 48 | 'settings' => (object) [ 49 | 'subject_line' => 'Test Campaign', 50 | ], 51 | 'tracking' => (object) [ 52 | 'html_clicks' => TRUE, 53 | 'text_clicks' => FALSE, 54 | ], 55 | ]; 56 | 57 | return $response; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /tests/src/MailchimpConnectedSites.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 24 | 25 | parent::__construct($api_class); 26 | } 27 | 28 | public function getClient() { 29 | return $this->api_class->client; 30 | } 31 | 32 | public function getEndpoint() { 33 | return 'https://us1.api.mailchimp.com/3.0'; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/src/MailchimpEcommerce.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 47 | 48 | parent::__construct($api_class); 49 | } 50 | 51 | public function getClient() { 52 | return $this->api_class->client; 53 | } 54 | 55 | public function getEndpoint() { 56 | return 'https://us1.api.mailchimp.com/3.0'; 57 | } 58 | 59 | /** 60 | * @inheritdoc 61 | */ 62 | public function getStore($store_id, $parameters = []) { 63 | parent::getStore($store_id, $parameters); 64 | 65 | return (isset($this->stores[$store_id])) ? $this->stores[$store_id] : NULL; 66 | } 67 | 68 | /** 69 | * @inheritdoc 70 | */ 71 | public function addStore($id, $store, $parameters = [], $batch = FALSE) { 72 | parent::addStore($id, $store, $batch); 73 | 74 | $parameters = [ 75 | 'id' => $id, 76 | ]; 77 | $parameters += $store; 78 | 79 | $this->stores[$id] = (object) $parameters; 80 | } 81 | 82 | /** 83 | * @inheritdoc 84 | */ 85 | public function getCustomer($store_id, $customer_id, $parameters = []) { 86 | parent::getCustomer($store_id, $customer_id, $parameters); 87 | 88 | if (isset($this->customers[$store_id])) { 89 | return (isset($this->customers[$store_id][$customer_id])) ? $this->customers[$store_id][$customer_id] : NULL; 90 | } 91 | 92 | return NULL; 93 | } 94 | 95 | /** 96 | * @inheritdoc 97 | */ 98 | public function addCustomer($store_id, $customer, $batch = FALSE) { 99 | parent::addCustomer($store_id, $customer, $batch); 100 | 101 | if (!isset($this->customers[$store_id])) { 102 | $this->customers[$store_id] = []; 103 | } 104 | 105 | $this->customers[$store_id][$customer['id']] = (object) $customer; 106 | } 107 | 108 | /** 109 | * @inheritdoc 110 | */ 111 | public function getOrder($store_id, $order_id, $parameters = []) { 112 | parent::getOrder($store_id, $order_id, $parameters); 113 | 114 | if (isset($this->orders[$store_id])) { 115 | return (isset($this->orders[$store_id][$order_id])) ? $this->orders[$store_id][$order_id] : NULL; 116 | } 117 | 118 | return NULL; 119 | } 120 | 121 | /** 122 | * @inheritdoc 123 | */ 124 | public function addOrder($store_id, $id, array $customer, array $order, $batch = FALSE) { 125 | parent::addOrder($store_id, $id, $customer, $order, $batch); 126 | 127 | if (empty($store_id)) { 128 | throw new MailchimpAPIException('Store ID cannot be empty.'); 129 | } 130 | 131 | if (empty($id)) { 132 | throw new MailchimpAPIException('Order ID cannot be empty.'); 133 | } 134 | 135 | if (empty($customer)) { 136 | throw new MailchimpAPIException('Customer cannot be empty.'); 137 | } 138 | 139 | if (empty($order)) { 140 | throw new MailchimpAPIException('Order cannot be empty.'); 141 | } 142 | 143 | if (!isset($order['lines']) || empty($order['lines'])) { 144 | throw new MailchimpAPIException('Order must contain at least one line item.'); 145 | } 146 | 147 | if (!isset($this->orders[$store_id])) { 148 | $this->orders[$store_id] = []; 149 | } 150 | 151 | $parameters = [ 152 | 'id' => $id, 153 | 'customer' => (object) $customer, 154 | ]; 155 | 156 | $parameters += $order; 157 | 158 | $this->orders[$store_id][$id] = (object) $parameters; 159 | } 160 | 161 | } 162 | -------------------------------------------------------------------------------- /tests/src/MailchimpLists.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 24 | 25 | parent::__construct($api_class); 26 | } 27 | 28 | public function getClient() { 29 | return $this->api_class->client; 30 | } 31 | 32 | public function getEndpoint() { 33 | return 'https://us1.api.mailchimp.com/3.0'; 34 | } 35 | 36 | /** 37 | * @inheritdoc 38 | */ 39 | public function getLists($parameters = []) { 40 | parent::getLists($parameters); 41 | 42 | $response = (object) [ 43 | 'lists' => [ 44 | (object) [ 45 | 'id' => '57afe96172', 46 | 'name' => 'Test List One', 47 | ], 48 | (object) [ 49 | 'id' => 'f4b7b26b2e', 50 | 'name' => 'Test List Two', 51 | ], 52 | (object) [ 53 | 'id' => '587693d673', 54 | 'name' => 'Test List Three', 55 | ], 56 | ], 57 | 'total_items' => 3, 58 | ]; 59 | 60 | return $response; 61 | } 62 | 63 | /** 64 | * @inheritdoc 65 | */ 66 | public function getList($list_id, $parameters = []) { 67 | parent::getList($list_id, $parameters); 68 | 69 | $response = (object) [ 70 | 'id' => $list_id, 71 | 'name' => 'Test List One', 72 | ]; 73 | 74 | return $response; 75 | } 76 | 77 | /** 78 | * @inheritdoc 79 | */ 80 | public function getInterestCategories($list_id, $parameters = []) { 81 | parent::getInterestCategories($list_id, $parameters); 82 | 83 | $response = (object) [ 84 | 'list_id' => $list_id, 85 | 'categories' => [ 86 | (object) [ 87 | 'list_id' => $list_id, 88 | 'id' => 'a1e9f4b7f6', 89 | 'title' => 'Test Interest Category', 90 | ], 91 | ], 92 | 'total_items' => 1, 93 | ]; 94 | 95 | return $response; 96 | } 97 | 98 | /** 99 | * @inheritdoc 100 | */ 101 | public function addInterestCategories($list_id, $title, $type, $parameters = [], $batch = FALSE) { 102 | parent::addInterestCategories($list_id, $title, $type, $parameters = [], $batch = FALSE); 103 | 104 | $response = (object) [ 105 | 'list_id' => $list_id, 106 | 'id' => 'a1e9f4b7f6', 107 | 'title' => $title, 108 | 'type' => $type, 109 | ]; 110 | 111 | return $response; 112 | } 113 | 114 | /** 115 | * @inheritdoc 116 | */ 117 | public function updateInterestCategories($list_id, $interest_category_id, $title, $type, $parameters = [], $batch = FALSE) { 118 | parent::updateInterestCategories($list_id, $interest_category_id, $title, $type, $parameters = [], $batch = FALSE); 119 | 120 | $response = (object) [ 121 | 'list_id' => $list_id, 122 | 'id' => 'a1e9f4b7f6', 123 | 'title' => $title, 124 | 'type' => $type, 125 | 'interest_category_id' => $interest_category_id, 126 | ]; 127 | 128 | return $response; 129 | } 130 | 131 | /** 132 | * @inheritdoc 133 | */ 134 | public function deleteInterestCategories($list_id, $interest_category_id, $parameters = [], $batch = FALSE) { 135 | parent::deleteInterestCategories($list_id, $interest_category_id, $parameters = [], $batch = FALSE); 136 | 137 | return (!empty($list_id) && !empty($interest_category_id)); 138 | } 139 | 140 | /** 141 | * @inheritdoc 142 | */ 143 | public function getInterests($list_id, $interest_category_id, $parameters = []) { 144 | parent::getInterests($list_id, $interest_category_id, $parameters); 145 | 146 | $response = (object) [ 147 | 'interests' => [ 148 | (object) [ 149 | 'category_id' => $interest_category_id, 150 | 'list_id' => $list_id, 151 | 'id' => '9143cf3bd1', 152 | 'name' => 'Test Interest', 153 | ], 154 | ], 155 | 'total_items' => 1, 156 | ]; 157 | 158 | return $response; 159 | } 160 | 161 | /** 162 | * @inheritdoc 163 | */ 164 | public function addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE) { 165 | parent::addInterests($list_id, $interest_category_id, $name, $parameters = [], $batch = FALSE); 166 | 167 | $response = (object) [ 168 | 'list_id' => $list_id, 169 | 'category_id' => $interest_category_id, 170 | 'interest_id' => '9143cf3bd1', 171 | 'name' => $name, 172 | ]; 173 | 174 | return $response; 175 | } 176 | 177 | /** 178 | * @inheritdoc 179 | */ 180 | public function updateInterests($list_id, $interest_category_id, $interest_id, $name, $parameters = [], $batch = FALSE) { 181 | parent::updateInterests($list_id, $interest_category_id, $interest_id, $name, $parameters = [], $batch = FALSE); 182 | 183 | $response = (object) [ 184 | 'list_id' => $list_id, 185 | 'category_id' => $interest_category_id, 186 | 'interest_id' => $interest_id, 187 | 'name' => $name, 188 | ]; 189 | 190 | return $response; 191 | } 192 | 193 | /** 194 | * @inheritdoc 195 | */ 196 | public function deleteInterests($list_id, $interest_category_id, $interest_id, $parameters = [], $batch = FALSE) { 197 | parent::deleteInterests($list_id, $interest_category_id, $interest_id, $parameters = [], $batch = FALSE); 198 | 199 | return (!empty($list_id) && !empty($interest_category_id) && !empty($interest_id)); 200 | } 201 | 202 | /** 203 | * @inheritdoc 204 | */ 205 | public function getMergeFields($list_id, $parameters = []) { 206 | parent::getMergeFields($list_id, $parameters); 207 | 208 | $response = (object) [ 209 | 'merge_fields' => [ 210 | (object) [ 211 | 'merge_id' => 1, 212 | 'tag' => 'FNAME', 213 | 'list_id' => $list_id, 214 | ], 215 | (object) [ 216 | 'merge_id' => 2, 217 | 'tag' => 'LNAME', 218 | 'list_id' => $list_id, 219 | ], 220 | ], 221 | 'total_items' => 2, 222 | ]; 223 | 224 | return $response; 225 | } 226 | 227 | /** 228 | * @inheritdoc 229 | */ 230 | public function getMemberInfo($list_id, $email, $parameters = []) { 231 | parent::getMemberInfo($list_id, $email, $parameters); 232 | 233 | $response = (object) [ 234 | 'id' => md5(strtolower($email)), 235 | 'email_address' => $email, 236 | 'status' => 'subscribed', 237 | ]; 238 | 239 | return $response; 240 | } 241 | 242 | /** 243 | * @inheritdoc 244 | */ 245 | public function addMember($list_id, $email, $parameters = [], $batch = FALSE) { 246 | parent::addMember($list_id, $email, $parameters, $batch); 247 | 248 | $response = (object) [ 249 | 'id' => md5(strtolower($email)), 250 | 'email_address' => $email, 251 | ]; 252 | 253 | foreach ($parameters as $key => $value) { 254 | $response->{$key} = $value; 255 | } 256 | 257 | return $response; 258 | } 259 | 260 | /** 261 | * @inheritdoc 262 | */ 263 | public function removeMember($list_id, $email) { 264 | parent::removeMember($list_id, $email); 265 | } 266 | 267 | /** 268 | * @inheritdoc 269 | */ 270 | public function updateMember($list_id, $email, $parameters = [], $batch = FALSE) { 271 | parent::updateMember($list_id, $email, $parameters, $batch); 272 | 273 | $response = (object) [ 274 | 'id' => md5(strtolower($email)), 275 | 'email_address' => $email, 276 | ]; 277 | 278 | foreach ($parameters as $key => $value) { 279 | $response->{$key} = $value; 280 | } 281 | 282 | return $response; 283 | } 284 | 285 | /** 286 | * @inheritdoc 287 | */ 288 | public function addOrUpdateMember($list_id, $email, $parameters = [], $batch = FALSE) { 289 | parent::addOrUpdateMember($list_id, $email, $parameters, $batch); 290 | 291 | $response = (object) [ 292 | 'id' => md5(strtolower($email)), 293 | 'email_address' => $email, 294 | ]; 295 | 296 | foreach ($parameters as $key => $value) { 297 | $response->{$key} = $value; 298 | } 299 | 300 | return $response; 301 | } 302 | 303 | /** 304 | * @inheritdoc 305 | */ 306 | public function getSegments($list_id, $parameters = []) { 307 | parent::getSegments($list_id, $parameters); 308 | 309 | $response = (object) [ 310 | 'segments' => [ 311 | (object) [ 312 | 'id' => 49377, 313 | 'name' => 'Test Segment One', 314 | 'type' => 'static', 315 | 'list_id' => $list_id, 316 | ], 317 | (object) [ 318 | 'id' => 49378, 319 | 'name' => 'Test Segment Two', 320 | 'type' => 'static', 321 | 'list_id' => $list_id, 322 | ], 323 | ], 324 | 'total_items' => 2, 325 | ]; 326 | 327 | return $response; 328 | } 329 | 330 | /** 331 | * @inheritdoc 332 | */ 333 | public function getSegment($list_id, $segment_id, $parameters = []) { 334 | parent::getSegment($list_id, $segment_id, $parameters); 335 | 336 | $response = (object) [ 337 | 'id' => 49377, 338 | 'name' => 'Test Segment One', 339 | 'type' => 'static', 340 | 'list_id' => $list_id, 341 | ]; 342 | 343 | return $response; 344 | } 345 | 346 | /** 347 | * @inheritdoc 348 | */ 349 | public function addSegment($list_id, $name, $parameters = [], $batch = FALSE) { 350 | parent::addSegment($list_id, $name, $parameters, $batch); 351 | 352 | $response = (object) []; 353 | 354 | if (!empty($list_id) && !empty($name) && !empty($parameters['type'])) { 355 | $response->id = 49381; 356 | $response->name = $name; 357 | $response->type = $parameters['type']; 358 | $response->list_id = $list_id; 359 | } 360 | 361 | return $response; 362 | } 363 | 364 | /** 365 | * @inheritdoc 366 | */ 367 | public function updateSegment($list_id, $segment_id, $name, $parameters = [], $batch = FALSE) { 368 | parent::updateSegment($list_id, $segment_id, $name, $parameters); 369 | 370 | $response = (object) [ 371 | 'id' => $segment_id, 372 | 'name' => $name, 373 | 'member_count' => (isset($parameters['static_segment'])) ? count($parameters['static_segment']) : 0, 374 | 'list_id' => $list_id, 375 | ]; 376 | 377 | return $response; 378 | } 379 | 380 | /** 381 | * @inheritdoc 382 | */ 383 | public function getWebhooks($list_id, $parameters = []) { 384 | parent::getWebhooks($list_id, $parameters); 385 | 386 | $response = (object) [ 387 | 'webhooks' => [ 388 | (object) [ 389 | 'id' => '37b9c73a88', 390 | 'url' => 'http://example.org', 391 | 'events' => (object) [ 392 | 'subscribe' => TRUE, 393 | 'unsubscribe' => FALSE, 394 | ], 395 | 'sources' => (object) [ 396 | 'user' => TRUE, 397 | 'api' => FALSE, 398 | ], 399 | 'list_id' => $list_id, 400 | ], 401 | ], 402 | 'total_items' => 1, 403 | ]; 404 | 405 | return $response; 406 | } 407 | 408 | /** 409 | * @inheritdoc 410 | */ 411 | public function addWebhook($list_id, $url, $parameters = [], $batch = FALSE) { 412 | parent::addWebhook($list_id, $url, $parameters, $batch); 413 | 414 | $response = (object) [ 415 | 'id' => 'ab24521a00', 416 | 'url' => $url, 417 | 'list_id' => $list_id, 418 | ]; 419 | 420 | foreach ($parameters as $key => $value) { 421 | $response->{$key} = $value; 422 | } 423 | 424 | return $response; 425 | } 426 | 427 | /** 428 | * @inheritdoc 429 | */ 430 | public function deleteWebhook($list_id, $webhook_id, $parameters = []) { 431 | parent::deleteWebhook($list_id, $webhook_id, $parameters); 432 | 433 | return (!empty($list_id) && !empty($webhook_id)); 434 | } 435 | 436 | } 437 | -------------------------------------------------------------------------------- /tests/src/MailchimpReports.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 24 | 25 | parent::__construct($api_class); 26 | } 27 | 28 | public function getClient() { 29 | return $this->api_class->client; 30 | } 31 | 32 | public function getEndpoint() { 33 | return 'https://us1.api.mailchimp.com/3.0'; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/src/MailchimpTemplates.php: -------------------------------------------------------------------------------- 1 | client = new MailchimpTestHttpClient(); 24 | 25 | parent::__construct($api_class); 26 | } 27 | 28 | public function getClient() { 29 | return $this->api_class->client; 30 | } 31 | 32 | public function getEndpoint() { 33 | return 'https://us1.api.mailchimp.com/3.0'; 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /tests/src/MailchimpTestHttpClient.php: -------------------------------------------------------------------------------- 1 | method = $method; 38 | $this->uri = $uri; 39 | $this->options = $options; 40 | 41 | return new MailchimpTestHttpResponse(); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /tests/src/MailchimpTestHttpResponse.php: -------------------------------------------------------------------------------- 1 |