Commit d7e9514f authored by Adam Zambrzycki's avatar Adam Zambrzycki Committed by Adam Zambrzycki
Browse files

Issue #3261041 by zambrey, jhhansen: Send Vipps HTTP headers to Recurring API

parent 7d494aad
Loading
Loading
Loading
Loading
+53 −58
Original line number Diff line number Diff line
@@ -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;
@@ -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;
  }

  /**
@@ -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;
@@ -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(),
    ]);

@@ -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(),
    ]);

@@ -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);
@@ -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);
@@ -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());
@@ -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(),
    ]);

@@ -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();
@@ -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(),
    ]);

@@ -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(),
    ]);

@@ -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(),
    ];
  }
}
+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