CheckoutSdkFactory.php in Commerce PayPal 8
File
src/CheckoutSdkFactory.php
View source
<?php
namespace Drupal\commerce_paypal;
use Drupal\commerce_order\AdjustmentTransformerInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Http\ClientFactory;
use Drupal\Core\State\StateInterface;
use GuzzleHttp\HandlerStack;
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class CheckoutSdkFactory implements CheckoutSdkFactoryInterface {
protected $clientFactory;
protected $stack;
protected $adjustmentTransformer;
protected $eventDispatcher;
protected $moduleHandler;
protected $state;
protected $time;
protected $instances = [];
public function __construct(ClientFactory $client_factory, HandlerStack $stack, AdjustmentTransformerInterface $adjustment_transformer, EventDispatcherInterface $event_dispatcher, ModuleHandlerInterface $module_handler, StateInterface $state, TimeInterface $time) {
$this->clientFactory = $client_factory;
$this->stack = $stack;
$this->adjustmentTransformer = $adjustment_transformer;
$this->eventDispatcher = $event_dispatcher;
$this->moduleHandler = $module_handler;
$this->state = $state;
$this->time = $time;
}
public function get(array $configuration) {
$client_id = $configuration['client_id'];
if (!isset($this->instances[$client_id])) {
$client = $this
->getClient($configuration);
$this->instances[$client_id] = new CheckoutSdk($client, $this->adjustmentTransformer, $this->eventDispatcher, $this->moduleHandler, $this->time, $configuration);
}
return $this->instances[$client_id];
}
protected function getClient(array $config) {
switch ($config['mode']) {
case 'live':
$base_uri = 'https://api.paypal.com';
break;
case 'test':
default:
$base_uri = 'https://api.sandbox.paypal.com';
break;
}
$attribution_id = isset($config['payment_solution']) && $config['payment_solution'] == 'custom_card_fields' ? 'Centarro_Commerce_PCP' : 'CommerceGuys_Cart_SPB';
$options = [
'base_uri' => $base_uri,
'headers' => [
'PayPal-Partner-Attribution-Id' => $attribution_id,
],
];
$client = $this->clientFactory
->fromOptions($options);
$token_key = 'commerce_paypal.oauth2_token.' . md5($config['client_id'] . $config['secret']);
$config = [
ClientCredentials::CONFIG_CLIENT_ID => $config['client_id'],
ClientCredentials::CONFIG_CLIENT_SECRET => $config['secret'],
ClientCredentials::CONFIG_TOKEN_URL => '/v1/oauth2/token',
'token_key' => $token_key,
];
$grant_type = new ClientCredentials($client, $config, $this->state);
$middleware = new OAuthMiddleware($client, $grant_type);
$token = $this->state
->get($token_key);
if (!empty($token)) {
$middleware
->setAccessToken($token['token'], 'client_credentials', $token['expires']);
}
$this->stack
->push($middleware
->onBefore());
$this->stack
->push($middleware
->onFailure(2));
$options['handler'] = $this->stack;
$options['auth'] = 'oauth2';
return $this->clientFactory
->fromOptions($options);
}
}