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 = JFactory::getUser()->get("id");
     $aUserId = $this->app->getUserState("auser_id");
     if (!empty($userId) and !empty($aUserId) or empty($userId) and empty($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()) {
         if ($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->amount->setValue($item->amount)->format();
     $item->amountCurrency = $this->amount->setValue($item->amount)->formatCurrency();
     $this->item->event = new stdClass();
     // onBeforePaymentAuthorize
     JPluginHelper::importPlugin('crowdfundingpayment');
     $dispatcher = JEventDispatcher::getInstance();
     $results = $dispatcher->trigger('onBeforePaymentAuthorize', array('com_crowdfunding.before.payment.authorize', &$item, &$this->amount, &$this->params));
     if (!empty($results)) {
         $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));
     }
 }