quickbooks_api-8.x-1.0-beta4/src/Controller/AuthController.php

src/Controller/AuthController.php
<?php

namespace Drupal\quickbooks_api\Controller;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\quickbooks_api\QuickbooksService;
use QuickBooksOnline\API\DataService\DataService;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Defines QBOOauthController class.
 */
class AuthController implements ContainerInjectionInterface {

  use StringTranslationTrait;
  use MessengerTrait;

  /**
   * Builds the oauth callback endpoint.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   Symfony Request stack.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
   *   Drupal Configuration.
   * @param \Drupal\Core\State\StateInterface $state
   *   Drupal State API.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $log
   *   Quickbooks API log channel.
   */
  public function __construct(protected RequestStack $requestStack, protected ConfigFactoryInterface $configFactory, protected StateInterface $state, protected LoggerChannelInterface $log) {}

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('request_stack'),
      $container->get('config.factory'),
      $container->get('state'),
      $container->get('logger.channel.quickbooks_api'),
    );
  }

  /**
   * Callback method for handling oauth returns.
   *
   * @return array
   *   Returns simple markup render array.
   */
  public function authenticate() : array {
    $build = [
      '#markup' => $this->t('Connection with Quickbooks Online'),
    ];

    $realm_id = $this->requestStack->getMainRequest()->query->get('realmId');
    $code = $this->requestStack->getMainRequest()->query->get('code');

    // Throw a 403 if no realmId or code found in query parameters.
    if (!$realm_id || !$code) {
      throw new AccessDeniedHttpException("Oauth callback should not respond if no realm id or code found in query parameters.");
    }

    $config = $this->configFactory->get(QuickbooksService::CONFIG_KEY);
    $environment = $config->get('environment');

    $oauth_route = Url::fromRoute('quickbooks_api.oauth');

    // Prepare Data Services.
    $data_service = DataService::Configure([
      'auth_mode' => 'oauth2',
      'ClientID' => $config->get('client_id'),
      'ClientSecret' => $config->get('client_secret'),
      'RedirectURI' => $oauth_route->setAbsolute(TRUE)->toString(),
      'scope' => "com.intuit.quickbooks.accounting",
      'baseUrl' => $environment,
    ]);

    $helper = $data_service->getOAuth2LoginHelper();
    $tokens = $helper->exchangeAuthorizationCodeForToken($code, $realm_id);

    $this->state->set(QuickbooksService::STATE_ACCESS_TOKEN, $tokens->getAccessToken());
    $this->state->set(QuickbooksService::STATE_REFRESH_TOKEN, $tokens->getRefreshToken());
    $this->state->set(QuickbooksService::STATE_ACCESS_TOKEN_EXPIRY, QuickbooksService::addTokenBuffer(strtotime($tokens->getAccessTokenExpiresAt())));
    $this->state->set(QuickbooksService::STATE_REFRESH_EXPIRY, QuickbooksService::addTokenBuffer(strtotime($tokens->getRefreshTokenExpiresAt())));

    // Delete the state security key.
    $this->state->delete(QuickbooksService::STATE_OAUTH_SECURITY);

    // Log everything.
    $this->log->info("Quickbooks API Authorized");
    // Update our markup array.
    $build['#markup'] = $this->t('Connection with Quickbooks Online: established!');

    $build['#attached']['library'][] = 'quickbooks_api/quickbooks_api_close';

    return $build;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc