/**
  * This method does some validations before the system to provide payment methods.
  *
  * @param string    $context This string gives information about that where it has been executed the trigger.
  * @param stdClass    $item    A project data.
  * @param Prism\Money\Money    $money  An Money object.
  * @param Joomla\Registry\Registry $params  The parameters of the component
  *
  * @throws \InvalidArgumentException
  *
  * @return null|string
  */
 public function onBeforePaymentAuthorize($context, &$item, &$money, &$params)
 {
     if (strcmp('com_crowdfunding.before.payment.authorize', $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;
     }
     // Get user ID.
     $userId = JFactory::getUser()->get('id');
     $html = array();
     // Display login form
     if (!$userId) {
         $html[] = '<p class="bg-warning p-5">';
         $html[] = '<span class="fa fa-exclamation-triangle"></span>';
         $html[] = JText::_('PLG_CROWDFUNDINGPAYMENT_FRAUD_PREVENTION_ERROR_NOT_REGISTERED');
         $html[] = '</p>';
     }
     // Verifications
     // Get component parameters
     $componentParams = JComponentHelper::getParams('com_crowdfundingfinance');
     /** @var  $componentParams Joomla\Registry\Registry */
     // Verify maximum allowed amount for contribution.
     $allowedContributedAmount = $money->setAmount($componentParams->get('protection_max_contributed_amount'))->parse();
     // Validate maximum allowed amount.
     if ($allowedContributedAmount and $allowedContributedAmount < (double) $item->amount) {
         $html[] = '<p class="bg-warning p-5">';
         $html[] = '<span class="fa fa-exclamation-triangle"></span>';
         $html[] = JText::sprintf('PLG_CROWDFUNDINGPAYMENT_FRAUD_PREVENTION_ERROR_CONTRIBUTION_AMOUNT_S', $money->setAmount($allowedContributedAmount)->formatCurrency());
         $html[] = '</p>';
     }
     // Verify the number of payments per project.
     $allowedPaymentsPerProject = (int) $componentParams->get('protection_payments_per_project');
     if ($allowedPaymentsPerProject > 0) {
         $userStatistics = new Crowdfunding\Statistics\User(JFactory::getDbo(), $userId);
         $paymentsPerProject = (int) $userStatistics->getNumberOfPayments($item->id);
         // Validate number of payments per project.
         if ($paymentsPerProject >= $allowedPaymentsPerProject) {
             $html[] = '<p class="bg-warning p-5">';
             $html[] = '<span class="fa fa-exclamation-triangle"></span>';
             $html[] = JText::sprintf('PLG_CROWDFUNDINGPAYMENT_FRAUD_PREVENTION_ERROR_PAYMENT_PER_PROJECT_D', $allowedPaymentsPerProject);
             $html[] = '</p>';
         }
     }
     return count($html) > 0 ? implode("\n", $html) : null;
 }
 /**
  * Display an input field for amount.
  *
  * @param float  $value
  * @param Prism\Money\Money $moneyFormatter
  * @param array  $options
  *
  * @throws \InvalidArgumentException
  *
  * @return string
  */
 public static function inputAmount($value, Prism\Money\Money $moneyFormatter, $options)
 {
     $currency = $moneyFormatter->getCurrency();
     $symbol = $currency->getSymbol();
     $currencyCode = $currency->getCode();
     $html = '<div class="input-group">';
     if ($symbol) {
         $html .= '<div class="input-group-addon">' . $symbol . '</div>';
     }
     $name = Joomla\Utilities\ArrayHelper::getValue($options, 'name');
     $id = '';
     if (Joomla\Utilities\ArrayHelper::getValue($options, 'id')) {
         $id = 'id="' . Joomla\Utilities\ArrayHelper::getValue($options, 'id') . '"';
     }
     $class = 'class="form-control ';
     if (Joomla\Utilities\ArrayHelper::getValue($options, 'class')) {
         $class .= Joomla\Utilities\ArrayHelper::getValue($options, 'class');
     }
     $class .= '"';
     if (!$value or !is_numeric($value)) {
         $value = 0.0;
     }
     $html .= '<input type="text" name="' . $name . '" value="' . $moneyFormatter->setAmount($value)->format() . '" ' . $id . ' ' . $class . ' />';
     if ($currencyCode) {
         $html .= '<div class="input-group-addon">' . $currencyCode . '</div>';
     }
     $html .= '</div>';
     return $html;
 }
 protected function preparePayment($paymentSession)
 {
     // If missing the flag "step1", redirect to first step.
     if (!$paymentSession->step1) {
         $this->returnToStep1($paymentSession, JText::_('COM_CROWDFUNDING_ERROR_INVALID_AMOUNT'));
     }
     // Check for both user states. The user must have only one state - registered user or anonymous user.
     $userId = (int) JFactory::getUser()->get('id');
     $aUserId = $this->app->getUserState('auser_id');
     if ($userId > 0 and strlen($aUserId) > 0 or $userId === 0 and !$aUserId) {
         // Reset anonymous hash user ID and redirect to first step.
         $this->app->setUserState('auser_id', '');
         $this->returnToStep1($paymentSession);
     }
     if (!$this->item->days_left) {
         $this->returnToStep1($paymentSession, JText::_('COM_CROWDFUNDING_ERROR_PROJECT_COMPLETED'));
     }
     // Validate reward
     $this->reward = null;
     $keys = array('id' => $paymentSession->rewardId, 'project_id' => $this->item->id);
     $this->reward = new Crowdfunding\Reward(JFactory::getDbo());
     $this->reward->load($keys);
     if ($this->reward->getId() and ($this->reward->isLimited() and !$this->reward->getAvailable())) {
         $this->returnToStep1($paymentSession, JText::_('COM_CROWDFUNDING_ERROR_REWARD_NOT_AVAILABLE'));
     }
     // Set the amount that will be displayed in the view.
     $this->paymentAmount = $paymentSession->amount;
     // Validate the amount.
     if (!$this->paymentAmount) {
         $this->returnToStep1($paymentSession, JText::_('COM_CROWDFUNDING_ERROR_INVALID_AMOUNT'));
     }
     // Events
     $item = new stdClass();
     $item->id = $this->item->id;
     $item->title = $this->item->title;
     $item->slug = $this->item->slug;
     $item->catslug = $this->item->catslug;
     $item->rewardId = $paymentSession->rewardId;
     $item->amount = $paymentSession->amount;
     $item->currencyCode = $this->currency->getCode();
     $item->amountFormated = $this->money->setAmount($item->amount)->format();
     $item->amountCurrency = $this->money->setAmount($item->amount)->formatCurrency();
     $this->item->event = new stdClass();
     // onBeforePaymentAuthorize
     JPluginHelper::importPlugin('crowdfundingpayment');
     $dispatcher = JEventDispatcher::getInstance();
     $results = (array) $dispatcher->trigger('onBeforePaymentAuthorize', array('com_crowdfunding.before.payment.authorize', &$item, &$this->money, &$this->params));
     if (count($results) > 0) {
         $this->item->event->onBeforePaymentAuthorize = trim(implode("\n", $results));
     } else {
         // onProjectPayment
         $results = $dispatcher->trigger('onProjectPayment', array('com_crowdfunding.payment', &$item, &$this->params));
         $this->item->event->onProjectPayment = trim(implode("\n", $results));
     }
     return $paymentSession;
 }
 /**
  * Calculate the fee that the site owner is going to receive.
  *
  * @param array $data
  * @param Prism\Money\Money $money
  * @param string $title
  *
  * @return string
  */
 public static function ownerMissedAmount($data, $money, $title)
 {
     // Get the data from the aggregated list.
     $canceled = Joomla\Utilities\ArrayHelper::getValue($data, 'canceled', array(), 'array');
     $failed = Joomla\Utilities\ArrayHelper::getValue($data, 'failed', array(), 'array');
     $refunded = Joomla\Utilities\ArrayHelper::getValue($data, 'refunded', array(), 'array');
     $canceledAmount = Joomla\Utilities\ArrayHelper::getValue($canceled, 'amount', 0, 'float');
     $failedAmount = Joomla\Utilities\ArrayHelper::getValue($failed, 'amount', 0, 'float');
     $refundedAmount = Joomla\Utilities\ArrayHelper::getValue($refunded, 'amount', 0, 'float');
     $html[] = $money->setAmount($canceledAmount + $failedAmount + $refundedAmount)->formatCurrency();
     $html[] = '<a class="btn btn-mini hasTooltip" href="javascript:void(0);" title="' . htmlentities($title, ENT_QUOTES, 'UTF-8') . '">';
     $html[] = '<i class="icon-info"></i>';
     $html[] = '</a>';
     return implode("\n", $html);
 }
 /**
  * Generates information about transaction amount.
  *
  * @param stdClass $item
  * @param Prism\Money\Money $money
  * @param Crowdfunding\Currencies $currencies
  *
  * @throws \UnexpectedValueException
  *
  * @return string
  */
 public static function transactionAmount($item, Prism\Money\Money $money, Crowdfunding\Currencies $currencies)
 {
     $item->txn_amount = (double) $item->txn_amount;
     $item->fee = (double) $item->fee;
     $currency = $currencies->getCurrency($item->txn_currency);
     if ($currency !== null) {
         $money->setCurrency($currency);
         $output = $money->setAmount($item->txn_amount)->formatCurrency();
     } else {
         $output = $item->txn_amount;
     }
     if ($item->fee > 0.0) {
         $fee = $currency !== null ? $money->setAmount($item->fee)->formatCurrency() : $item->fee;
         // Prepare project owner amount.
         $projectOwnerAmount = round($item->txn_amount - $item->fee, 2);
         $projectOwnerAmount = $currency !== null ? $money->setAmount($projectOwnerAmount)->formatCurrency() : $projectOwnerAmount;
         $title = JText::sprintf('COM_CROWDFUNDING_TRANSACTION_AMOUNT_FEE', $projectOwnerAmount, $fee);
         $output .= '<a class="btn btn-micro hasTooltip" href="javascript:void(0);" title="' . addslashes($title) . '">';
         $output .= '<i class="icon-question"></i>';
         $output .= '</a>';
     }
     return $output;
 }