Exemplo n.º 1
0
 /**
  * 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;
 }