Skip to content

Commit

Permalink
Better client identification with clientName in TpayApi
Browse files Browse the repository at this point in the history
  • Loading branch information
s4ddly committed Jun 25, 2024
1 parent 9f0286a commit c871734
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Api/TpayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,18 @@ class TpayApi
/** @var string */
private $apiUrl;

/** @var string|null */
private $clientName;

/**
* @param string $clientId
* @param string $clientSecret
* @param bool $productionMode
* @param string $scope
* @param null|string $apiUrlOverride
* @param null|string $clientName
*/
public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read', $apiUrlOverride = null)
public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read', $apiUrlOverride = null, $clientName = null)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
Expand All @@ -71,6 +75,7 @@ public function __construct($clientId, $clientSecret, $productionMode = false, $
$this->apiUrl = true === $this->productionMode
? ApiAction::TPAY_API_URL_PRODUCTION
: ApiAction::TPAY_API_URL_SANDBOX;
$this->clientName = $clientName;
if (null !== $apiUrlOverride) {
if (!filter_var($apiUrlOverride, FILTER_VALIDATE_URL)) {
throw new RuntimeException(sprintf('Invalid URL provided: %s', $apiUrlOverride));
Expand Down Expand Up @@ -127,6 +132,10 @@ public function accounts()
if (null === $this->accounts) {
$this->accounts = (new AccountsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);

if ($this->clientName) {
$this->accounts->setClientName($this->clientName);
}
}

return $this->accounts;
Expand All @@ -139,6 +148,10 @@ public function authorization()
if (null === $this->authorization) {
$this->authorization = (new AuthorizationApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);

if ($this->clientName) {
$this->authorization->setClientName($this->clientName);
}
}

return $this->authorization;
Expand All @@ -151,6 +164,10 @@ public function refunds()
if (null === $this->refunds) {
$this->refunds = (new RefundsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);

if ($this->clientName) {
$this->refunds->setClientName($this->clientName);
}
}

return $this->refunds;
Expand All @@ -163,6 +180,10 @@ public function reports()
if (null === $this->reports) {
$this->reports = (new ReportsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);

if ($this->clientName) {
$this->reports->setClientName($this->clientName);
}
}

return $this->reports;
Expand All @@ -175,6 +196,10 @@ public function transactions()
if (null === $this->transactions) {
$this->transactions = (new TransactionsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);

if ($this->clientName) {
$this->transactions->setClientName($this->clientName);
}
}

return $this->transactions;
Expand All @@ -190,6 +215,11 @@ private function authorize()
}

$authApi = (new AuthorizationApi(new Token(), $this->productionMode))->overrideApiUrl($this->apiUrl);

if ($this->clientName) {
$authApi->setClientName($this->clientName);
}

$fields = [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
Expand Down

0 comments on commit c871734

Please sign in to comment.