/**
  * 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
  *
  * @throws \InvalidArgumentException
  * @throws \OutOfBoundsException
  * @throws \RuntimeException
  * @throws \UnexpectedValueException
  *
  * @return null|stdClass
  */
 public function onPaymentNotify($context, $params)
 {
     if (strcmp('com_crowdfunding.notify.' . $this->serviceAlias, $context) !== 0) {
         return null;
     }
     if ($this->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 = $this->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 = ArrayHelper::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?
     $gateway = ArrayHelper::getValue($custom, 'gateway');
     if (!$this->isValidPaymentGateway($gateway)) {
         $this->log->add(JText::_($this->textPrefix . '_ERROR_INVALID_PAYMENT_GATEWAY'), $this->debugType, array('custom' => $custom, '_POST' => $_POST));
         return null;
     }
     // Get PayPal URL
     if ($this->params->get('paypal_sandbox', 1)) {
         $url = trim($this->params->get('paypal_sandbox_url', 'https://www.sandbox.paypal.com/cgi-bin/webscr'));
     } else {
         $url = trim($this->params->get('paypal_url', 'https://www.paypal.com/cgi-bin/webscr'));
     }
     $paypalIpn = new Prism\Payment\PayPal\Ipn($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 have to be returned by this method.
     $paymentResult = new stdClass();
     $paymentResult->project = null;
     $paymentResult->reward = null;
     $paymentResult->transaction = null;
     $paymentResult->paymentSession = null;
     $paymentResult->serviceProvider = $this->serviceProvider;
     $paymentResult->serviceAlias = $this->serviceAlias;
     if ($paypalIpn->isVerified()) {
         $containerHelper = new Crowdfunding\Container\Helper();
         $currency = $containerHelper->fetchCurrency($this->container, $params);
         // Get payment session data
         $paymentSessionId = ArrayHelper::getValue($custom, 'payment_session_id', 0, 'int');
         $paymentSessionRemote = $this->getPaymentSession(array('id' => $paymentSessionId));
         // Check for valid payment session.
         if (!$paymentSessionRemote->getId()) {
             $this->log->add(JText::_($this->textPrefix . '_ERROR_PAYMENT_SESSION'), $this->errorType, $paymentSessionRemote->getProperties());
             return null;
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_PAYMENT_SESSION'), $this->debugType, $paymentSessionRemote->getProperties()) : null;
         // Validate transaction data
         $validData = $this->validateData($_POST, $currency->getCode(), $paymentSessionRemote);
         if ($validData === null) {
             return null;
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_VALID_DATA'), $this->debugType, $validData) : null;
         // Set the receiver ID.
         $project = $containerHelper->fetchProject($this->container, $validData['project_id']);
         $validData['receiver_id'] = $project->getUserId();
         // Get reward object.
         $reward = null;
         if ($validData['reward_id']) {
             $reward = $containerHelper->fetchReward($this->container, $validData['reward_id'], $project->getId());
         }
         // Save transaction data.
         // If it is not completed, return empty results.
         // If it is complete, continue with process transaction data
         $transaction = $this->storeTransaction($validData);
         if ($transaction === null) {
             return null;
         }
         // Generate object of data, based on the transaction properties.
         $paymentResult->transaction = $transaction;
         // Generate object of data based on the project properties.
         $paymentResult->project = $project;
         // Generate object of data based on the reward properties.
         if ($reward !== null and $reward instanceof Crowdfunding\Reward) {
             $paymentResult->reward = $reward;
         }
         // Generate data object, based on the payment session properties.
         $paymentResult->paymentSession = $paymentSessionRemote;
         // Removing intention.
         $this->removeIntention($paymentSessionRemote, $transaction);
     } 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 $paymentResult;
 }
Example #2
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 onPaymentNotify($context, &$params)
 {
     if (strcmp("com_crowdfunding.notify.paypal", $context) != 0) {
         return null;
     }
     if ($this->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 = $this->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 = Joomla\Utilities\ArrayHelper::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
     if ($this->params->get('paypal_sandbox', 1)) {
         $url = Joomla\String\String::trim($this->params->get('paypal_sandbox_url', "https://www.sandbox.paypal.com/cgi-bin/webscr"));
     } else {
         $url = Joomla\String\String::trim($this->params->get('paypal_url', "https://www.paypal.com/cgi-bin/webscr"));
     }
     $paypalIpn = new Prism\Payment\PayPal\Ipn($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 have to be returned by this method.
     $result = array("project" => null, "reward" => null, "transaction" => null, "payment_session" => null, "payment_service" => $this->paymentService);
     if ($paypalIpn->isVerified()) {
         // Get currency
         $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $params->get("project_currency"));
         // Get payment session data
         $paymentSessionId = Joomla\Utilities\ArrayHelper::getValue($custom, "payment_session_id", 0, "int");
         $paymentSession = $this->getPaymentSession(array("id" => $paymentSessionId));
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_PAYMENT_SESSION"), $this->debugType, $paymentSession->getProperties()) : null;
         // Validate transaction data
         $validData = $this->validateData($_POST, $currency->getCode(), $paymentSession);
         if (is_null($validData)) {
             return $result;
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_VALID_DATA"), $this->debugType, $validData) : null;
         // Get project.
         $projectId = Joomla\Utilities\ArrayHelper::getValue($validData, "project_id");
         $project = Crowdfunding\Project::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 = Joomla\Utilities\ArrayHelper::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"] = Joomla\Utilities\ArrayHelper::toObject($transactionData);
         // Generate object of data based on the project properties.
         $properties = $project->getProperties();
         $result["project"] = Joomla\Utilities\ArrayHelper::toObject($properties);
         // Generate object of data based on the reward properties.
         if (!empty($reward)) {
             $properties = $reward->getProperties();
             $result["reward"] = Joomla\Utilities\ArrayHelper::toObject($properties);
         }
         // Generate data object, based on the payment session properties.
         $properties = $paymentSession->getProperties();
         $result["payment_session"] = Joomla\Utilities\ArrayHelper::toObject($properties);
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESULT_DATA"), $this->debugType, $result) : null;
         // Remove payment session.
         $txnStatus = isset($result["transaction"]->txn_status) ? $result["transaction"]->txn_status : null;
         $this->closePaymentSession($paymentSession, $txnStatus);
     } 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;
 }
 /**
  * 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 onPaymentNotify($context, &$params)
 {
     if (strcmp('com_crowdfunding.notify.' . $this->serviceAlias, $context) !== 0) {
         return null;
     }
     if ($this->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 = $this->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 = Joomla\Utilities\ArrayHelper::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;
     // Validate payment services.
     $gateway = Joomla\Utilities\ArrayHelper::getValue($custom, 'gateway');
     if (!$this->isValidPaymentGateway($gateway)) {
         $this->log->add(JText::_($this->textPrefix . '_ERROR_INVALID_PAYMENT_GATEWAY'), $this->debugType, array('custom' => $custom, '_POST' => $_POST));
         return null;
     }
     // Get PayPal URL
     if ($this->params->get('paypal_sandbox', 1)) {
         $url = JString::trim($this->params->get('paypal_sandbox_url', 'https://www.sandbox.paypal.com/cgi-bin/webscr'));
     } else {
         $url = JString::trim($this->params->get('paypal_url', 'https://www.paypal.com/cgi-bin/webscr'));
     }
     $paypalIPN = new Prism\Payment\PayPal\Ipn($url, $_POST);
     $loadCertificate = (bool) $this->params->get('paypal_load_certificate', 0);
     $paypalIPN->verify($loadCertificate);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_IPN_OBJECT'), $this->debugType, $paypalIPN) : null;
     // Prepare the array that will be returned by this method
     $result = array('project' => null, 'reward' => null, 'transaction' => null, 'payment_session' => null, 'service_provider' => $this->serviceProvider, 'service_alias' => $this->serviceAlias);
     if ($paypalIPN->isVerified()) {
         // Get currency
         $currencyId = $params->get('project_currency');
         $currency = Crowdfunding\Currency::getInstance(JFactory::getDbo(), $currencyId);
         // Get payment session data
         $paymentSessionId = Joomla\Utilities\ArrayHelper::getValue($custom, 'payment_session_id', 0, 'int');
         $paymentSession = $this->getPaymentSession(array('id' => $paymentSessionId));
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_PAYMENT_SESSION'), $this->debugType, $paymentSession->getProperties()) : null;
         // Validate transaction data
         $validData = $this->validateData($_POST, $currency->getCode(), $paymentSession);
         if ($validData === null) {
             return $result;
         }
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_VALID_DATA'), $this->debugType, $validData) : null;
         // Get project.
         $projectId = Joomla\Utilities\ArrayHelper::getValue($validData, 'project_id');
         $project = Crowdfunding\Project::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 ($transactionData === null) {
             return $result;
         }
         // Update the number of distributed reward.
         $rewardId = Joomla\Utilities\ArrayHelper::getValue($transactionData, 'reward_id', 0, 'int');
         $reward = null;
         if ($rewardId > 0) {
             $reward = $this->updateReward($transactionData);
             // Validate the reward.
             if (!$reward) {
                 $transactionData['reward_id'] = 0;
             }
         }
         //  Prepare the data that will be returned
         $result['transaction'] = Joomla\Utilities\ArrayHelper::toObject($transactionData);
         // Generate object of data based on the project properties
         $properties = $project->getProperties();
         $result['project'] = Joomla\Utilities\ArrayHelper::toObject($properties);
         // Generate object of data based on the reward properties
         if ($reward !== null and $reward instanceof Crowdfunding\Reward) {
             $properties = $reward->getProperties();
             $result['reward'] = Joomla\Utilities\ArrayHelper::toObject($properties);
         }
         // Generate data object, based on the payment session properties.
         $properties = $paymentSession->getProperties();
         $result['payment_session'] = Joomla\Utilities\ArrayHelper::toObject($properties);
         // DEBUG DATA
         JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_RESULT_DATA'), $this->debugType, $result) : null;
         // Remove payment session.
         $txnStatus = isset($result['transaction']->txn_status) ? $result['transaction']->txn_status : null;
         $this->closePaymentSession($paymentSession, $txnStatus);
     } else {
         // Log error
         $this->log->add(JText::_($this->textPrefix . '_ERROR_INVALID_TRANSACTION_DATA'), $this->debugType, array('error message' => $paypalIPN->getError(), 'paypalIPN' => $paypalIPN, '_POST' => $_POST));
     }
     return $result;
 }
Example #4
0
 /**
  * Process PAY notification data from PayPal.
  * This method updates transaction record.
  *
  * @param array $result
  * @param string $url  The parameters of the component
  * @param bool $loadCertificate
  * @param Joomla\Registry\Registry $params  The parameters of the component
  *
  * @return null|array
  */
 protected function processPay(&$result, $url, $loadCertificate, &$params)
 {
     // Get raw post data and parse it.
     $rowPostString = file_get_contents("php://input");
     $string = new Prism\String($rowPostString);
     $rawPost = $string->parseNameValue();
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESPONSE"), $this->debugType, $_POST) : null;
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_RESPONSE_INPUT"), $this->debugType, $rawPost) : null;
     $paypalIpn = new Prism\Payment\PayPal\Ipn($url, $rawPost);
     $paypalIpn->verify($loadCertificate);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_IPN_OBJECT"), $this->debugType, $paypalIpn) : null;
     if ($paypalIpn->isVerified()) {
         // Parse raw post transaction data.
         $rawPostTransaction = $paypalIpn->getTransactionData();
         if (!empty($rawPostTransaction)) {
             $_POST["transaction"] = $this->filterRawPostTransaction($rawPostTransaction);
         }
         JDEBUG ? $this->log->add(JText::_("PLG_CROWDFUNDINGPAYMENT_PAYPALADAPTIVE_DEBUG_FILTERED_RAW_POST"), $this->debugType, $_POST) : null;
         unset($rawPostTransaction);
         unset($rawPost);
         $preApprovalKey = Joomla\Utilities\ArrayHelper::getValue($_POST, "preapproval_key");
         // Validate transaction data
         $this->updateTransactionDataOnPay($_POST, $preApprovalKey);
     } else {
         // Log error
         $this->log->add(JText::_($this->textPrefix . "_ERROR_INVALID_TRANSACTION_DATA"), $this->debugType, array("error message" => $paypalIpn->getError(), "paypalIPN" => $paypalIpn, "_POST" => $_POST, "RAW POST" => $rawPost));
     }
     return $result;
 }