Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTER-4313: As a Bold developer, I want to access a brands system version in the API responses. #297

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Model/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class Config implements ConfigInterface
private const PATH_LIFE_ELEMENTS = 'checkout/bold_checkout_custom_elements/life_elements';
private const PATH_VALIDATE_COUPON_CODES = 'checkout/bold_checkout_advanced/validate_coupon_codes';
private const PATH_UPDATE_CHECK = 'checkout/bold_checkout_advanced/updates_check';
private const PATH_SYSTEM_INFO_HEADERS = 'checkout/bold_checkout_advanced/system_info_headers';

public const INTEGRATION_PATHS = [
self::PATH_INTEGRATION_EMAIL,
Expand Down Expand Up @@ -385,4 +386,15 @@ public function isUpdatesCheckEnabled(): bool
self::PATH_UPDATE_CHECK
);
}

/**
* @inheritDoc
*/
public function isSystemInfoEnabled(int $websiteId): bool
{
return $this->configManagement->isSetFlag(
self::PATH_SYSTEM_INFO_HEADERS,
$websiteId
);
}
}
8 changes: 8 additions & 0 deletions Model/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,12 @@ public function getValidateCouponCodes(int $websiteId): bool;
* @return bool
*/
public function isUpdatesCheckEnabled(): bool;

/**
* Is system info sending in headers enabled.
*
* @param int $websiteId
* @return bool
*/
public function isSystemInfoEnabled(int $websiteId): bool;
}
30 changes: 21 additions & 9 deletions Model/Http/BoldClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
namespace Bold\Checkout\Model\Http;

use Bold\Checkout\Api\Data\Http\Client\ResultInterface;
use Bold\Checkout\Api\Data\Http\Client\ResultInterfaceFactory;
use Bold\Checkout\Api\Http\ClientInterface;
use Bold\Checkout\Model\ConfigInterface;
use Bold\Checkout\Model\Http\Client\Command\DeleteCommand;
use Bold\Checkout\Model\Http\Client\Command\GetCommand;
use Bold\Checkout\Model\Http\Client\Command\PatchCommand;
use Bold\Checkout\Model\Http\Client\Command\PostCommand;
use Bold\Checkout\Model\Http\Client\Command\PutCommand;
use Bold\Checkout\Model\Http\Client\SystemInfoHeaders;
use Bold\Checkout\Model\Http\Client\UserAgent;
use Magento\Framework\Exception\LocalizedException;

/**
* Client to perform http request to Bold.
Expand Down Expand Up @@ -57,6 +56,11 @@ class BoldClient implements ClientInterface
*/
private $putCommand;

/**
* @var SystemInfoHeaders
*/
private $systemInfoHeaders;

/**
* @param ConfigInterface $config
* @param UserAgent $userAgent
Expand All @@ -65,6 +69,7 @@ class BoldClient implements ClientInterface
* @param PatchCommand $patchCommand
* @param DeleteCommand $deleteCommand
* @param PutCommand $putCommand
* @param SystemInfoHeaders $systemInfoHeaders
*/
public function __construct(
ConfigInterface $config,
Expand All @@ -73,7 +78,8 @@ public function __construct(
PostCommand $postCommand,
PatchCommand $patchCommand,
DeleteCommand $deleteCommand,
PutCommand $putCommand
PutCommand $putCommand,
SystemInfoHeaders $systemInfoHeaders
) {
$this->config = $config;
$this->userAgent = $userAgent;
Expand All @@ -82,6 +88,7 @@ public function __construct(
$this->patchCommand = $patchCommand;
$this->deleteCommand = $deleteCommand;
$this->putCommand = $putCommand;
$this->systemInfoHeaders = $systemInfoHeaders;
}

/**
Expand Down Expand Up @@ -142,12 +149,17 @@ public function delete(int $websiteId, string $url, array $data): ResultInterfac
*/
private function getHeaders(int $websiteId): array
{
return [
'Authorization' => 'Bearer ' . $this->config->getApiToken($websiteId),
'Content-Type' => 'application/json',
'User-Agent' => $this->userAgent->getUserAgentData(),
'Bold-API-Version-Date' => self::BOLD_API_VERSION_DATE,
];
$systemInfoHeaders = $this->config->isSystemInfoEnabled($websiteId) ? $this->systemInfoHeaders->getData() : [];

return array_merge(
[
'Authorization' => 'Bearer ' . $this->config->getApiToken($websiteId),
'Content-Type' => 'application/json',
'User-Agent' => $this->userAgent->getUserAgentData(),
'Bold-API-Version-Date' => self::BOLD_API_VERSION_DATE,
],
$systemInfoHeaders
);
}

/**
Expand Down
35 changes: 25 additions & 10 deletions Model/Http/BoldStorefrontClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Bold\Checkout\Model\Http\Client\Command\GetCommand;
use Bold\Checkout\Model\Http\Client\Command\PostCommand;
use Bold\Checkout\Model\Http\Client\Command\PutCommand;
use Bold\Checkout\Model\Http\Client\SystemInfoHeaders;
use Magento\Checkout\Model\Session;
use Magento\Framework\Exception\LocalizedException;

Expand Down Expand Up @@ -48,28 +49,36 @@ class BoldStorefrontClient implements ClientInterface
*/
private $putCommand;

/**
* @var SystemInfoHeaders
*/
private $systemInfoHeaders;

/**
* @param ConfigInterface $config
* @param Session $checkoutSession
* @param GetCommand $getCommand
* @param PostCommand $postCommand
* @param DeleteCommand $deleteCommand
* @param PutCommand $putCommand
* @param SystemInfoHeaders $systemInfoHeaders
*/
public function __construct(
ConfigInterface $config,
Session $checkoutSession,
GetCommand $getCommand,
PostCommand $postCommand,
DeleteCommand $deleteCommand,
PutCommand $putCommand
PutCommand $putCommand,
SystemInfoHeaders $systemInfoHeaders
) {
$this->config = $config;
$this->getCommand = $getCommand;
$this->postCommand = $postCommand;
$this->checkoutSession = $checkoutSession;
$this->deleteCommand = $deleteCommand;
$this->putCommand = $putCommand;
$this->systemInfoHeaders = $systemInfoHeaders;
}

/**
Expand All @@ -78,7 +87,7 @@ public function __construct(
public function get(int $websiteId, string $url): ResultInterface
{
$url = $this->getUrl($websiteId, $url);
$headers = $this->getHeaders();
$headers = $this->getHeaders($websiteId);
return $this->getCommand->execute($websiteId, $url, $headers);
}

Expand All @@ -88,7 +97,7 @@ public function get(int $websiteId, string $url): ResultInterface
public function post(int $websiteId, string $url, array $data): ResultInterface
{
$url = $this->getUrl($websiteId, $url);
$headers = $this->getHeaders();
$headers = $this->getHeaders($websiteId);
$result = $this->postCommand->execute($websiteId, $url, $headers, $data);
$applicationState = $result->getBody()['data']['application_state'] ?? null;
if (!$result->getErrors() && $applicationState) {
Expand All @@ -106,7 +115,7 @@ public function post(int $websiteId, string $url, array $data): ResultInterface
public function put(int $websiteId, string $url, array $data): ResultInterface
{
$url = $this->getUrl($websiteId, $url);
$headers = $this->getHeaders();
$headers = $this->getHeaders($websiteId);
$result = $this->putCommand->execute($websiteId, $url, $headers, $data);
$applicationState = $result->getBody()['data']['application_state'] ?? null;
if (!$result->getErrors() && $applicationState) {
Expand All @@ -132,7 +141,7 @@ public function patch(int $websiteId, string $url, array $data): ResultInterface
public function delete(int $websiteId, string $url, array $data): ResultInterface
{
$url = $this->getUrl($websiteId, $url);
$headers = $this->getHeaders();
$headers = $this->getHeaders($websiteId);
$result = $this->deleteCommand->execute($websiteId, $url, $headers, $data);
$applicationState = $result->getBody()['data']['application_state'] ?? null;
if (!$result->getErrors() && $applicationState) {
Expand All @@ -147,19 +156,25 @@ public function delete(int $websiteId, string $url, array $data): ResultInterfac
/**
* Get request headers.
*
* @param int $websiteId
* @return array
* @throws LocalizedException
*/
private function getHeaders(): array
private function getHeaders(int $websiteId): array
{
$boldCheckoutData = $this->checkoutSession->getBoldCheckoutData();
if (!$boldCheckoutData) {
throw new LocalizedException(__('Bold Checkout data is not set.'));
}
return [
'Authorization' => 'Bearer ' . $boldCheckoutData['data']['jwt_token'],
'Content-Type' => 'application/json',
];
$systemInfoHeaders = $this->config->isSystemInfoEnabled($websiteId) ? $this->systemInfoHeaders->getData() : [];

return array_merge(
[
'Authorization' => 'Bearer ' . $boldCheckoutData['data']['jwt_token'],
'Content-Type' => 'application/json',
],
$systemInfoHeaders
);
}

/**
Expand Down
82 changes: 82 additions & 0 deletions Model/Http/Client/SystemInfoHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

namespace Bold\Checkout\Model\Http\Client;

use Bold\Checkout\Model\ModuleInfo\InstalledModulesProvider;
use Bold\Checkout\Model\ModuleInfo\ModuleComposerVersionProvider;
use Magento\Framework\App\ProductMetadataInterface;

/**
* Get system info headers.
*/
class SystemInfoHeaders
{
private const HEADER_PREFIX_MAGENTO_EDITION = 'Magento-Edition';
private const HEADER_PREFIX_MAGENTO_VERSION = 'Magento-Version';
private const HEADER_PREFIX_PHP_VERSION = 'PHP-Version';
private const HEADER_PREFIX_MODULE_VERSION = 'Module-Version';

/**
* @var ProductMetadataInterface
*/
private $productMetadata;

/**
* @var ModuleComposerVersionProvider
*/
private $moduleVersionProvider;

/**
* @var InstalledModulesProvider
*/
private $installedModulesProvider;

/**
* @param ProductMetadataInterface $productMetadata
* @param ModuleComposerVersionProvider $moduleVersionProvider
* @param InstalledModulesProvider $installedModulesProvider
*/
public function __construct(
ProductMetadataInterface $productMetadata,
ModuleComposerVersionProvider $moduleVersionProvider,
InstalledModulesProvider $installedModulesProvider
)
{
$this->productMetadata = $productMetadata;
$this->moduleVersionProvider = $moduleVersionProvider;
$this->installedModulesProvider = $installedModulesProvider;
}

/**
* Get system info headers.
*
* @return array
*/
public function getData(): array
{
return [
self::HEADER_PREFIX_MAGENTO_EDITION => $this->productMetadata->getEdition(),
self::HEADER_PREFIX_MAGENTO_VERSION => $this->productMetadata->getVersion(),
self::HEADER_PREFIX_PHP_VERSION => phpversion(),
self::HEADER_PREFIX_MODULE_VERSION => $this->getModuleData(),
];
}

/**
* Get installed modules versions.
*
* @return string
*/
private function getModuleData(): string
{
$result = [];
foreach ($this->installedModulesProvider->getModuleList() as $module) {
$version = $this->moduleVersionProvider->getVersion($module);
$result[] = sprintf('%s %s', $module, $version);
}

return join('; ', $result);
}
}
26 changes: 20 additions & 6 deletions Model/Http/PlatformClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bold\Checkout\Model\ConfigInterface;
use Bold\Checkout\Model\Http\Client\Command\GetCommand;
use Bold\Checkout\Model\Http\Client\Command\PostCommand;
use Bold\Checkout\Model\Http\Client\SystemInfoHeaders;
use DateTime;
use Magento\Framework\Exception\LocalizedException;

Expand All @@ -31,19 +32,27 @@ class PlatformClient implements ClientInterface
*/
private $postCommand;

/**
* @var SystemInfoHeaders
*/
private $systemInfoHeaders;

/**
* @param ConfigInterface $config
* @param GetCommand $getCommand
* @param PostCommand $postCommand
* @param SystemInfoHeaders $systemInfoHeaders
*/
public function __construct(
ConfigInterface $config,
GetCommand $getCommand,
PostCommand $postCommand
PostCommand $postCommand,
SystemInfoHeaders $systemInfoHeaders
) {
$this->config = $config;
$this->getCommand = $getCommand;
$this->postCommand = $postCommand;
$this->systemInfoHeaders = $systemInfoHeaders;
}

/**
Expand Down Expand Up @@ -114,10 +123,15 @@ private function getHeaders(int $websiteId): array
$secret = $this->config->getSharedSecret($websiteId);
$timestamp = date(DateTime::RFC3339);
$hmac = base64_encode(hash_hmac('sha256', $timestamp, $secret, true));
return [
'X-HMAC-Timestamp' => $timestamp,
'X-HMAC' => $hmac,
'Content-Type' => 'application/json',
];
$systemInfoHeaders = $this->config->isSystemInfoEnabled($websiteId) ? $this->systemInfoHeaders->getData() : [];

return array_merge(
[
'X-HMAC-Timestamp' => $timestamp,
'X-HMAC' => $hmac,
'Content-Type' => 'application/json',
],
$systemInfoHeaders
);
}
}
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[Check for Bold modules new version availability.]]></comment>
</field>
<field id="system_info_headers" translate="label" type="select" sortOrder="100" showInDefault="1" showInWebsite="1">
<label>Enable Sharing System Data</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment><![CDATA[If enabled, additional information about your Magento installation will be added to each Bold request.]]></comment>
</field>
<field id="clear_state" translate="label" type="button" sortOrder="1000" showInDefault="1" showInWebsite="1">
<label>Clear Bold Extension</label>
<frontend_model>Bold\Checkout\Block\System\Config\Form\Field\ClearState</frontend_model>
Expand Down