Ejemplo n.º 1
0
 /**
  * This method returns intention
  * based on user ID or anonymous hash user ID.
  *
  * @param array  $options   The keys used to load data of the intention from database.
  *
  * @throws UnexpectedValueException
  *
  * @return CrowdFundingIntention
  */
 public function getIntention(array $options)
 {
     $userId = JArrayHelper::getValue($options, "user_id");
     $aUserId = JArrayHelper::getValue($options, "auser_id");
     $projectId = JArrayHelper::getValue($options, "project_id");
     $token = JArrayHelper::getValue($options, "token");
     $txnId = JArrayHelper::getValue($options, "txn_id");
     // Prepare keys for anonymous user.
     if (!empty($aUserId)) {
         $intentionKeys = array("auser_id" => $aUserId, "project_id" => $projectId);
     } elseif (!empty($userId)) {
         // Prepare keys for registered user.
         $intentionKeys = array("user_id" => $userId, "project_id" => $projectId);
     } elseif (!empty($token)) {
         // Prepare keys for token.
         $intentionKeys = array("token" => $token);
     } elseif (!empty($txnId)) {
         // Prepare keys for transaction ID.
         $intentionKeys = array("txn_id" => $txnId);
     } else {
         throw new UnexpectedValueException(JText::_("LIB_CROWDFUNDING_INVALID_INTENTION_KEYS"));
     }
     jimport("crowdfunding.intention");
     $intention = new CrowdFundingIntention(JFactory::getDbo());
     $intention->load($intentionKeys);
     return $intention;
 }
Ejemplo n.º 2
0
 public function process()
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     // Check for request forgeries.
     $requestMethod = $app->input->getMethod();
     if (strcmp("POST", $requestMethod) == 0) {
         JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
     } else {
         JSession::checkToken("get") or jexit(JText::_('JINVALID_TOKEN'));
     }
     // Get params
     $params = JComponentHelper::getParams("com_crowdfunding");
     /** @var  $params Joomla\Registry\Registry */
     // Get the data from the form
     $itemId = $this->input->getInt('id', 0);
     $rewardId = $this->input->getInt('rid', 0);
     // Get amount
     $amount = $this->input->get("amount", 0, "float");
     // Get user ID
     $user = JFactory::getUser();
     $userId = (int) $user->get("id");
     // Anonymous user ID
     $aUserId = "";
     $model = $this->getModel();
     /** @var $model CrowdFundingModelBacking */
     // Get the item
     $item = $model->getItem($itemId);
     $returnUrl = CrowdFundingHelperRoute::getBackingRoute($item->slug, $item->catslug);
     // Authorise the user
     if (!$user->authorise("crowdfunding.donate", "com_crowdfunding")) {
         $this->setRedirect(JRoute::_($returnUrl, false), JText::_('COM_CROWDFUNDING_ERROR_NO_PERMISSIONS'), "notice");
         return;
     }
     // Check for valid project
     if (empty($item->id)) {
         $this->setRedirect(JRoute::_(CrowdFundingHelperRoute::getDiscoverRoute()), JText::_('COM_CROWDFUNDING_ERROR_INVALID_PROJECT'), "notice");
         return;
     }
     // Check for maintenance (debug) state
     if ($params->get("debug_payment_disabled", 0)) {
         $msg = JString::trim($params->get("debug_disabled_functionality_msg"));
         if (!$msg) {
             $msg = JText::_("COM_CROWDFUNDING_DEBUG_MODE_DEFAULT_MSG");
         }
         $this->setRedirect(JRoute::_($returnUrl, false), $msg, "notice");
         return;
     }
     // Check for agreed conditions from the user
     if ($params->get("backing_terms", 0)) {
         $terms = $this->input->get("terms", 0, "int");
         if (!$terms) {
             $this->setRedirect(JRoute::_($returnUrl, false), JText::_("COM_CROWDFUNDING_ERROR_TERMS_NOT_ACCEPTED"), "notice");
             return;
         }
     }
     // Check for valid amount
     if (!$amount) {
         $this->setRedirect(JRoute::_($returnUrl, false), JText::_("COM_CROWDFUNDING_ERROR_INVALID_AMOUNT"), "notice");
         return;
     }
     // Store payment process data
     // Get the payment process object and
     // store the selected data from the user.
     $paymentSessionContext = CrowdFundingConstants::PAYMENT_SESSION_CONTEXT . $item->id;
     $paymentSession = $app->getUserState($paymentSessionContext);
     $paymentSession->step1 = true;
     $paymentSession->amount = $amount;
     $paymentSession->rewardId = $rewardId;
     $app->setUserState($paymentSessionContext, $paymentSession);
     // Create an intention.
     // Generate hash user ID used for anonymous payment.
     if (!$userId) {
         $aUserId = $app->getUserState("auser_id");
         if (!$aUserId) {
             // Generate a hash ID for anonymous user.
             jimport("itprism.string");
             $anonymousUserId = new ITPrismString();
             $anonymousUserId->generateRandomString(32);
             $aUserId = (string) $anonymousUserId;
             $app->setUserState("auser_id", $aUserId);
         }
         $intentionKeys = array("auser_id" => $aUserId, "project_id" => $item->id);
     } else {
         $intentionKeys = array("user_id" => $userId, "project_id" => $item->id);
     }
     jimport("crowdfunding.intention");
     $intention = new CrowdFundingIntention(JFactory::getDbo());
     $intention->load($intentionKeys);
     $date = new JDate();
     $custom = array("user_id" => $userId, "auser_id" => $aUserId, "project_id" => $item->id, "reward_id" => $rewardId, "record_date" => $date->toSql(), "session_id" => $paymentSession->session_id);
     $intention->bind($custom);
     $intention->store();
     // Redirect to next page
     $link = CrowdFundingHelperRoute::getBackingRoute($item->slug, $item->catslug, "payment");
     $this->setRedirect(JRoute::_($link, false));
 }
Ejemplo n.º 3
0
 /**
  * This method processes transaction data that comes from PayPal instant notifier.
  *
  * @param string    $context This string gives information about that where it has been executed the trigger.
  * @param Joomla\Registry\Registry $params  The parameters of the component
  *
  * @return null|array
  */
 public function onPaymenNotify($context, &$params)
 {
     if (strcmp("com_crowdfunding.notify.paypal", $context) != 0) {
         return null;
     }
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     if ($app->isAdmin()) {
         return null;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp("raw", $docType) != 0) {
         return null;
     }
     // Validate request method
     $requestMethod = $app->input->getMethod();
     if (strcmp("POST", $requestMethod) != 0) {
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_REQUEST_METHOD"), $this->debugType, JText::sprintf($this->textPrefix . "_ERROR_INVALID_TRANSACTION_REQUEST_METHOD", $requestMethod));
         return null;
     }
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESPONSE"), $this->debugType, $_POST) : null;
     // Decode custom data
     $custom = JArrayHelper::getValue($_POST, "custom");
     $custom = json_decode(base64_decode($custom), true);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_CUSTOM"), $this->debugType, $custom) : null;
     // Verify gateway. Is it PayPal?
     if (!$this->isPayPalGateway($custom)) {
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_PAYMENT_GATEWAY"), $this->debugType, array("custom" => $custom, "_POST" => $_POST));
         return null;
     }
     // Get PayPal URL
     $sandbox = $this->params->get('paypal_sandbox', 0);
     if (!$sandbox) {
         $url = JString::trim($this->params->get('paypal_url', "https://www.paypal.com/cgi-bin/webscr"));
     } else {
         $url = JString::trim($this->params->get('paypal_sandbox_url', "https://www.sandbox.paypal.com/cgi-bin/webscr"));
     }
     jimport("itprism.payment.paypal.ipn");
     $paypalIpn = new ITPrismPayPalIpn($url, $_POST);
     $loadCertificate = (bool) $this->params->get("paypal_load_certificate", 0);
     $paypalIpn->verify($loadCertificate);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_VERIFY_OBJECT"), $this->debugType, $paypalIpn) : null;
     // Prepare the array that will be returned by this method
     $result = array("project" => null, "reward" => null, "transaction" => null, "payment_service" => "PayPal");
     if ($paypalIpn->isVerified()) {
         // Get currency
         jimport("crowdfunding.currency");
         $currencyId = $params->get("project_currency");
         $currency = CrowdFundingCurrency::getInstance(JFactory::getDbo(), $currencyId);
         // Get intention data
         $intentionId = JArrayHelper::getValue($custom, "intention_id", 0, "int");
         jimport("crowdfunding.intention");
         $intention = new CrowdFundingIntention(JFactory::getDbo());
         $intention->load($intentionId);
         // Get payment session as intention.
         if (!$intention->getId()) {
             jimport("crowdfunding.payment.session");
             $keys = array("intention_id" => $intentionId);
             $intention = new CrowdFundingPaymentSession(JFactory::getDbo());
             $intention->load($keys);
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_INTENTION"), $this->debugType, $intention->getProperties()) : null;
         // Validate transaction data
         $validData = $this->validateData($_POST, $currency->getAbbr(), $intention);
         if (is_null($validData)) {
             return $result;
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_VALID_DATA"), $this->debugType, $validData) : null;
         // Get project.
         jimport("crowdfunding.project");
         $projectId = JArrayHelper::getValue($validData, "project_id");
         $project = CrowdFundingProject::getInstance(JFactory::getDbo(), $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, $validData);
             return $result;
         }
         // Set the receiver of funds
         $validData["receiver_id"] = $project->getUserId();
         // Save transaction data.
         // If it is not completed, return empty results.
         // If it is complete, continue with process transaction data
         $transactionData = $this->storeTransaction($validData, $project);
         if (is_null($transactionData)) {
             return $result;
         }
         // Update the number of distributed reward.
         $rewardId = JArrayHelper::getValue($transactionData, "reward_id");
         $reward = null;
         if (!empty($rewardId)) {
             $reward = $this->updateReward($transactionData);
             // Validate the reward.
             if (!$reward) {
                 $transactionData["reward_id"] = 0;
             }
         }
         // Generate object of data, based on the transaction properties.
         $result["transaction"] = JArrayHelper::toObject($transactionData);
         // Generate object of data based on the project properties
         $properties = $project->getProperties();
         $result["project"] = JArrayHelper::toObject($properties);
         // Generate object of data based on the reward properties
         if (!empty($reward)) {
             $properties = $reward->getProperties();
             $result["reward"] = JArrayHelper::toObject($properties);
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESULT_DATA"), $this->debugType, $result) : null;
         // Remove intention
         $txnStatus = isset($result["transaction"]->txn_status) ? $result["transaction"]->txn_status : null;
         $this->removeIntention($intention, $txnStatus);
         unset($intention);
     } else {
         // Log error
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_TRANSACTION_DATA"), $this->debugType, array("error message" => $paypalIpn->getError(), "paypalVerify" => $paypalIpn, "_POST" => $_POST));
     }
     return $result;
 }