Exemple #1
0
 /**
  * @param int $projectId
  * @param Joomla\Registry\Registry $params
  * @param object $paymentSession
  *
  * @return stdClass
  * @throws UnexpectedValueException
  */
 public function prepareItem($projectId, $params, $paymentSession)
 {
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($projectId);
     if (!$project->getId()) {
         throw new UnexpectedValueException(JText::_("COM_CROWDFUNDING_ERROR_INVALID_PROJECT"));
     }
     if ($project->isCompleted()) {
         throw new UnexpectedValueException(JText::_("COM_CROWDFUNDING_ERROR_COMPLETED_PROJECT"));
     }
     // Get currency
     $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $params->get("project_currency"));
     // Create amount object.
     $amount = new Crowdfunding\Amount($params);
     $amount->setCurrency($currency);
     $item = new stdClass();
     $item->id = $project->getId();
     $item->title = $project->getTitle();
     $item->slug = $project->getSlug();
     $item->catslug = $project->getCatSlug();
     $item->rewardId = $paymentSession->rewardId;
     $item->starting_date = $project->getFundingStart();
     $item->ending_date = $project->getFundingEnd();
     $item->amount = $paymentSession->amount;
     $item->currencyCode = $currency->getCode();
     $item->amountFormated = $amount->setValue($item->amount)->format();
     $item->amountCurrency = $amount->setValue($item->amount)->formatCurrency();
     return $item;
 }
 /**
  * Get the data that is going to be passed to the layout
  *
  * @return  array
  */
 public function getLayoutData()
 {
     // Get the basic field data
     $data = parent::getLayoutData();
     // Load the current username if available.
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $projectTitle = '';
     if (is_numeric($this->value)) {
         $project->load($this->value);
         $projectTitle = $project->get('title');
     }
     $extraData = array('projectTitle' => $projectTitle);
     return array_merge($data, $extraData);
 }
Exemple #3
0
 public function save($key = null, $urlVar = null)
 {
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     $data = $this->input->post->get('jform', array(), 'array');
     $itemId = Joomla\Utilities\ArrayHelper::getValue($data, "id", 0, "int");
     $projectId = Joomla\Utilities\ArrayHelper::getValue($data, "project_id", 0, "int");
     $partnerId = Joomla\Utilities\ArrayHelper::getValue($data, "partner_id", 0, "int");
     $redirectOptions = array("task" => $this->getTask(), "id" => $itemId);
     $model = $this->getModel();
     /** @var $model CrowdfundingPartnersModelPartner */
     $form = $model->getForm($data, false);
     /** @var $form JForm */
     if (!$form) {
         throw new Exception(JText::_("COM_CROWDFUNDINGPARTNERS_ERROR_FORM_CANNOT_BE_LOADED"));
     }
     // Validate the form data
     $validData = $model->validate($form, $data);
     // Check for errors
     if ($validData === false) {
         $this->displayNotice($form->getErrors(), $redirectOptions);
         return;
     }
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($projectId);
     if (!$project->getId()) {
         $this->displayError(JText::_('COM_CROWDFUNDINGPARTNERS_ERROR_INVALID_PROJECT'), $redirectOptions);
         return;
     }
     if ($partnerId == $project->getUserId()) {
         $this->displayError(JText::_('COM_CROWDFUNDINGPARTNERS_ERROR_CANNOT_ASSIGN_PARTNER'), $redirectOptions);
         return;
     }
     try {
         $itemId = $model->save($validData);
         $redirectOptions["id"] = $itemId;
     } catch (Exception $e) {
         JLog::add($e->getMessage());
         throw new Exception(JText::_('COM_CROWDFUNDINGPARTNERS_ERROR_SYSTEM'));
     }
     $this->displayMessage(JText::_('COM_CROWDFUNDINGPARTNERS_PARTNER_SAVED'), $redirectOptions);
 }
Exemple #4
0
 /**
  * Method to delete one or more records.
  *
  * @param   array &$pks An array of record primary keys.
  *
  * @return  boolean  True if successful, false if an error occurs.
  *
  * @since   12.2
  */
 public function delete(&$pks)
 {
     $params = JComponentHelper::getParams($this->option);
     /** @var  $params Joomla\Registry\Registry */
     $folderImages = $params->get("images_directory", "images/crowdfunding");
     foreach ($pks as $id) {
         $project = new Crowdfunding\Project(JFactory::getDbo());
         $project->load($id);
         $this->deleteProjectImages($project, $folderImages);
         $this->deleteAdditionalImages($project, $folderImages);
         $this->removeIntentions($project);
         $this->removeComments($project);
         $this->removeUpdates($project);
         $this->removeRewards($project);
         $this->removeTransactions($project);
     }
     return parent::delete($pks);
 }
Exemple #5
0
 public function save($key = null, $urlVar = null)
 {
     JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     $response = new Prism\Response\Json();
     $data = $this->input->post->get('jform', array(), 'array');
     $itemId = Joomla\Utilities\ArrayHelper::getValue($data, "project_id", 0, "int");
     $terms = $this->input->post->getInt("terms", 0);
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($itemId);
     $returnUrl = CrowdfundingHelperRoute::getBackingRoute($project->getSlug(), $project->getCatSlug());
     if (!$project->getId()) {
         // Send response to the browser
         $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_FAIL"))->setText(JText::_("COM_CROWDFUNDINGDATA_INVALID_ITEM"))->setRedirectUrl($returnUrl)->failure();
         echo $response;
         JFactory::getApplication()->close();
     }
     $model = $this->getModel();
     /** @var $model CrowdfundingDataModelRecord */
     $form = $model->getForm($data, false);
     /** @var $form JForm */
     if (!$form) {
         throw new Exception(JText::_("COM_CROWDFUNDINGDATA_ERROR_FORM_CANNOT_BE_LOADED"), 500);
     }
     // Validate the form data
     $validData = $model->validate($form, $data);
     // Check for errors
     if ($validData === false) {
         $errors_ = $form->getErrors();
         $errors = array();
         /** @var $error RuntimeException */
         foreach ($errors_ as $error) {
             $errors[] = $error->getMessage();
         }
         // Send response to the browser
         $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_FAIL"))->setText(implode("\n", $errors))->setRedirectUrl($returnUrl)->failure();
         echo $response;
         JFactory::getApplication()->close();
     }
     // Get the payment session object and session ID.
     $paymentSessionContext = Crowdfunding\Constants::PAYMENT_SESSION_CONTEXT . $project->getId();
     $paymentSession = $app->getUserState($paymentSessionContext);
     try {
         $validData["session_id"] = $paymentSession->session_id;
         $model->save($validData);
     } catch (Exception $e) {
         $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_FAIL"))->setText(JText::_('COM_CROWDFUNDINGDATA_ERROR_SYSTEM'))->setRedirectUrl($returnUrl)->failure();
         echo $response;
         JFactory::getApplication()->close();
     }
     $componentParams = JComponentHelper::getParams("com_crowdfunding");
     /** @var  $componentParams Joomla\Registry\Registry */
     $processUrl = JUri::base() . "index.php?option=com_crowdfunding&task=backing.process&id=" . (int) $project->getId() . "&rid=" . (int) $paymentSession->rewardId . "&amount=" . rawurldecode($paymentSession->amount) . "&" . JSession::getFormToken() . "=1";
     // Set the value of terms of use condition.
     if ($componentParams->get("backing_terms", 0) and !empty($terms)) {
         $processUrl .= "&terms=1";
     }
     $filter = JFilterInput::getInstance();
     $processUrl = $filter->clean($processUrl);
     $response->setTitle(JText::_("COM_CROWDFUNDINGDATA_SUCCESS"))->setText(JText::_("COM_CROWDFUNDINGDATA_DATA_SAVED_SUCCESSFULLY"))->setRedirectUrl($processUrl)->success();
     echo $response;
     JFactory::getApplication()->close();
 }
 protected function sendReportMail($report, $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');
     // Get project
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($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) {
         JLog::add($this->errorPrefix . $mailer->ErrorInfo, JLog::WARNING, 'com_crowdfunding');
         return false;
     }
     return true;
 }
Exemple #7
0
 /**
  * Capture payments.
  *
  * @param string                   $context
  * @param object                   $item
  * @param Joomla\Registry\Registry $params
  *
  * @return array|null
  */
 public function onPaymentsCapture($context, &$item, &$params)
 {
     $allowedContext = array("com_crowdfunding.payments.capture.paypal", "com_crowdfundingfinance.payments.capture.paypal");
     if (!in_array($context, $allowedContext)) {
         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;
     }
     // Load project object and set "memo".
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($item->project_id);
     $fundingType = $project->getFundingType();
     $fees = $this->getFees($fundingType);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_FEES"), $this->debugType, $fees) : null;
     // Create transport object.
     $options = new Joomla\Registry\Registry();
     /** @var  $options Joomla\Registry\Registry */
     $transport = new JHttpTransportCurl($options);
     $http = new JHttp($options, $transport);
     // Create payment object.
     $options = new Joomla\Registry\Registry();
     /** @var  $options Joomla\Registry\Registry */
     $notifyUrl = $this->getCallbackUrl();
     $cancelUrl = $this->getCancelUrl($project->getSlug(), $project->getCatSlug());
     $returnUrl = $this->getReturnUrl($project->getSlug(), $project->getCatSlug());
     $options->set("urls.notify", $notifyUrl);
     $options->set("urls.cancel", $cancelUrl);
     $options->set("urls.return", $returnUrl);
     $this->prepareCredentials($options);
     $options->set("payment.action_type", "PAY");
     $options->set("payment.preapproval_key", $item->txn_id);
     $options->set("payment.fees_payer", $this->params->get("paypal_fees_payer"));
     $options->set("payment.currency_code", $item->txn_currency);
     $options->set("request.envelope", $this->envelope);
     $title = JText::sprintf($this->textPrefix . "_INVESTING_IN_S", htmlentities($project->getTitle(), ENT_QUOTES, "UTF-8"));
     $options->set("payment.memo", $title);
     // Get API url.
     $apiUrl = $this->getApiUrl();
     try {
         // Calculate the fee.
         $fee = $this->calculateFee($fundingType, $fees, $item->txn_amount);
         // Get receiver list and set it to service options.
         $receiverList = $this->getReceiverList($item, $fee, $fundingType);
         $options->set("payment.receiver_list", $receiverList);
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_DOCAPTURE_OPTIONS"), $this->debugType, $options) : null;
         $adaptive = new Prism\Payment\PayPal\Adaptive($apiUrl, $options);
         $adaptive->setTransport($http);
         $response = $adaptive->doCapture();
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_DOCAPTURE_RESPONSE"), $this->debugType, $response) : null;
         // Include extra data to transaction record.
         if ($response->isSuccess()) {
             $note = JText::_("PLG_CROWDFUNDINGPAYMENT_PAYPALADAPTIVE_RESPONSE_NOTE_CAPTURE_PREAPPROVAL");
             $extraData = $this->prepareExtraData($response, $note);
             $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
             $transaction->load($item->id);
             $transaction->setFee($fee);
             $transaction->addExtraData($extraData);
             $transaction->store();
             // DEBUG DATA
             JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_DOCAPTURE_TRANSACTION"), $this->debugType, $transaction->getProperties()) : null;
         }
     } catch (Exception $e) {
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_ERROR_DOCAPTURE"), $this->debugType, $e->getMessage()) : null;
         $message = array("text" => JText::sprintf($this->textPrefix . "_CAPTURED_UNSUCCESSFULLY", $item->txn_id), "type" => "error");
         return $message;
     }
     $message = array("text" => JText::sprintf($this->textPrefix . "_CAPTURED_SUCCESSFULLY", $item->txn_id), "type" => "message");
     return $message;
 }
Exemple #8
0
 /**
  * This method performs the transaction.
  *
  * @param string $context
  * @param Joomla\Registry\Registry $params
  *
  * @return null|array
  */
 public function onPaymentNotify($context, &$params)
 {
     if (strcmp("com_crowdfunding.notify.banktransfer", $context) != 0) {
         return null;
     }
     if ($this->app->isAdmin()) {
         return null;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp("raw", $docType) != 0) {
         return null;
     }
     $projectId = $this->app->input->getInt("pid");
     $amount = $this->app->input->getFloat("amount");
     // Prepare the array that will be returned by this method
     $result = array("project" => null, "reward" => null, "transaction" => null, "payment_session" => null, "redirect_url" => null, "message" => null);
     // Get project
     $project = new Crowdfunding\Project(JFactory::getDbo());
     $project->load($projectId);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_PROJECT_OBJECT"), $this->debugType, $project->getProperties()) : null;
     // Check for valid project
     if (!$project->getId()) {
         // Log data in the database
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_PROJECT"), $this->debugType, array("PROJECT OBJECT" => $project->getProperties(), "REQUEST METHOD" => $this->app->input->getMethod(), "_REQUEST" => $_REQUEST));
         return null;
     }
     $currencyId = $params->get("project_currency");
     $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $currencyId, $params);
     // Prepare return URL
     $result["redirect_url"] = Joomla\String\String::trim($this->params->get('return_url'));
     if (!$result["redirect_url"]) {
         $filter = JFilterInput::getInstance();
         $uri = JUri::getInstance();
         $domain = $filter->clean($uri->toString(array("scheme", "host")));
         $result["redirect_url"] = $domain . JRoute::_(CrowdfundingHelperRoute::getBackingRoute($project->getSlug(), $project->getCatslug(), "share"), false);
     }
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RETURN_URL"), $this->debugType, $result["redirect_url"]) : null;
     // Payment Session
     $userId = JFactory::getUser()->get("id");
     $aUserId = $this->app->getUserState("auser_id");
     // Reset anonymous user hash ID,
     // because the payment session based in it will be removed when transaction completes.
     if (!empty($aUserId)) {
         $this->app->setUserState("auser_id", "");
     }
     $paymentSessionContext = Crowdfunding\Constants::PAYMENT_SESSION_CONTEXT . $project->getId();
     $paymentSessionLocal = $this->app->getUserState($paymentSessionContext);
     $paymentSession = $this->getPaymentSession(array("session_id" => $paymentSessionLocal->session_id));
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_PAYMENT_SESSION_OBJECT"), $this->debugType, $paymentSession->getProperties()) : null;
     // Validate payment session record.
     if (!$paymentSession->getId()) {
         // Log data in the database
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_PAYMENT_SESSION"), $this->debugType, $paymentSession->getProperties());
         // Send response to the browser
         $response = array("success" => false, "title" => JText::_($this->textPrefix . "_FAIL"), "text" => JText::_($this->textPrefix . "_ERROR_INVALID_PROJECT"));
         return $response;
     }
     // Validate a reward and update the number of distributed ones.
     // If the user is anonymous, the system will store 0 for reward ID.
     // The anonymous users can't select rewards.
     $rewardId = $paymentSession->isAnonymous() ? 0 : (int) $paymentSession->getRewardId();
     if (!empty($rewardId)) {
         $validData = array("reward_id" => $rewardId, "project_id" => $projectId, "txn_amount" => $amount);
         $reward = $this->updateReward($validData);
         // Validate the reward.
         if (!$reward) {
             $rewardId = 0;
         }
     }
     // Prepare transaction data
     $transactionId = new Prism\String();
     $transactionId->generateRandomString(12, "BT");
     $transactionId = Joomla\String\String::strtoupper($transactionId);
     $transactionData = array("txn_amount" => $amount, "txn_currency" => $currency->getCode(), "txn_status" => "pending", "txn_id" => $transactionId, "project_id" => $projectId, "reward_id" => $rewardId, "investor_id" => (int) $userId, "receiver_id" => (int) $project->getUserId(), "service_provider" => "Bank Transfer");
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_TRANSACTION_DATA"), $this->debugType, $transactionData) : null;
     // Auto complete transaction
     if ($this->params->get("auto_complete", 0)) {
         $transactionData["txn_status"] = "completed";
         $project->addFunds($amount);
         $project->storeFunds();
     }
     // Store transaction data
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->bind($transactionData);
     $transaction->store();
     // Generate object of data, based on the transaction properties.
     $properties = $transaction->getProperties();
     $result["transaction"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     // Generate object of data, based on the project properties.
     $properties = $project->getProperties();
     $result["project"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     // Generate object of data, based on the reward properties.
     if (!empty($reward)) {
         $properties = $reward->getProperties();
         $result["reward"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     }
     // Generate data object, based on the payment session properties.
     $properties = $paymentSession->getProperties();
     $result["payment_session"] = Joomla\Utilities\ArrayHelper::toObject($properties);
     // Set message to the user.
     $result["message"] = JText::sprintf($this->textPrefix . "_TRANSACTION_REGISTERED", $transaction->getTransactionId(), $transaction->getTransactionId());
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESULT_DATA"), $this->debugType, $result) : null;
     // Close payment session and remove payment session record.
     $txnStatus = isset($result["transaction"]->txn_status) ? $result["transaction"]->txn_status : null;
     $this->closePaymentSession($paymentSession, $txnStatus);
     return $result;
 }