/**
  *
  * 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);
         }
     }
 }
示例#2
0
 /**
  * Save transaction.
  *
  * @param array     $data
  *
  * @return boolean
  */
 protected function storeTransaction($data)
 {
     // Get transaction by txn ID
     jimport("virtualcurrency.transaction");
     $keys = array("txn_id" => JArrayHelper::getValue($data, "txn_id"));
     $transaction = new VirtualCurrencyTransaction(JFactory::getDbo());
     $transaction->load($keys);
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . "_DEBUG_TRANSACTION_OBJECT"), $this->debugType, $transaction->getProperties()) : null;
     // Check for existed transaction
     if ($transaction->getId()) {
         // If the current status if completed,
         // stop the process.
         if ($transaction->isCompleted()) {
             return false;
         }
     }
     // Store the new transaction data.
     $transaction->bind($data);
     $transaction->store();
     // If it is not completed (it might be pending or other status),
     // stop the process. Only completed transaction will continue
     // and will process the units.
     if (!$transaction->isCompleted()) {
         return false;
     }
     // If the new transaction is completed,
     // update user account.
     $keys = array("user_id" => $transaction->getReceiverId(), "currency_id" => $transaction->getCurrencyId());
     jimport("virtualcurrency.account");
     $account = new VirtualCurrencyAccount(JFactory::getDbo());
     $account->load($keys);
     $account->increaseAmount($transaction->getUnits());
     $account->updateAmount();
     return true;
 }