/**
  * Disconnect user from payment gateway.
  *
  * @param string                   $context
  * @param Joomla\Registry\Registry $params
  *
  * @return null|array
  */
 public function onPayoutsDeauthorize($context, $params)
 {
     if (strcmp('com_crowdfundingfinance.payouts.deauthorize.stripeconnect', $context) !== 0) {
         return null;
     }
     if ($this->app->isAdmin()) {
         return null;
     }
     $doc = JFactory::getDocument();
     /**  @var $doc JDocumentHtml */
     // Check document type
     $docType = $doc->getType();
     if (strcmp('html', $docType) !== 0) {
         return null;
     }
     // Prepare output data.
     $output = array('redirect_url' => '', 'message' => '');
     $errorOutput = array('redirect_url' => JRoute::_(CrowdfundingHelperRoute::getDiscoverRoute()), 'message' => '');
     // DEBUG DATA
     JDEBUG ? $this->log->add(JText::_($this->textPrefix . '_DEBUG_GET_RESPONSE_AUTHORIZE'), $this->debugType, $_GET) : null;
     $userId = JFactory::getUser()->get('id');
     if (!$userId) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_NOT_REGISTERED_USER');
         return $errorOutput;
     }
     // Get token
     $state = $this->app->input->get('state');
     if (!$state) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_AUTHORIZATION_DATA');
         return $errorOutput;
     }
     // Get project ID and redirect URL from the session.
     $stateData = $this->app->getUserState($state);
     if (count($stateData) === 0 or (!$stateData['redirect_url'] or !$stateData['project_id'])) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_AUTHORIZATION_DATA');
         return $errorOutput;
     }
     $cfFinanceParams = JComponentHelper::getParams('com_crowdfundingfinance');
     $apiKeys = Crowdfundingfinance\Stripe\Helper::getKeys($cfFinanceParams);
     if (!$apiKeys['client_id'] or !$apiKeys['secret_key']) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_CONFIGURATION');
         return $errorOutput;
     }
     $payout = new Crowdfundingfinance\Payout(JFactory::getDbo());
     $payout->setSecretKey($this->app->get('secret'));
     $payout->load(array('project_id' => (int) $stateData['project_id']));
     if (!$payout->getId()) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_INVALID_PAYOUT');
         return $errorOutput;
     }
     $alias = !$apiKeys['test'] ? 'production' : 'test';
     $stripeData = $payout->getStripe();
     if (!$stripeData->get('stripeconnect.' . $alias . '.account_id')) {
         $errorOutput['message'] = JText::_($this->textPrefix . '_ERROR_NOT_CONNECTED');
         return $errorOutput;
     }
     Crowdfundingfinance\Stripe\Helper::deauthorize($apiKeys, $stripeData->get('stripeconnect.' . $alias . '.account_id'));
     $stripeData->set('stripeconnect.' . $alias . '.access_token', '');
     $stripeData->set('stripeconnect.' . $alias . '.refresh_token', '');
     $stripeData->set('stripeconnect.' . $alias . '.account_id', '');
     $stripeData->set('stripeconnect.' . $alias . '.expires', 0);
     $payout->setStripe($stripeData);
     $payout->storeStripe();
     // Get next URL.
     $output['redirect_url'] = base64_decode($stateData['redirect_url']);
     return $output;
 }
 /**
  * Return payment receiver.
  *
  * @param $paymentReceiverOption
  * @param $itemId
  *
  * @return null|string
  */
 protected function getPaymentReceiver($paymentReceiverOption, $itemId)
 {
     if ($this->params->get('paypal_sandbox', 1)) {
         return strtolower(trim($this->params->get('paypal_sandbox_business_name')));
     } else {
         if (strcmp('site_owner', $paymentReceiverOption) === 0) {
             // Site owner
             return strtolower(trim($this->params->get('paypal_business_name')));
         } else {
             if (!JComponentHelper::isEnabled('com_crowdfundingfinance')) {
                 return null;
             } else {
                 $payout = new Crowdfundingfinance\Payout(JFactory::getDbo());
                 $payout->load(array('project_id' => $itemId));
                 if (!$payout->getPaypalEmail()) {
                     return null;
                 }
                 return strtolower(trim($payout->getPaypalEmail()));
             }
         }
     }
 }