public static function donation_button_constant_contact_handler($posted) { $cc_list_id = get_option("donation_button_constantcontact_lists"); $cc_api_key = get_option("constantcontact_api_key"); $access_token = get_option("constantcontact_access_token"); $fname = isset($posted['first_name']) ? $posted['first_name'] : ''; $lname = isset($posted['last_name']) ? $posted['last_name'] : ''; $email = isset($posted['payer_email']) ? $posted['payer_email'] : $posted['receiver_email']; $debug = get_option('log_enable_constant_contact') == 'yes' ? 'yes' : 'no'; if ('yes' == $debug) { $log = new Donation_Button_Logger(); } if (isset($cc_api_key) && !empty($cc_api_key) && (isset($access_token) && !empty($access_token)) && (isset($cc_list_id) && !empty($cc_list_id))) { try { $ConstantContact = new ConstantContact($cc_api_key); $response = $ConstantContact->getContactByEmail($access_token, $email); if (empty($response->results)) { $Contact = new Contact(); $Contact->addEmail($email); $Contact->addList($cc_list_id); $Contact->first_name = $fname; $Contact->last_name = $lname; $NewContact = $ConstantContact->addContact($access_token, $Contact, false); if (isset($NewContact) && 'yes' == $debug) { $log->add('ConstantContact', ' ConstantContact new contact ' . $email . ' added to selected contact list'); } } else { $Contact = $response->results[0]; $Contact->first_name = $fname; $Contact->last_name = $lname; $Contact->addList($cc_list_id); $new_contact = $ConstantContact->updateContact($access_token, $Contact, false); if (isset($new_contact) && 'yes' == $debug) { $log->add('ConstantContact', ' ConstantContact update contact ' . $email . ' to selected contact list'); } } } catch (CtctException $ex) { $error = $ex->getErrors(); $log->add('ConstantContact', print_r($error, true)); } } else { if ('yes' == $debug) { $log->add('ConstantContact', 'Constant Contact API Key OR Constant Contact Access Token does not set'); } } }
function annadir_newsletter($mail = false) { $handle = new Mensajes(); $mail = (string) $mail; if (!$mail || !is_email($mail)) { $handle->add_error('No se ha recibido un mail válido'); } else { require_once '../classes/php-sdk-development/src/Ctct/autoload.php'; $cc = new ConstantContact(APIKEY_CONSTANT); $contacts = $cc->getContacts(ACCESS_TOKEN_CONSTANT); $lists = $cc->getLists(ACCESS_TOKEN_CONSTANT); $list = '2021474646'; try { $response = $cc->getContactByEmail(ACCESS_TOKEN_CONSTANT, $mail); if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($mail); $contact->addList($list); $returnContact = $cc->addContact(ACCESS_TOKEN_CONSTANT, $contact, true); } else { $contact = $response->results[0]; $contact->addList($list); $returnContact = $cc->updateContact(ACCESS_TOKEN_CONSTANT, $contact, true); } $handle->add_mensaje('Tu correo se ha añadido correctamente a la lista de Bunch of Makers'); } catch (CtctException $ex) { $errores = reset($ex->getErrors()); $handle->add_error($errores['error_message']); } } return $handle->imprimir(false, true) . '<script>finalizar_subscripcion();</script>'; }
/** * * */ public function addContact($listId, $email, $firstName, $lastName, $additional = array()) { try { $contact = new Contact(); $contact->addEmail($email); $contact->addList($listId); $contact->first_name = $firstName; $contact->last_name = $lastName; $this->cc->addContact($this->accessToken, $contact); return true; } catch (CtctException $ex) { $error = (array) $ex->getErrors(); $this->_errorMessage = $error[0]['error_message']; return false; } }
/** * @since 1.2.0 * @param type $posted * @return type */ public static function ofw_constantcontact_handler($posted) { if (!isset($posted) || empty($posted)) { return; } $debug = get_option('ofw_log_enable_constant_contact') == 'yes' ? 'yes' : 'no'; if ('yes' == $debug) { require_once 'class-offers-for-woocommerce-logger.php'; $log = new Angelleye_Offers_For_Woocommerce_Logger(); } $concontact_api_key = get_option('ofw_constantcontact_api_key'); $constantcontact_access_token = get_option('ofw_constantcontact_access_token'); $cclistsid = get_option('ofw_constantcontact_lists'); $first_name = isset($posted['offer_name']) ? $posted['offer_name'] : ''; $last_name = ''; $offer_email = isset($posted['offer_email']) ? $posted['offer_email'] : ''; if (isset($concontact_api_key) && !empty($concontact_api_key) && (isset($constantcontact_access_token) && !empty($constantcontact_access_token))) { $ConstantContact = new ConstantContact($concontact_api_key); $response = $ConstantContact->getContactByEmail($constantcontact_access_token, $offer_email); if (empty($response->results)) { $Contact = new Contact(); $Contact->addEmail($offer_email); $Contact->first_name = $first_name; $Contact->last_name = $last_name; $Contact->addList($cclistsid); $NewContact = $ConstantContact->addContact($constantcontact_access_token, $Contact, false); if ('yes' == $debug) { $log->add('ConstantContact', 'ConstantContact new contact ' . $offer_email . ' added to selected contact list'); } } else { $Contact = $response->results[0]; $Contact->first_name = $first_name; $Contact->last_name = $last_name; $Contact->addList($cclistsid); $new_contact = $ConstantContact->updateContact($constantcontact_access_token, $Contact, false); $log->add('ConstantContact', 'ConstantContact update contact ' . $offer_email . ' to selected contact list'); } } else { if ('yes' == $debug) { $log->add('ConstantContact', 'Constant Contact API Key OR Constant Contact Access Token does not set'); } } }
/** * * */ public function addContact($email, $firstName, $lastName, $lists = array(), $additional = array(), $actualUser = true) { try { $contact = new Contact(); $contact->addEmail($email); foreach ($lists as $v) { $contact->addList($v); } $contact->first_name = $firstName; $contact->last_name = $lastName; !empty($additional['company']) ? $contact->company_name = $additional['company'] : false; !empty($additional['job_title']) ? $contact->job_title = $additional['job_title'] : false; !empty($additional['work_phone']) ? $contact->work_phone = $additional['work_phone'] : false; //$contact->addAddress(Address::create( array("address_type"=>"BUSINESS","line1"=>$street,"city"=>$city,"state"=>$state,"postal_code"=>$zip))); $this->cc->addContact($this->accessToken, $contact, $actualUser); return true; } catch (CtctException $ex) { $error = (array) $ex->getErrors(); $this->_errorMessage = $error[0]['error_message']; return false; } }
public function testGetContactsFromList() { $response = self::$client->get('/')->json(); $result = new ResultSet($response['results'], $response['meta']); $this->assertInstanceOf('Ctct\\Components\\ResultSet', $result); $contact = Contact::create($result->results[1]); $this->assertInstanceOf('Ctct\\Components\\Contacts\\Contact', $contact); $this->assertEquals(231, $contact->id); $this->assertEquals("ACTIVE", $contact->status); $this->assertEquals("", $contact->fax); $this->assertEquals("", $contact->prefix_name); $this->assertEquals("Jimmy", $contact->first_name); $this->assertEquals("Roving", $contact->last_name); $this->assertEquals("Bear Tamer", $contact->job_title); $this->assertEquals("Animal Trainer Pro", $contact->company_name); $this->assertEquals("details", $contact->source_details); $this->assertEquals(false, $contact->confirmed); $this->assertEquals("", $contact->source); // custom fields $this->assertInstanceOf('Ctct\\Components\\Contacts\\CustomField', $contact->custom_fields[0]); $this->assertEquals("CustomField1", $contact->custom_fields[0]->name); $this->assertEquals("1", $contact->custom_fields[0]->value); //addresses $this->assertInstanceOf('Ctct\\Components\\Contacts\\Address', $contact->addresses[0]); $this->assertEquals("Suite 101", $contact->addresses[0]->line1); $this->assertEquals("line2", $contact->addresses[0]->line2); $this->assertEquals("line3", $contact->addresses[0]->line3); $this->assertEquals("Brookfield", $contact->addresses[0]->city); $this->assertEquals("PERSONAL", $contact->addresses[0]->address_type); $this->assertEquals("WI", $contact->addresses[0]->state_code); $this->assertEquals("us", $contact->addresses[0]->country_code); $this->assertEquals("53027", $contact->addresses[0]->postal_code); $this->assertEquals("", $contact->addresses[0]->sub_postal_code); //notes $this->assertEquals(0, count($contact->notes)); //lists $this->assertInstanceOf('Ctct\\Components\\Contacts\\ContactList', $contact->lists[0]); $this->assertEquals(1, $contact->lists[0]->id); $this->assertEquals("ACTIVE", $contact->lists[0]->status); // EmailAddress $this->assertInstanceOf('Ctct\\Components\\Contacts\\EmailAddress', $contact->email_addresses[0]); $this->assertEquals("ACTIVE", $contact->email_addresses[0]->status); $this->assertEquals("NO_CONFIRMATION_REQUIRED", $contact->email_addresses[0]->confirm_status); $this->assertEquals("ACTION_BY_OWNER", $contact->email_addresses[0]->opt_in_source); $this->assertEquals("2012-06-22T10:29:09.976Z", $contact->email_addresses[0]->opt_in_date); $this->assertEquals("", $contact->email_addresses[0]->opt_out_date); $this->assertEquals("*****@*****.**", $contact->email_addresses[0]->email_address); }
/** * Añade un mail al newsletter * * @param bool|string $mail mail que será añadido al newsletter. * @return string mensaje de error o de éxito. * @throws Exception si el algún setting del Newsletter no ha sido configurado. */ public static function Add($mail = false) { global $mensaje; $mail = (string) $mail; if (!$mail || filter_var($mail, FILTER_VALIDATE_EMAIL) === false) { $mensaje->add_error('No se ha recibido un mail válido'); } else { try { $cc = new ConstantContact(NewsletterHelper::ApiKey()); $list = NewsletterHelper::ListNumber(); $response = $cc->contactService->getContacts(NewsletterHelper::AccessToken(), array('email' => $mail)); // Parameters $actionByVisitor = true; $params = array(); if ($actionByVisitor == true) { $params['action_by'] = "ACTION_BY_VISITOR"; } if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($mail); $contact->addList($list); $returnContact = $cc->contactService->addContact(NewsletterHelper::AccessToken(), $contact, $params); } else { /** * @var $contact Contact */ $contact = $response->results[0]; $contact->addList($list); $returnContact = $cc->contactService->updateContact(NewsletterHelper::AccessToken(), $contact, $params); } $mensaje->add_mensaje('Tu correo se ha añadido correctamente a la lista'); } catch (Exception $ex) { if ($ex instanceof CtctException) { $allErrors = $ex->getErrors(); $errores = reset($allErrors); $mensaje->add_error($errores->error_message); } else { $mensaje->add_error($ex->getMessage()); } } } }
function wpcc_add_contact() { $cc = new ConstantContact(WPCC_APIKEY); try { $lists = $cc->listService->getLists(WPCC_ACCESS_TOKEN); } catch (CtctException $ex) { foreach ($ex->getErrors() as $error) { print_r($error); } if (!isset($lists)) { $lists = null; } } // check if the form was submitted if (isset($_POST['email']) && strlen($_POST['email']) > 1) { $action = "Getting Contact By Email Address"; try { // check to see if a contact with the email address already exists in the account $response = $cc->contactService->getContacts(WPCC_ACCESS_TOKEN, array("email" => $_POST['email'])); // create a new contact if one does not exist if (empty($response->results)) { $action = "Creating Contact"; $contact = new Contact(); $contact->addEmail($_POST['email']); $contact->addList('1818691277'); $contact->first_name = $_POST['first_name']; $contact->last_name = $_POST['last_name']; $address = new Address(); $address->address_type = "BUSINESS"; // PERSONAL, BUSINESS, UNKNOWN $address->line1 = $_POST['address_line_1']; $address->line2 = $_POST['address_line_2']; $address->city = $_POST['city']; $address->state_code = $_POST['state_code']; $address->postal_code = $_POST['postal_code']; $address->country_code = "US"; $contact->addAddress($address); $returnContact = $cc->contactService->addContact(WPCC_ACCESS_TOKEN, $contact); } else { $action = "Updating Contact"; $contact = $response->results[0]; if ($contact instanceof Contact) { $contact->addList('1818691277'); $contact->first_name = $_POST['first_name']; $contact->last_name = $_POST['last_name']; //If address exists, update that address. Else, create a new address object. if (count($contact->addresses) > 0) { $address = $contact->addresses[0]; } else { $address = new Address(); } $address->address_type = "BUSINESS"; // PERSONAL, BUSINESS, UNKNOWN $address->line1 = $_POST['address_line_1']; $address->line2 = $_POST['address_line_2']; $address->city = $_POST['city']; $address->state = ''; $address->state_code = $_POST['state_code']; $address->postal_code = $_POST['postal_code']; $address->country_code = $_POST['country_code']; $contact->addAddress($address); $customField_1 = new CustomField(); $customField_1->name = "CustomField1"; $customField_1->value = date('m/d/y', strtotime($_POST['dob'])); $contact->addCustomField($customField_1); /* * The third parameter of updateContact defaults to false, but if this were set to true it would tell * Constant Contact that this action is being performed by the contact themselves, and gives the ability to * opt contacts back in and trigger Welcome/Change-of-interest emails. * * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->contactService->updateContact(WPCC_ACCESS_TOKEN, $contact); } else { $e = new CtctException(); $e->setErrors(array("type", "Contact type not returned")); throw $e; } } // catch any exceptions thrown during the process and print the errors to screen } catch (CtctException $ex) { echo '<span class="label label-important">Error ' . $action . '</span>'; echo '<div class="container alert-error"><pre class="failure-pre">'; print_r($ex->getErrors()); echo '</pre></div>'; die; } } }
/** * Update contact details for a specific contact * @param string $accessToken - Constant Contact OAuth2 access token * @param Contact $contact - Contact to be updated * @param array $params - query params to be appended to the request * @return Contact */ public function updateContact($accessToken, Contact $contact, array $params = array()) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $contact->toJson()); return Contact::create(json_decode($response->body, true)); }
/** * Update contact details for a specific contact * @param string $accessToken - Constant Contact OAuth2 access token * @param Contact $contact - Contact to be updated * @param boolean $actionByVisitor - is the action being taken by the visitor * @return Contact */ public function updateContact($accessToken, Contact $contact, $actionByVisitor = false) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id); $params = array(); if ($actionByVisitor == true) { $params['action_by'] = "ACTION_BY_VISITOR"; } $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->put($url, parent::getHeaders($accessToken), $contact->toJson()); return Contact::create(json_decode($response->body, true)); }
/** * Update contact details for a specific contact * @param string $accessToken - Constant Contact OAuth2 access token * @param Contact $contact - Contact to be updated * @param array $params - associative array of query parameters and values to append to the request. * Allowed parameters include: * action_by - Whether the contact is taking the action, or the account owner. Must be one of * ACTION_BY_OWNER or ACTION_BY_VISITOR * @return Contact * @throws CtctException */ public function updateContact($accessToken, Contact $contact, array $params = array()) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.contact'), $contact->id); $request = parent::createBaseRequest($accessToken, 'PUT', $baseUrl); if ($params) { $query = $request->getQuery(); foreach ($params as $name => $value) { $query->add($name, $value); } } $stream = Stream::factory(json_encode($contact)); $request->setBody($stream); try { $response = parent::getClient()->send($request); } catch (ClientException $e) { throw parent::convertException($e); } return Contact::create($response->json()); }
function addContact($data, $list_id = false) { $errors = array(); $cc = new ConstantContact(ConstantContactV2::$cckey); // attempt to fetch lists in the account, catching any exceptions try { $lists = $cc->getLists(ConstantContactV2::$cctoken); } catch (CtctException $ex) { foreach ($ex->getErrors() as $error) { $errors[] = $error; } } // get list from the params, or just the first ACTIVE list if no param was passed $the_list = false; foreach ($lists as $list) { if (!$the_list && $list->status == 'ACTIVE') { $the_list = $list; } if ($list_id == $list->id) { $the_list = $list; break; } } try { if ($the_list) { $response = $cc->getContactByEmail(ConstantContactV2::$cctoken, $data['email']); //If the contact doesn't exist, add. If it does, update. if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($data['email']); $contact->addList($the_list->id); $contact->first_name = $data['first_name']; $contact->last_name = $data['last_name']; /* * The third parameter of addContact defaults to false, but if this were set to true it would tell Constant * Contact that this action is being performed by the contact themselves, and gives the ability to * opt contacts back in and trigger Welcome/Change-of-interest emails. * * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->addContact(ConstantContactV2::$cctoken, $contact, false); return true; } else { $contact = $response->results[0]; $contact->addList($the_list->id); $contact->first_name = $data['first_name']; $contact->last_name = $data['last_name']; /* * The third parameter of updateContact defaults to false, but if this were set to true it would tell * Constant Contact that this action is being performed by the contact themselves, and gives the ability to * opt contacts back in and trigger Welcome/Change-of-interest emails. * * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->updateContact(ConstantContactV2::$cctoken, $contact, false); return true; } } } catch (CtctException $ex) { /*echo '<pre>'; print_r($ex->getErrors()); echo '</pre>'; die();*/ $errors[] = $ex->getErrors(); } return $errors; }
<?php require_once 'Ctct/autoload.php'; define("APIKEY", $options_array['constant_contact']['api_key']); define("ACCESS_TOKEN", $options_array['constant_contact']['access_token']); use Ctct\ConstantContact; use Ctct\Components\Contacts\Contact; use Ctct\Components\Contacts\ContactList; use Ctct\Components\Contacts\EmailAddress; use Ctct\Exceptions\CtctException; $cc = new ConstantContact(APIKEY); try { $response = $cc->getContactByEmail(ACCESS_TOKEN, $email); if (empty($response->results)) { $action = "Creating Contact"; $contact = new Contact(); $contact->addEmail($email); $contact->addList($constant_contact_list); $contact->first_name = $fname; $contact->last_name = $lname; $returnContact = $cc->addContact(ACCESS_TOKEN, $contact, false); } else { $action = "Updating Contact"; $contact = $response->results[0]; $contact->addList($constant_contact_list); $contact->first_name = $fname; $contact->last_name = $lname; $returnContact = $cc->updateContact(ACCESS_TOKEN, $contact, false); } } catch (CtctException $ex) { }
/** * Get all contacts from an individual list * @param string $accessToken - Constant Contact OAuth2 access token * @param string $list_id - list id to retrieve contacts for * @param array $params - query params to attach to request * @return ResultSet */ public function getContactsFromList($accessToken, $list_id, $params = null) { $baseUrl = Config::get('endpoints.base_url') . sprintf(Config::get('endpoints.list_contacts'), $list_id); $url = $this->buildUrl($baseUrl, $params); $response = parent::getRestClient()->get($url, parent::getHeaders($accessToken)); $body = json_decode($response->body, true); $contacts = array(); foreach ($body['results'] as $contact) { $contacts[] = Contact::create($contact); } return new ResultSet($contacts, $body['meta']); }
public function addEmailToNewsLetter() { if ($this->request->is('POST') || $this->request->is('PUT')) { $api_key = Configure::read('constant_contact_key'); $api_token = Configure::read('constant_contact_token'); $cc = new ConstantContact($api_key); // attempt to fetch lists in the account, catching any exceptions and printing the errors to screen try { $lists = $cc->getLists($api_token); } catch (Exception $ex) { throw new Exception(); } // check if the form was submitted $action = "Getting Contact By Email Address"; try { // check to see if a contact with the email addess already exists in the account $response = $cc->getContactByEmail($api_token, $_POST['data']['email']); // create a new contact if one does not exist if (empty($response->results)) { $action = "Creating Contact"; $contact = new Contact(); $contact->addEmail($_POST['data']['email']); $contact->addList('1339315632'); $contact->first_name = $_POST['data']['first_name']; $contact->last_name = $_POST['data']['last_name']; /* * The third parameter of addContact defaults to false, but if this were set to true it would tell Constant Contact that this action is being performed by the contact themselves, and gives the ability to opt contacts back in and trigger Welcome/Change-of-interest emails. See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->addContact($api_token, $contact, true); // update the existing contact if address already existed } else { $action = "Updating Contact"; $contact = $response->results[0]; $contact->addList('1339315632'); $contact->first_name = $_POST['data']['first_name']; $contact->last_name = $_POST['data']['last_name']; $returnContact = $cc->updateContact($api_token, $contact, true); } // catch any exceptions thrown during the process and print the errors to screen } catch (Exception $ex) { $this->Session->setFlash('ニュースレターの配信登録に失敗しました。お問い合わせください。', 'default', array('class' => 'message error')); $this->redirect('/'); exit; } if ($returnContact) { $this->Session->setFlash('ニュースレターの配信登録を完了しました。', 'default', array('class' => 'message success')); $this->redirect('/'); } exit; } else { throw new Exception(); } }
public function constantcontact($service, $email) { $apikey = $service['thekey']; $service['additional'] = json_decode($service['additional'], 1); $access_token = $service['additional']['access_token']; $listId = $service['list']; $cc = new ConstantContact($apikey); $contact = new Contact(); $contact->addEmail($email); $contact->addList($listId); $contact->status = 'ACTIVE'; $cc->contactService->addContact($access_token, $contact); echo "ok"; die; }
print_r($error); } if (!isset($lists)) { $lists = null; } } // check if the form was submitted if (isset($_POST['email']) && strlen($_POST['email']) > 1) { $action = "Getting Contact By Email Address"; try { // check to see if a contact with the email address already exists in the account $response = $cc->contactService->getContacts(ACCESS_TOKEN, array("email" => $_POST['email'])); // create a new contact if one does not exist if (empty($response->results)) { $action = "Creating Contact"; $contact = new Contact(); $contact->addEmail($_POST['email']); $contact->addList($_POST['list']); $contact->first_name = $_POST['first_name']; $contact->last_name = $_POST['last_name']; /* * The third parameter of addContact defaults to false, but if this were set to true it would tell Constant * Contact that this action is being performed by the contact themselves, and gives the ability to * opt contacts back in and trigger Welcome/Change-of-interest emails. * * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->contactService->addContact(ACCESS_TOKEN, $contact); // update the existing contact if address already existed } else { $action = "Updating Contact";
/** * Añade un mail al newsletter * * @param bool|string $mail mail que será añadido al newsletter. * @return string mensaje de error o de éxito. * @throws Exception si el algún setting del Newsletter no ha sido configurado. */ public static function Add($mail = false) { global $mensaje; $mail = (string) $mail; if (!$mail || !is_email($mail)) { $mensaje->add_error('No se ha recibido un mail válido'); } else { try { $cc = new ConstantContact(NewsletterHelper::ApiKey()); $list = NewsletterHelper::ListNumber(); $response = $cc->getContactByEmail(NewsletterHelper::AccessToken(), $mail); if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($mail); $contact->addList($list); $returnContact = $cc->addContact(NewsletterHelper::AccessToken(), $contact, true); } else { $contact = $response->results[0]; $contact->addList($list); $returnContact = $cc->updateContact(NewsletterHelper::AccessToken(), $contact, true); } $mensaje->add_mensaje('Tu correo se ha añadido correctamente a la lista'); } catch (Exception $ex) { if ($ex instanceof CtctException) { $errores = reset($ex->getErrors()); $mensaje->add_error($errores['error_message']); } else { $mensaje->add_error($ex->getMessage()); } } } }
public function contactEntry($first_name, $last_name, $email) { try { // test to see if all the settings are correct if ($this->ready()) { // check to see if a contact with the email address already exists in the account $response = $this->cc->getContactByEmail(ACCESS_TOKEN, $email); // create a new contact if one does not exist if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($email); $contact->addList($this->list); $contact->first_name = $first_name; $contact->last_name = $last_name; $returnContact = $this->cc->addContact(ACCESS_TOKEN, $contact, false); } else { $contact = $response->results[0]; $contact->addList($this->list); $contact->first_name = $first_name; $contact->last_name = $last_name; $returnContact = $this->cc->updateContact(ACCESS_TOKEN, $contact, false); } $this->error = false; } } catch (CtctException $ex) { $this->error = true; } return $this->error; }
public function subscribe() { if (!$this->getRequest()->isAjax()) { //if not ajax, say go and eat your grass. exit("Action is not allowed!"); } $return = array('action' => '', 'message' => ''); $config = SiteConfig::current_site_config(); $cc = new ConstantContact($config->CcApiKey); $list = $this->getRequest()->postVar('list'); if ($config->CcDisplayZip) { $postcode = array('postal_code' => $this->getRequest()->postVar('postcode')); } else { $postcode = 0; } $email = $this->getRequest()->postVar('email'); $first_name = $this->getRequest()->postVar('first_name'); $last_name = $this->getRequest()->postVar('last_name'); if (empty($email) || empty($first_name) || empty($last_name) || empty($list)) { $return['action'] = 'E'; $return['message'] = $config->CcRequiredMessage ? $config->CcRequiredMessage : 'Required fields are missing.'; } else { try { // check to see if a contact with the email addess already exists in the account $response = $cc->getContactByEmail($config->CcAccessToken, $email); // create a new contact if one does not exist if (empty($response->results)) { $return['action'] = "C"; $contact = new Contact(); $contact->addEmail($email); if (!is_array($list)) { $contact->addList($list); } else { foreach ($list as $l) { $contact->addList($l); } } $contact->first_name = $first_name; $contact->last_name = $last_name; if ($postcode) { $contact->addAddress($postcode); } $returnContact = $cc->addContact($config->CcAccessToken, $contact); if (!empty($returnContact)) { $return['message'] = $config->CcAddedMessage; } // update the existing contact if address already existed } else { $return['action'] = "U"; $contact = $response->results[0]; if (!is_array($list)) { $contact->addList($list); } else { foreach ($list as $l) { $contact->addList($l); } } $contact->first_name = $first_name; $contact->last_name = $last_name; if ($postcode) { $contact->addAddress($postcode); } $returnContact = $cc->updateContact($config->CcAccessToken, $contact); if (!empty($returnContact)) { $return['message'] = $config->CcUpdatedMessage; } } // catch any exceptions thrown during the process and print the errors to screen } catch (CtctException $ex) { $ex = $ex->getErrors(); print_r($ex); $return['action'] = 'E'; $return['message'] = $config->CcErrorMessage; } } return json_encode($return); }
public function testUpdateContact() { $response = self::$client->put('/'); $contact = Contact::create($response->json()); $this->assertInstanceOf('Ctct\\Components\\Contacts\\Contact', $contact); $this->assertEquals(238, $contact->id); $this->assertEquals("ACTIVE", $contact->status); $this->assertEquals("555-1212", $contact->fax); $this->assertEquals("Mr.", $contact->prefix_name); $this->assertEquals("John", $contact->first_name); $this->assertEquals("Smith", $contact->last_name); $this->assertEquals("Software Engineer", $contact->job_title); $this->assertEquals("Constant Contact", $contact->company_name); $this->assertEquals("555-1212", $contact->home_phone); $this->assertEquals("555-1213", $contact->work_phone); $this->assertEquals("555-1214", $contact->cell_phone); $this->assertEquals("69f9d72b-0a5e-479d-b844-722b1da9595f", $contact->source_details); $this->assertEquals(false, $contact->confirmed); $this->assertEquals("API", $contact->source); // custom fields $this->assertInstanceOf('Ctct\\Components\\Contacts\\CustomField', $contact->custom_fields[0]); $this->assertEquals("CustomField1", $contact->custom_fields[0]->name); $this->assertEquals("3/28/2011 11:09 AM EDT", $contact->custom_fields[0]->value); $this->assertEquals("CustomField2", $contact->custom_fields[1]->name); $this->assertEquals("Site owner", $contact->custom_fields[1]->value); //addresses $this->assertInstanceOf('Ctct\\Components\\Contacts\\Address', $contact->addresses[0]); $this->assertEquals("1601 Trapelo Rd", $contact->addresses[0]->line1); $this->assertEquals("Suite 329", $contact->addresses[0]->line2); $this->assertEquals("Line 3", $contact->addresses[0]->line3); $this->assertEquals("Waltham", $contact->addresses[0]->city); $this->assertEquals("PERSONAL", $contact->addresses[0]->address_type); $this->assertEquals("MA", $contact->addresses[0]->state_code); $this->assertEquals("us", $contact->addresses[0]->country_code); $this->assertEquals("01720", $contact->addresses[0]->postal_code); $this->assertEquals("7885", $contact->addresses[0]->sub_postal_code); //notes $this->assertInstanceOf('Ctct\\Components\\Contacts\\Note', $contact->notes[0]); $this->assertEquals(1, $contact->notes[0]->id); $this->assertEquals("Here are some cool notes to add", $contact->notes[0]->note); $this->assertEquals("2012-12-03T17:09:22.702Z", $contact->notes[0]->created_date); //lists $this->assertInstanceOf('Ctct\\Components\\Contacts\\ContactList', $contact->lists[0]); $this->assertEquals(9, $contact->lists[0]->id); $this->assertEquals("ACTIVE", $contact->lists[0]->status); // EmailAddress $this->assertInstanceOf('Ctct\\Components\\Contacts\\EmailAddress', $contact->email_addresses[0]); $this->assertEquals("ACTIVE", $contact->email_addresses[0]->status); $this->assertEquals("NO_CONFIRMATION_REQUIRED", $contact->email_addresses[0]->confirm_status); $this->assertEquals("ACTION_BY_VISITOR", $contact->email_addresses[0]->opt_in_source); $this->assertEquals("2012-09-17T14:40:41.271Z", $contact->email_addresses[0]->opt_in_date); $this->assertEquals("2012-03-29T14:59:25.427Z", $contact->email_addresses[0]->opt_out_date); $this->assertEquals("*****@*****.**", $contact->email_addresses[0]->email_address); }
$lists = $cc->getLists(ACCESS_TOKEN); } catch (CtctException $ex) { foreach ($ex->getErrors() as $error) { print_r($error); } } // check if the form was submitted if (isset($_POST['email']) && strlen($_POST['email']) > 1) { $action = "Getting Contact By Email Address"; try { // check to see if a contact with the email addess already exists in the account $response = $cc->getContactByEmail(ACCESS_TOKEN, $_POST['email']); // create a new contact if one does not exist if (empty($response->results)) { $action = "Creating Contact"; $contact = new Contact(); $contact->addEmail($_POST['email']); $contact->addList($_POST['list']); $contact->first_name = $_POST['first_name']; $contact->last_name = $_POST['last_name']; /* * The third parameter of addContact defaults to false, but if this were set to true it would tell Constant * Contact that this action is being performed by the contact themselves, and gives the ability to * opt contacts back in and trigger Welcome/Change-of-interest emails. * * See: http://developer.constantcontact.com/docs/contacts-api/contacts-index.html#opt_in */ $returnContact = $cc->addContact(ACCESS_TOKEN, $contact, false); // update the existing contact if address already existed } else { $action = "Updating Contact";
/** * Añade un mail al newsletter * * @param bool|string $mail mail que será añadido al newsletter. * @return string mensaje de error o de éxito. * @throws Exception si el algún setting del Newsletter no ha sido configurado. */ public static function Add($mail = false) { $handle = new Mensajes(); $mail = (string) $mail; if (!$mail || !is_email($mail)) { $handle->add_error('No se ha recibido un mail válido'); } else { require_once '../php-sdk-development/src/Ctct/autoload.php'; $cc = new ConstantContact(NewsletterHelper::ApiKey()); $list = NewsletterHelper::ListNumber(); try { $response = $cc->getContactByEmail(NewsletterHelper::AccessToken(), $mail); if (empty($response->results)) { $contact = new Contact(); $contact->addEmail($mail); $contact->addList($list); $returnContact = $cc->addContact(NewsletterHelper::AccessToken(), $contact, true); } else { $contact = $response->results[0]; $contact->addList($list); $returnContact = $cc->updateContact(NewsletterHelper::AccessToken(), $contact, true); } $handle->add_mensaje('Tu correo se ha añadido correctamente a la lista'); } catch (CtctException $ex) { $errores = reset($ex->getErrors()); $handle->add_error($errores['error_message']); } } return $handle->imprimir(false, true); //.'<script>finalizar_subscripcion();</script>'; }