Esempio 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;
 }
 /**
  *
  * Add virtual currency to user account after registration.
  *
  * @param integer $userId
  */
 protected function giveUnits($userId)
 {
     $units = (int) $this->params->get("give_units_number", 0);
     $currencyId = $this->params->get("give_units_unit");
     if (!empty($units) and !empty($currencyId)) {
         jimport("virtualcurrency.currency");
         $currency = VirtualCurrencyCurrency::getInstance(JFactory::getDbo(), $currencyId);
         if ($currency->getId()) {
             // Get the id of the sender ( the bank that generates currency )
             $componentParams = JComponentHelper::getParams("com_virtualcurrency");
             /** @var  $componentParams Joomla\Registry\Registry */
             $senderId = $componentParams->get("payments_bank_id");
             // Get account ID
             JLoader::register("VirtualCurrencyHelper", JPATH_ADMINISTRATOR . DIRECTORY_SEPARATOR . "components" . DIRECTORY_SEPARATOR . "com_virtualcurrency" . DIRECTORY_SEPARATOR . "helpers" . DIRECTORY_SEPARATOR . "virtualcurrency.php");
             $keys = array("user_id" => $userId, "currency_id" => $currency->getId());
             // Add the units to the account
             jimport("virtualcurrency.account");
             $account = new VirtualCurrencyAccount(JFactory::getDbo());
             $account->load($keys);
             $account->increaseAmount($units);
             $account->updateAmount();
             // Store transaction
             jimport("virtualcurrency.transaction");
             $transaction = new VirtualCurrencyTransaction(JFactory::getDbo());
             $seed = substr(md5(uniqid(time() * rand(), true)), 0, 16);
             $data = array("units" => $units, "txn_id" => JString::strtoupper("GEN_" . JString::substr(JApplicationHelper::getHash($seed), 0, 16)), "txn_amount" => 0, "txn_currency" => $currency->getCode(), "txn_status" => "completed", "service_provider" => "System", "currency_id" => $currency->getId(), "sender_id" => $senderId, "receiver_id" => $userId);
             $transaction->bind($data);
             $transaction->store();
         }
         // Integrate with notifier
         // Notification services
         $nServices = $this->params->get("give_units_integrate");
         if (!empty($nServices)) {
             $message = JText::sprintf("PLG_USER_VIRTUALCURRENCYNEWACCOUNT_NOTIFICATION_AFTER_REGISTRATION", $units, $currency->getTitle());
             $this->notify($nServices, $message, $userId);
         }
     }
 }
Esempio n. 3
0
 /**
  * Send emails to the administrator and buyer of units.
  *
  * @param object $currency
  * @param object $transaction
  * @param Joomla\Registry\Registry $params
  */
 protected function sendMails($currency, $transaction, $params)
 {
     $app = JFactory::getApplication();
     /** @var $app JApplicationSite */
     // Get website
     $uri = JUri::getInstance();
     $website = $uri->toString(array("scheme", "host"));
     $emailMode = $this->params->get("email_mode", "plain");
     jimport("virtualcurrency.currency");
     $units = VirtualCurrencyCurrency::getInstance(JFactory::getDbo(), $currency->id);
     jimport("virtualcurrency.realcurrency");
     $currencyId = $params->get("payments_currency_id");
     $realCurrency = VirtualCurrencyRealCurrency::getInstance(JFactory::getDbo(), $currencyId, $params);
     // Prepare data for parsing
     $data = array("site_name" => $app->get("sitename"), "site_url" => JUri::root(), "item_title" => $currency->title, "order_url" => $website . JRoute::_(VirtualCurrencyHelperRoute::getAccountsRoute()), "units" => $units->getAmountString($transaction->units), "units_title" => $units->getTitle(), "amount" => $realCurrency->getAmountString($transaction->txn_amount), "transaction_id" => $transaction->txn_id);
     // DEBUG DATA
     JDEBUG ? $this->log->add($this->textPrefix . "_DEBUG_MAIL_DATA", $this->debugType, var_export($data, true)) : null;
     // Send mail to the administrator
     $emailId = $this->params->get("admin_mail_id", 0);
     if (!empty($emailId)) {
         jimport("virtualcurrency.email");
         $email = new VirtualCurrencyEmail();
         $email->setDb(JFactory::getDbo());
         $email->load($emailId);
         if (!$email->getSenderName()) {
             $email->setSenderName($app->get("fromname"));
         }
         if (!$email->getSenderEmail()) {
             $email->setSenderEmail($app->get("mailfrom"));
         }
         $recipientName = $email->getSenderName();
         $recipientMail = $email->getSenderEmail();
         // Prepare data for parsing
         $data["sender_name"] = $email->getSenderName();
         $data["sender_email"] = $email->getSenderEmail();
         $data["recipient_name"] = $recipientName;
         $data["recipient_email"] = $recipientMail;
         $email->parse($data);
         $subject = $email->getSubject();
         $body = $email->getBody($emailMode);
         $mailer = JFactory::getMailer();
         if (strcmp("html", $emailMode) == 0) {
             // Send as HTML message
             $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, VirtualCurrencyConstants::MAIL_MODE_HTML);
         } else {
             // Send as plain text.
             $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, VirtualCurrencyConstants::MAIL_MODE_PLAIN_TEXT);
         }
         // Check for an error.
         if ($return !== true) {
             $this->log->add(JText::_($this->textPrefix . "_ERROR_MAIL_SENDING_ADMIN"), $this->debugType);
         }
     }
     // Send mail to buyer
     $emailId = $this->params->get("user_mail_id", 0);
     $userId = $transaction->receiver_id;
     if (!empty($emailId) and !empty($userId)) {
         $email = new VirtualCurrencyEmail();
         $email->setDb(JFactory::getDbo());
         $email->load($emailId);
         if (!$email->getSenderName()) {
             $email->setSenderName($app->get("fromname"));
         }
         if (!$email->getSenderEmail()) {
             $email->setSenderEmail($app->get("mailfrom"));
         }
         $user = JFactory::getUser($userId);
         $recipientName = $user->get("name");
         $recipientMail = $user->get("email");
         // Prepare data for parsing
         $data["sender_name"] = $email->getSenderName();
         $data["sender_email"] = $email->getSenderEmail();
         $data["recipient_name"] = $recipientName;
         $data["recipient_email"] = $recipientMail;
         $email->parse($data);
         $subject = $email->getSubject();
         $body = $email->getBody($emailMode);
         $mailer = JFactory::getMailer();
         if (strcmp("html", $emailMode) == 0) {
             // Send as HTML message
             $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, VirtualCurrencyConstants::MAIL_MODE_HTML);
         } else {
             // Send as plain text.
             $return = $mailer->sendMail($email->getSenderEmail(), $email->getSenderName(), $recipientMail, $subject, $body, VirtualCurrencyConstants::MAIL_MODE_PLAIN_TEXT);
         }
         // Check for an error.
         if ($return !== true) {
             // Log error
             $this->log->add(JText::_($this->textPrefix . "_ERROR_MAIL_SENDING_USER"), $this->debugType);
         }
     }
 }