Loading src/Service/VippsHttpClient.php +53 −58 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\vipps_recurring_payments\Service; use Drupal\Core\Extension\ModuleExtensionList; use Drupal\vipps_recurring_payments\RequestStorage\RequestStorageInterface; use Drupal\vipps_recurring_payments\ResponseApiData\AgreementData; use Drupal\vipps_recurring_payments\ResponseApiData\ChargeItemResponse; Loading Loading @@ -33,21 +34,37 @@ class VippsHttpClient { private $config; /** * API version. * The module version. * * @var string */ const VERSION = '1.28'; protected $version; /** * Vipps Http Client calls constructor. * * @param \GuzzleHttp\ClientInterface $httpClient * Http client. * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list * The update registry service. * @param \Drupal\vipps_recurring_payments\Service\VippsApiConfig $config * Configurations. */ public function __construct(ClientInterface $httpClient, VippsApiConfig $config) { public function __construct(ClientInterface $httpClient, ModuleExtensionList $module_extension_list, VippsApiConfig $config) { $this->httpClient = $httpClient; $this->config = $config; $list = $module_extension_list->getAllInstalledInfo(); $this->version = $list['vipps_recurring_payments']['version']; } /** * Returns the module version. * * @return string */ public function getVersion(): string { return $this->version; } /** Loading @@ -64,7 +81,7 @@ class VippsHttpClient { 'client_id' => $this->config->getClientId(), 'client_secret' => $this->config->getClientSecret(), 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), ], ] + $this->getDefaultHeaders(), ]); return $this->getResponseBody($response)->access_token; Loading @@ -89,11 +106,7 @@ class VippsHttpClient { 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -129,11 +142,7 @@ class VippsHttpClient { 'Idempotent-Key' => $agreementId . time(), 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading @@ -160,11 +169,7 @@ class VippsHttpClient { 'headers' => [ 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); $responseData = $this->getResponseBody($response); Loading @@ -191,11 +196,7 @@ class VippsHttpClient { 'headers' => [ 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); return new ChargeItemResponse($response); Loading @@ -219,11 +220,7 @@ class VippsHttpClient { 'headers' => [ 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); return json_decode($response->getBody()->getContents()); Loading Loading @@ -251,11 +248,7 @@ class VippsHttpClient { 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -283,11 +276,7 @@ class VippsHttpClient { 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); $statusCode = $response->getStatusCode(); Loading Loading @@ -329,11 +318,7 @@ class VippsHttpClient { 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Idempotency-Key' => "{$agreementId}_{$chargeId}_{$requestStorage->getData()['amount']}", 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -376,11 +361,7 @@ class VippsHttpClient { 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Idempotency-Key' => "{$agreementId}_{$chargeId}_{$requestStorage->getData()['amount']}", 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -411,4 +392,18 @@ class VippsHttpClient { return json_decode($response->getBody()->getContents()); } /** * Returns default headers for every Vipps API call. * * @return array */ protected function getDefaultHeaders(): array { return [ 'Merchant-Serial-Number' => $this->config->getMsn(), 'Vipps-System-Name' => 'Drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this->getVersion(), ]; } } vipps_recurring_payments.services.yml +1 −1 Original line number Diff line number Diff line services: vipps_recurring_payments:http_client: class: Drupal\vipps_recurring_payments\Service\VippsHttpClient arguments: ['@http_client', '@vipps_recurring_payments:api_config'] arguments: ['@http_client', '@extension.list.module', '@vipps_recurring_payments:api_config'] vipps_recurring_payments:api_config: class: Drupal\vipps_recurring_payments\Service\VippsApiConfig Loading Loading
src/Service/VippsHttpClient.php +53 −58 Original line number Diff line number Diff line Loading @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Drupal\vipps_recurring_payments\Service; use Drupal\Core\Extension\ModuleExtensionList; use Drupal\vipps_recurring_payments\RequestStorage\RequestStorageInterface; use Drupal\vipps_recurring_payments\ResponseApiData\AgreementData; use Drupal\vipps_recurring_payments\ResponseApiData\ChargeItemResponse; Loading Loading @@ -33,21 +34,37 @@ class VippsHttpClient { private $config; /** * API version. * The module version. * * @var string */ const VERSION = '1.28'; protected $version; /** * Vipps Http Client calls constructor. * * @param \GuzzleHttp\ClientInterface $httpClient * Http client. * @param \Drupal\Core\Extension\ModuleExtensionList $module_extension_list * The update registry service. * @param \Drupal\vipps_recurring_payments\Service\VippsApiConfig $config * Configurations. */ public function __construct(ClientInterface $httpClient, VippsApiConfig $config) { public function __construct(ClientInterface $httpClient, ModuleExtensionList $module_extension_list, VippsApiConfig $config) { $this->httpClient = $httpClient; $this->config = $config; $list = $module_extension_list->getAllInstalledInfo(); $this->version = $list['vipps_recurring_payments']['version']; } /** * Returns the module version. * * @return string */ public function getVersion(): string { return $this->version; } /** Loading @@ -64,7 +81,7 @@ class VippsHttpClient { 'client_id' => $this->config->getClientId(), 'client_secret' => $this->config->getClientSecret(), 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), ], ] + $this->getDefaultHeaders(), ]); return $this->getResponseBody($response)->access_token; Loading @@ -89,11 +106,7 @@ class VippsHttpClient { 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -129,11 +142,7 @@ class VippsHttpClient { 'Idempotent-Key' => $agreementId . time(), 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading @@ -160,11 +169,7 @@ class VippsHttpClient { 'headers' => [ 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); $responseData = $this->getResponseBody($response); Loading @@ -191,11 +196,7 @@ class VippsHttpClient { 'headers' => [ 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); return new ChargeItemResponse($response); Loading @@ -219,11 +220,7 @@ class VippsHttpClient { 'headers' => [ 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); return json_decode($response->getBody()->getContents()); Loading Loading @@ -251,11 +248,7 @@ class VippsHttpClient { 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -283,11 +276,7 @@ class VippsHttpClient { 'Content-Type' => 'application/json', 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), ]); $statusCode = $response->getStatusCode(); Loading Loading @@ -329,11 +318,7 @@ class VippsHttpClient { 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Idempotency-Key' => "{$agreementId}_{$chargeId}_{$requestStorage->getData()['amount']}", 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -376,11 +361,7 @@ class VippsHttpClient { 'Authorization' => "Bearer {$token}", 'Ocp-Apim-Subscription-Key' => $this->config->getSubscriptionKey(), 'Idempotency-Key' => "{$agreementId}_{$chargeId}_{$requestStorage->getData()['amount']}", 'Vipps-System-Name' => 'drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this::VERSION, ], ] + $this->getDefaultHeaders(), 'json' => $requestStorage->getData(), ]); Loading Loading @@ -411,4 +392,18 @@ class VippsHttpClient { return json_decode($response->getBody()->getContents()); } /** * Returns default headers for every Vipps API call. * * @return array */ protected function getDefaultHeaders(): array { return [ 'Merchant-Serial-Number' => $this->config->getMsn(), 'Vipps-System-Name' => 'Drupal', 'Vipps-System-Version' => \Drupal::VERSION, 'Vipps-System-Plugin-Name' => 'vipps-recurring-payments', 'Vipps-System-Plugin-Version' => $this->getVersion(), ]; } }
vipps_recurring_payments.services.yml +1 −1 Original line number Diff line number Diff line services: vipps_recurring_payments:http_client: class: Drupal\vipps_recurring_payments\Service\VippsHttpClient arguments: ['@http_client', '@vipps_recurring_payments:api_config'] arguments: ['@http_client', '@extension.list.module', '@vipps_recurring_payments:api_config'] vipps_recurring_payments:api_config: class: Drupal\vipps_recurring_payments\Service\VippsApiConfig Loading