/** * Create a currency object, store it to the instances and return it. * * <code> * $currencyId = 1; * * $currency = VirtualCurrencyCurrency::getInstance(JFactory::getDbo(), $currencyId); * </code> * * @param JDatabaseDriver $db * @param integer $id * * @return null|self */ public static function getInstance($db, $id) { if (!isset(self::$instances[$id])) { $currency = new VirtualCurrencyCurrency($db); $currency->load($id); self::$instances[$id] = $currency; } return self::$instances[$id]; }
<?php } ?> <div class="row-fluid"> <div class="span12"> <?php $layout = new JLayoutFile('wizard', $this->layoutsBasePath); echo $layout->render($this->layoutData); ?> </div> </div> <?php foreach ($this->currencies as $currency_) { $currency = new VirtualCurrencyCurrency(); $currency->bind($currency_); ?> <div class="row-fluid"> <div class="span12"> <div class="bs-docs-example"> <form method="post" action="<?php echo JRoute::_("index.php"); ?> " id="form-payment" autocomplete="off"> <h3><?php echo JText::sprintf("COM_VIRTUALCURRENCY_BUY_CURRENCY", $this->escape($currency->getTitle())); ?> </h3>
/** * * 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); } } }
/** * 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; }
/** * @param JApplicationSite $app * @param array $paymentProcessData */ protected function prepareCurrency($app, $paymentProcessData) { // Load currencies $options = array("state" => 1); jimport("virtualcurrency.currencies"); $this->currencies = new VirtualCurrencyCurrencies(JFactory::getDbo()); $this->currencies->load($options); // Get amount from session $this->currencyAmount = $paymentProcessData["amount"]; // Get item if there is one $itemId = $paymentProcessData["item_id"]; if (!empty($itemId)) { $item = new VirtualCurrencyCurrency(JFactory::getDbo()); $item->load($itemId); // Compare amount with the minimum allowed amount. if ($this->currencyAmount < $item->getParam("minimum")) { // Reset payment data. $paymentProcessData = $this->paymentData; $app->setUserState("payment.data", $paymentProcessData); $this->layoutData->flagStep1 = false; } } // Check days left. If there is no days, disable the button. $this->disabledButton = ""; // Check for debug mode if ($this->params->get("debug_payment_disabled", 0)) { $msg = JString::trim($this->params->get("debug_disabled_functionality_msg")); if (!$msg) { $msg = JText::_("COM_VIRTUALCURRENCY_DEBUG_MODE_DEFAULT_MSG"); } $app->enqueueMessage($msg, "notice"); $this->disabledButton = 'disabled="disabled"'; } }
/** * 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); } } }
/** * 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)); }
/** * Return a currency data, getting it by currency ID. * * <code> * $currencyId = 1; * * $currencies = new VirtualCurrencyCurrencies(JFactory::getDbo()); * $currencies->load(); * * $currency = $currencies->getCurrency($currencyId); * </code> * * @param integer $id * * @return VirtualCurrencyCurrency|null */ public function getCurrency($id) { foreach ($this->items as $item) { if ($id == $item["id"]) { $currency = new VirtualCurrencyCurrency(); $currency->bind($item); return $currency; } } return null; }