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');
         }
     }
 }
Exemple #2
0
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>';
}
 /**
  * @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');
         }
     }
 }
 // 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";
     $contact = $response->results[0];
     $contact->addList($_POST['list']);
     $contact->first_name = $_POST['first_name'];
     $contact->last_name = $_POST['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(ACCESS_TOKEN, $contact, false);
 /**
  * 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>';
 }
 // 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, true);
     // update the existing contact if address already existed
 } else {
     $action = "Updating Contact";
     $contact = $response->results[0];
     $contact->addList($_POST['list']);
     $contact->first_name = $_POST['first_name'];
     $contact->last_name = $_POST['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(ACCESS_TOKEN, $contact, true);
 /**
  * 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 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);
 }
 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;
 }
 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();
     }
 }