Skip to content

Commit

Permalink
API url override
Browse files Browse the repository at this point in the history
  • Loading branch information
radowoj-tpay committed Feb 15, 2024
1 parent 01d6382 commit 118bab8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 14 deletions.
29 changes: 26 additions & 3 deletions src/Api/ApiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tpay\OpenApi\Api;

use RuntimeException;
use Tpay\OpenApi\Curl\Curl;
use Tpay\OpenApi\Dictionary\HttpCodesDictionary;
use Tpay\OpenApi\Manager\Manager;
Expand All @@ -24,6 +25,9 @@ class ApiAction
/** @var Token */
protected $Token;

/** @var string */
protected $apiUrl;

/** @var bool */
private $productionMode;

Expand All @@ -39,7 +43,26 @@ public function __construct($Token, $productionMode)
$this->Token = $Token;
$this->Curl = new Curl();
$this->Manager = new Manager();
$this->clientName = 'tpay-com/tpay-openapi-php:1.6.6|PHP:'.phpversion();
$this->clientName = 'tpay-com/tpay-openapi-php:1.6.8|PHP:'.phpversion();
$this->apiUrl = true === $this->productionMode
? ApiAction::TPAY_API_URL_PRODUCTION
: ApiAction::TPAY_API_URL_SANDBOX;
}

/**
* @param string $apiUrl
*
* @return self
*/
public function overrideApiUrl($apiUrl)
{
if (!filter_var($apiUrl, FILTER_VALIDATE_URL)) {
throw new RuntimeException(sprintf('Invalid URL provided: %s', $apiUrl));
}

$this->apiUrl = $apiUrl;

return $this;
}

public function run($requestMethod, $apiMethod, $fields = [], $requestBody = null, $headers = [])
Expand All @@ -65,7 +88,7 @@ public function download($requestMethod, $apiMethod, $fields = [], $requestBody

$requestUrl = sprintf(
'%s%s',
true === $this->productionMode ? static::TPAY_API_URL_PRODUCTION : static::TPAY_API_URL_SANDBOX,
$this->apiUrl,
$apiMethod
);
if (is_string($this->Token->access_token->getValue()) && '/oauth/auth' !== $apiMethod) {
Expand Down Expand Up @@ -135,7 +158,7 @@ protected function sendRequest($apiMethod, $requestMethod, $fields = [], $header
{
$requestUrl = sprintf(
'%s%s',
true === $this->productionMode ? static::TPAY_API_URL_PRODUCTION : static::TPAY_API_URL_SANDBOX,
$this->apiUrl,
$apiMethod
);
if (is_string($this->Token->access_token->getValue()) && '/oauth/auth' !== $apiMethod) {
Expand Down
40 changes: 29 additions & 11 deletions src/Api/TpayApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,31 @@ class TpayApi
/** @var string */
private $scope;

/** @var string */
private $apiUrl;

/**
* @param string $clientId
* @param string $clientSecret
* @param bool $productionMode
* @param string $scope
* @param string $clientId
* @param string $clientSecret
* @param bool $productionMode
* @param string $scope
* @param null|string $apiUrlOverride
*/
public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read')
public function __construct($clientId, $clientSecret, $productionMode = false, $scope = 'read', $apiUrlOverride = null)
{
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->productionMode = $productionMode;
$this->scope = $scope;
$this->apiUrl = true === $this->productionMode
? ApiAction::TPAY_API_URL_PRODUCTION
: ApiAction::TPAY_API_URL_SANDBOX;
if (null !== $apiUrlOverride) {
if (!filter_var($apiUrlOverride, FILTER_VALIDATE_URL)) {
throw new RuntimeException(sprintf('Invalid URL provided: %s', $apiUrlOverride));
}
$this->apiUrl = $apiUrlOverride;
}
}

/**
Expand Down Expand Up @@ -103,7 +116,8 @@ public function accounts()
{
$this->authorize();
if (null === $this->accounts) {
$this->accounts = new AccountsApi($this->token, $this->productionMode);
$this->accounts = (new AccountsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);
}

return $this->accounts;
Expand All @@ -114,7 +128,8 @@ public function authorization()
{
$this->authorize();
if (null === $this->authorization) {
$this->authorization = new AuthorizationApi($this->token, $this->productionMode);
$this->authorization = (new AuthorizationApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);
}

return $this->authorization;
Expand All @@ -125,7 +140,8 @@ public function refunds()
{
$this->authorize();
if (null === $this->refunds) {
$this->refunds = new RefundsApi($this->token, $this->productionMode);
$this->refunds = (new RefundsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);
}

return $this->refunds;
Expand All @@ -136,7 +152,8 @@ public function reports()
{
$this->authorize();
if (null === $this->reports) {
$this->reports = new ReportsApi($this->token, $this->productionMode);
$this->reports = (new ReportsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);
}

return $this->reports;
Expand All @@ -147,7 +164,8 @@ public function transactions()
{
$this->authorize();
if (null === $this->transactions) {
$this->transactions = new TransactionsApi($this->token, $this->productionMode);
$this->transactions = (new TransactionsApi($this->token, $this->productionMode))
->overrideApiUrl($this->apiUrl);
}

return $this->transactions;
Expand All @@ -162,7 +180,7 @@ private function authorize()
return;
}

$authApi = new AuthorizationApi(new Token(), $this->productionMode);
$authApi = (new AuthorizationApi(new Token(), $this->productionMode))->overrideApiUrl($this->apiUrl);
$fields = [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
Expand Down

0 comments on commit 118bab8

Please sign in to comment.