/**
  * Method to change state of reward.
  *
  * @throws Exception
  * @return  void
  */
 public function changeState()
 {
     // Check for request forgeries.
     JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
     $userId = JFactory::getUser()->get('id');
     if (!$userId) {
         $redirectOptions = array('force_direction' => JRoute::_('index.php?option=com_users&view=login', false));
         $this->displayNotice(JText::_('COM_CROWDFUNDING_ERROR_NOT_LOG_IN'), $redirectOptions);
         return;
     }
     $redirect = base64_decode($this->input->get('redirect'));
     $redirectOptions = array('force_direction' => JRoute::_($redirect, false));
     $txnId = $this->input->get->getInt('txn_id');
     $state = $this->input->get->getInt('state');
     $state = !$state ? 0 : 1;
     if (!$txnId) {
         $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_INVALID_TRANSACTION'), $redirectOptions);
         return;
     }
     $keys = array('id' => $txnId, 'receiver_id' => $userId);
     /** @var $transaction Crowdfunding\Transaction */
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->load($keys);
     if (!$transaction->getId()) {
         $this->displayWarning(JText::_('COM_CROWDFUNDING_ERROR_INVALID_TRANSACTION'), $redirectOptions);
         return;
     }
     try {
         $transaction->updateRewardState($state);
     } catch (Exception $e) {
         JLog::add($e->getMessage(), JLog::ERROR, 'com_crowdfunding');
         throw new Exception(JText::_('COM_CROWDFUNDING_ERROR_SYSTEM'));
     }
     if (!$state) {
         $msg = JText::_('COM_CROWDFUNDING_REWARD_HAS_BEEN_SET_AS_NOT_SENT');
     } else {
         $msg = JText::_('COM_CROWDFUNDING_REWARD_HAS_BEEN_SET_AS_SENT');
     }
     $this->displayMessage($msg, $redirectOptions);
 }
 /**
  * Create and return transaction object.
  *
  * @param array $data
  *
  * @return Crowdfunding\Transaction
  */
 protected function getTransaction($data)
 {
     $keys = array('txn_id' => Joomla\Utilities\ArrayHelper::getValue($data, 'txn_id'));
     // Prepare keys used for getting transaction from DB.
     if (array_key_exists('parent_txn_id', $data)) {
         $keys = array('txn_id' => Joomla\Utilities\ArrayHelper::getValue($data, 'parent_txn_id'));
     }
     // Get transaction by ID
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->load($keys);
     return $transaction;
 }
Exemple #3
0
 /**
  * Save transaction data.
  *
  * @param array     $transactionData
  * @param Crowdfunding\Project  $project
  *
  * @return null|array
  */
 protected function storeTransaction($transactionData, $project)
 {
     // Get transaction by txn ID
     $keys = array('txn_id' => Joomla\Utilities\ArrayHelper::getValue($transactionData, 'txn_id'));
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->load($keys);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_TRANSACTION_OBJECT'), $this->debugType, $transaction->getProperties()) : null;
     // Check for existed transaction
     // If the current status if completed, stop the payment process.
     if ($transaction->getId() and $transaction->isCompleted()) {
         return null;
     }
     // Add extra data.
     if (array_key_exists('extra_data', $transactionData)) {
         if (!empty($transactionData['extra_data'])) {
             $transaction->addExtraData($transactionData['extra_data']);
         }
         unset($transactionData['extra_data']);
     }
     // Store the new transaction data.
     $transaction->bind($transactionData);
     $transaction->store();
     // If it is not completed (it might be pending or other status),
     // stop the process. Only completed transaction will continue
     // and will process the project, rewards,...
     if (!$transaction->isCompleted()) {
         return null;
     }
     // Set transaction ID.
     $transactionData['id'] = $transaction->getId();
     // If the new transaction is completed,
     // update project funded amount.
     $amount = Joomla\Utilities\ArrayHelper::getValue($transactionData, 'txn_amount');
     $project->addFunds($amount);
     $project->storeFunds();
     return $transactionData;
 }
Exemple #4
0
 /**
  * Create and return transaction object.
  *
  * @param array $data
  *
  * @return Crowdfunding\Transaction
  */
 protected function getTransaction($data)
 {
     // Prepare keys used for getting transaction from DB.
     if (isset($data["parent_txn_id"])) {
         $keys = array("txn_id" => Joomla\Utilities\ArrayHelper::getValue($data, "parent_txn_id"));
     } else {
         $keys = array("txn_id" => Joomla\Utilities\ArrayHelper::getValue($data, "txn_id"));
     }
     // Get transaction by ID
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->load($keys);
     return $transaction;
 }
Exemple #5
0
 /**
  * Create and return transaction object.
  *
  * @param array $data
  *
  * @return Crowdfunding\Transaction
  */
 protected function getTransaction($data)
 {
     $transactionType = Joomla\Utilities\ArrayHelper::getValue($data, "transaction_type");
     // Prepare keys used for getting transaction from DB.
     if (strcmp($transactionType, "Adaptive Payment PREAPPROVAL") == 0) {
         $keys["txn_id"] = Joomla\Utilities\ArrayHelper::getValue($data, "preapproval_key");
     } elseif (strcmp($transactionType, "Adaptive Payment PAY") == 0) {
         $keys["txn_id"] = Joomla\Utilities\ArrayHelper::getValue($data, "preapproval_key");
     } else {
         $keys = array();
     }
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_TRANSACTION_KEYS"), $this->debugType, $keys) : null;
     // Get transaction by ID
     $transaction = new Crowdfunding\Transaction(JFactory::getDbo());
     $transaction->load($keys);
     return $transaction;
 }
Exemple #6
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;
 }