/** * This method processes transaction data that comes from PayPal instant notifier. * * @param string $context * @param Joomla\Registry\Registry $params The parameters of the component * * @return null|object */ public function onPaymenNotify($context, &$params) { if (strcmp("com_virtualcurrency.notify.paypal", $context) != 0) { return null; } $app = JFactory::getApplication(); /** @var $app JApplicationSite */ if ($app->isAdmin()) { return null; } $doc = JFactory::getDocument(); /** @var $doc JDocumentRaw */ // 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("currency" => null, "transaction" => null, "payment_service" => "PayPal"); if ($paypalIpn->isVerified()) { // Get currency jimport("virtualcurrency.realcurrency"); $realCurrencyId = $params->get("payments_currency_id"); $realCurrency = VirtualCurrencyRealCurrency::getInstance(JFactory::getDbo(), $realCurrencyId); // Get intention data $paymentId = JArrayHelper::getValue($custom, "payment_id", 0, "int"); jimport("virtualcurrency.payment.session"); $paymentSession = new VirtualCurrencyPaymentSession(JFactory::getDbo()); $paymentSession->load($paymentId); // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_PAYMENT_SESSION"), $this->debugType, $paymentSession->getProperties()) : null; // Validate transaction data $validData = $this->validateData($_POST, $realCurrency->getAbbr(), $paymentSession, $params); 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("virtualcurrency.currency"); $currencyId = JArrayHelper::getValue($validData, "currency_id"); $currency = VirtualCurrencyCurrency::getInstance(JFactory::getDbo(), $currencyId); // DEBUG DATA JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_CURRENCY_OBJECT"), $this->debugType, $currency->getProperties()) : null; // Check for valid project if (!$currency->getId()) { // Log data in the database $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_CURRENCY"), $this->debugType, $validData); return $result; } // Save transaction data. // If it is not completed, return empty results. // If it is complete, continue with process transaction data if (!$this->storeTransaction($validData, $currency)) { return $result; } // Prepare the data that will be returned $result["transaction"] = JArrayHelper::toObject($validData); // Generate object of data based on the project properties $properties = $currency->getProperties(); $result["currency"] = 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->removePaymentSession($paymentSession, $txnStatus); unset($paymentSession); } 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; }
/** * Process step 1. */ public function step1() { // Check for request forgeries. JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN')); $userId = JFactory::getUser()->get("id"); if (!$userId) { $redirectOptions = array("force_direction" => "login_form"); $this->displayNotice(JText::_('COM_VIRTUALCURRENCY_ERROR_NOT_LOG_IN'), $redirectOptions); return; } $app = JFactory::getApplication(); /** @var $app JApplicationSite */ // Get the data from the form $itemId = $this->input->post->getInt('id', 0); $redirectOptionsError = array("view" => "peyment"); // Check for maintenance (debug) state $params = JComponentHelper::getParams($this->option); /** @var $params Joomla\Registry\Registry */ if ($this->inDebugMode($params)) { return; } // Check terms and use if ($params->get("ordering_service_terms", 0)) { $terms = $app->input->post->get("terms", 0); if (!$terms) { $this->displayNotice(JText::_("COM_VIRTUALCURRENCY_ERROR_TERMS_NOT_ACCEPTED"), $redirectOptionsError); return; } } // Check for valid number of units. $amount = $app->input->post->get("amount", 0, "float"); if (!$amount) { $this->displayNotice(JText::_('COM_VIRTUALCURRENCY_ERROR_INVALID_AMOUNT'), $redirectOptionsError); return; } // Check for valid item $item = new VirtualCurrencyCurrency(JFactory::getDbo()); $item->load($itemId); if (!$item->getId()) { $this->displayNotice(JText::_('COM_VIRTUALCURRENCY_ERROR_INVALID_CURRENCY'), $redirectOptionsError); return; } // Check for valid allowed items for buying if ($amount < $item->getParam("minimum")) { $this->displayNotice(JText::_('COM_VIRTUALCURRENCY_ERROR_INVALID_AMOUNT'), $redirectOptionsError); return; } $paymentProcessData = $app->getUserState("payment.data"); $paymentProcessData["item_id"] = $item->getId(); $paymentProcessData["amount"] = $amount; $paymentProcessData["step1"] = true; // Store data to temporary table $data = array("user_id" => $userId, "currency_id" => $item->getId(), "amount" => $amount); jimport("virtualcurrency.payment.session"); $paymentSession = new VirtualCurrencyPaymentSession(JFactory::getDbo()); if (!empty($paymentProcessData["payment_id"])) { $paymentSession->load($paymentProcessData["payment_id"]); } $paymentSession->bind($data); $paymentSession->store(); // Remove old payment session records $paymentSession->cleanOld(); $paymentProcessData["payment_id"] = $paymentSession->getId(); // Set payment data to the sessions $app->setUserState("payment.data", $paymentProcessData); // Redirect to next page $redirectOptions = array("view" => "payment", "layout" => "services"); $link = $this->prepareRedirectLink($redirectOptions); $this->setRedirect(JRoute::_($link, false)); }