Skip to content

Commit 3c920c3

Browse files
committed
SP-126 Added X-BitPay-Platform-Info header
1 parent 4d677af commit 3c920c3

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

src/BitPaySDK/Client.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function __construct(RESTcli $restCli, Tokens $tokenCache)
7171
* @param string|null $privateKeySecret Private Key encryption password.
7272
* @param string|null $proxy The url of your proxy to forward requests through. Example:
7373
* http://********.com:3128
74+
* @param string|null $platformInfo Value for the X-BitPay-Platform header.
7475
* @return Client
7576
* @throws BitPayApiException
7677
* @throws BitPayGenericException
@@ -80,12 +81,13 @@ public static function createWithData(
8081
string $privateKey,
8182
Tokens $tokens,
8283
?string $privateKeySecret = null,
83-
?string $proxy = null
84+
?string $proxy = null,
85+
?string $platformInfo = null,
8486
): Client {
8587
try {
8688
$key = self::initKeys($privateKey, $privateKeySecret);
8789

88-
$restCli = new RESTcli($environment, $key, $proxy);
90+
$restCli = new RESTcli($environment, $key, $proxy, $platformInfo);
8991
$tokenCache = $tokens;
9092

9193
return new Client($restCli, $tokenCache);
@@ -99,11 +101,12 @@ public static function createWithData(
99101
/**
100102
* Constructor for use if the keys and SIN are managed by this library.
101103
*
102-
* @param string $configFilePath The path to the configuration file.
104+
* @param string $configFilePath The path to the configuration file.
105+
* @param string|null $platformInfo Value for the X-BitPay-Platform header.
103106
* @return Client
104107
* @throws BitPayGenericException
105108
*/
106-
public static function createWithFile(string $configFilePath): Client
109+
public static function createWithFile(string $configFilePath, ?string $platformInfo = null): Client
107110
{
108111
try {
109112
$configData = self::getConfigData($configFilePath);
@@ -113,7 +116,7 @@ public static function createWithFile(string $configFilePath): Client
113116
$key = self::initKeys($config['PrivateKeyPath'], $config['PrivateKeySecret']);
114117
$proxy = $config['Proxy'] ?? null;
115118

116-
$restCli = new RESTcli($env, $key, $proxy);
119+
$restCli = new RESTcli($env, $key, $proxy, $platformInfo);
117120
$tokenCache = new Tokens($config['ApiTokens']['merchant'], $config['ApiTokens']['payout']);
118121

119122
return new Client($restCli, $tokenCache);

src/BitPaySDK/PosClient.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,31 @@
2727
class PosClient extends Client
2828
{
2929
protected string $env;
30+
31+
/**
32+
* Value for the X-BitPay-Platform-Info header
33+
* @var string
34+
*
35+
*/
36+
protected string $platformInfo;
3037
protected Tokens $token;
3138
protected RESTcli $RESTcli;
3239

3340
/**
3441
* Constructor for the BitPay SDK to use with the POS facade.
3542
*
36-
* @param $token string The token generated on the BitPay account.
37-
* @param string|null $environment string The target environment [Default: Production].
43+
* @param string $token The token generated on the BitPay account.
44+
* @param string|null $environment The target environment [Default: Production].
45+
* @param string|null $platformInfo Value for the X-BitPay-Platform-Info header.
3846
*
3947
* @throws BitPayGenericException
4048
*/
41-
public function __construct(string $token, string $environment = null)
49+
public function __construct(string $token, string $environment = null, ?string $platformInfo = null)
4250
{
4351
try {
4452
$this->token = new Tokens(null, null, $token);
4553
$this->env = strtolower($environment) === "test" ? Env::TEST : Env::PROD;
54+
$this->platformInfo = $platformInfo !== null ? trim($platformInfo) : '';
4655
$this->init();
4756
parent::__construct($this->RESTcli, new Tokens(null, null, $token));
4857
} catch (Exception $e) {
@@ -60,7 +69,7 @@ public function __construct(string $token, string $environment = null)
6069
private function init(): void
6170
{
6271
try {
63-
$this->RESTcli = new RESTcli($this->env, new PrivateKey());
72+
$this->RESTcli = new RESTcli($this->env, new PrivateKey(), null, $this->platformInfo);
6473
} catch (Exception $e) {
6574
BitPayExceptionProvider::throwGenericExceptionWithMessage(
6675
'failed to build configuration : ' . $e->getMessage()
@@ -71,7 +80,7 @@ private function init(): void
7180
/**
7281
* Fetch the supported currencies.
7382
*
74-
* @return array A list of BitPay Invoice objects.
83+
* @return array A list of BitPay Invoice objects.
7584
* @throws BitPayGenericException
7685
* @throws BitPayApiException
7786
*/

src/BitPaySDK/Util/RESTcli/RESTcli.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,31 @@ class RESTcli
4444
*/
4545
protected string $identity;
4646

47+
/**
48+
* Value for the X-BitPay-Platform-Info header.
49+
* @var string
50+
*/
51+
protected string $platformInfo;
52+
4753
/**
4854
* @var string
4955
*/
5056
protected string $proxy;
5157

58+
5259
/**
5360
* RESTcli constructor.
5461
* @param string $environment
5562
* @param PrivateKey $ecKey
5663
* @param string|null $proxy
5764
* @throws BitPayApiException
5865
*/
59-
public function __construct(string $environment, PrivateKey $ecKey, ?string $proxy = null)
66+
public function __construct(string $environment, PrivateKey $ecKey, ?string $proxy = null, ?string $platformInfo)
6067
{
6168
$this->ecKey = $ecKey;
6269
$this->baseUrl = $environment == Env::TEST ? Env::TEST_URL : Env::PROD_URL;
6370
$this->proxy = $proxy !== null ? trim($proxy) : '';
71+
$this->platformInfo = $platformInfo !== null ? trim($platformInfo) : '';
6472
$this->init();
6573
}
6674

@@ -89,6 +97,10 @@ public function init(): void
8997
$config['proxy'] = $this->proxy;
9098
}
9199

100+
if ($this->platformInfo !== '') {
101+
$config['defaults']['headers']['x-bitpay-platform-info'] = $this->platformInfo;
102+
}
103+
92104
$this->client = new GuzzleHttpClient($config);
93105
} catch (Exception $e) {
94106
BitPayExceptionProvider::throwApiExceptionWithMessage($e->getMessage());
@@ -128,6 +140,10 @@ public function post($uri, array $formData = [], bool $signatureRequired = true)
128140
$headers['x-identity'] = $this->identity;
129141
}
130142

143+
if ($this->platformInfo !== '') {
144+
$headers['x-bitpay-platform-info'] = $this->platformInfo;
145+
}
146+
131147
$method = "POST";
132148

133149
LoggerProvider::getLogger()->logRequest($method, $fullURL, $jsonRequestData);
@@ -190,6 +206,10 @@ public function get($uri, array $parameters = null, bool $signatureRequired = tr
190206
$headers['x-identity'] = $this->identity;
191207
}
192208

209+
if ($this->platformInfo !== '') {
210+
$headers['x-bitpay-platform-info'] = $this->platformInfo;
211+
}
212+
193213
$method = 'GET';
194214

195215
LoggerProvider::getLogger()->logRequest($method, $fullURL, null);
@@ -247,6 +267,10 @@ public function delete($uri, array $parameters = null): string
247267
BitPayExceptionProvider::throwGenericExceptionWithMessage('Wrong ecKey. ' . $e->getMessage());
248268
}
249269

270+
if ($this->platformInfo !== '') {
271+
$headers['x-bitpay-platform-info'] = $this->platformInfo;
272+
}
273+
250274
$method = 'DELETE';
251275

252276
$jsonRequestData = json_encode($parameters, JSON_THROW_ON_ERROR);
@@ -305,6 +329,10 @@ public function update($uri, array $formData = []): string
305329
BitPayExceptionProvider::throwGenericExceptionWithMessage('Wrong ecKey. ' . $e->getMessage());
306330
}
307331

332+
if ($this->platformInfo !== '') {
333+
$headers['x-bitpay-platform-info'] = $this->platformInfo;
334+
}
335+
308336
$method = 'PUT';
309337

310338
LoggerProvider::getLogger()->logRequest($method, $fullURL, $jsonRequestData);

0 commit comments

Comments
 (0)