From 3c920c3ec5b8f3abcbe58b6276cfeba101bd0e15 Mon Sep 17 00:00:00 2001 From: Robert Brodie Date: Mon, 12 Aug 2024 17:30:03 -0400 Subject: [PATCH] SP-126 Added X-BitPay-Platform-Info header --- src/BitPaySDK/Client.php | 13 ++++++----- src/BitPaySDK/PosClient.php | 19 +++++++++++----- src/BitPaySDK/Util/RESTcli/RESTcli.php | 30 +++++++++++++++++++++++++- 3 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/BitPaySDK/Client.php b/src/BitPaySDK/Client.php index 6d0587d5..4589cb7f 100644 --- a/src/BitPaySDK/Client.php +++ b/src/BitPaySDK/Client.php @@ -71,6 +71,7 @@ public function __construct(RESTcli $restCli, Tokens $tokenCache) * @param string|null $privateKeySecret Private Key encryption password. * @param string|null $proxy The url of your proxy to forward requests through. Example: * http://********.com:3128 + * @param string|null $platformInfo Value for the X-BitPay-Platform header. * @return Client * @throws BitPayApiException * @throws BitPayGenericException @@ -80,12 +81,13 @@ public static function createWithData( string $privateKey, Tokens $tokens, ?string $privateKeySecret = null, - ?string $proxy = null + ?string $proxy = null, + ?string $platformInfo = null, ): Client { try { $key = self::initKeys($privateKey, $privateKeySecret); - $restCli = new RESTcli($environment, $key, $proxy); + $restCli = new RESTcli($environment, $key, $proxy, $platformInfo); $tokenCache = $tokens; return new Client($restCli, $tokenCache); @@ -99,11 +101,12 @@ public static function createWithData( /** * Constructor for use if the keys and SIN are managed by this library. * - * @param string $configFilePath The path to the configuration file. + * @param string $configFilePath The path to the configuration file. + * @param string|null $platformInfo Value for the X-BitPay-Platform header. * @return Client * @throws BitPayGenericException */ - public static function createWithFile(string $configFilePath): Client + public static function createWithFile(string $configFilePath, ?string $platformInfo = null): Client { try { $configData = self::getConfigData($configFilePath); @@ -113,7 +116,7 @@ public static function createWithFile(string $configFilePath): Client $key = self::initKeys($config['PrivateKeyPath'], $config['PrivateKeySecret']); $proxy = $config['Proxy'] ?? null; - $restCli = new RESTcli($env, $key, $proxy); + $restCli = new RESTcli($env, $key, $proxy, $platformInfo); $tokenCache = new Tokens($config['ApiTokens']['merchant'], $config['ApiTokens']['payout']); return new Client($restCli, $tokenCache); diff --git a/src/BitPaySDK/PosClient.php b/src/BitPaySDK/PosClient.php index d9faa302..7a6ae5ea 100644 --- a/src/BitPaySDK/PosClient.php +++ b/src/BitPaySDK/PosClient.php @@ -27,22 +27,31 @@ class PosClient extends Client { protected string $env; + + /** + * Value for the X-BitPay-Platform-Info header + * @var string + * + */ + protected string $platformInfo; protected Tokens $token; protected RESTcli $RESTcli; /** * Constructor for the BitPay SDK to use with the POS facade. * - * @param $token string The token generated on the BitPay account. - * @param string|null $environment string The target environment [Default: Production]. + * @param string $token The token generated on the BitPay account. + * @param string|null $environment The target environment [Default: Production]. + * @param string|null $platformInfo Value for the X-BitPay-Platform-Info header. * * @throws BitPayGenericException */ - public function __construct(string $token, string $environment = null) + public function __construct(string $token, string $environment = null, ?string $platformInfo = null) { try { $this->token = new Tokens(null, null, $token); $this->env = strtolower($environment) === "test" ? Env::TEST : Env::PROD; + $this->platformInfo = $platformInfo !== null ? trim($platformInfo) : ''; $this->init(); parent::__construct($this->RESTcli, new Tokens(null, null, $token)); } catch (Exception $e) { @@ -60,7 +69,7 @@ public function __construct(string $token, string $environment = null) private function init(): void { try { - $this->RESTcli = new RESTcli($this->env, new PrivateKey()); + $this->RESTcli = new RESTcli($this->env, new PrivateKey(), null, $this->platformInfo); } catch (Exception $e) { BitPayExceptionProvider::throwGenericExceptionWithMessage( 'failed to build configuration : ' . $e->getMessage() @@ -71,7 +80,7 @@ private function init(): void /** * Fetch the supported currencies. * - * @return array A list of BitPay Invoice objects. + * @return array A list of BitPay Invoice objects. * @throws BitPayGenericException * @throws BitPayApiException */ diff --git a/src/BitPaySDK/Util/RESTcli/RESTcli.php b/src/BitPaySDK/Util/RESTcli/RESTcli.php index f953ba45..5cbec655 100644 --- a/src/BitPaySDK/Util/RESTcli/RESTcli.php +++ b/src/BitPaySDK/Util/RESTcli/RESTcli.php @@ -44,11 +44,18 @@ class RESTcli */ protected string $identity; + /** + * Value for the X-BitPay-Platform-Info header. + * @var string + */ + protected string $platformInfo; + /** * @var string */ protected string $proxy; + /** * RESTcli constructor. * @param string $environment @@ -56,11 +63,12 @@ class RESTcli * @param string|null $proxy * @throws BitPayApiException */ - public function __construct(string $environment, PrivateKey $ecKey, ?string $proxy = null) + public function __construct(string $environment, PrivateKey $ecKey, ?string $proxy = null, ?string $platformInfo) { $this->ecKey = $ecKey; $this->baseUrl = $environment == Env::TEST ? Env::TEST_URL : Env::PROD_URL; $this->proxy = $proxy !== null ? trim($proxy) : ''; + $this->platformInfo = $platformInfo !== null ? trim($platformInfo) : ''; $this->init(); } @@ -89,6 +97,10 @@ public function init(): void $config['proxy'] = $this->proxy; } + if ($this->platformInfo !== '') { + $config['defaults']['headers']['x-bitpay-platform-info'] = $this->platformInfo; + } + $this->client = new GuzzleHttpClient($config); } catch (Exception $e) { BitPayExceptionProvider::throwApiExceptionWithMessage($e->getMessage()); @@ -128,6 +140,10 @@ public function post($uri, array $formData = [], bool $signatureRequired = true) $headers['x-identity'] = $this->identity; } + if ($this->platformInfo !== '') { + $headers['x-bitpay-platform-info'] = $this->platformInfo; + } + $method = "POST"; LoggerProvider::getLogger()->logRequest($method, $fullURL, $jsonRequestData); @@ -190,6 +206,10 @@ public function get($uri, array $parameters = null, bool $signatureRequired = tr $headers['x-identity'] = $this->identity; } + if ($this->platformInfo !== '') { + $headers['x-bitpay-platform-info'] = $this->platformInfo; + } + $method = 'GET'; LoggerProvider::getLogger()->logRequest($method, $fullURL, null); @@ -247,6 +267,10 @@ public function delete($uri, array $parameters = null): string BitPayExceptionProvider::throwGenericExceptionWithMessage('Wrong ecKey. ' . $e->getMessage()); } + if ($this->platformInfo !== '') { + $headers['x-bitpay-platform-info'] = $this->platformInfo; + } + $method = 'DELETE'; $jsonRequestData = json_encode($parameters, JSON_THROW_ON_ERROR); @@ -305,6 +329,10 @@ public function update($uri, array $formData = []): string BitPayExceptionProvider::throwGenericExceptionWithMessage('Wrong ecKey. ' . $e->getMessage()); } + if ($this->platformInfo !== '') { + $headers['x-bitpay-platform-info'] = $this->platformInfo; + } + $method = 'PUT'; LoggerProvider::getLogger()->logRequest($method, $fullURL, $jsonRequestData);