/**
  * Authorize user to payment gateway.
  *
  * @param string                   $context
  * @param Joomla\Registry\Registry $params
  *
  * @return null|array
  */
 public function onPayoutsAuthorize($context, $params)
 {
     if (strcmp('com_crowdfundingfinance.payouts.authorize.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
     $code = $this->app->input->get('code');
     $state = $this->app->input->get('state');
     if (!$code or !$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;
     }
     // Prepare expiration date.
     $date = new JDate();
     $date->add(new DateInterval('P' . $cfFinanceParams->get('stripe_expiration_period', '7') . 'D'));
     // Prepare Stripe data.
     $provider = new AdamPaterson\OAuth2\Client\Provider\Stripe(['clientId' => $apiKeys['client_id'], 'clientSecret' => $apiKeys['secret_key']]);
     $token = $provider->getAccessToken('authorization_code', ['code' => $code]);
     // Get resource owner.
     $resourceOwner = $provider->getResourceOwner($token);
     $alias = !$apiKeys['test'] ? 'production' : 'test';
     $stripe = new \Joomla\Registry\Registry(array('stripeconnect' => array($alias => array('access_token' => $token->getToken(), 'refresh_token' => $token->getRefreshToken(), 'expires' => $date->getTimestamp(), 'account_id' => $resourceOwner->getId()))));
     $payout = new Crowdfundingfinance\Payout(JFactory::getDbo());
     $payout->setSecretKey($this->app->get('secret'));
     $payout->load(array('project_id' => (int) $stateData['project_id']));
     // Create user record.
     if (!$payout->getId()) {
         $payout->setProjectId((int) $stateData['project_id']);
     }
     $payout->setStripe($stripe);
     $payout->store();
     // Get next URL.
     $output['redirect_url'] = base64_decode($stateData['redirect_url']);
     return $output;
 }