protected function sendReportMail($report, $emailId)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     // Send mail to the administrator
     if (!$emailId) {
         return false;
     }
     // Get website
     $uri = JUri::getInstance();
     $website = $uri->toString(array('scheme', 'host'));
     $emailMode = $this->params->get('email_mode', 'plain');
     // Get project
     $project = Crowdfunding\Project::getInstance(JFactory::getDbo(), $report->project_id);
     // Prepare data for parsing
     $data = array('site_name' => $app->get('sitename'), 'site_url' => JUri::root(), 'item_title' => $project->getTitle(), 'item_url' => $website . JRoute::_(CrowdfundingHelperRoute::getDetailsRoute($project->getSlug(), $project->getCatSlug())), 'report_subject' => $report->subject, 'report_description' => $report->description);
     $email = new Emailtemplates\Email();
     $email->setDb(JFactory::getDbo());
     $email->load($emailId);
     if (!$email->getSenderName()) {
         $email->setSenderName($app->get('fromname'));
     }
     if (!$email->getSenderEmail()) {
         $email->setSenderEmail($app->get('mailfrom'));
     }
     // Prepare recipient data.
     $componentParams = JComponentHelper::getParams('com_crowdfunding');
     /** @var  $componentParams Joomla\Registry\Registry */
     $recipientId = (int) $componentParams->get('administrator_id');
     if ($recipientId > 0) {
         $recipient = JFactory::getUser($recipientId);
         $recipientName = $recipient->get('name');
         $recipientMail = $recipient->get('email');
     } else {
         $recipientName = $app->get('fromname');
         $recipientMail = $app->get('mailfrom');
     }
     // Prepare data for parsing
     $data['sender_name'] = $email->getSenderName();
     $data['sender_email'] = $email->getSenderEmail();
     $data['recipient_name'] = $recipientName;
     $data['recipient_email'] = $recipientMail;
     $email->parse($data);
     $subject = $email->getSubject();
     $body = $email->getBody($emailMode);
     $mailer = JFactory::getMailer();
     if (strcmp('html', $emailMode) === 0) {
         // Send as HTML message
         $result = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_HTML);
     } else {
         // Send as plain text.
         $result = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_PLAIN);
     }
     // Log the error.
     if ($result !== true) {
         $this->log->add(JText::sprintf('PLG_CONTENT_CROWDFUNDINGADMINMAIL_ERROR_SEND_MAIL', $this->name), 'PLG_CONTENT_ADMIN_EMAIL_ERROR', JText::sprintf('PLG_CONTENT_CROWDFUNDINGADMINMAIL_ERROR_SEND_MAIL_NOTE', $mailer->ErrorInfo));
         return false;
     }
     return true;
 }
 protected function sendMail($project, $emailId)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     // Get website
     $uri = JUri::getInstance();
     $website = $uri->toString(array("scheme", "host"));
     $emailMode = $this->params->get("email_mode", "plain");
     // Route project URI
     $appSite = JApplicationCms::getInstance('site');
     $router = $appSite->getRouter('site');
     $routedUri = $router->build(CrowdfundingHelperRoute::getDetailsRoute($project->slug, $project->catslug));
     if ($routedUri instanceof JUri) {
         $routedUri = $routedUri->toString();
     }
     if (0 === strpos($routedUri, "/administrator")) {
         $routedUri = str_replace("/administrator", "", $routedUri);
     }
     // Prepare data for parsing
     $data = array("site_name" => $app->get("sitename"), "site_url" => JUri::root(), "item_title" => $project->title, "item_url" => $website . $routedUri);
     // Send mail to the administrator
     if (!$emailId) {
         return false;
     }
     $email = new EmailTemplates\Email();
     $email->setDb(JFactory::getDbo());
     $email->load($emailId);
     if (!$email->getSenderName()) {
         $email->setSenderName($app->get("fromname"));
     }
     if (!$email->getSenderEmail()) {
         $email->setSenderEmail($app->get("mailfrom"));
     }
     $recipientName = $project->name;
     $recipientMail = $project->email;
     // Prepare data for parsing
     $data["sender_name"] = $email->getSenderName();
     $data["sender_email"] = $email->getSenderEmail();
     $data["recipient_name"] = $recipientName;
     $data["recipient_email"] = $recipientMail;
     $email->parse($data);
     $subject = $email->getSubject();
     $body = $email->getBody($emailMode);
     $mailer = JFactory::getMailer();
     if (strcmp("html", $emailMode) == 0) {
         // Send as HTML message
         $result = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_HTML);
     } else {
         // Send as plain text.
         $result = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, Prism\Constants::MAIL_MODE_PLAIN);
     }
     // Log the error.
     if ($result !== true) {
         $this->log->add(JText::sprintf("PLG_CONTENT_CROWDFUNDINGUSERMAIL_ERROR_SEND_MAIL", $this->name), "PLG_CONTENT_USERE_MAIL_ERROR", JText::sprintf("PLG_CONTENT_CROWDFUNDINGUSERMAIL_ERROR_SEND_MAIL_NOTE", $mailer->ErrorInfo));
         return false;
     }
     return true;
 }
 /**
  * Disconnect user from payment gateway.
  *
  * @param string                   $context
  * @param Joomla\Registry\Registry $params
  *
  * @return null|array
  */
 public function onPayoutsDeauthorize($context, $params)
 {
     if (strcmp('com_crowdfundingfinance.payouts.deauthorize.stripeconnect', $context) !== 0) {
         return null;
     }
     if ($this->app->isAdmin()) {
         return null;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp('html', $docType) !== 0) {
         return null;
     }
     // Prepare output data.
     $output = array('redirect_url' => '', 'message' => '');
     $errorOutput = array('redirect_url' => JRoute::_(CrowdfundingHelperRoute::getDiscoverRoute()), 'message' => '');
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_GET_RESPONSE_AUTHORIZE'), $this->debugType, $_GET) : null;
     $userId = JFactory::getUser()->get('id');
     if (!$userId) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_NOT_REGISTERED_USER');
         return $errorOutput;
     }
     // Get token
     $state = $this->app->input->get('state');
     if (!$state) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_AUTHORIZATION_DATA');
         return $errorOutput;
     }
     // Get project ID and redirect URL from the session.
     $stateData = $this->app->getUserState($state);
     if (count($stateData) === 0 or (!$stateData['redirect_url'] or !$stateData['project_id'])) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_AUTHORIZATION_DATA');
         return $errorOutput;
     }
     $cfFinanceParams = JComponentHelper::getParams('com_crowdfundingfinance');
     $apiKeys = Crowdfundingfinance\Stripe\Helper::getKeys($cfFinanceParams);
     if (!$apiKeys['client_id'] or !$apiKeys['secret_key']) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_CONFIGURATION');
         return $errorOutput;
     }
     $payout = new Crowdfundingfinance\Payout(JFactory::getDbo());
     $payout->setSecretKey($this->app->get('secret'));
     $payout->load(array('project_id' => (int) $stateData['project_id']));
     if (!$payout->getId()) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_PAYOUT');
         return $errorOutput;
     }
     $alias = !$apiKeys['test'] ? 'production' : 'test';
     $stripeData = $payout->getStripe();
     if (!$stripeData->get('stripeconnect.' . $alias . '.account_id')) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_NOT_CONNECTED');
         return $errorOutput;
     }
     Crowdfundingfinance\Stripe\Helper::deauthorize($apiKeys, $stripeData->get('stripeconnect.' . $alias . '.account_id'));
     $stripeData->set('stripeconnect.' . $alias . '.access_token', '');
     $stripeData->set('stripeconnect.' . $alias . '.refresh_token', '');
     $stripeData->set('stripeconnect.' . $alias . '.account_id', '');
     $stripeData->set('stripeconnect.' . $alias . '.expires', 0);
     $payout->setStripe($stripeData);
     $payout->storeStripe();
     // Get next URL.
     $output['redirect_url'] = base64_decode($stateData['redirect_url']);
     return $output;
 }
 /**
  * Catch a request from payment plugin via AJAX and process a transaction.
  */
 public function notifyAjax()
 {
     $response = new Prism\Response\Json();
     // Check for disabled payment functionality
     if ($this->params->get('debug_payment_disabled', 0)) {
         $errorData = JText::sprintf('COM_CROWDFUNDING_TRANSACTION_DATA', var_export($_REQUEST, true));
         $this->log->add(JText::_('COM_CROWDFUNDING_ERROR_PAYMENT_HAS_BEEN_DISABLED'), 'ERROR_CONTROLLER_NOTIFIER_AJAX', $errorData);
         // Send response to the browser
         $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_PAYMENT_HAS_BEEN_DISABLED_MESSAGE'))->failure();
         echo $response;
         $this->app->close();
     }
     // Get model object.
     $model = $this->getModel();
     $paymentResult = null;
     $redirectUrl = null;
     $message = null;
     $project = null;
     /** @var Crowdfunding\Project $project */
     // Trigger the event
     try {
         // Import Crowdfunding Payment Plugins
         JPluginHelper::importPlugin('crowdfundingpayment');
         // Trigger onPaymentNotify event.
         $dispatcher = JEventDispatcher::getInstance();
         $results = $dispatcher->trigger('onPaymentNotify', array($this->context, &$this->params));
         if (is_array($results) and count($results) > 0) {
             foreach ($results as $result) {
                 if (is_object($result) and isset($result->transaction)) {
                     $paymentResult = $result;
                     $project = isset($result->project) ? $result->project : null;
                     $redirectUrl = isset($result->redirectUrl) ? $result->redirectUrl : null;
                     $message = isset($result->message) ? $result->message : null;
                     break;
                 }
             }
         }
         // If there is no transaction data, the status might be pending or another one.
         // So, we have to stop the script execution.
         if (!$paymentResult) {
             // Send response to the browser
             $response->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_TRANSACTION_NOT_PROCESSED_SUCCESSFULLY'))->failure();
             echo $response;
             $this->app->close();
         }
         // Trigger the event onAfterPaymentNotify
         $dispatcher->trigger('onAfterPaymentNotify', array($this->context, &$paymentResult, &$this->params));
         // Trigger the event onAfterPayment
         $dispatcher->trigger('onAfterPayment', array($this->context, &$paymentResult, &$this->params));
     } catch (Exception $e) {
         // Store log data to the database.
         $error = 'AJAX NOTIFIER ERROR: ' . $e->getMessage() . "\n";
         $errorData = 'INPUT:' . var_export($this->app->input, true) . "\n";
         $this->log->add($error, 'ERROR_CONTROLLER_NOTIFIER_AJAX', $errorData);
         // Send response to the browser
         $response->failure()->setTitle(JText::_('COM_CROWDFUNDING_FAIL'))->setText(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM'));
         // Send notification about the error to the administrator.
         $model->sendMailToAdministrator();
         echo $response;
         $this->app->close();
     }
     // Generate redirect URL
     if (!$redirectUrl and is_object($project)) {
         $uri = JUri::getInstance();
         $redirectUrl = $uri->toString(array('scheme', 'host')) . JRoute::_(CrowdfundingHelperRoute::getBackingRoute($project->getSlug(), $project->getCatSlug(), 'share'));
     }
     if (!$message) {
         $message = JText::_('COM_CROWDFUNDING_TRANSACTION_PROCESSED_SUCCESSFULLY');
     }
     // Send response to the browser
     $response->success()->setTitle(JText::_('COM_CROWDFUNDING_SUCCESS'))->setText($message)->setRedirectUrl($redirectUrl);
     echo $response;
     $this->app->close();
 }