Ejemplo n.º 1
0
 public function __construct(&$subject, $config = array())
 {
     parent::__construct($subject, $config);
     $this->textPrefix .= '_' . strtoupper($this->serviceAlias);
     $this->debugType .= '_' . strtoupper($this->serviceAlias);
     $this->errorType .= '_' . strtoupper($this->serviceAlias);
     // Create log object
     $this->log = new Prism\Log\Log();
     // Set database log writer if Joomla! debug is enabled.
     if ($this->logTable !== null and $this->logTable !== '' and JDEBUG) {
         $this->log->addAdapter(new Prism\Log\Adapter\Database(\JFactory::getDbo(), $this->logTable));
     }
     // Set file log adapter.
     if ($this->logFile !== null and $this->logFile !== '') {
         $file = \JPath::clean($this->app->get('log_path') . DIRECTORY_SEPARATOR . basename($this->logFile));
         $this->log->addAdapter(new Prism\Log\Adapter\File($file));
     }
     $this->container = Prism\Container::getContainer();
 }
 /**
  * Pre-processor for $transactionManager->process($context, $options)
  *
  * @param   string        $context
  * @param   Transaction   $transaction
  * @param   array         $options
  *
  * @throws  \RuntimeException
  * @throws  \InvalidArgumentException
  * @throws  \UnexpectedValueException
  * @throws  \OutOfBoundsException
  *
  * @return  void
  */
 public function onAfterProcessTransaction($context, Transaction $transaction, array $options = array())
 {
     // Check for allowed context.
     if (!in_array($context, $this->allowedContext, true)) {
         return;
     }
     $completedOrPending = Constants::PAYMENT_STATUS_COMPLETED | Constants::PAYMENT_STATUS_PENDING;
     $canceledOrRefundedOrFialed = Constants::PAYMENT_STATUS_CANCELED | Constants::PAYMENT_STATUS_REFUNDED | Constants::PAYMENT_STATUS_FAILED;
     $statuses = array('completed' => Constants::PAYMENT_STATUS_COMPLETED, 'pending' => Constants::PAYMENT_STATUS_PENDING, 'canceled' => Constants::PAYMENT_STATUS_CANCELED, 'refunded' => Constants::PAYMENT_STATUS_REFUNDED, 'failed' => Constants::PAYMENT_STATUS_FAILED);
     $oldStatus = ArrayHelper::getValue($options, 'old_status');
     $newStatus = ArrayHelper::getValue($options, 'new_status');
     $oldStatusBit = ($oldStatus and array_key_exists($oldStatus, $statuses)) ? $statuses[$oldStatus] : null;
     $newStatusBit = ($newStatus and array_key_exists($newStatus, $statuses)) ? $statuses[$newStatus] : null;
     // Check if it is new record.
     $isNew = false;
     if ($oldStatusBit === null and $newStatusBit !== null) {
         $isNew = true;
     }
     $container = Container::getContainer();
     $containerHelper = new Helper();
     // Add funds when create new transaction record, and it is completed and pending.
     if ($isNew and $transaction->getProjectId() > 0 and ($transaction->isCompleted() or $transaction->isPending())) {
         $project = $containerHelper->fetchProject($container, $transaction->getProjectId());
         $project->addFunds($transaction->getAmount());
         $project->storeFunds();
         if ($transaction->getRewardId()) {
             $reward = $containerHelper->fetchReward($container, $transaction->getRewardId(), $transaction->getProjectId());
             $this->increaseDistributedReward($transaction, $reward);
         }
     } else {
         // If someone change the status from completed/pending to another one, remove funds.
         if ($completedOrPending & $oldStatusBit and $canceledOrRefundedOrFialed & $newStatusBit) {
             $project = $containerHelper->fetchProject($container, $transaction->getProjectId());
             $project->removeFunds($transaction->getAmount());
             $project->storeFunds();
             if ($transaction->getRewardId()) {
                 $reward = $containerHelper->fetchReward($container, $transaction->getRewardId(), $transaction->getProjectId());
                 $this->decreaseDistributedReward($transaction, $reward);
             }
         } elseif ($canceledOrRefundedOrFialed & $oldStatusBit and $completedOrPending & $newStatusBit) {
             $project = $containerHelper->fetchProject($container, $transaction->getProjectId());
             $project->addFunds($transaction->getAmount());
             $project->storeFunds();
             if ($transaction->getRewardId()) {
                 $reward = $containerHelper->fetchReward($container, $transaction->getRewardId(), $transaction->getProjectId());
                 $this->increaseDistributedReward($transaction, $reward);
             }
         }
     }
 }