commerce_square-8.x-1.x-dev/commerce_square.module

commerce_square.module
<?php

/**
 * @file
 * Defines common functionality for the commerce_square module.
 */

use Drupal\commerce_square\ErrorHelper;
use Square\Environment;
use Square\Exceptions\ApiException;
use Square\Models\ObtainTokenRequest;

/**
 * Implements hook_theme().
 */
function commerce_square_theme() {
  return [
    'commerce_square_credit_card_logos' => [
      'variables' => [
        'credit_cards' => [],
      ],
    ],
  ];
}

/**
 * Implements hook_cron().
 */
function commerce_square_cron() {
  $logger = \Drupal::logger('commerce_square');
  /** @var \Drupal\commerce_square\Connect $connect */
  $connect = \Drupal::service('commerce_square.connect');

  if (empty($connect->getAccessToken(Environment::PRODUCTION))) {
    $logger->debug('No access token, skipping');
    return;
  }

  $access_token_expiry = $connect->getAccessTokenExpiration(Environment::PRODUCTION);
  if (!empty($access_token_expiry)) {
    try {
      $client = $connect->getClient(Environment::PRODUCTION);
      $oauth_api = $client->getOAuthApi();

      // Obtain token request.
      $obtain_token_request = new ObtainTokenRequest(
        $connect->getAppId(Environment::PRODUCTION),
        'refresh_token'
      );
      $obtain_token_request->setClientSecret($connect->getAppSecret());
      $obtain_token_request->setRefreshToken($connect->getRefreshToken(Environment::PRODUCTION));
      $api_response = $oauth_api->obtainToken($obtain_token_request);

      if ($api_response->isSuccess()) {
        $token_response = $api_response->getResult();
        $state = \Drupal::state();
        $state->setMultiple([
          'commerce_square.production_access_token' => $token_response->getAccessToken(),
          'commerce_square.production_access_token_expiry' => strtotime($token_response->getExpiresAt()),
          'commerce_square.production_refresh_token' => $token_response->getRefreshToken(),
        ]);
      }
      else {
        throw ErrorHelper::convertException(
          new ApiException(
            $api_response->getBody(),
            $api_response->getRequest(),
            NULL
          )
        );
      }
    }
    catch (ApiException $e) {
      $logger->error(t('Error when renewing access token: :s', [':s' => $e->getMessage()]));
    }
  }
}

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

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