Skip to content

Commit

Permalink
chore: Add methods to get and set the application information on the …
Browse files Browse the repository at this point in the history
…user agent headers

Signed-off-by: Bruno Gaspar <[email protected]>
  • Loading branch information
brunogaspar committed Jul 14, 2019
1 parent e9aaa84 commit 9aaba2b
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 2 deletions.
56 changes: 54 additions & 2 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,14 @@ protected function createHandler()
$request = $request->withHeader('Stripe-Account', $accountId);
}

$request = $request->withHeader('Stripe-Version', $config->getApiVersion());
$request = $request->withHeader('User-Agent', $this->generateUserAgent());

$request = $request->withHeader('User-Agent', 'Cartalyst-Stripe/'.$config->getVersion());
$request = $request->withHeader('Stripe-Version', $config->getApiVersion());

$request = $request->withHeader('Authorization', 'Basic '.base64_encode($config->getApiKey()));

$request = $request->withHeader('X-Stripe-Client-User-Agent', $this->generateClientUserAgentHeader());

return $request;
}));

Expand All @@ -229,4 +231,54 @@ protected function createHandler()

return $stack;
}

/**
* Generates the main user agent string.
*
* @return string
*/
protected function generateUserAgent()
{
$appInfo = $this->config->getAppInfo();

$userAgent = 'Cartalyst-Stripe/'.$this->config->getVersion();

if ($appInfo || ! empty($appInfo)) {
$userAgent .= ' '.$appInfo['name'];

if ($appVersion = $appInfo['version']) {
$userAgent .= "/{$appVersion}";
}

if ($appUrl = $appInfo['url']) {
$userAgent .= " ({$appUrl})";
}
}

return $userAgent;
}

/**
* Generates the client user agent header value.
*
* @return string
*/
protected function generateClientUserAgentHeader()
{
$appInfo = $this->config->getAppInfo();

$userAgent = [
'bindings_version' => $this->config->getVersion(),
'lang' => 'php',
'lang_version' => phpversion(),
'publisher' => 'cartalyst',
'uname' => php_uname(),
];

if ($appInfo || ! empty($appInfo)) {
$userAgent['application'] = $appInfo;
}

return json_encode($userAgent);
}
}
38 changes: 38 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class Config implements ConfigInterface
*/
protected $accountId;

/**
* The application's information.
*
* @var array|null
*/
protected $appInfo;

/**
* Constructor.
*
Expand Down Expand Up @@ -173,4 +180,35 @@ public function setAccountId($accountId)

return $this;
}

/**
* Returns the application's information.
*
* @return array|null
*/
public function getAppInfo()
{
return $this->appInfo;
}

/**
* Sets the application's information.
*
* @param string $appName
* @param string $appVersion
* @param string $appUrl
* @param string $appPartnerId
* @return $this
*/
public function setAppInfo($appName, $appVersion = null, $appUrl = null, $appPartnerId = null)
{
$this->appInfo = [
'name' => $appName,
'version' => $appVersion,
'url' => $appUrl,
'partner_id' => $appPartnerId,
];

return $this;
}
}
24 changes: 24 additions & 0 deletions src/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,30 @@ public function accountId($accountId)
return $this;
}

/**
* Returns the application's information.
*
* @return array|null
*/
public function getAppInfo()
{
return $this->config->getAppInfo();
}

/**
* Sets the application's information.
*
* @param string $appName
* @param string $appVersion
* @param string $appUrl
* @param string $appPartnerId
* @return $this
*/
public function setAppInfo($appName, $appVersion = null, $appUrl = null, $appPartnerId = null)
{
return $this->config->setAppInfo($appName, $appVersion, $appUrl, $appPartnerId);
}

/**
* Returns the amount converter class and method name.
*
Expand Down

0 comments on commit 9aaba2b

Please sign in to comment.