/** * Determine if an API key is valid * * @throws InvalidApiKey * @throws \Exception */ public function validateKey() { try { $this->api->get(''); } catch (\Exception $e) { if (starts_with($e->getMessage(), '{')) { $json = json_decode($e->getMessage()); if ($json->status == 401) { throw new InvalidApiKey($json->detail); } } throw $e; } }
/** * @throws \Exception */ public function interests(string $interestCategoryId) : array { $result = $this->api->get('lists/' . $this->id() . '/interest-categories/' . $interestCategoryId . '/interests'); return $result->get('interests'); }
/** * Unsubscribe * * @param string $email * @param string $listId * @return boolean */ public function unsubscribe($email, $listId) { return $this->api->request('lists/' . $listId . '/members/' . $this->getEmailHash($email), array('status' => 'unsubscribed'), 'patch'); }
/** * Check if email exists in one or more lists. * * @param $email * @param $formListId * @return array|mixed */ public function checkIfSubscribed($email, $formListId) { if ($email != '' && $this->validateEmail($email)) { // validate email $listIdStr = $formListId != '' ? $formListId : $this->getSetting('mcsubListId'); // check if we got an api key and a list id if ($this->getSetting('mcsubApikey') != '' && $listIdStr != '') { // create a new api instance, and subscribe $mc = new Mailchimp($this->getSetting('mcsubApikey')); // split id string on | in case more than one list id is supplied $listIdArr = explode("|", $listIdStr); // loop over list id's and subscribe $results = array(); foreach ($listIdArr as $listId) { // check if email is subscribed try { $existsCheck = $mc->request('lists/' . $listId . '/members/' . md5(strtolower($email))); array_push($results, $this->_getMessage(200, $email, array(), Craft::t("The email address passed exists on this list"), true)); } catch (\Exception $e) { // subscriber didn't exist array_push($results, $this->_getMessage(1000, $email, array(), Craft::t("The email address passed does not exist on this list"), false)); } } if (count($results) > 1) { return $this->_parseMultipleListsResult($results); } else { return $results[0]; } } else { // error, no API key or list id return $this->_getMessage(2000, $email, $vars, Craft::t("API Key or List ID not supplied. Check your settings.")); } } else { // error, invalid email return $this->_getMessage(1000, $email, $vars, Craft::t("Invalid email")); } }
/** * @param $mailchimpListId * * @return \Illuminate\Support\Collection */ protected function getListInfo($mailchimpListId) { return $this->mc->get("/lists/{$mailchimpListId}"); }