Skip to content

Commit

Permalink
SP-126 Added X-BitPay-Platform-Info header
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbrodie committed Aug 12, 2024
1 parent 4d677af commit 3c920c3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
13 changes: 8 additions & 5 deletions src/BitPaySDK/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
19 changes: 14 additions & 5 deletions src/BitPaySDK/PosClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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()
Expand All @@ -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
*/
Expand Down
30 changes: 29 additions & 1 deletion src/BitPaySDK/Util/RESTcli/RESTcli.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,31 @@ 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
* @param PrivateKey $ecKey
* @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();
}

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 3c920c3

Please sign in to comment.